lastfm-nodejs-client 1.2.4 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ import { WeeklyTrackChartResponse } from '../@types';
2
+ import { createOptions } from './createOptions';
3
+ import request from './request';
4
+
5
+
6
+ /**
7
+ * GET: Weekly track chart - LastFM
8
+ *
9
+ * https://www.last.fm/api/show/user.getWeeklyTrackChart
10
+ * @returns Weekly track chart
11
+ */
12
+ export function getWeeklyTrackChart(
13
+ method: string,
14
+ user: string,
15
+ period: string,
16
+ limit: string,
17
+ ): Promise<WeeklyTrackChartResponse> {
18
+ const options = createOptions(method, user, period, limit);
19
+ return request<WeeklyTrackChartResponse>(options);
20
+ }
package/src/index.ts CHANGED
@@ -1,256 +1,18 @@
1
- import config from './config';
2
- import method from './method';
3
- import request from './request';
4
- import {
5
- AuthResponse,
6
- LovedTracksResponse,
7
- RecentTracksResponse,
8
- TopAlbumsResponse,
9
- TopArtistsResponse,
10
- TopTrackResponse,
11
- UserResponse,
12
- WeeklyAlbumChartResponse,
13
- WeeklyArtistChartResponse,
14
- WeeklyChartListResponse,
15
- WeeklyTrackChartResponse,
16
- } from '../@types';
17
-
18
- function LastFmApi() {
19
- /**
20
- * POST: Auth - LastFM
21
- *
22
- * https://www.last.fm/api/show/auth.getToken
23
- *
24
- * Authentication tokens are API account specific.
25
- * They are valid for 60 minutes from the moment they are granted.
26
- * Can only used once (they are consumed when a session is created).
27
- * @returns Auth token
28
- */
29
- function auth(
30
- method: string,
31
- user: string,
32
- period: string,
33
- limit: string,
34
- ): Promise<AuthResponse> {
35
- const options = {
36
- method,
37
- user,
38
- period,
39
- limit,
40
- }
41
- return request<AuthResponse>(options);
42
- }
43
-
44
- /**
45
- * GET: User profile information - LastFM
46
- *
47
- * https://www.last.fm/api/show/user.getInfo
48
- * @returns User profile data
49
- */
50
- function getInfo(
51
- method: string,
52
- user: string,
53
- period: string,
54
- limit: string,
55
- ): Promise<UserResponse> {
56
- const options = {
57
- method,
58
- user,
59
- period,
60
- limit,
61
- }
62
- return request<UserResponse>(options);
63
- }
64
-
65
- /**
66
- * GET: Love Tracks - LastFM
67
- *
68
- * https://www.last.fm/api/show/user.getLovedTracks
69
- * @returns Loved Tracks;
70
- */
71
- function getLovedTracks(
72
- method: string,
73
- user: string,
74
- period: string,
75
- limit: string,
76
- ): Promise<LovedTracksResponse> {
77
- const options = {
78
- method,
79
- user,
80
- period,
81
- limit,
82
- }
83
- return request<LovedTracksResponse>(options);
84
- }
85
-
86
- /**
87
- * GET: Recent Tracks - LastFM
88
- *
89
- * https://www.last.fm/api/show/user.getRecentTracks
90
- * @returns Recent Tracks
91
- */
92
- function getRecentTracks(
93
- method: string,
94
- user: string,
95
- period: string,
96
- limit: string,
97
- ): Promise<RecentTracksResponse> {
98
- const options = {
99
- method,
100
- user,
101
- period,
102
- limit,
103
- }
104
- return request<RecentTracksResponse>(options);
105
- }
106
-
107
- /**
108
- * GET: Top Albums - LastFM
109
- *
110
- * https://www.last.fm/api/show/user.getTopAlbums
111
- * @returns Top Albums
112
- */
113
- function getTopAlbums(
114
- method: string,
115
- user: string,
116
- period: string,
117
- limit: string,
118
- ): Promise<TopAlbumsResponse> {
119
- const options = {
120
- method,
121
- user,
122
- period,
123
- limit,
124
- }
125
- return request<TopAlbumsResponse>(options);
126
- }
127
-
128
- /**
129
- * GET: Top Artist - LastFM
130
- *
131
- * https://www.last.fm/api/show/user.getTopArtists
132
- * @returns Top Artists
133
- */
134
- function getTopArtists(
135
- method: string,
136
- user: string,
137
- period: string,
138
- limit: string,
139
- ): Promise<TopArtistsResponse> {
140
- const options = {
141
- method,
142
- user,
143
- period,
144
- limit,
145
- }
146
- return request<TopArtistsResponse>(options);
147
- }
148
-
149
- /**
150
- * GET: Top Tracks - LastFM
151
- *
152
- * https://www.last.fm/api/show/user.getTopTracks
153
- * @returns Top Tracks
154
- */
155
- function getTopTracks(
156
- method: string,
157
- user: string,
158
- period: string,
159
- limit: string,
160
- ): Promise<TopTrackResponse> {
161
- const options = {
162
- method,
163
- user,
164
- period,
165
- limit,
166
- }
167
- return request<TopTrackResponse>(options);
168
- }
169
-
170
- /**
171
- * GET: Weekly album chart - LastFM
172
- *
173
- * https://www.last.fm/api/show/user.getWeeklyAlbumChart
174
- * @returns Weekly album chart
175
- */
176
- function getWeeklyAlbumChart(
177
- method: string,
178
- user: string,
179
- period: string,
180
- limit: string,
181
- ): Promise<WeeklyAlbumChartResponse> {
182
- const options = {
183
- method,
184
- user,
185
- period,
186
- limit,
187
- }
188
- return request<WeeklyAlbumChartResponse>(options);
189
- }
190
-
191
- /**
192
- * GET: Weekly artist chart - LastFM
193
- *
194
- * https://www.last.fm/api/show/user.getWeeklyArtistChart
195
- * @returns Weekly artist chart
196
- */
197
- function getWeeklyArtistChart(
198
- method: string,
199
- user: string,
200
- period: string,
201
- limit: string,
202
- ): Promise<WeeklyArtistChartResponse> {
203
- const options = {
204
- method,
205
- user,
206
- period,
207
- limit,
208
- }
209
- return request<WeeklyArtistChartResponse>(options);
210
- }
211
-
212
- /**
213
- * GET: Weekly chart list - LastFM
214
- *
215
- * https://www.last.fm/api/show/user.getWeeklyChartList
216
- * @returns Weekly chart list
217
- */
218
- function getWeeklyChartList(
219
- method: string,
220
- user: string,
221
- period: string,
222
- limit: string,
223
- ): Promise<WeeklyChartListResponse> {
224
- const options = {
225
- method,
226
- user,
227
- period,
228
- limit,
229
- }
230
- return request<WeeklyChartListResponse>(options);
231
- }
232
-
233
- /**
234
- * GET: Weekly track chart - LastFM
235
- *
236
- * https://www.last.fm/api/show/user.getWeeklyTrackChart
237
- * @returns Weekly track chart
238
- */
239
- function getWeeklyTrackChart(
240
- method: string,
241
- user: string,
242
- period: string,
243
- limit: string,
244
- ): Promise<WeeklyTrackChartResponse> {
245
- const options = {
246
- method,
247
- user,
248
- period,
249
- limit,
250
- }
251
- return request<WeeklyTrackChartResponse>(options);
252
- }
253
-
1
+ import { auth } from './auth';
2
+ import { config } from './config';
3
+ import { getInfo } from './getInfo';
4
+ import { getLovedTracks } from './getLovedTracks';
5
+ import { getRecentTracks } from './getRecentTracks';
6
+ import { getTopAlbums } from './getTopAlbums';
7
+ import { getTopArtists } from './getTopArtists';
8
+ import { getTopTracks } from './getTopTracks';
9
+ import { getWeeklyAlbumChart } from './getWeeklyAlbumChart';
10
+ import { getWeeklyArtistChart } from './getWeeklyArtistChart';
11
+ import { getWeeklyChartList } from './getWeeklyChartList';
12
+ import { getWeeklyTrackChart } from './getWeeklyTrackChart';
13
+ import { method } from './method';
14
+
15
+ export default function LastFmApi() {
254
16
  return {
255
17
  auth,
256
18
  config,
@@ -267,5 +29,3 @@ function LastFmApi() {
267
29
  method,
268
30
  };
269
31
  }
270
-
271
- export default LastFmApi;
package/src/method.ts CHANGED
@@ -1,15 +1,33 @@
1
- export default {
1
+ interface UserMethod {
2
+ getInfo: string;
3
+ getLovedTracks: string;
4
+ getRecentTracks: string;
5
+ getTopAlbums: string;
6
+ getTopArtists: string;
7
+ getTopTracks: string;
8
+ getWeeklyAlbumChart: string;
9
+ getWeeklyArtistChart: string;
10
+ getWeeklyChartList: string;
11
+ getWeeklyTrackChart: string;
12
+ };
13
+
14
+ export interface Method {
15
+ auth: string;
16
+ user: UserMethod;
17
+ };
18
+
19
+ export const method = {
2
20
  auth: 'auth.getToken',
3
21
  user: {
4
22
  getInfo: 'user.getInfo',
5
- loved_tracks: 'user.getLovedTracks',
6
- recent_tracks: 'user.getRecentTracks',
7
- top_albums: 'user.getTopAlbums',
8
- top_artists: 'user.getTopArtists',
9
- top_tracks: 'user.getTopTracks',
10
- weekly_album_chart: 'user.getWeeklyAlbumChart',
11
- weekly_artist_chart: 'user.getWeeklyArtistChart',
12
- weekly_chart_list: 'user.getWeeklyChartList',
13
- weekly_track_chart: 'user.getWeeklyTrackChart',
23
+ getLovedTracks: 'user.getLovedTracks',
24
+ getRecentTracks: 'user.getRecentTracks',
25
+ getTopAlbums: 'user.getTopAlbums',
26
+ getTopArtists: 'user.getTopArtists',
27
+ getTopTracks: 'user.getTopTracks',
28
+ getWeeklyAlbumChart: 'user.getWeeklyAlbumChart',
29
+ getWeeklyArtistChart: 'user.getWeeklyArtistChart',
30
+ getWeeklyChartList: 'user.getWeeklyChartList',
31
+ getWeeklyTrackChart: 'user.getWeeklyTrackChart',
14
32
  },
15
- };
33
+ } satisfies Method;
package/src/request.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import fetch from 'cross-fetch';
2
- import config from './config';
2
+ import { config } from './config';
3
3
 
4
4
  interface RequestOptions {
5
5
  method: string;
@@ -8,14 +8,35 @@ interface RequestOptions {
8
8
  limit?: string;
9
9
  }
10
10
 
11
+ enum ErrorResponse {
12
+ InvalidService = 2,
13
+ InvalidMethod = 3,
14
+ AuthenticationFailed = 4,
15
+ InvalidFormat = 5,
16
+ InvalidParameters = 6,
17
+ InvalidResource = 7,
18
+ OperationFailed = 8,
19
+ InvalidSessionKey = 9,
20
+ InvalidAPIKey = 10,
21
+ ServiceOffline = 11,
22
+ InvalidMethodSignature = 13,
23
+ TemporaryError = 16,
24
+ SuspendedAPIKey = 26,
25
+ RateLimitExceeded = 29
26
+ }
27
+
11
28
  const buildUrl = (options: RequestOptions): string => {
12
29
  const params = new URLSearchParams();
30
+
13
31
  params.append('method', options.method);
32
+
14
33
  if (options.user) params.append('user', options.user);
15
34
  if (options.period) params.append('period', options.period);
16
35
  if (options.limit) params.append('limit', options.limit);
36
+
17
37
  params.append('api_key', config.api_key);
18
38
  params.append('format', config.format.json);
39
+
19
40
  return `${config.base_url}?${params.toString()}`;
20
41
  }
21
42
 
@@ -28,14 +49,84 @@ const request = async <Response>(options: RequestOptions): Promise<Response> =>
28
49
  },
29
50
  })
