searchitunes
Advanced tools
Comparing version 2.5.6 to 2.6.0
@@ -9,3 +9,3 @@ { | ||
"description": "Search the Apple iTunes Store and App Store with this lightweight module", | ||
"version": "2.5.6", | ||
"version": "2.6.0", | ||
"repository": { | ||
@@ -12,0 +12,0 @@ "type": "git", |
@@ -52,7 +52,8 @@ # searchitunes | ||
param | type | default | description | ||
:-----------|:-------|:----------------|:----------- | ||
[timeout] | number | 5000 | Wait time out in ms | ||
[userAgent] | string | searchitunes.js | User-Agent header | ||
... | mixed | | API parameters | ||
param | type | default | description | ||
:------------|:-------|:----------------|:----------- | ||
[timeout] | number | 5000 | Wait time out in ms | ||
[userAgent] | string | searchitunes.js | User-Agent header | ||
[throwEmpty] | bool | true | Throw `no results` instead of returning `[]` | ||
... | mixed | | API parameters | ||
@@ -87,3 +88,3 @@ ```js | ||
**ID params** | ||
#### ID params | ||
@@ -110,2 +111,34 @@ - amgAlbumId | ||
#### 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 ) | ||
; | ||
``` | ||
## Errors | ||
API errors are handled and thrown, prefixed with `API: ` | ||
When nothing is found it will throw with `no results` message. You can prevent | ||
this behavior by setting `throwEmpty` to `false` to return an empty array. | ||
```js | ||
searchitunes( { | ||
throwEmpty: false, | ||
entity: 'software', | ||
term: 'github', | ||
} ) | ||
.then( console.log ) | ||
.catch( console.error ) | ||
; | ||
``` | ||
## Unlicense | ||
@@ -112,0 +145,0 @@ |
@@ -15,15 +15,19 @@ /* | ||
* | ||
* @param {object} params Parameters to send along | ||
* @param {number} [params.timeout=5000] Wait timeout in ms | ||
* @param {string} [params.userAgent] Custom User-Agent header | ||
* @param {object} params Parameters to send along | ||
* @param {number} [params.timeout=5000] Wait timeout in ms | ||
* @param {string} [params.userAgent] Custom User-Agent header | ||
* @param {bool} [params.throwEmpty=true] Throw 'no results' instead of returning an empty array | ||
*/ | ||
module.exports = async function SearchItunes ( { | ||
timeout = 5000, | ||
userAgent = 'searchitunes.js', | ||
throwEmpty = true, | ||
trackId, | ||
} ) { | ||
let first; | ||
let params = arguments[0]; | ||
let url = 'https://itunes.apple.com/search?'; | ||
let url = 'https://itunes.apple.com/search'; | ||
@@ -40,2 +44,3 @@ let options = { | ||
delete params.userAgent; | ||
delete params.throwEmpty; | ||
@@ -48,3 +53,3 @@ // Convert trackId from a search response | ||
// Search or lookup | ||
// Lookup API | ||
const idKeys = [ | ||
@@ -59,10 +64,24 @@ 'amgAlbumId', | ||
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 request | ||
// Process | ||
params = new URLSearchParams( params ); | ||
@@ -74,11 +93,31 @@ url += '?' + params.toString(); | ||
// API error | ||
if ( data.errorMessage ) { | ||
throw new Error( `API: ${data.errorMessage}` ); | ||
} | ||
// Empty result | ||
if ( ! data.results || ! data.results.length ) { | ||
throw new Error( 'no results' ); | ||
if ( throwEmpty ) { | ||
throw new Error( 'no results' ); | ||
} | ||
return []; | ||
} | ||
if ( first ) { | ||
// Lookup response | ||
if ( idKey ) { | ||
// Bulk lookup | ||
if ( bulkRequest ) { | ||
return data.results; | ||
} | ||
// Single lookup | ||
return data.results[0]; | ||
} | ||
// Search response | ||
return data; | ||
}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9422
109
174