From 3b9e9e3d976aac043a8783ff47c5a8c56188eb81 Mon Sep 17 00:00:00 2001 From: Jonas <30421456+jonaskuske@users.noreply.github.com> Date: Fri, 15 Oct 2021 17:36:00 +0200 Subject: [PATCH 1/2] fix: only lookup up to 200 ids per request --- lib/common.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/common.js b/lib/common.js index 92a8c58..0fa08ab 100644 --- a/lib/common.js +++ b/lib/common.js @@ -63,7 +63,7 @@ const doRequest = (url, headers, requestOptions) => new Promise(function (resolv const LOOKUP_URL = 'https://itunes.apple.com/lookup'; -function lookup (ids, idField, country, lang, requestOptions) { +function doLookupRequest (ids, idField, country, lang, requestOptions) { idField = idField || 'id'; country = country || 'us'; const langParam = lang ? `&lang=${lang}` : ''; @@ -77,6 +77,23 @@ function lookup (ids, idField, country, lang, requestOptions) { .then((res) => res.map(cleanApp)); } +/** + * Lookups are limited to 200 ids per request, so we split the id set into chunks of 200, + * then send multiple lookup requests and merge the results back into a single array + */ +function lookup (ids, idField, country, lang, requestOptions) { + const allIds = ids.slice(); // create copy so .splice doesn't mutate the original array + const idChunks = []; + + while (allIds.length) { + idChunks.push(allIds.splice(0, 200)); + } + + const reqs = idChunks.map((ids) => doLookupRequest(ids, idField, country, lang, requestOptions)); + + return Promise.all(reqs).then((resultChunks) => [].concat.apply([], resultChunks)); +} + function storeId (countryCode) { const markets = c.markets; const defaultStore = '143441'; From d20027bbb8555c516dbf713d52279f72d97be1c6 Mon Sep 17 00:00:00 2001 From: Jonas <30421456+jonaskuske@users.noreply.github.com> Date: Tue, 19 Oct 2021 06:29:32 +0200 Subject: [PATCH 2/2] style: use spread operator --- lib/common.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/common.js b/lib/common.js index 0fa08ab..e3ed2a7 100644 --- a/lib/common.js +++ b/lib/common.js @@ -82,7 +82,7 @@ function doLookupRequest (ids, idField, country, lang, requestOptions) { * then send multiple lookup requests and merge the results back into a single array */ function lookup (ids, idField, country, lang, requestOptions) { - const allIds = ids.slice(); // create copy so .splice doesn't mutate the original array + const allIds = [...ids]; // create copy so .splice doesn't mutate the original array const idChunks = []; while (allIds.length) { @@ -91,7 +91,7 @@ function lookup (ids, idField, country, lang, requestOptions) { const reqs = idChunks.map((ids) => doLookupRequest(ids, idField, country, lang, requestOptions)); - return Promise.all(reqs).then((resultChunks) => [].concat.apply([], resultChunks)); + return Promise.all(reqs).then((resultChunks) => [].concat(...resultChunks)); } function storeId (countryCode) {