30
51
  .then((res) => {
31
- if (res.status >= 400) {
32
- throw new Error('Bad response from server');
52
+ if(!res.ok) {
53
+ throw new Error(res.statusText);
33
54
  }
34
- return res.json();
55
+
56
+ switch (res.status) {
57
+ case 200: {
58
+ return res.json();
59
+ }
60
+ case 400: {
61
+ throw new Error('Bad request');
62
+ }
63
+ case 401: {
64
+ throw new Error('Unauthorized');
65
+ }
66
+ case 403: {
67
+ throw new Error('Forbidden');
68
+ }
69
+ case 404: {
70
+ throw new Error('Not found');
71
+ }
72
+ case 500: {
73
+ throw new Error('Internal server error');
74
+ }
75
+ case 503: {
76
+ throw new Error('Service unavailable');
77
+ }
78
+ case ErrorResponse.InvalidAPIKey: {
79
+ throw new Error('Invalid API key');
80
+ }
81
+ case ErrorResponse.InvalidMethod: {
82
+ throw new Error('Invalid method');
83
+ }
84
+ case ErrorResponse.InvalidParameters: {
85
+ throw new Error('Invalid parameters');
86
+ }
87
+ case ErrorResponse.InvalidResource: {
88
+ throw new Error('Invalid resource');
89
+ }
90
+ case ErrorResponse.InvalidSessionKey: {
91
+ throw new Error('Invalid session key');
92
+ }
93
+ case ErrorResponse.InvalidService: {
94
+ throw new Error('Invalid service');
95
+ }
96
+ case ErrorResponse.OperationFailed: {
97
+ throw new Error('Operation failed');
98
+ }
99
+ case ErrorResponse.RateLimitExceeded: {
100
+ throw new Error('Rate limit exceeded');
101
+ }
102
+ case ErrorResponse.ServiceOffline: {
103
+ throw new Error('Service offline');
104
+ }
105
+ case ErrorResponse.SuspendedAPIKey: {
106
+ throw new Error('Suspended API key');
107
+ }
108
+ case ErrorResponse.TemporaryError: {
109
+ throw new Error('Temporary error');
110
+ }
111
+ case ErrorResponse.AuthenticationFailed: {
112
+ throw new Error('Authentication failed');
113
+ }
114
+ case ErrorResponse.InvalidFormat: {
115
+ throw new Error('Invalid format');
116
+ }
117
+ case ErrorResponse.InvalidMethodSignature: {
118
+ throw new Error('Invalid method signature');
119
+ }
120
+
121
+ default: {
122
+ throw new Error('Unknown error');
123
+ }
124
+ }
125
+
35
126
  })
36
127
  .then((json) => json)
37
128
  .catch((error) => {
38
- console.log(error);
129
+ console.log('🚨 error:', error);
39
130
  })) as Response;
