diff --git a/lib/common.js b/lib/common.js index ef41a63..ca4d259 100644 --- a/lib/common.js +++ b/lib/common.js @@ -72,7 +72,7 @@ const doRequest = (url, headers, requestOptions, limit) => new Promise(function const LOOKUP_URL = 'https://itunes.apple.com/lookup'; -function lookup (ids, idField, country, lang, requestOptions, limit) { +function doLookupRequest (ids, idField, country, lang, requestOptions, limit) { idField = idField || 'id'; country = country || 'us'; const langParam = lang ? `&lang=${lang}` : ''; @@ -86,6 +86,23 @@ function lookup (ids, idField, country, lang, requestOptions, limit) { .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, limit) { + const allIds = [...ids]; // 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, limit)); + + return Promise.all(reqs).then((resultChunks) => [].concat(...resultChunks)); +} + function storeId (countryCode) { const markets = c.markets; const defaultStore = '143441';