Skip to content

Commit

Permalink
Add user agent support
Browse files Browse the repository at this point in the history
  • Loading branch information
yayuyokitano authored Nov 25, 2020
1 parent 90a424f commit 86ebc85
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lastfm-typed",
"version": "0.3.0",
"version": "0.4.0",
"description": "Typed API wrapper for Last.FM using promises",
"main": "dist/index.js",
"author": "yayuyokitano",
Expand Down
10 changes: 9 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import {LFMArgumentObject, LFMRequest} from "./request";

export default class LFMBase {

protected key:string;
protected secret:string;
protected userAgent:string;

public constructor(apiKey:string, apiSecret:string = "") {
public constructor(apiKey:string, apiSecret:string = "", userAgent:string = "lastfm-typed-npm") {
this.key = apiKey;
this.secret = apiSecret;
this.userAgent = userAgent;
}

protected checkLimit(limit:number|undefined, maxLimit:number) {
Expand Down Expand Up @@ -43,4 +47,8 @@ export default class LFMBase {
return query.replace(/:/g, " ");
}

protected async sendRequest(apiKey:string, apiSecret:string, params:LFMArgumentObject) {
return await new LFMRequest(apiKey, apiSecret, this.userAgent, params).execute();
}

}
13 changes: 6 additions & 7 deletions src/classes/album.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as AlbumInterface from "../interfaces/albumInterface";
import Base from "../base";
import { AlbumInput } from "../interfaces/shared";
Expand All @@ -11,39 +10,39 @@ export default class AlbumClass extends Base {
tags = tags.join(",");
}

return await new LFMRequest(this.key, this.secret, { method: "album.addTags", tags, sk, artist, album }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "album.addTags", tags, sk, artist, album }) as {};

}

public async getInfo(album:AlbumInput, params?:{autocorrect?:0|1, username?:string, sk?:string, lang?:string}) {

return (await new LFMRequest(this.key, this.secret, { method: "album.getInfo", ...album, ...params }).execute()).album as AlbumInterface.getInfo;
return (await this.sendRequest(this.key, this.secret, { method: "album.getInfo", ...album, ...params })).album as AlbumInterface.getInfo;

}

public async getTags(album:AlbumInput, usernameOrSessionKey:string, params?:{autocorrect?:0|1}) {

return this.convertGetTags((await new LFMRequest(this.key, this.secret, { method: "album.getTags", ...album, user: usernameOrSessionKey, ...params }).execute()).tags) as AlbumInterface.getTags;
return this.convertGetTags((await this.sendRequest(this.key, this.secret, { method: "album.getTags", ...album, user: usernameOrSessionKey, ...params })).tags) as AlbumInterface.getTags;

}

public async getTopTags(album:AlbumInput, params?:{autocorrect?:0|1}) {

return (await new LFMRequest(this.key, this.secret, { method: "album.getTopTags", ...album, ...params }).execute()).toptags as AlbumInterface.getTopTags;
return (await this.sendRequest(this.key, this.secret, { method: "album.getTopTags", ...album, ...params })).toptags as AlbumInterface.getTopTags;

}

public async removeTag(artist:string, album:string, tag:string, sk:string) {

return await new LFMRequest(this.key, this.secret, { method: "album.removeTag", tag, sk, artist, album }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "album.removeTag", tag, sk, artist, album }) as {};

}

public async search(album:string, params?:{limit?:number, page?:number}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, {method: "album.search", album, ...params }).execute()).results as AlbumInterface.search;
return (await this.sendRequest(this.key, this.secret, {method: "album.search", album, ...params })).results as AlbumInterface.search;

}

Expand Down
21 changes: 10 additions & 11 deletions src/classes/artist.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as ArtistInterface from "../interfaces/artistInterface";
import Base from "../base";
import { ArtistInput } from "../interfaces/shared";
Expand All @@ -11,69 +10,69 @@ export default class ArtistClass extends Base {
tags = tags.join(",");
}

