chore: 🤖 Add support for pagination in query method (#1991)
* chore: 🤖 Add support for pagination in query method
This commit is contained in:
1 parent
7dbd581a37
commit
459029530b
2 files changed
+25
-3
No files matched your search
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user