Skip to content

Commit

Permalink
lastFm.userGetInfo defaults to authenticated user when params argumen…
Browse files Browse the repository at this point in the history
…t or params.user is omitted
  • Loading branch information
rattletone committed Oct 8, 2019
1 parent e53d486 commit f91203b
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 16 deletions.
12 changes: 5 additions & 7 deletions README.md
Expand Up @@ -34,21 +34,19 @@ const lastFm = new LastFm("API_KEY", "SECRET", "SESSION_KEY");

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()`.

Parameters can be passed to the API through the `params` argument as an object that will be sent directly with the request, either as a query for a GET request, or a body for a POST request. The property names will not be transformed or abstracted, and so they must match the endpoint parameters exactly.

```js
lastFm.userGetRecentTracks({
user: "USER"
});
```

Parameters can be passed to the API through the `params` argument as an object that will be sent directly with the request, either as a query for a GET request, or a body for a POST request. The property names will not be transformed or abstracted, and so they must match the endpoint parameters exactly.
#### Notes

**Note**: Endpoints `Auth.getToken` and `Tag.getTopTags` do not require additional parameters, as such, methods `lastFm.authGetToken()` and `lastFm.tagGetTopTags()` do not accept a `params` argument.
* `lastFm.authGetToken()` and `lastFm.tagGetTopTags()` do not accept a `params` argument, as endpoints `Auth.getToken` and `Tag.getTopTags` do not require additional parameters.

```js
lastFm.userGetRecentTracks({
user: "USER"
});
```
* `lastFm.userGetInfo()` params.user and params argument are optional and will default to the authenticated user in accordance with endpoint `User.getInfo`.

### Capturing Responses and Handling Errors

Expand Down
18 changes: 13 additions & 5 deletions lib/LastFm.js
Expand Up @@ -1085,22 +1085,30 @@ class LastFm {

/**
* Get info of a user
* @param {Object} params
* @param {string} params.user - User to get info of
* @param {Object} [params]
* @param {string} [params.user] - User to get info of
* @param {callback} [callback]
* @returns {(Promise|LastFm)}
*/

userGetInfo(params, callback) {
callback = callback === undefined ? typeof params === "function" ? params : undefined : callback;
params = typeof params === "object" ? params : {};

const apiRequest = new ApiRequest()
.set(params)
.set({
api_key: this.apiKey,
method: "user.getInfo"
})
.send(callback);
});

return apiRequest || this;
if(!params.user) {
apiRequest
.set({ sk: this.sessionKey })
.sign(this.secret);
}

return apiRequest.send(params.user ? "GET" : "POST", callback) || this;
}

/**
Expand Down
58 changes: 54 additions & 4 deletions test/LastFm.js
Expand Up @@ -3333,10 +3333,10 @@ describe("lastFm()", () => {
expect(ApiRequest).toHaveBeenCalled();
});

it("calls ApiRequest.set() twice", () => {
it("calls ApiRequest.set() twice when params argument is passed and \"user\" property is set", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);

lastFm.userGetInfo({});
lastFm.userGetInfo({ user: "USER" });

expect(mockSet).toHaveBeenCalledTimes(2);
});
Expand Down Expand Up @@ -3364,13 +3364,63 @@ describe("lastFm()", () => {
});
});

it("calls ApiRequest.send(), passing callback argument as first argument", () => {
it("calls ApiRequest.set() a third time when called without arguments, passing object with \"sk\" property set to \"lastFm.sessionKey\"", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);

lastFm.userGetInfo();

expect(mockSet.mock.calls[2][0]).toStrictEqual({ sk: lastFm.sessionKey });
});

it("calls ApiRequest.set() a third time when called with a function as first argument, passing object with \"sk\" property set to \"lastFm.sessionKey\"", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);

lastFm.userGetInfo(() => {});

expect(mockSet.mock.calls[2][0]).toStrictEqual({ sk: lastFm.sessionKey });
});

it("calls ApiRequest.sign() when called without argument, passing \"lastFm.secret\" as first argument", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);

lastFm.userGetInfo();

expect(mockSign).toHaveBeenCalledWith(lastFm.secret);
});

it("calls ApiRequest.sign() when called with a function as first argument, passing \"lastFm.secret\" as first argument", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);

lastFm.userGetInfo(() => {});

expect(mockSign).toHaveBeenCalledWith(lastFm.secret);
});

it("calls ApiRequest.send(), passing \"GET\" as first argument and callback argument as second argument, when \"user\" is set in params argument", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);
const callback = () => {};

lastFm.userGetInfo({ user: "USER" }, callback);

expect(mockSend).toHaveBeenCalledWith("GET", callback);
});

it("calls ApiRequest.send(), passing \"POST\" as first argument and callback argument as second argument, when \"user\" is not set in params argument", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);
const callback = () => {};

lastFm.userGetInfo({}, callback);

expect(mockSend).toHaveBeenCalledWith(callback);
expect(mockSend).toHaveBeenCalledWith("POST", callback);
});

it("calls ApiRequest.send(), passing \"POST\" as first argument and callback argument as second argument, when callback is passed as first argument", () => {
const lastFm = new LastFm(apiKey, secret, sessionKey);
const callback = () => {};

lastFm.userGetInfo(callback);

expect(mockSend).toHaveBeenCalledWith("POST", callback);
});

it("returns what ApiRequest.send() returns", () => {
Expand Down

0 comments on commit f91203b

Please sign in to comment.