From 459029530b230dcc33053c36c3f01c964d2d63be Mon Sep 17 00:00:00 2001 From: Dharini Jeeva Date: Wed, 15 Nov 2023 12:54:54 -0800 Subject: [PATCH] =?UTF-8?q?chore:=20=F0=9F=A4=96=20Add=20support=20for=20p?= =?UTF-8?q?agination=20in=20query=20method=20(#1991)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * chore: 🤖 Add support for pagination in query method --- addons/api/addon/adapters/application.js | 26 +++++++++++++++++++-- addons/api/addon/serializers/application.js | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/addons/api/addon/adapters/application.js b/addons/api/addon/adapters/application.js index 763cb6d98..18e1c3c67 100644 --- a/addons/api/addon/adapters/application.js +++ b/addons/api/addon/adapters/application.js @@ -118,12 +118,34 @@ export default class ApplicationAdapter extends RESTAdapter.extend( /** * Intercepts "empty" responses and adds an empty `items` array. + * This query method now supports pagination by checking the responseType and + * passing in the refresh token if the responseType indicates more data * @override * @method query * @return {Promise} promise */ - query() { - return super.query(...arguments).then(prenormalizeArrayResponse); + async query(store, schema, query) { + let result; + let data = []; + + // Run this loop as long as the response_type is delta, + // which indicates that there are more items in the list + do { + result = await super.query(store, schema, query); + //add the result items to a data array + if (result && result.items) { + data.push(...result.items); + //pass in the refresh token for subsequent calls to fetch the remaining list items + query.refresh_token = result.refresh_token; + //break the loop as soon the response_type becomes complete + if (result.response_type === 'complete') { + break; + } + } + } while (result && result.response_type === 'delta'); + + result.items = data; + return prenormalizeArrayResponse(result); } /** diff --git a/addons/api/addon/serializers/application.js b/addons/api/addon/serializers/application.js index 52f9c813d..3e631334f 100644 --- a/addons/api/addon/serializers/application.js +++ b/addons/api/addon/serializers/application.js @@ -149,7 +149,7 @@ export default class ApplicationSerializer extends RESTSerializer { ); // Copy the data rooted under `items` in the existing payload into the new // payload under the expected root key name. - transformedPayload[payloadKey] = structuredClone(payload.items); + transformedPayload[payloadKey] = structuredClone(payload?.items); // Return the result of normalizing the transformed payload. return super.normalizeArrayResponse( store,