lastfm-node-client
Advanced tools
Comparing version 1.1.2 to 2.0.0
@@ -1,33 +0,5 @@ | ||
const { URL, URLSearchParams } = require("url"); | ||
const crypto = require("crypto"); | ||
const http = require("http"); | ||
const querystring = require("querystring"); | ||
function httpRequest(options, reqBody, callback) { | ||
const request = http.request(options, response => { | ||
let resBody = ""; | ||
response.on("data", data => { | ||
resBody += data; | ||
}); | ||
response.on("end", () => { | ||
callback(null, JSON.parse(resBody)); | ||
}); | ||
response.on("error", err => { | ||
callback(err, null); | ||
}); | ||
}); | ||
request.on("error", err => { | ||
callback(err, null); | ||
}); | ||
if(options.method === "POST") { | ||
request.write(reqBody); | ||
} | ||
request.end(); | ||
} | ||
class ApiRequest { | ||
@@ -39,12 +11,14 @@ /** | ||
constructor() { | ||
this.format = "json"; | ||
this.params = new Map().set("format", "json"); | ||
} | ||
/** | ||
* Set properties of an ApiRequest | ||
* @param {Object} object - Properties to set | ||
* Set elements in ApiRequest.params | ||
* @param {Object} params - Object whose entries to set in ApiRequest.params | ||
*/ | ||
set(object) { | ||
Object.assign(this, object); | ||
set(params) { | ||
Object | ||
.entries(params) | ||
.forEach(([key, value]) => this.params.set(key, value)); | ||
@@ -61,37 +35,26 @@ return this; | ||
sign(secret) { | ||
const params = Object | ||
.entries(this) | ||
const paramsStr = Array.from(this.params) | ||
.filter(([key]) => key !== "format" && key !== "callback") | ||
.sort(([a], [b]) => { | ||
for(let i = 0; i < a.length && i < b.length; i++) { | ||
if(a.charCodeAt(i) < b.charCodeAt(i)) { | ||
for (let i = 0; i < a.length || i < b.length; i++) { | ||
const charCodeA = a.charCodeAt(i) || 0; | ||
const charCodeB = b.charCodeAt(i) || 0; | ||
if (charCodeA < charCodeB) { | ||
return -1; | ||
} | ||
if(a.charCodeAt(i) > b.charCodeAt(i)) { | ||
if (charCodeA > charCodeB) { | ||
return 1; | ||
} | ||
} | ||
if(a.length > b.length) { | ||
return -1; | ||
} | ||
if(a.length < b.length) { | ||
return 1; | ||
} | ||
return 0; | ||
}); | ||
}) | ||
.map(param => param.join("")) | ||
.join(""); | ||
let paramString = ""; | ||
for(const [name, value] of params) { | ||
if(name !== "format" && name !== "callback") { | ||
paramString += name + value; | ||
} | ||
} | ||
this.api_sig = crypto | ||
.createHash("md5") | ||
.update(paramString + secret) | ||
.update(paramsStr + secret) | ||
.digest("hex"); | ||
@@ -108,16 +71,24 @@ | ||
send(...args) { | ||
const method = typeof args[0] === "string" ? args[0] : undefined; | ||
const callback = args.length > 1 ? typeof args[1] === "function" ? args[1] : undefined : typeof args[0] === "function" ? args[0] : undefined; | ||
const apiRoot = new URL("http://ws.audioscrobbler.com/2.0/"); | ||
const querystring = (new URLSearchParams(this)).toString(); | ||
send(method, callback) { | ||
callback = callback === undefined ? typeof method === "function" ? method : undefined : typeof callback === "function" ? callback : undefined; | ||
method = typeof method === "string" ? method : undefined; | ||
if (this.params.has("callback")) { | ||
this.params.delete("callback"); | ||
} | ||
const paramsObj = {}; | ||
this.params.forEach((value, key) => paramsObj[key] = value); | ||
const paramsStr = querystring.stringify(paramsObj); | ||
const options = { | ||
"hostname": apiRoot.hostname, | ||
"path": apiRoot.pathname | ||
hostname: "ws.audioscrobbler.com", | ||
path: "/2.0" | ||
}; | ||
if(method === "POST") { | ||
if (method === "POST") { | ||
options.method = "POST"; | ||
options.headers = { | ||
"Content-Length": Buffer.byteLength(querystring), | ||
"Content-Length": Buffer.byteLength(paramsStr), | ||
"Content-Type": "application/x-www-form-urlencoded" | ||
@@ -127,20 +98,46 @@ }; | ||
else { | ||
options.path += `?${querystring}`; | ||
options.path += `?${paramsStr}`; | ||
} | ||
if(callback) { | ||
httpRequest(options, querystring, callback); | ||
const apiRequest = new Promise((resolve, reject) => { | ||
const httpRequest = http.request(options, httpResponse => { | ||
let data = ""; | ||
return; | ||
} | ||
httpResponse.setEncoding("utf8"); | ||
httpResponse.on("data", chunk => data += chunk); | ||
httpResponse.on("end", () => resolve(data)); | ||
httpResponse.on("error", err => reject(err)); | ||
}); | ||
return new Promise((resolve, reject) => httpRequest(options, querystring, (err, data) => { | ||
if(err) { | ||
reject(err); | ||
httpRequest.on("error", err => reject(err)); | ||
return; | ||
if (method === "POST") { | ||
httpRequest.write(paramsStr); | ||
} | ||
resolve(data); | ||
})); | ||
httpRequest.end(); | ||
}).then(apiResponse => { | ||
let data; | ||
try { | ||
data = JSON.parse(apiResponse); | ||
} | ||
catch (err) { | ||
throw new Error("Unable to parse API response to JSON"); | ||
} | ||
if (data.error) { | ||
throw new Error(data.message); | ||
} | ||
return data; | ||
}); | ||
if (callback) { | ||
apiRequest.then(data => callback(null, data), err => callback(err, null)); | ||
return undefined; | ||
} | ||
return apiRequest; | ||
} | ||
@@ -147,0 +144,0 @@ } |
@@ -13,28 +13,27 @@ const ApiRequest = require("./ApiRequest"); | ||
* Create a LastFm instance | ||
* @param {Object} options | ||
* @param {string} options.apiKey | ||
* @param {string} [options.secret] | ||
* @param {string} [options.sessionKey] | ||
* @param {string} apiKey | ||
* @param {string} secret | ||
* @param {string} sessionKey | ||
*/ | ||
constructor({ apiKey, secret, sessionKey } = {}) { | ||
if(typeof apiKey !== "string") { | ||
constructor(apiKey, secret, sessionKey) { | ||
if (typeof apiKey !== "string") { | ||
throw new TypeError("apiKey must be of type string"); | ||
} | ||
if(secret !== undefined && typeof secret !== "string") { | ||
throw new TypeError("secret must be of type string"); | ||
} | ||
this.apiKey = apiKey; | ||
if(sessionKey !== undefined && typeof sessionKey !== "string") { | ||
throw new TypeError("sessionKey must be of type string"); | ||
} | ||
if (secret !== undefined) { | ||
if (typeof secret !== "string") { | ||
throw new TypeError("secret must be of type string"); | ||
} | ||
this.apiKey = apiKey; | ||
if(secret) { | ||
this.secret = secret; | ||
} | ||
if(sessionKey) { | ||
if (sessionKey !== undefined) { | ||
if (typeof sessionKey !== "string") { | ||
throw new TypeError("sessionKey must be of type string"); | ||
} | ||
this.sessionKey = sessionKey; | ||
@@ -55,8 +54,8 @@ } | ||
albumAddTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "album.addTags", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "album.addTags", | ||
sk: this.sessionKey | ||
}) | ||
@@ -83,7 +82,7 @@ .sign(this.secret) | ||
albumGetInfo(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "album.getInfo" | ||
api_key: this.apiKey, | ||
method: "album.getInfo" | ||
}) | ||
@@ -108,7 +107,7 @@ .send(callback); | ||
albumGetTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "album.getTags" | ||
api_key: this.apiKey, | ||
method: "album.getTags" | ||
}) | ||
@@ -132,7 +131,7 @@ .send(callback); | ||
albumGetTopTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "album.getTopTags" | ||
api_key: this.apiKey, | ||
method: "album.getTopTags" | ||
}) | ||
@@ -155,8 +154,8 @@ .send(callback); | ||
albumRemoveTag(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "album.removeTag", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "album.removeTag", | ||
sk: this.sessionKey | ||
}) | ||
@@ -180,7 +179,7 @@ .sign(this.secret) | ||
albumSearch(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "album.search" | ||
api_key: this.apiKey, | ||
method: "album.search" | ||
}) | ||
@@ -202,8 +201,8 @@ .send(callback); | ||
artistAddTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.addTags", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "artist.addTags", | ||
sk: this.sessionKey | ||
}) | ||
@@ -225,7 +224,7 @@ .sign(this.secret) | ||
artistGetCorrection(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getCorrection" | ||
api_key: this.apiKey, | ||
method: "artist.getCorrection" | ||
}) | ||
@@ -250,7 +249,7 @@ .send(callback); | ||
artistGetInfo(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getInfo" | ||
api_key: this.apiKey, | ||
method: "artist.getInfo" | ||
}) | ||
@@ -274,7 +273,7 @@ .send(callback); | ||
artistGetSimilar(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getSimilar" | ||
api_key: this.apiKey, | ||
method: "artist.getSimilar" | ||
}) | ||
@@ -298,7 +297,7 @@ .send(callback); | ||
artistGetTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getTags" | ||
api_key: this.apiKey, | ||
method: "artist.getTags" | ||
}) | ||
@@ -323,7 +322,7 @@ .send(callback); | ||
artistGetTopAlbums(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getTopAlbums" | ||
api_key: this.apiKey, | ||
method: "artist.getTopAlbums" | ||
}) | ||
@@ -346,7 +345,7 @@ .send(callback); | ||
artistGetTopTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getTopTags" | ||
api_key: this.apiKey, | ||
method: "artist.getTopTags" | ||
}) | ||
@@ -371,7 +370,7 @@ .send(callback); | ||
artistGetTopTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.getTopTracks" | ||
api_key: this.apiKey, | ||
method: "artist.getTopTracks" | ||
}) | ||
@@ -393,8 +392,8 @@ .send(callback); | ||
artistRemoveTag(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.removeTag", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "artist.removeTag", | ||
sk: this.sessionKey | ||
}) | ||
@@ -418,7 +417,7 @@ .sign(this.secret) | ||
artistSearch(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "artist.search" | ||
api_key: this.apiKey, | ||
method: "artist.search" | ||
}) | ||
@@ -440,7 +439,7 @@ .send(callback); | ||
authGetMobileSession(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "auth.getMobileSession" | ||
api_key: this.apiKey, | ||
method: "auth.getMobileSession" | ||
}) | ||
@@ -462,7 +461,7 @@ .sign(this.secret) | ||
authGetSession(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "auth.getSession" | ||
api_key: this.apiKey, | ||
method: "auth.getSession" | ||
}) | ||
@@ -482,6 +481,6 @@ .sign(this.secret) | ||
authGetToken(callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "auth.getToken" | ||
api_key: this.apiKey, | ||
method: "auth.getToken" | ||
}) | ||
@@ -504,7 +503,7 @@ .sign(this.secret) | ||
chartGetTopArtists(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "chart.getTopArtists" | ||
api_key: this.apiKey, | ||
method: "chart.getTopArtists" | ||
}) | ||
@@ -526,7 +525,7 @@ .send(callback); | ||
chartGetTopTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "chart.getTopTags" | ||
api_key: this.apiKey, | ||
method: "chart.getTopTags" | ||
}) | ||
@@ -548,7 +547,7 @@ .send(callback); | ||
chartGetTopTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "chart.getTopTracks" | ||
api_key: this.apiKey, | ||
method: "chart.getTopTracks" | ||
}) | ||
@@ -571,7 +570,7 @@ .send(callback); | ||
geoGetTopArtists(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "geo.getTopArtists" | ||
api_key: this.apiKey, | ||
method: "geo.getTopArtists" | ||
}) | ||
@@ -595,7 +594,7 @@ .send(callback); | ||
geoGetTopTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "geo.getTopTracks" | ||
api_key: this.apiKey, | ||
method: "geo.getTopTracks" | ||
}) | ||
@@ -618,7 +617,7 @@ .send(callback); | ||
libraryGetArtists(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "library.getArtists" | ||
api_key: this.apiKey, | ||
method: "library.getArtists" | ||
}) | ||
@@ -640,7 +639,7 @@ .send(callback); | ||
tagGetInfo(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getInfo" | ||
api_key: this.apiKey, | ||
method: "tag.getInfo" | ||
}) | ||
@@ -661,7 +660,7 @@ .send(callback); | ||
tagGetSimilar(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getSimilar" | ||
api_key: this.apiKey, | ||
method: "tag.getSimilar" | ||
}) | ||
@@ -684,7 +683,7 @@ .send(callback); | ||
tagGetTopAlbums(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getTopAlbums" | ||
api_key: this.apiKey, | ||
method: "tag.getTopAlbums" | ||
}) | ||
@@ -707,7 +706,7 @@ .send(callback); | ||
tagGetTopArtists(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getTopArtists" | ||
api_key: this.apiKey, | ||
method: "tag.getTopArtists" | ||
}) | ||
@@ -726,6 +725,6 @@ .send(callback); | ||
tagGetTopTags(callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getTopTags" | ||
api_key: this.apiKey, | ||
method: "tag.getTopTags" | ||
}) | ||
@@ -748,7 +747,7 @@ .send(callback); | ||
tagGetTopTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getTopTracks" | ||
api_key: this.apiKey, | ||
method: "tag.getTopTracks" | ||
}) | ||
@@ -769,7 +768,7 @@ .send(callback); | ||
tagGetWeeklyChartList(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "tag.getWeeklyChartList" | ||
api_key: this.apiKey, | ||
method: "tag.getWeeklyChartList" | ||
}) | ||
@@ -792,8 +791,8 @@ .send(callback); | ||
trackAddTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.addTags", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "track.addTags", | ||
sk: this.sessionKey | ||
}) | ||
@@ -816,7 +815,7 @@ .sign(this.secret) | ||
trackGetCorrection(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.getCorrection" | ||
api_key: this.apiKey, | ||
method: "track.getCorrection" | ||
}) | ||
@@ -841,7 +840,7 @@ .send(callback); | ||
trackGetInfo(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.getInfo" | ||
api_key: this.apiKey, | ||
method: "track.getInfo" | ||
}) | ||
@@ -866,7 +865,7 @@ .send(callback); | ||
trackGetSimilar(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.getSimilar" | ||
api_key: this.apiKey, | ||
method: "track.getSimilar" | ||
}) | ||
@@ -891,7 +890,7 @@ .send(callback); | ||
trackGetTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.getTags" | ||
api_key: this.apiKey, | ||
method: "track.getTags" | ||
}) | ||
@@ -915,7 +914,7 @@ .send(callback); | ||
trackGetTopTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.getTopTags" | ||
api_key: this.apiKey, | ||
method: "track.getTopTags" | ||
}) | ||
@@ -937,8 +936,8 @@ .send(callback); | ||
trackLove(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.love", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "track.love", | ||
sk: this.sessionKey | ||
}) | ||
@@ -962,8 +961,8 @@ .sign(this.secret) | ||
trackRemoveTag(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.removeTag", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "track.removeTag", | ||
sk: this.sessionKey | ||
}) | ||
@@ -993,8 +992,8 @@ .sign(this.secret) | ||
trackScrobble(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.scrobble", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "track.scrobble", | ||
sk: this.sessionKey | ||
}) | ||
@@ -1024,16 +1023,9 @@ .sign(this.secret) | ||
trackScrobbleMany(paramsArr, callback) { | ||
if(paramsArr.length === 1) { | ||
return this.trackScrobble(paramsArr[0], callback); | ||
} | ||
const params = {}; | ||
const paramsMany = {}; | ||
let i = paramsArr.length; | ||
paramsArr.forEach((paramsObj, i) => Object | ||
.entries(paramsObj) | ||
.forEach(([name, value]) => params[`${name}[${i}]`] = value)); | ||
while(i--) { | ||
for(const [param, value] of Object.entries(paramsArr[i])) { | ||
paramsMany[`${param}[${i}]`] = value; | ||
} | ||
} | ||
return this.trackScrobble(paramsMany, callback); | ||
return this.trackScrobble(params, callback); | ||
} | ||
@@ -1053,7 +1045,7 @@ | ||
trackSearch(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.search" | ||
api_key: this.apiKey, | ||
method: "track.search" | ||
}) | ||
@@ -1075,8 +1067,8 @@ .send(callback); | ||
trackUnlove(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.unlove", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "track.unlove", | ||
sk: this.sessionKey | ||
}) | ||
@@ -1103,8 +1095,8 @@ .sign(this.secret) | ||
trackUpdateNowPlaying(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "track.updateNowPlaying", | ||
"sk": this.sessionKey | ||
api_key: this.apiKey, | ||
method: "track.updateNowPlaying", | ||
sk: this.sessionKey | ||
}) | ||
@@ -1118,26 +1110,2 @@ .sign(this.secret) | ||
/** | ||
* Get tracks of an artist scrobbled by a user | ||
* @param {Object} params | ||
* @param {string} params.user - User whose scrobbled tracks of an artist to get | ||
* @param {string} params.artist - Artist whose tracks scrobbled by a user to get | ||
* @param {string} [params.startTimestamp] - Timestamp to get tracks from | ||
* @param {string} [params.endTimestamp] - Timestamp to get tracks to | ||
* @param {string} [params.page] - Page number to get | ||
* @param {callback} [callback] | ||
* @returns {(Promise|LastFm)} | ||
*/ | ||
userGetArtistTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getArtistTracks" | ||
}) | ||
.send(callback); | ||
return apiRequest || this; | ||
} | ||
/** | ||
* Get friends of a user | ||
@@ -1154,7 +1122,7 @@ * @param {Object} params | ||
userGetFriends(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getFriends" | ||
api_key: this.apiKey, | ||
method: "user.getFriends" | ||
}) | ||
@@ -1175,7 +1143,7 @@ .send(callback); | ||
userGetInfo(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getInfo" | ||
api_key: this.apiKey, | ||
method: "user.getInfo" | ||
}) | ||
@@ -1198,7 +1166,7 @@ .send(callback); | ||
userGetLovedTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getLovedTracks" | ||
api_key: this.apiKey, | ||
method: "user.getLovedTracks" | ||
}) | ||
@@ -1223,7 +1191,7 @@ .send(callback); | ||
userGetPersonalTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getPersonalTags" | ||
api_key: this.apiKey, | ||
method: "user.getPersonalTags" | ||
}) | ||
@@ -1249,7 +1217,7 @@ .send(callback); | ||
userGetRecentTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getRecentTracks" | ||
api_key: this.apiKey, | ||
method: "user.getRecentTracks" | ||
}) | ||
@@ -1273,7 +1241,7 @@ .send(callback); | ||
userGetTopAlbums(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getTopAlbums" | ||
api_key: this.apiKey, | ||
method: "user.getTopAlbums" | ||
}) | ||
@@ -1297,7 +1265,7 @@ .send(callback); | ||
userGetTopArtists(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getTopArtists" | ||
api_key: this.apiKey, | ||
method: "user.getTopArtists" | ||
}) | ||
@@ -1319,7 +1287,7 @@ .send(callback); | ||
userGetTopTags(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getTopTags" | ||
api_key: this.apiKey, | ||
method: "user.getTopTags" | ||
}) | ||
@@ -1343,7 +1311,7 @@ .send(callback); | ||
userGetTopTracks(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getTopTracks" | ||
api_key: this.apiKey, | ||
method: "user.getTopTracks" | ||
}) | ||
@@ -1366,7 +1334,7 @@ .send(callback); | ||
userGetWeeklyAlbumChart(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getWeeklyAlbumChart" | ||
api_key: this.apiKey, | ||
method: "user.getWeeklyAlbumChart" | ||
}) | ||
@@ -1389,7 +1357,7 @@ .send(callback); | ||
userGetWeeklyArtistChart(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getWeeklyArtistChart" | ||
api_key: this.apiKey, | ||
method: "user.getWeeklyArtistChart" | ||
}) | ||
@@ -1410,7 +1378,7 @@ .send(callback); | ||
userGetWeeklyChartList(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getWeeklyChartList" | ||
api_key: this.apiKey, | ||
method: "user.getWeeklyChartList" | ||
}) | ||
@@ -1433,7 +1401,7 @@ .send(callback); | ||
userGetWeeklyTrackChart(params, callback) { | ||
const apiRequest = (new ApiRequest()) | ||
const apiRequest = new ApiRequest() | ||
.set(params) | ||
.set({ | ||
"api_key": this.apiKey, | ||
"method": "user.getWeeklyTrackChart" | ||
api_key: this.apiKey, | ||
method: "user.getWeeklyTrackChart" | ||
}) | ||
@@ -1440,0 +1408,0 @@ .send(callback); |
{ | ||
"name": "lastfm-node-client", | ||
"version": "1.1.2", | ||
"version": "2.0.0", | ||
"description": "JavaScript library for interfacing with the Last.fm API", | ||
"files": [ | ||
"lib" | ||
], | ||
"main": "lib/LastFm.js", | ||
@@ -24,10 +27,12 @@ "repository": { | ||
"devDependencies": { | ||
"coveralls": "3.0.4", | ||
"eslint": "5.16.0", | ||
"jest": "24.8.0", | ||
"nock": "10.0.6" | ||
"coveralls": "^3.0.6", | ||
"eslint": "^6.4.0", | ||
"eslint-plugin-jest": "^22.17.0", | ||
"eslint-plugin-node": "^10.0.0", | ||
"jest": "^24.9.0", | ||
"nock": "^11.3.4" | ||
}, | ||
"engines": { | ||
"node": ">=7.0.0" | ||
"node": ">=8.10.0" | ||
} | ||
} |
@@ -21,18 +21,15 @@ # Last.Fm Node Client | ||
Node.js **7.0.0** or later is required. | ||
Node.js **8.10.0** or later is required. | ||
## Usage | ||
First, you must instantiate the LastFm Class with an object parameter containing the details of your API account. `apiKey` is required, however since many endpoints of the API do not require authentication, `secret` and `sessionKey` are optional. | ||
First, you must instantiate the LastFm Class with arguments containing the details of your API account. `apiKey` is required, however since many endpoints of the API do not require authentication, `secret` and `sessionKey` are optional. | ||
```js | ||
const LastFm = require("lastfm-node-client"); | ||
const lastFm = new LastFm({ | ||
"apiKey": "API_KEY", | ||
"secret": "SECRET", | ||
"sessionKey": "SESSION_KEY" | ||
}); | ||
const lastFm = new LastFm("API_KEY", "SECRET", "SESSION_KEY"); | ||
``` | ||
### Making requests | ||
### Making Requests | ||
@@ -42,3 +39,5 @@ The Last.fm API is structured into packages and methods, accessed as `Package.method`. The LastFm Class contains directly corresponding methods for each package method, written as `lastFm.packageMethod()`. For example, endpoint `User.getRecentTracks` is accessed as `lastFm.userGetRecentTracks()`. | ||
```js | ||
lastFm.userGetRecentTracks(); | ||
lastFm.userGetRecentTracks({ | ||
user: "USER" | ||
}); | ||
``` | ||
@@ -52,9 +51,9 @@ | ||
lastFm.userGetRecentTracks({ | ||
"user": "USER" | ||
user: "USER" | ||
}); | ||
``` | ||
### Capturing responses | ||
### Capturing Responses and Handling Errors | ||
Every method returns a promise of the pending request by default. To access the response, you can chain `.then()` to the method, or use `await`. | ||
Every method returns a promise of the pending request by default. To access the response, you can chain `.then()` to the method, or use `await`. Errors thrown while making a request or [errors returned by the API](https://www.last.fm/api/errorcodes) will reject the promise. | ||
@@ -65,3 +64,3 @@ Chaining `.then()`: | ||
lastFm.userGetRecentTracks({ | ||
"user": "USER" | ||
user: "USER" | ||
}) | ||
@@ -77,3 +76,3 @@ .then(data => { | ||
const data = await lastFm.userGetRecentTracks({ | ||
"user": "USER" | ||
user: "USER" | ||
}); | ||
@@ -84,9 +83,7 @@ | ||
An optional callback can be passed as the last argument. It is invoked with conventional `(err, data)` parameters; `err` being any exceptions thrown in the event of an error, `data` containing the JSON response of the API upon success. | ||
An optional callback can be passed as the last argument. It is invoked with conventional `(err, data)` arguments; `err` being any errors thrown while making a request or [errors returned by the API](https://www.last.fm/api/errorcodes), `data` containing the JSON response of the API upon success. | ||
**Note**: "Success" in this context means a successful request, however the API may return an error response for a number of reasons detailed in their [error codes documentation](https://www.last.fm/api/errorcodes). | ||
```js | ||
lastFm.userGetRecentTracks({ | ||
"user": "USER" | ||
user: "USER" | ||
}, | ||
@@ -100,3 +97,3 @@ (err, data)) => { | ||
## Utility methods | ||
## Utility Methods | ||
@@ -112,14 +109,14 @@ These methods do not correspond to an exact API endpoint, but are abstractions of the already provided methods to provide easier usage. | ||
{ | ||
"artist": "ARTIST", | ||
"album": "ALBUM", | ||
"track": "TRACK", | ||
"timestamp": "TIMESTAMP" | ||
artist: "ARTIST", | ||
album: "ALBUM", | ||
track: "TRACK", | ||
timestamp: "TIMESTAMP" | ||
}, | ||
{ | ||
"artist": "ARTIST", | ||
"album": "ALBUM", | ||
"track": "TRACK", | ||
"timestamp": "TIMESTAMP" | ||
artist: "ARTIST", | ||
album: "ALBUM", | ||
track: "TRACK", | ||
timestamp: "TIMESTAMP" | ||
} | ||
]); | ||
``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
47818
6
5
1281
115
2