I've been trying to follow other forum instructions the past few days but haven't been able to get my query to work, so turning to this post.
I have a REST API resource I'm using to make a query within my app. The query is simple and just passes URL parameters (and authorization stored in the Resource) like /endpoint/v2/slug?offset=100&per_page=100&where[created_at][gte]=2024-03-01T13:37:38.230Z&order=-created_at
If the result set is greater than the per_page
value, then the API response includes a "next" url in the links section as well as a "next.offset" value in the meta section of the output.
So I need to trigger the query, check if data.meta.next.offset exists, and if it does, trigger the query again but with the new offset value, then join all these query data requests together into one data set.
This is the closest I feel like I've gotten, but there is nothing in the output and I feel like there might be a simpler way.
function getAllDonationsRecursively(offset = 0, allDonations = []) {
return new Promise((resolve, reject) => {
getDonations.trigger({
onSuccess: (data) => {
const currentDonations = data.data;
allDonations.push(...currentDonations);
const nextPage = data.meta.next.offset;
if (nextPage) {
const nextOffset = new URL(nextPage);
getAllDonationsRecursively(nextOffset, allDonations).then(resolve).catch(reject);
} else {
resolve(allDonations);
}
},
onFailure: (error) => {
reject(error);
},
additionalScope: {
offset
}
});
});
}
return getAllDonationsRecursively();
Any tips or ideas on how to get this working? Below is also an example of the data response to see what I have access to (shortened for simplicity):
{
"links": {
"self": "https://api.domain.com/endpoint/v2/slug?per_page=100",
"next": "https://api.domain.com/endpoint/v2/slug?offset=100&per_page=100"
},
"data": [
{
"type": "Donation",
"id": "123456789"
},
{
"type": "Donation",
"id": "987654321"
}
],
"meta": {
"total_count": 234,
"count": 100,
"next": {
"offset": 100
}
}