return await new LFMRequest(this.key, this.secret, { method: "artist.addTags", tags, sk, artist }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "artist.addTags", tags, sk, artist }) as {};

}

public async getCorrection(artist:string) {

return (((await new LFMRequest(this.key, this.secret, { method: "artist.getCorrection", artist }).execute())?.corrections?.correction) || {}) as ArtistInterface.getCorrection|{};
return (((await this.sendRequest(this.key, this.secret, { method: "artist.getCorrection", artist }))?.corrections?.correction) || {}) as ArtistInterface.getCorrection|{};

}

public async getInfo(artist:ArtistInput, params?:{autocorrect?:0|1, username?:string, sk?:string, lang?:string}) {

return (await new LFMRequest(this.key, this.secret, { method: "artist.getInfo", ...artist, ...params }).execute()).artist as ArtistInterface.getInfo;
return (await this.sendRequest(this.key, this.secret, { method: "artist.getInfo", ...artist, ...params })).artist as ArtistInterface.getInfo;

}

public async getSimilar(artist:ArtistInput, params?:{limit?:number, autocorrect?:0|1}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, { method: "artist.getSimilar", ...artist, ...params }).execute()).similarartists as ArtistInterface.getSimilar;
return (await this.sendRequest(this.key, this.secret, { method: "artist.getSimilar", ...artist, ...params })).similarartists as ArtistInterface.getSimilar;

}

public async getTags(artist:ArtistInput, usernameOrSessionKey:string, params?:{autocorrect?:0|1}) {

return this.convertGetTags((await new LFMRequest(this.key, this.secret, { method: "artist.getTags", ...artist, user: usernameOrSessionKey, ...params }).execute()).tags) as ArtistInterface.getTags;
return this.convertGetTags((await this.sendRequest(this.key, this.secret, { method: "artist.getTags", ...artist, user: usernameOrSessionKey, ...params })).tags) as ArtistInterface.getTags;

}

public async getTopAlbums(artist:ArtistInput, params?:{autocorrect?:0|1, page?:number, limit?:number}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, { method: "artist.getTopAlbums", ...artist, ...params }).execute()).topalbums as ArtistInterface.getTopAlbums;
return (await this.sendRequest(this.key, this.secret, { method: "artist.getTopAlbums", ...artist, ...params })).topalbums as ArtistInterface.getTopAlbums;

}

public async getTopTags(artist:ArtistInput, params?:{autocorrect?:0|1}) {

return (await new LFMRequest(this.key, this.secret, { method: "artist.getTopTags", ...artist, ...params }).execute()).toptags as ArtistInterface.getTopTags;
return (await this.sendRequest(this.key, this.secret, { method: "artist.getTopTags", ...artist, ...params })).toptags as ArtistInterface.getTopTags;

}

public async getTopTracks(artist:ArtistInput, params?:{autocorrect?:0|1, page?:number, limit?:number}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, { method: "artist.getTopTracks", ...artist, ...params }).execute()).toptracks as ArtistInterface.getTopTracks;
return (await this.sendRequest(this.key, this.secret, { method: "artist.getTopTracks", ...artist, ...params })).toptracks as ArtistInterface.getTopTracks;

}

public async removeTag(artist:string, tag:string, sk:string) {

return await new LFMRequest(this.key, this.secret, { method: "artist.removeTag", tag, sk, artist }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "artist.removeTag", tag, sk, artist }) as {};

}

public async search(artist:string, params?:{limit:number, page:number}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, {method: "artist.search", artist, ...params}).execute()).results as ArtistInterface.search;
return (await this.sendRequest(this.key, this.secret, {method: "artist.search", artist, ...params})).results as ArtistInterface.search;

}

Expand Down
7 changes: 3 additions & 4 deletions src/classes/auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import LFMRequest from "../request";
import * as AuthInterface from "../interfaces/authInterface";
import Base from "../base";

