Skip to content

Commit

Permalink
Feature: Added bulk lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
fvdm committed Dec 16, 2023
1 parent e32718a commit 19b4133
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 8 deletions.
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down
32 changes: 26 additions & 6 deletions searchitunes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module.exports = async function SearchItunes ( {
trackId,

} ) {
let first;

let params = arguments[0];
let url = 'https://itunes.apple.com/search';
Expand Down Expand Up @@ -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
Expand All @@ -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];
}

Expand Down
25 changes: 24 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down

0 comments on commit 19b4133

Please sign in to comment.