40
131
  };
41
132
 
package/tsconfig.json CHANGED
@@ -26,9 +26,9 @@
26
26
  // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
27
27
 
28
28
  /* Modules */
29
- "module": "CommonJS" /* Specify what module code is generated. */,
29
+ "module": "NodeNext" /* Specify what module code is generated. */,
30
30
  "rootDir": "./src" /* Specify the root folder within your source files. */,
31
- "moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
31
+ "moduleResolution": "NodeNext" /* Specify how TypeScript looks up a file from a given module specifier. */,
32
32
  // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
33
33
  // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
34
34
  // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
@@ -45,17 +45,17 @@
45
45
  // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
46
46
 
47
47
  /* Emit */
48
- "declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
49
- // "declarationMap": true, /* Create sourcemaps for d.ts files. */
48
+ "declaration": false /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
49
+ "declarationMap": false, /* Create sourcemaps for d.ts files. */
50
50
  // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
51
51
  // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
52
52
  // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
53
53
  "outDir": "./dist" /* Specify an output folder for all emitted files. */,
54
- // "removeComments": true, /* Disable emitting comments. */
54
+ "removeComments": false, /* Disable emitting comments. */
55
55
  // "noEmit": true, /* Disable emitting files from a compilation. */
56
56
  // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
57
57
  // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