export default class AuthClass extends Base {

public async getToken() {

const token = await new LFMRequest(this.key, this.secret, { method: "auth.getToken" }).execute();
const token = await this.sendRequest(this.key, this.secret, { method: "auth.getToken" });
if (typeof token.token === "undefined") {
throw Error ("Something went wrong while getting the token. Probably because of Last.FM");
}
Expand All @@ -17,13 +16,13 @@ export default class AuthClass extends Base {

public async getSession(token:string) {

return (await new LFMRequest(this.key, this.secret, { method: "auth.getSession", token }).execute()).session as AuthInterface.getSession;
return (await this.sendRequest(this.key, this.secret, { method: "auth.getSession", token })).session as AuthInterface.getSession;

}

public async getMobileSession(username:string, password:string) {

return (await new LFMRequest(this.key, this.secret, { method: "auth.getMobileSession", username, password }).execute()).session as AuthInterface.getSession;
return (await this.sendRequest(this.key, this.secret, { method: "auth.getMobileSession", username, password })).session as AuthInterface.getSession;

}

Expand Down
3 changes: 1 addition & 2 deletions src/classes/chart.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as ChartInterface from "../interfaces/chartInterface";
import Base from "../base";

Expand Down Expand Up @@ -26,7 +25,7 @@ export default class ChartClass extends Base {

this.checkLimit(params?.limit, 1000);

return await new LFMRequest(this.key, this.secret, {method, ...params}).execute();
return await this.sendRequest(this.key, this.secret, {method, ...params});

}

Expand Down
3 changes: 1 addition & 2 deletions src/classes/geo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as GeoInterface from "../interfaces/geoInterface";
import Base from "../base";

Expand All @@ -20,7 +19,7 @@ export default class GeoClass extends Base {

this.checkLimit(params?.limit, 1000);

return await new LFMRequest(this.key, this.secret, {method, country, ...params}).execute();
return await this.sendRequest(this.key, this.secret, {method, country, ...params});

}

Expand Down
3 changes: 1 addition & 2 deletions src/classes/library.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as LibraryInterface from "../interfaces/libraryInterface";
import Base from "../base";

Expand All @@ -8,7 +7,7 @@ export default class LibraryClass extends Base {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, { method: "library.getArtists", user: usernameOrSessionKey, ...params }).execute()).artists as LibraryInterface.getArtists;
return (await this.sendRequest(this.key, this.secret, { method: "library.getArtists", user: usernameOrSessionKey, ...params })).artists as LibraryInterface.getArtists;

}

Expand Down
5 changes: 2 additions & 3 deletions src/classes/tag.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as TagInterface from "../interfaces/tagInterface";
import {ShortMetadata} from "../interfaces/shared";
import Base from "../base";
Expand All @@ -7,7 +6,7 @@ export default class TagClass extends Base {

public async getInfo(tag:string, params?:{lang?:string}) {

return (await new LFMRequest(this.key, this.secret, {method: "tag.getInfo", tag, ...params}).execute()).tag as TagInterface.getInfo;
return (await this.sendRequest(this.key, this.secret, {method: "tag.getInfo", tag, ...params})).tag as TagInterface.getInfo;

}

Expand Down Expand Up @@ -54,7 +53,7 @@ export default class TagClass extends Base {

this.checkLimit(params?.limit || params?.num_res, 1000);

return await new LFMRequest(this.key, this.secret, {method, tag, ...params}).execute();
return await this.sendRequest(this.key, this.secret, {method, tag, ...params});

}

Expand Down
25 changes: 12 additions & 13 deletions src/classes/track.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import LFMRequest from "../request";
import * as TrackInterface from "../interfaces/trackInterface";
import Base from "../base";
import { TrackInput } from "../interfaces/shared";
Expand All @@ -23,51 +22,51 @@ export default class TrackClass extends Base {
tags = tags.join(",");
}

return await new LFMRequest(this.key, this.secret, { method: "track.addTags", tags, sk, artist, track }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "track.addTags", tags, sk, artist, track }) as {};

}

