From 19b4133330166cf7a01e2ab8ee92c0836e18e59e Mon Sep 17 00:00:00 2001 From: Franklin Date: Sat, 16 Dec 2023 13:27:28 +0100 Subject: [PATCH] Feature: Added bulk lookups --- README.md | 15 ++++++++++++++- searchitunes.js | 32 ++++++++++++++++++++++++++------ test.js | 25 ++++++++++++++++++++++++- 3 files changed, 64 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6869f96..0e12b91 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ the item's details. - [Lookup API docs](https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/iTuneSearchAPI/LookupExamples.html) -**ID params** +#### ID params - amgAlbumId - amgArtistId @@ -105,6 +105,19 @@ searchitunes( { id: 123456 } ) ``` +#### Bulk lookup + +To request multiple objects at once you set one of the ID params above to an +_array_ with the item ID's. This will always return an _array_. + +```js +searchitunes( { id: [ 123, 789 ] } ) + .then( arr => arr.forEach( processItems ) + .catch( console.error ) +; +``` + + ## Unlicense This is free and unencumbered software released into the public domain. diff --git a/searchitunes.js b/searchitunes.js index b2bd356..a354c44 100644 --- a/searchitunes.js +++ b/searchitunes.js @@ -24,7 +24,6 @@ module.exports = async function SearchItunes ( { trackId, } ) { - let first; let params = arguments[0]; let url = 'https://itunes.apple.com/search'; @@ -56,11 +55,25 @@ module.exports = async function SearchItunes ( { 'upc', ]; - const hasKeys = Object.keys( params ).some( key => idKeys.includes( key ) ); + let bulkRequest = false; + let idKey = false; + let key; - if ( hasKeys ) { - url = 'https://itunes.apple.com/lookup'; - first = true; + for ( let i = 0; i < idKeys.length; i++ ) { + key = idKeys[i]; + + if ( params[key] ) { + idKey = true; + url = 'https://itunes.apple.com/lookup'; + + // Bulk request + if ( Array.isArray( params[key] ) ) { + bulkRequest = true; + params[key] = params[key].join( ',' ); + } + + break; + } } // Process @@ -74,7 +87,14 @@ module.exports = async function SearchItunes ( { throw new Error( 'no results' ); } - if ( first ) { + if ( idKey ) { + + // Bulk lookup + if ( bulkRequest ) { + return data.results; + } + + // Single lookup return data.results[0]; } diff --git a/test.js b/test.js index b719a25..5695a05 100644 --- a/test.js +++ b/test.js @@ -68,7 +68,7 @@ dotest.add( 'Error: request error', async test => { } ); -dotest.add( 'Lookup by ID', async test => { +dotest.add( 'Lookup by ID - One item', async test => { let error; let data; @@ -91,6 +91,29 @@ dotest.add( 'Lookup by ID', async test => { } ); +dotest.add( 'Lookup by ID - Bulk', async test => { + let error; + let data; + + try { + data = await app( { + id: [1477376905, 6448311069], + timeout, + } ); + } + catch ( err ) { + error = err; + } + + test( error ) + .isArray( 'fail', 'data', data ) + .isExactly( 'fail', 'data.length', data?.length, 2 ) + .isUndefined( 'fail', 'error', error ) + .done() + ; +} ); + + dotest.add( 'Lookup by trackId', async test => { let error; let data;