58
- // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
58
+ "downlevelIteration": false, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
59
59
  // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
60
60
  // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
61
61
  // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
@@ -101,4 +101,4 @@
101
101
  "skipDefaultLibCheck": true /* Skip type checking .d.ts files that are included with TypeScript. */,
102
102
  "skipLibCheck": true /* Skip type checking all .d.ts files. */
103
103
  }
104
- }
104
+ }
package/dist/config.d.ts DELETED
@@ -1,12 +0,0 @@
1
- declare const _default: {
2
- api_key: string;
3
- app_name: string;
4
- base_url: string;
5
- format: {
6
- json: string;
7
- xml: string;
8
- };
9
- share_secret: string;
10
- username: string;
11
- };
12
- export default _default;
package/dist/index.d.ts DELETED
@@ -1,41 +0,0 @@
1
- import { AuthResponse, LovedTracksResponse, RecentTracksResponse, TopAlbumsResponse, TopArtistsResponse, TopTrackResponse, UserResponse, WeeklyAlbumChartResponse, WeeklyArtistChartResponse, WeeklyChartListResponse, WeeklyTrackChartResponse } from '../@types';
2
- declare function LastFmApi(): {
3
- auth: (method: string, user: string, period: string, limit: string) => Promise<AuthResponse>;
4
- config: {
5
- api_key: string;
6
- app_name: string;
7
- base_url: string;
8
- format: {
9
- json: string;
10
- xml: string;
11
- };
12
- share_secret: string;
13
- username: string;
14
- };
15
- getInfo: (method: string, user: string, period: string, limit: string) => Promise<UserResponse>;
16
- getLovedTracks: (method: string, user: string, period: string, limit: string) => Promise<LovedTracksResponse>;
17
- getRecentTracks: (method: string, user: string, period: string, limit: string) => Promise<RecentTracksResponse>;
18
- getTopAlbums: (method: string, user: string, period: string, limit: string) => Promise<TopAlbumsResponse>;
19
- getTopArtists: (method: string, user: string, period: string, limit: string) => Promise<TopArtistsResponse>;
20
- getTopTracks: (method: string, user: string, period: string, limit: string) => Promise<TopTrackResponse>;
21
- getWeeklyAlbumChart: (method: string, user: string, period: string, limit: string) => Promise<WeeklyAlbumChartResponse>;
22
- getWeeklyArtistChart: (method: string, user: string, period: string, limit: string) => Promise<WeeklyArtistChartResponse>;
23
- getWeeklyChartList: (method: string, user: string, period: string, limit: string) => Promise<WeeklyChartListResponse>;
24
- getWeeklyTrackChart: (method: string, user: string, period: string, limit: string) => Promise<WeeklyTrackChartResponse>;
25
- method: {
26
- auth: string;
27
- user: {
28
- getInfo: string;
29
- loved_tracks: string;
30
- recent_tracks: string;
31
- top_albums: string;
32
- top_artists: string;
33
- top_tracks: string;
34
- weekly_album_chart: string;
35
- weekly_artist_chart: string;
36
- weekly_chart_list: string;
37
- weekly_track_chart: string;
38
- };
39
- };
40
- };
41
- export default LastFmApi;
package/dist/method.d.ts DELETED
@@ -1,16 +0,0 @@
1
- declare const _default: {
2
- auth: string;
3
- user: {
4
- getInfo: string;
5
- loved_tracks: string;
6
- recent_tracks: string;
7
- top_albums: string;
8
- top_artists: string;
9
- top_tracks: string;
10
- weekly_album_chart: string;
11
- weekly_artist_chart: string;
12
- weekly_chart_list: string;
13
- weekly_track_chart: string;
14
- };
15
- };
16
- export default _default;
package/dist/request.d.ts DELETED
@@ -1,8 +0,0 @@
1
- interface RequestOptions {
2
- method: string;
3
- user?: string;
4
- period?: string;
5
- limit?: string;
6
- }
7
- declare const request: <Response_1>(options: RequestOptions) => Promise<Response_1>;
8
- export default request;