From 7c57c6791d75ecf22a81ceb11c8765f62f8427f3 Mon Sep 17 00:00:00 2001 From: rattletone Date: Fri, 2 Aug 2019 17:55:36 +0100 Subject: [PATCH] Named ApiRequest.sign() parameters. Fixed ApiRequest test labels. --- lib/ApiRequest.js | 7 ++++--- lib/LastFm.js | 20 ++++++++++---------- test/ApiRequest.js | 14 +++++++------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/lib/ApiRequest.js b/lib/ApiRequest.js index ef4d1ce..55abe37 100644 --- a/lib/ApiRequest.js +++ b/lib/ApiRequest.js @@ -101,9 +101,10 @@ class ApiRequest { * @param {Function} [callback] - Callback of API request */ - 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; + send(method, callback) { + callback = callback === undefined ? typeof method === "function" ? method : undefined : typeof callback === "function" ? callback : undefined; + method = typeof method === "string" ? method : undefined; + const params = Object.fromEntries(Object .entries(this) .filter(([key]) => key !== "callback") diff --git a/lib/LastFm.js b/lib/LastFm.js index c56e203..7b4d60c 100644 --- a/lib/LastFm.js +++ b/lib/LastFm.js @@ -20,21 +20,21 @@ class LastFm { throw new TypeError("apiKey must be of type string"); } - if(secret !== undefined && typeof secret !== "string") { - throw new TypeError("secret must be of type string"); - } - - if(sessionKey !== undefined && typeof sessionKey !== "string") { - throw new TypeError("sessionKey must be of type string"); - } - this.apiKey = apiKey; - if(secret) { + if(secret !== undefined) { + if(typeof secret !== "string") { + throw new TypeError("secret must be of type string"); + } + this.secret = secret; } - if(sessionKey) { + if(sessionKey !== undefined) { + if(typeof sessionKey !== "string") { + throw new TypeError("sessionKey must be of type string"); + } + this.sessionKey = sessionKey; } } diff --git a/test/ApiRequest.js b/test/ApiRequest.js index 3064546..9b102b8 100644 --- a/test/ApiRequest.js +++ b/test/ApiRequest.js @@ -106,7 +106,7 @@ describe("ApiRequest()", () => { }); }); - test("invoke callback if funtion is passed as the only argument", done => { + test("invoke callback if function is passed as the first argument and no second argument is passed", done => { const apiRequest = new ApiRequest(); nock("http://ws.audioscrobbler.com") @@ -121,7 +121,7 @@ describe("ApiRequest()", () => { }); }); - test("invoke callback if funtion is passed as the second argument", done => { + test("invoke callback if function is passed as the second argument", done => { const apiRequest = new ApiRequest(); nock("http://ws.audioscrobbler.com") @@ -136,7 +136,7 @@ describe("ApiRequest()", () => { }); }); - test("return promise if function is not passed as the only argument", () => { + test("return promise if function is not passed as the first argument and no second argument is passed", () => { const apiRequest = new ApiRequest(); nock("http://ws.audioscrobbler.com") @@ -215,10 +215,10 @@ describe("ApiRequest()", () => { .replyWithError({}); apiRequest.send((err, data) => { - expect(err).toBeDefined(); - expect(data).toBeNull(); - done(); - }); + expect(err).toBeDefined(); + expect(data).toBeNull(); + done(); + }); }); }); });