searchitunes
Advanced tools
Comparing version 2.5.3 to 2.5.4
@@ -9,3 +9,3 @@ { | ||
"description": "Search the Apple iTunes Store and App Store with this lightweight module", | ||
"version": "2.5.3", | ||
"version": "2.5.4", | ||
"repository": { | ||
@@ -12,0 +12,0 @@ "type": "git", |
@@ -20,3 +20,3 @@ /* | ||
function keysInObject (obj) { | ||
async function keysInObject (obj) { | ||
const keys = [ | ||
@@ -50,21 +50,17 @@ 'amgAlbumId', | ||
function httpResponse ({ | ||
async function httpResponse ({ | ||
res, | ||
first = false, | ||
}) { | ||
return new Promise ((resolve, reject) => { | ||
const data = JSON.parse (res.body); | ||
const data = JSON.parse (res.body); | ||
if (!data.results || !data.results.length) { | ||
reject (new Error ('no results')); | ||
return; | ||
} | ||
if (!data.results || !data.results.length) { | ||
throw new Error ('no results'); | ||
} | ||
if (first) { | ||
resolve (data.results[0]); | ||
return; | ||
} | ||
if (first) { | ||
return data.results[0]; | ||
} | ||
resolve (data); | ||
}); | ||
return data; | ||
} | ||
@@ -83,4 +79,7 @@ | ||
module.exports = async params => { | ||
let res; | ||
module.exports = async function SearchItunes ({ | ||
timeout = 5000, | ||
userAgent = 'searchitunes.js', | ||
trackId, | ||
}) { | ||
let first; | ||
@@ -90,24 +89,13 @@ let options = { | ||
url: 'https://itunes.apple.com/search', | ||
parameters: params, | ||
timeout: 5000, | ||
parameters: arguments[0], | ||
timeout, | ||
headers: { | ||
'Accept': 'application/json', | ||
'User-Agent': 'searchitunes.js', | ||
'User-Agent': userAgent, | ||
}, | ||
}; | ||
// Process internal settings | ||
if (params.timeout) { | ||
options.timeout = params.timeout; | ||
delete options.parameters.timeout; | ||
} | ||
if (params.userAgent) { | ||
options.headers['User-Agent'] = params.userAgent; | ||
delete options.parameters.userAgent; | ||
} | ||
// Convert trackId from a search response | ||
if (params.trackId) { | ||
options.parameters.id = params.trackId; | ||
if (trackId) { | ||
options.parameters.id = trackId; | ||
delete options.parameters.trackId; | ||
@@ -117,3 +105,5 @@ } | ||
// Search or lookup | ||
if (keysInObject (options.parameters)) { | ||
const hasKeys = await keysInObject (options.parameters); | ||
if (hasKeys) { | ||
options.url = 'https://itunes.apple.com/lookup'; | ||
@@ -124,5 +114,9 @@ first = true; | ||
// Process request | ||
res = await doRequest (options); | ||
delete options.parameters.timeout; | ||
delete options.parameters.userAgent; | ||
const res = await doRequest (options); | ||
return httpResponse ({ res, first }); | ||
}; | ||
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
8549
110