lastfm-nodejs-client 1.2.4 → 1.3.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.
Files changed (53) hide show
  1. package/@types/index.d.ts +16 -16
  2. package/CHANGELOG.md +30 -24
  3. package/README.md +4 -0
  4. package/bun.lockb +0 -0
  5. package/dist/auth.d.ts +12 -0
  6. package/dist/auth.js +23 -0
  7. package/dist/config.js +1 -0
  8. package/dist/createOptions.d.ts +6 -0
  9. package/dist/createOptions.js +12 -0
  10. package/dist/getInfo.d.ts +8 -0
  11. package/dist/getInfo.js +19 -0
  12. package/dist/getLovedTracks.d.ts +8 -0
  13. package/dist/getLovedTracks.js +19 -0
  14. package/dist/getRecentTracks.d.ts +8 -0
  15. package/dist/getRecentTracks.js +19 -0
  16. package/dist/getTopAlbums.d.ts +8 -0
  17. package/dist/getTopAlbums.js +19 -0
  18. package/dist/getTopArtists.d.ts +8 -0
  19. package/dist/getTopArtists.js +19 -0
  20. package/dist/getTopTracks.d.ts +8 -0
  21. package/dist/getTopTracks.js +19 -0
  22. package/dist/getWeeklyAlbumChart.d.ts +8 -0
  23. package/dist/getWeeklyAlbumChart.js +19 -0
  24. package/dist/getWeeklyArtistChart.d.ts +8 -0
  25. package/dist/getWeeklyArtistChart.js +19 -0
  26. package/dist/getWeeklyChartList.d.ts +8 -0
  27. package/dist/getWeeklyChartList.js +19 -0
  28. package/dist/getWeeklyTrackChart.d.ts +8 -0
  29. package/dist/getWeeklyTrackChart.js +19 -0
  30. package/dist/index.d.ts +23 -14
  31. package/dist/index.js +22 -181
  32. package/dist/method.d.ts +16 -0
  33. package/dist/method.js +2 -0
  34. package/dist/request.js +84 -3
  35. package/package.json +5 -4
  36. package/src/auth.ts +23 -0
  37. package/src/config.ts +13 -1
  38. package/src/createOptions.ts +12 -0
  39. package/src/getInfo.ts +17 -0
  40. package/src/getLovedTracks.ts +19 -0
  41. package/src/getRecentTracks.ts +19 -0
  42. package/src/getTopAlbums.ts +19 -0
  43. package/src/getTopArtists.ts +19 -0
  44. package/src/getTopTracks.ts +19 -0
  45. package/src/getWeeklyAlbumChart.ts +19 -0
  46. package/src/getWeeklyArtistChart.ts +19 -0
  47. package/src/getWeeklyChartList.ts +19 -0
  48. package/src/getWeeklyTrackChart.ts +20 -0
  49. package/src/index.ts +12 -252
  50. package/src/method.ts +19 -1
  51. package/src/request.ts +90 -3
  52. package/tsconfig.json +1 -1
  53. package/src/types.d.ts +0 -392
package/src/index.ts CHANGED
@@ -1,256 +1,18 @@
1
+ import { auth } from './auth';
1
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';
2
13
  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
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,3 +1,21 @@
1
+ interface UserMethod {
2
+ getInfo: string;
3
+ loved_tracks: string;
4
+ recent_tracks: string;
5
+ top_albums: string;
6
+ top_artists: string;
7
+ top_tracks: string;
8
+ weekly_album_chart: string;
9
+ weekly_artist_chart: string;
10
+ weekly_chart_list: string;
11
+ weekly_track_chart: string;
12
+ };
13
+
14
+ export interface Method {
15
+ auth: string;
16
+ user: UserMethod;
17
+ };
18
+
1
19
  export default {
2
20
  auth: 'auth.getToken',
3
21
  user: {
@@ -12,4 +30,4 @@ export default {
12
30
  weekly_chart_list: 'user.getWeeklyChartList',
13
31
  weekly_track_chart: 'user.getWeeklyTrackChart',
14
32
  },
15
- };
33
+ } satisfies Method;
package/src/request.ts CHANGED
@@ -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,10 +49,76 @@ 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
+ switch (res.status) {
53
+ case 200: {
54
+ return res.json();
55
+ }
56
+ case 400: {
57
+ throw new Error('Bad request');
58
+ }
59
+ case 401: {
60
+ throw new Error('Unauthorized');
61
+ }
62
+ case 403: {
63
+ throw new Error('Forbidden');
64
+ }
65
+ case 404: {
66
+ throw new Error('Not found');
67
+ }
68
+ case 500: {
69
+ throw new Error('Internal server error');
70
+ }
71
+ case 503: {
72
+ throw new Error('Service unavailable');
73
+ }
74
+ case ErrorResponse.InvalidAPIKey: {
75
+ throw new Error('Invalid API key');
76
+ }
77
+ case ErrorResponse.InvalidMethod: {
78
+ throw new Error('Invalid method');
79
+ }
80
+ case ErrorResponse.InvalidParameters: {
81
+ throw new Error('Invalid parameters');
82
+ }
83
+ case ErrorResponse.InvalidResource: {
84
+ throw new Error('Invalid resource');
85
+ }
86
+ case ErrorResponse.InvalidSessionKey: {
87
+ throw new Error('Invalid session key');
88
+ }
89
+ case ErrorResponse.InvalidService: {
90
+ throw new Error('Invalid service');
91
+ }
92
+ case ErrorResponse.OperationFailed: {
93
+ throw new Error('Operation failed');
94
+ }
95
+ case ErrorResponse.RateLimitExceeded: {
96
+ throw new Error('Rate limit exceeded');
97
+ }
98
+ case ErrorResponse.ServiceOffline: {
99
+ throw new Error('Service offline');
100
+ }
101
+ case ErrorResponse.SuspendedAPIKey: {
102
+ throw new Error('Suspended API key');
103
+ }
104
+ case ErrorResponse.TemporaryError: {
105
+ throw new Error('Temporary error');
106
+ }
107
+ case ErrorResponse.AuthenticationFailed: {
108
+ throw new Error('Authentication failed');
109
+ }
110
+ case ErrorResponse.InvalidFormat: {
111
+ throw new Error('Invalid format');
112
+ }
113
+ case ErrorResponse.InvalidMethodSignature: {
114
+ throw new Error('Invalid method signature');
115
+ }
116
+
117
+ default: {
118
+ throw new Error('Unknown error');
119
+ }
33
120
  }
34
- return res.json();
121
+
35
122
  })
36
123
  .then((json) => json)
37
124
  .catch((error) => {
package/tsconfig.json CHANGED
@@ -51,7 +51,7 @@
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. */