public async getCorrection(artist:string, track:string) {

return (((await new LFMRequest(this.key, this.secret, { method: "track.getCorrection", artist, track }).execute())?.corrections?.correction) || {}) as TrackInterface.getCorrection|{};
return (((await this.sendRequest(this.key, this.secret, { method: "track.getCorrection", artist, track }))?.corrections?.correction) || {}) as TrackInterface.getCorrection|{};

}

public async getInfo(track:TrackInput, params?:{autocorrect?:0|1, username?:string, sk?:string}) {

return (await new LFMRequest(this.key, this.secret, { method: "track.getInfo", ...track, ...params }).execute()).track as TrackInterface.getInfo;
return (await this.sendRequest(this.key, this.secret, { method: "track.getInfo", ...track, ...params })).track as TrackInterface.getInfo;

}

public async getSimilar(track:TrackInput, params?:{limit?:number, autocorrect?:0|1}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, { method: "track.getSimilar", ...track, ...params }).execute()).similartracks as TrackInterface.getSimilar;
return (await this.sendRequest(this.key, this.secret, { method: "track.getSimilar", ...track, ...params })).similartracks as TrackInterface.getSimilar;

}

public async getTags(track:TrackInput, usernameOrSessionKey:string, params?:{autocorrect?:0|1}) {

return this.convertGetTags((await new LFMRequest(this.key, this.secret, { method: "track.getTags", ...track, user: usernameOrSessionKey, ...params }).execute()).tags) as TrackInterface.getTags;
return this.convertGetTags((await this.sendRequest(this.key, this.secret, { method: "track.getTags", ...track, user: usernameOrSessionKey, ...params })).tags) as TrackInterface.getTags;

}

public async getTopTags(track:TrackInput, params?:{autocorrect?:0|1}) {

return (await new LFMRequest(this.key, this.secret, { method: "track.getTopTags", ...track, ...params }).execute()).toptags as TrackInterface.getTopTags;
return (await this.sendRequest(this.key, this.secret, { method: "track.getTopTags", ...track, ...params })).toptags as TrackInterface.getTopTags;

}

public async love(artist:string, track:string, sk:string) {

return await new LFMRequest(this.key, this.secret, { method: "track.love", artist, track, sk }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "track.love", artist, track, sk }) as {};

}

public async removeTag(artist:string, track:string, tag:string, sk:string) {

return await new LFMRequest(this.key, this.secret, { method: "track.removeTag", tag, sk, artist, track }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "track.removeTag", tag, sk, artist, track }) as {};

}

Expand All @@ -83,21 +82,21 @@ export default class TrackClass extends Base {
}
}

return (await new LFMRequest(this.key, this.secret, {method: "track.scrobble", ...params, sk}).execute()).scrobbles as TrackInterface.scrobble;
return (await this.sendRequest(this.key, this.secret, {method: "track.scrobble", ...params, sk})).scrobbles as TrackInterface.scrobble;

}

public async search(track:string, params?:{limit?:number, page?:number, artist?:string}) {

this.checkLimit(params?.limit, 1000);

return (await new LFMRequest(this.key, this.secret, {method: "track.search", track, ...params}).execute()).results as TrackInterface.search;
return (await this.sendRequest(this.key, this.secret, {method: "track.search", track, ...params})).results as TrackInterface.search;

}

public async unlove(artist:string, track:string, sk:string) {

return await new LFMRequest(this.key, this.secret, { method: "track.unlove", artist, track, sk }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "track.unlove", artist, track, sk }) as {};

}

Expand All @@ -109,7 +108,7 @@ export default class TrackClass extends Base {
albumArtist?:string;
}) {

return await new LFMRequest(this.key, this.secret, { method: "track.updateNowPlaying", artist, track, sk, ...params }).execute() as {};
return await this.sendRequest(this.key, this.secret, { method: "track.updateNowPlaying", artist, track, sk, ...params }) as {};

}

Expand Down
Loading

0 comments on commit 86ebc85

Please sign in to comment.