@benliam12/spotify-api-helper 1.0.0-DEV.4 → 1.0.0-DEV.6
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.
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { SpotifyConfiguration } from "./SpotifyConfiguration.js";
|
|
2
|
-
import { Album, Track, Artist } from "./SpotifyTypes.js";
|
|
2
|
+
import { Album, Track, Artist, SearchType } from "./SpotifyTypes.js";
|
|
3
3
|
/**
|
|
4
4
|
* Result wrapper for operations that can fail
|
|
5
5
|
*/
|
|
@@ -70,12 +70,12 @@ export declare class SpotifyHelper {
|
|
|
70
70
|
* Handle errors consistently - log and/or call callback
|
|
71
71
|
*/
|
|
72
72
|
private handleError;
|
|
73
|
-
test(): string;
|
|
74
73
|
/**
|
|
75
74
|
* Get an album by ID.
|
|
76
75
|
* Returns null if the request fails.
|
|
77
76
|
*/
|
|
78
77
|
getAlbum(albumId: string): Promise<Album | null>;
|
|
78
|
+
getAlbums(albumIds: string[]): Promise<(Album | null)[]>;
|
|
79
79
|
/**
|
|
80
80
|
* Get a track by ID.
|
|
81
81
|
* Returns null if the request fails.
|
|
@@ -96,6 +96,7 @@ export declare class SpotifyHelper {
|
|
|
96
96
|
* Returns empty array if the request fails.
|
|
97
97
|
*/
|
|
98
98
|
getAvailableMarkets(): Promise<string[]>;
|
|
99
|
+
search(query: string, type: SearchType, limit?: number, offset?: number): Promise<any | null>;
|
|
99
100
|
/**
|
|
100
101
|
* Check if the helper is properly authenticated.
|
|
101
102
|
* Useful for health checks.
|
|
@@ -105,6 +106,10 @@ export declare class SpotifyHelper {
|
|
|
105
106
|
* Manually clear the token (useful for testing or forcing a refresh).
|
|
106
107
|
*/
|
|
107
108
|
clearToken(): void;
|
|
109
|
+
/**
|
|
110
|
+
* Get the current configuration.
|
|
111
|
+
*/
|
|
112
|
+
getConfig(): SpotifyConfiguration;
|
|
108
113
|
/**
|
|
109
114
|
* Get token info for debugging (without exposing the actual token).
|
|
110
115
|
*/
|
|
@@ -165,9 +165,6 @@ export class SpotifyHelper {
|
|
|
165
165
|
}
|
|
166
166
|
this.options.onError(error);
|
|
167
167
|
}
|
|
168
|
-
test() {
|
|
169
|
-
return "SpotifyHelper is working!";
|
|
170
|
-
}
|
|
171
168
|
/**
|
|
172
169
|
* Get an album by ID.
|
|
173
170
|
* Returns null if the request fails.
|
|
@@ -181,13 +178,38 @@ export class SpotifyHelper {
|
|
|
181
178
|
const album = {
|
|
182
179
|
id: data.id,
|
|
183
180
|
name: data.name,
|
|
184
|
-
market: data.market,
|
|
185
181
|
album_type: data.album_type,
|
|
186
182
|
total_tracks: data.total_tracks,
|
|
187
|
-
|
|
183
|
+
data: data.data
|
|
188
184
|
};
|
|
189
185
|
return album;
|
|
190
186
|
}
|
|
187
|
+
async getAlbums(albumIds) {
|
|
188
|
+
const idsParam = albumIds.join(",");
|
|
189
|
+
if (albumIds.length === 0) {
|
|
190
|
+
return [];
|
|
191
|
+
}
|
|
192
|
+
if (albumIds.length > 20) {
|
|
193
|
+
throw new Error("Spotify API allows a maximum of 20 album IDs per request.");
|
|
194
|
+
}
|
|
195
|
+
const result = await this.makeAuthenticatedRequest(`https://api.spotify.com/v1/albums?ids=${idsParam}`, "getAlbums", { method: "GET" });
|
|
196
|
+
if (result.success) {
|
|
197
|
+
return result.data.map((data) => {
|
|
198
|
+
if (!data) {
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
const album = {
|
|
202
|
+
id: data.id,
|
|
203
|
+
name: data.name,
|
|
204
|
+
album_type: data.album_type,
|
|
205
|
+
total_tracks: data.total_tracks,
|
|
206
|
+
data: data.data
|
|
207
|
+
};
|
|
208
|
+
return album;
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
return [];
|
|
212
|
+
}
|
|
191
213
|
/**
|
|
192
214
|
* Get a track by ID.
|
|
193
215
|
* Returns null if the request fails.
|
|
@@ -197,9 +219,33 @@ export class SpotifyHelper {
|
|
|
197
219
|
if (!result.success) {
|
|
198
220
|
return null;
|
|
199
221
|
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
222
|
+
else {
|
|
223
|
+
const { id, name, album_type, total_tracks, ...data } = result.data.album;
|
|
224
|
+
const trackAlbumData = {
|
|
225
|
+
id: id,
|
|
226
|
+
name: name,
|
|
227
|
+
album_type: album_type,
|
|
228
|
+
total_tracks: total_tracks,
|
|
229
|
+
data: data
|
|
230
|
+
};
|
|
231
|
+
const trackArtistData = result.data.artists.map((artistData) => {
|
|
232
|
+
const { id, name, ...data } = artistData;
|
|
233
|
+
const artist = {
|
|
234
|
+
id: id,
|
|
235
|
+
name: name,
|
|
236
|
+
data: data
|
|
237
|
+
};
|
|
238
|
+
return artist;
|
|
239
|
+
});
|
|
240
|
+
const trackData = {
|
|
241
|
+
id: result.data.id,
|
|
242
|
+
name: result.data.name,
|
|
243
|
+
artists: trackArtistData,
|
|
244
|
+
album: trackAlbumData,
|
|
245
|
+
data: result.data
|
|
246
|
+
};
|
|
247
|
+
return trackData;
|
|
248
|
+
}
|
|
203
249
|
}
|
|
204
250
|
/**
|
|
205
251
|
* Get an artist by ID.
|
|
@@ -236,6 +282,13 @@ export class SpotifyHelper {
|
|
|
236
282
|
}
|
|
237
283
|
return result.data.markets || [];
|
|
238
284
|
}
|
|
285
|
+
async search(query, type, limit = 20, offset = 0) {
|
|
286
|
+
const result = await this.makeAuthenticatedRequest(`https://api.spotify.com/v1/search?q=${encodeURIComponent(query)}&type=${type}&limit=${limit}&offset=${offset}`, "search", { method: "GET" });
|
|
287
|
+
if (!result.success) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
return result.data;
|
|
291
|
+
}
|
|
239
292
|
/**
|
|
240
293
|
* Check if the helper is properly authenticated.
|
|
241
294
|
* Useful for health checks.
|
|
@@ -250,6 +303,12 @@ export class SpotifyHelper {
|
|
|
250
303
|
clearToken() {
|
|
251
304
|
this.clientToken = null;
|
|
252
305
|
}
|
|
306
|
+
/**
|
|
307
|
+
* Get the current configuration.
|
|
308
|
+
*/
|
|
309
|
+
getConfig() {
|
|
310
|
+
return this.config;
|
|
311
|
+
}
|
|
253
312
|
/**
|
|
254
313
|
* Get token info for debugging (without exposing the actual token).
|
|
255
314
|
*/
|
|
@@ -1,17 +1,30 @@
|
|
|
1
1
|
export interface Album {
|
|
2
2
|
id: string;
|
|
3
3
|
name: string;
|
|
4
|
-
market: string;
|
|
5
4
|
album_type: string;
|
|
6
5
|
total_tracks: number;
|
|
7
|
-
|
|
6
|
+
data: any;
|
|
8
7
|
}
|
|
9
8
|
export interface Track {
|
|
9
|
+
id: string;
|
|
10
|
+
name: string;
|
|
11
|
+
artists: Artist[];
|
|
12
|
+
album: Album;
|
|
13
|
+
data: any;
|
|
10
14
|
}
|
|
11
15
|
export interface Artist {
|
|
16
|
+
id: string;
|
|
17
|
+
name: string;
|
|
18
|
+
data: any;
|
|
12
19
|
}
|
|
13
20
|
export interface ClientToken {
|
|
14
21
|
access_token: string;
|
|
15
22
|
token_type: string;
|
|
16
23
|
expires_at: Date;
|
|
17
24
|
}
|
|
25
|
+
export declare enum SearchType {
|
|
26
|
+
ALBUM = "album",
|
|
27
|
+
ARTIST = "artist",
|
|
28
|
+
TRACK = "track",
|
|
29
|
+
PLAYLIST = "playlist"
|
|
30
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@benliam12/spotify-api-helper",
|
|
3
|
-
"version": "1.0.0-DEV.
|
|
3
|
+
"version": "1.0.0-DEV.6",
|
|
4
4
|
"description": "A utility package for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -10,12 +10,14 @@
|
|
|
10
10
|
],
|
|
11
11
|
"scripts": {
|
|
12
12
|
"build": "npm run clean && tsc",
|
|
13
|
-
"test": "jest",
|
|
13
|
+
"test": "node --experimental-vm-modules --no-warnings --env-file=.env.test.local node_modules/jest/bin/jest.js",
|
|
14
|
+
"test:watch": "node --experimental-vm-modules --no-warnings --env-file=.env.test.local node_modules/jest/bin/jest.js --watch",
|
|
15
|
+
"test:coverage": "node --experimental-vm-modules --no-warnings --env-file=.env.test.local node_modules/jest/bin/jest.js --coverage",
|
|
14
16
|
"clean": "rimraf dist",
|
|
15
17
|
"lint": "eslint src --ext .ts",
|
|
16
18
|
"prepare": "npm run build",
|
|
17
|
-
"release": "semantic-release --no-ci",
|
|
18
|
-
"release:dry": "semantic-release --dry-run --no-ci"
|
|
19
|
+
"release": "dotenv -e .env -- semantic-release --no-ci --dry-run=false",
|
|
20
|
+
"release:dry": "dotenv -e .env semantic-release --dry-run --no-ci"
|
|
19
21
|
},
|
|
20
22
|
"keywords": [
|
|
21
23
|
"utility",
|
|
@@ -36,6 +38,8 @@
|
|
|
36
38
|
"@types/node": "^22.19.3",
|
|
37
39
|
"@typescript-eslint/eslint-plugin": "^8.50.1",
|
|
38
40
|
"@typescript-eslint/parser": "^8.50.1",
|
|
41
|
+
"conventional-changelog-conventionalcommits": "^9.1.0",
|
|
42
|
+
"dotenv-cli": "^11.0.0",
|
|
39
43
|
"eslint": "^9.39.2",
|
|
40
44
|
"jest": "^29.7.0",
|
|
41
45
|
"rimraf": "^6.1.2",
|