searchitunes
Advanced tools
Comparing version 2.5.5 to 2.5.6
@@ -9,3 +9,3 @@ { | ||
"description": "Search the Apple iTunes Store and App Store with this lightweight module", | ||
"version": "2.5.5", | ||
"version": "2.5.6", | ||
"repository": { | ||
@@ -22,10 +22,8 @@ "type": "git", | ||
], | ||
"dependencies": { | ||
"httpreq": "^0.5.2" | ||
}, | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"dotest": "^2.12.2" | ||
"dotest": "^2" | ||
}, | ||
"engines": { | ||
"node": ">=14" | ||
"node": ">=18" | ||
}, | ||
@@ -32,0 +30,0 @@ "keywords": [ |
@@ -9,56 +9,4 @@ /* | ||
const { doRequest } = require( 'httpreq' ); | ||
/** | ||
* Check if one of the keys is a property | ||
* | ||
* @param {object} obj Object to process | ||
* | ||
* @return {Promise<boolean>} `true` = yes | ||
*/ | ||
async function keysInObject ( obj ) { | ||
const objKeys = Object.keys( obj ); | ||
const keys = [ | ||
'amgAlbumId', | ||
'amgArtistId', | ||
'amgVideoId', | ||
'id', | ||
'isbn', | ||
'upc', | ||
]; | ||
return objKeys.some( key => keys.includes( key ) ); | ||
} | ||
/** | ||
* Process HTTP response | ||
* | ||
* @param {object} res Response | ||
* @param {bool} [first=false] Only first result | ||
* | ||
* @return {Promise<object|array>} | ||
*/ | ||
async function httpResponse ( { | ||
res, | ||
first = false, | ||
} ) { | ||
const data = JSON.parse( res.body ); | ||
if ( ! data.results || ! data.results.length ) { | ||
throw new Error( 'no results' ); | ||
} | ||
if ( first ) { | ||
return data.results[0]; | ||
} | ||
return data; | ||
} | ||
/** | ||
* Send HTTP request | ||
@@ -79,7 +27,7 @@ * | ||
let first; | ||
let params = arguments[0]; | ||
let url = 'https://itunes.apple.com/search?'; | ||
let options = { | ||
method: 'POST', | ||
url: 'https://itunes.apple.com/search', | ||
parameters: arguments[0], | ||
timeout, | ||
signal: AbortSignal.timeout( parseInt( timeout, 10 ) ), | ||
headers: { | ||
@@ -91,13 +39,25 @@ 'Accept': 'application/json', | ||
delete params.timeout; | ||
delete params.userAgent; | ||
// Convert trackId from a search response | ||
if ( trackId ) { | ||
options.parameters.id = trackId; | ||
delete options.parameters.trackId; | ||
params.id = trackId; | ||
delete params.trackId; | ||
} | ||
// Search or lookup | ||
const hasKeys = await keysInObject( options.parameters ); | ||
const idKeys = [ | ||
'amgAlbumId', | ||
'amgArtistId', | ||
'amgVideoId', | ||
'id', | ||
'isbn', | ||
'upc', | ||
]; | ||
const hasKeys = Object.keys( params ).some( key => idKeys.includes( key ) ); | ||
if ( hasKeys ) { | ||
options.url = 'https://itunes.apple.com/lookup'; | ||
url = 'https://itunes.apple.com/lookup'; | ||
first = true; | ||
@@ -107,8 +67,17 @@ } | ||
// Process request | ||
delete options.parameters.timeout; | ||
delete options.parameters.userAgent; | ||
params = new URLSearchParams( params ); | ||
url += '?' + params.toString(); | ||
const res = await doRequest( options ); | ||
const res = await fetch( url, options ); | ||
const data = await res.json(); | ||
return httpResponse( { res, first } ); | ||
if ( ! data.results || ! data.results.length ) { | ||
throw new Error( 'no results' ); | ||
} | ||
if ( first ) { | ||
return data.results[0]; | ||
} | ||
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
Network access
Supply chain riskThis module accesses the network.
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
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
8006
81
- Removedhttpreq@^0.5.2
- Removedhttpreq@0.5.2(transitive)