Skip to content

Commit

Permalink
Merge pull request #247 from DougMidgley/develop
Browse files Browse the repository at this point in the history
May 28 2023 release of SFMC-SDK
  • Loading branch information
DougMidgley committed May 28, 2023
2 parents 4a79b4a + 1b497e6 commit 22af37c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ module.exports = class Rest {
},
this.options.requestAttempts
);

// determine iterator field if not provided
if (iteratorField && Array.isArray(responseBatch[iteratorField])) {
// if the iteratorField is set, use it
} else if (Array.isArray(responseBatch.items)) {
Expand All @@ -106,26 +108,38 @@ module.exports = class Rest {
} else {
throw new TypeError('Could not find an array to iterate over');
}

// merge results with existing
if (collector && Array.isArray(responseBatch[iteratorField])) {
collector[iteratorField].push(...responseBatch[iteratorField]);
} else if (!collector) {
collector = responseBatch;
}
// ! the transactional message API returns a value for "count" that represents the currently returned number of records, instead of the total amount. checking for count != pageSize is a workaround for this
// * opened Support Case #43988240 for this issue
if (
Array.isArray(collector[iteratorField]) &&
collector[iteratorField].length >= responseBatch[countKey] &&
(!isTransactionalMessageApi ||
(isTransactionalMessageApi &&
responseBatch[countKey] != responseBatch[pageSizeKey]))
isTransactionalMessageApi &&
responseBatch[countKey] != responseBatch[pageSizeKey]
) {
// ! the transactional message API returns a value for "count" that represents the currently returned number of records, instead of the total amount. checking for count != pageSize is a workaround for this
// * opened Support Case #43988240 for this issue
shouldPaginate = false;
} else {
}
// if results are less than size, no more requested needed
else if (responseBatch[iteratorField].length < pageSize) {
shouldPaginate = false;
}
// stop if total amount is same as the current amount (all have been retrieved)
else if (collector[iteratorField].length >= responseBatch[countKey]) {
shouldPaginate = false;
}
// stop if response contains no results (for whatever reason) - this is a known issue on legacy API
else if (responseBatch[iteratorField].length === 0) {
shouldPaginate = false;
}
// otherwise loop
else {
page++;
shouldPaginate = true;
if (this.options?.eventHandlers?.onLoop) {
// TODO in v1 change to undefined to ensure breaking changes considered
// eslint-disable-next-line unicorn/no-null
this.options.eventHandlers.onLoop(null, collector?.[iteratorField]);
}
Expand Down
38 changes: 38 additions & 0 deletions test/resources/rest.json
Original file line number Diff line number Diff line change
Expand Up @@ -374,5 +374,43 @@
"errorcode": 10000,
"documentation": ""
}
},
"keywordPage1": {
"status": 200,
"url": "https://mct0l7nxfq2r988t1kxfy8sc47ma.rest.marketingcloudapis.com/legacy/v1/beta/mobile/keyword/?view=simple&$top=50&$skip=0",
"response": {
"startIndex": 0,
"itemsPerPage": 50,
"totalResults": 12,
"entry": [
{
"id": "MGtuaEdmbDRJMEtzMmN5cGIyTlJzQTo4Njow"
},
{
"id": "ZVVGOUl2ZzlXVWVockxLSlUzTWRFQTo4Njow"
},
{
"id": "VVF0NnJIYjhyVS1xa2dITW5rTkNuUTo4Njow"
},
{
"id": "S3FsUGRJUks1RUNUdEJVR1VxcG9Hdzo4Njow"
},
{
"id": "TGwxNGZ3ZmFFVWlOM2ZmdGgzd1Rqdzo4Njow"
},
{
"id": "M211MUc3MmNJRU80d2FvdndUS3E1Zzo4Njow"
},
{
"id": "OFplbURfd1U2RXl3eHVxMU1GZGJjZzo4Njow"
},
{
"id": "a3ZYLXdXSXlsVWVKb09yVjZxMC1MZzo4Njow"
},
{
"id": "YkJrNDUxSVc4MDZ3eDBtVlZLMTRudzo4Njow"
}
]
}
}
}
15 changes: 15 additions & 0 deletions test/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ describe('rest', function () {
assert.lengthOf(mock.history.get, 2);
return;
});
it('GET Bulk: should return 9 keyword items', async function () {
//given
const { keywordPage1 } = resources;
mock.onGet(keywordPage1.url).reply(keywordPage1.status, keywordPage1.response);
// when
const payload = await defaultSdk().rest.getBulk(
'/legacy/v1/beta/mobile/keyword/?view=simple',
10
);
// then
assert.lengthOf(payload.entry, 9);
assert.lengthOf(mock.history.post, 1);
assert.lengthOf(mock.history.get, 1);
return;
});
it('GET: should return 5 journey items', async function () {
//given
const { journeysPage1 } = resources;
Expand Down

0 comments on commit 22af37c

Please sign in to comment.