Skip to content

Commit

Permalink
Usage and typings updates (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
scriptex committed Oct 31, 2023
1 parent 5deb751 commit 06fcd0d
Show file tree
Hide file tree
Showing 21 changed files with 1,993 additions and 548 deletions.
28 changes: 28 additions & 0 deletions .codebeatsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"TYPESCRIPT": {
"ABC": [
30,
60,
120,
180
],
"LOC": [
75,
120,
180,
240
],
"TOTAL_LOC": [
600,
750,
960,
1350
],
"BLOCK_NESTING": [
9,
12,
15,
18
]
}
}
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
LAST_FM_API_KEY=""
9 changes: 7 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@ jobs:
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- run: yarn build
- name: Build
run: |
yarn
yarn build
yarn demo
env:
LAST_FM_API_KEY: ${{ secrets.LAST_FM_API_KEY }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ ehthumbs.db
Thumbs.db

# Project specific
.env
/dist
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Thumbs.db
.github
_config.yml
_.config.yml
.codebeatsettings
.env
.env.example
.editorconfig
.gitattributes
.gitignore
Expand All @@ -37,3 +40,4 @@ tslint.json
yarn.lock

!dist
demo
86 changes: 86 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// @ts-nocheck

import { config } from 'dotenv';

import {
LastFMGeo,
LastFMTag,
LastFMUser,
LastFMAlbum,
LastFMChart,
LastFMTrack,
LastFMArtist,
LastFMLibrary
} from '../dist/index.js';

config();

const API_KEY = process.env.LAST_FM_API_KEY;

const geo = new LastFMGeo(API_KEY);
const tag = new LastFMTag(API_KEY);
const user = new LastFMUser(API_KEY);
const album = new LastFMAlbum(API_KEY);
const chart = new LastFMChart(API_KEY);
const track = new LastFMTrack(API_KEY);
const artist = new LastFMArtist(API_KEY);
const library = new LastFMLibrary(API_KEY);

const print = r => console.log(JSON.stringify(r, null, 2));

geo.getTopArtists({ country: 'Bulgaria', limit: 2 }).then(print);
geo.getTopTracks({ country: 'Bulgaria', limit: 2 }).then(print);

tag.getInfo({ tag: 'techno', limit: 2 }).then(print);
tag.getSimilar({ tag: 'techno', limit: 2 }).then(print);
tag.getTopAlbums({ tag: 'techno', limit: 2 }).then(print);
tag.getTopArtists({ tag: 'techno', limit: 2 }).then(print);
tag.getTopTags().then(print);
tag.getTopTracks({ tag: 'techno', limit: 2 }).then(print);
tag.getWeeklyChartList({ tag: 'techno' }).then(print);

user.getFriends({ user: 'scriptex', limit: 2 }).then(print);
user.getInfo({ user: 'scriptex' }).then(print);
user.getLovedTracks({ user: 'scriptex', limit: 2 }).then(print);
user.getPersonalTags({ user: 'scriptex', taggingtype: 'album', tag: 'techno', limit: 2 }).then(print);
user.getPersonalTags({ user: 'scriptex', taggingtype: 'artist', tag: 'techno', limit: 2 }).then(print);
user.getPersonalTags({ user: 'scriptex', taggingtype: 'track', tag: 'techno', limit: 2 }).then(print);
user.getRecentTracks({ user: 'scriptex', limit: 2 }).then(print);
user.getTopAlbums({ user: 'scriptex', limit: 2 }).then(print);
user.getTopArtists({ user: 'scriptex', limit: 2 }).then(print);
user.getTopTags({ user: 'scriptex', limit: 2 }).then(print);
user.getTopTracks({ user: 'scriptex', limit: 2 }).then(print);
user.getWeeklyAlbumChart({ user: 'scriptex', limit: 2 }).then(print);
user.getWeeklyArtistChart({ user: 'scriptex', limit: 2 }).then(print);
user.getWeeklyChartList({ user: 'scriptex' }).then(print);
user.getWeeklyTrackChart({ user: 'scriptex' }).then(print);

album.getInfo({ album: 'The Fat of The Land', artist: 'The Prodigy' }).then(print);
//NOSONAR
// album.getTags({ album: 'The Fat of The Land', artist: 'The Prodigy' }).then(print);
album.getTopTags({ album: 'The Fat of The Land', artist: 'The Prodigy' }).then(print);
album.search({ album: 'The Fat of The Land', artist: 'The Prodigy' }).then(print);

chart.getTopArtists({ limit: 2 }).then(print);
chart.getTopTags({ limit: 2 }).then(print);
chart.getTopTracks({ limit: 2 }).then(print);

track.getCorrection({ track: 'Firestarter', artist: 'The Prodigy' }).then(print);
track.getInfo({ track: 'Firestarter', artist: 'The Prodigy' }).then(print);
track.getSimilar({ track: 'Firestarter', artist: 'The Prodigy', limit: 2 }).then(print);
//NOSONAR
// track.getTags({ track: 'Firestarter', artist: 'The Prodigy', limit: 2 }).then(print);
track.getTopTags({ track: 'Firestarter', artist: 'The Prodigy', limit: 2 }).then(print);
track.search({ track: 'Firestarter', artist: 'The Prodigy', limit: 2 }).then(print);

artist.getCorrection({ artist: 'The Prodigy' }).then(print);
artist.getInfo({ artist: 'The Prodigy' }).then(print);
artist.getSimilar({ artist: 'The Prodigy', limit: 2 }).then(print);
//NOSONAR
// artist.getTags({ artist: 'The Prodigy' }).then(print);
artist.getTopAlbums({ artist: 'The Prodigy', limit: 2 }).then(print);
artist.getTopTags({ artist: 'The Prodigy' }).then(print);
artist.getTopTracks({ artist: 'The Prodigy', limit: 2 }).then(print);
artist.search({ artist: 'The Prodigy', limit: 2 }).then(print);

library.getArtists({ user: 'scriptex', limit: 2 }).then(print);
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lastfm-ts-api",
"version": "1.0.2",
"version": "2.0.0",
"description": "An API client for the Last.FM API written in TypeScript",
"keywords": [
"Last.FM",
Expand All @@ -17,18 +17,21 @@
"author": "Atanas Atanasov <[email protected]> (https://atanas.info)",
"funding": "https://github.com/sponsors/scriptex",
"main": "dist/index.js",
"type": "module",
"types": "dist/index.d.ts",
"repository": {
"type": "git",
"url": "github:scriptex/lastfm-ts-api"
},
"scripts": {
"watch": "tsc -w",
"build": "tsc"
"build": "tsc",
"demo": "node demo/index.js"
},
"dependencies": {},
"devDependencies": {
"@types/node": "20.8.9",
"dotenv": "16.3.1",
"tslib": "2.6.2",
"typescript": "5.2.2"
}
Expand Down
103 changes: 32 additions & 71 deletions src/album.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,21 @@
import { LastFM } from './base';
import { LastFMParam, LastFMApiRequest, LastFMRequestParams, LastFMUnknownFunction } from './api-request';

export interface LastFMAlbumParams {
readonly album: string;
readonly artist: string;
}

export interface LastFMAlbumOptionalParams {
readonly mbid?: string;
readonly autocorrect?: 0 | 1;
}

export interface LastFMAlbumAddTagsParams extends LastFMRequestParams<LastFMParam>, LastFMAlbumParams {
readonly tags: string | string[];
}

export interface LastFMAlbumGetInfoParams
extends LastFMRequestParams<0 | 1 | void>,
LastFMAlbumParams,
LastFMAlbumOptionalParams {
readonly lang?: string;
readonly username?: string;
}

export interface LastFMAlbumGetTagsParams
extends LastFMRequestParams<0 | 1 | void>,
LastFMAlbumParams,
LastFMAlbumOptionalParams {
readonly user?: string;
}

export interface LastFMAlbumGetTopTagsParams
extends LastFMRequestParams<0 | 1 | void>,
LastFMAlbumParams,
LastFMAlbumOptionalParams {}

export interface LastFMAlbumRemoveLastFMTagParams extends LastFMRequestParams<LastFMParam>, LastFMAlbumParams {
readonly tag: string;
}

export interface LastFMAlbumSearchParams extends LastFMRequestParams<number | void> {
readonly page?: number;
readonly album: string;
readonly limit?: number;
}
import { LastFM } from './base.js';
import { LastFMApiRequest } from './api-request.js';
import {
LastFMUnknownFunction,
LastFMAlbumSearchParams,
LastFMAlbumAddTagsParams,
LastFMAlbumGetInfoParams,
LastFMAlbumGetTagsParams,
LastFMAlbumSearchResponse,
LastFMAlbumGetInfoResponse,
LastFMAlbumGetTagsResponse,
LastFMAlbumGetTopTagsParams,
LastFMAlbumRemoveLastFMTagParams
} from './types.js';

export class LastFMAlbum extends LastFM {
constructor(apiKey: string, secret?: string, sessionKey?: string) {
super(apiKey, secret, sessionKey);
}

public addTags(
params: LastFMAlbumAddTagsParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
public addTags(params: LastFMAlbumAddTagsParams, callback?: LastFMUnknownFunction): Promise<void> {
return new LastFMApiRequest<void>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -67,9 +28,9 @@ export class LastFMAlbum extends LastFM {

public getInfo(
params: LastFMAlbumGetInfoParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumGetInfoResponse> {
return new LastFMApiRequest<LastFMAlbumGetInfoResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -80,9 +41,9 @@ export class LastFMAlbum extends LastFM {

public getTags(
params: LastFMAlbumGetTagsParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumGetTagsResponse> {
return new LastFMApiRequest<LastFMAlbumGetTagsResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -93,9 +54,9 @@ export class LastFMAlbum extends LastFM {

public getTopTags(
params: LastFMAlbumGetTopTagsParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumGetTagsResponse> {
return new LastFMApiRequest<LastFMAlbumGetTagsResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -104,11 +65,8 @@ export class LastFMAlbum extends LastFM {
.send(callback);
}

public removeTag(
params: LastFMAlbumRemoveLastFMTagParams,
callback: LastFMUnknownFunction
): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
public removeTag(params: LastFMAlbumRemoveLastFMTagParams, callback?: LastFMUnknownFunction): Promise<void> {
return new LastFMApiRequest<void>()
.set(params)
.set({
api_key: this.apiKey,
Expand All @@ -119,8 +77,11 @@ export class LastFMAlbum extends LastFM {
.send('POST', callback);
}

public search(params: LastFMAlbumSearchParams, callback: LastFMUnknownFunction): Promise<LastFMApiRequest> | void {
return new LastFMApiRequest()
public search(
params: LastFMAlbumSearchParams,
callback?: LastFMUnknownFunction
): Promise<LastFMAlbumSearchResponse> {
return new LastFMApiRequest<LastFMAlbumSearchResponse>()
.set(params)
.set({
api_key: this.apiKey,
Expand Down
Loading

0 comments on commit 06fcd0d

Please sign in to comment.