Skip to content

Commit 2f11e44

Browse files
committed
Adjusting response formats
1 parent 1891e36 commit 2f11e44

File tree

7 files changed

+41
-60
lines changed

7 files changed

+41
-60
lines changed

components/spotify/actions/get-artist-top-tracks/get-artist-top-tracks.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default {
3232
market,
3333
} = this;
3434

35-
const res = await this.spotify._makeRequest({
35+
const { data } = await this.spotify._makeRequest({
3636
$,
3737
url: `/artists/${artistId.value ?? artistId}/top-tracks`,
3838
params: {
@@ -42,6 +42,6 @@ export default {
4242

4343
$.export("$summary", `Successfully fetched top tracks for "${artistId.label ?? artistId}"`);
4444

45-
return res.tracks ?? [];
45+
return data?.tracks ?? [];
4646
},
4747
};

components/spotify/actions/get-categorys-playlist/get-categorys-playlist.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export default {
4949
offset,
5050
} = this;
5151

52-
const res = await this.spotify._makeRequest({
52+
const { data } = await this.spotify._makeRequest({
5353
$,
5454
url: `/browse/categories/${categoryId.value ?? categoryId}/playlists`,
5555
params: {
@@ -61,6 +61,6 @@ export default {
6161

6262
$.export("$summary", `Successfully fetched playlists for the "${categoryId.label ?? categoryId}" category`);
6363

64-
return res.playlists?.items ?? [];
64+
return data?.playlists?.items ?? [];
6565
},
6666
};

components/spotify/actions/get-playlist-items/get-playlist-items.mjs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,17 @@ export default {
5151
async run({ $ }) {
5252
const {
5353
playlistId,
54-
market,
5554
fields,
5655
limit,
5756
offset,
5857
additionalTypes,
5958
} = this;
6059

61-
const res = await this.spotify._makeRequest({
60+
const { data } = await this.spotify._makeRequest({
6261
$,
6362
url: `/playlists/${playlistId.value ?? playlistId}/tracks`,
6463
params: {
6564
fields,
66-
market,
6765
limit,
6866
offset,
6967
additional_types: additionalTypes && additionalTypes.join(",").toLowerCase(),
@@ -72,6 +70,6 @@ export default {
7270

7371
$.export("$summary", `Successfully fetched details for "${playlistId.label ?? playlistId}"`);
7472

75-
return res.items ?? [];
73+
return data?.items ?? [];
7674
},
7775
};

components/spotify/actions/get-recommendations/get-recommendations.mjs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export default {
6060

6161
const numSeeds = seedArtists.length + seedGenres.length + seedTracks.length;
6262
if (numSeeds > 5 || numSeeds < 1) {
63-
throw new ConfigurationError("Must provide between 1 and 5 seeds in in any combination of `seedArtists`, `seedTracks` and `seedGenres`.");
63+
throw new ConfigurationError("Must provide between 1 and 5 seeds in any combination of `seedArtists`, `seedTracks` and `seedGenres`.");
6464
}
6565

6666
const params = {
@@ -70,16 +70,16 @@ export default {
7070
limit,
7171
};
7272

73-
const response = await this.spotify.getRecommendations({
73+
const { data } = await this.spotify.getRecommendations({
7474
$,
7575
...params,
7676
});
7777

78-
if (response.tracks.length === 0) {
78+
if (data?.tracks?.length === 0) {
7979
$.export("$summary", "No recommendations found");
8080
return;
8181
}
82-
$.export("$summary", `Successfully retrieved ${response.tracks.length} recommendation(s).`);
83-
return response;
82+
$.export("$summary", `Successfully retrieved ${data?.tracks?.length} recommendation(s).`);
83+
return data;
8484
},
8585
};

components/spotify/actions/get-track/get-track.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ export default {
3333
market,
3434
} = this;
3535

36-
const res = await this.spotify._makeRequest({
36+
const { data } = await this.spotify._makeRequest({
3737
$,
3838
url: `/tracks/${trackId.value ?? trackId}`,
3939
params: {
4040
market,
4141
},
4242
});
4343

44-
$.export("$summary", `Successfully fetched info for the track, "${res.name}"`);
44+
$.export("$summary", `Successfully fetched info for the track, "${data?.name}"`);
4545

46-
return res;
46+
return data ?? {};
4747
},
4848
};

components/spotify/actions/remove-user-saved-tracks/remove-user-saved-tracks.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default {
2525

2626
const ids = this.spotify.sanitizedArray(savedUserTracksId);
2727

28-
const resp = await this.spotify._makeRequest({
28+
const { data } = await this.spotify._makeRequest({
2929
$,
3030
method: "DELETE",
3131
url: "/me/tracks",
@@ -37,6 +37,6 @@ export default {
3737
// eslint-disable-next-line multiline-ternary
3838
$.export("$summary", `Successfully removed ${ids.length} ${ids.length == 1 ? "item" : "items"} from "Liked Songs"`);
3939

40-
return resp;
40+
return data;
4141
},
4242
};

components/spotify/spotify.app.mjs

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -269,23 +269,24 @@ export default {
269269
return await this.retry($, config);
270270
},
271271
// Retry axios request if not successful
272-
async retry($, config, retries = 3) {
272+
async retry($, config, retries = 0) {
273273
try {
274274
return await axios($, {
275275
...config,
276276
returnFullResponse: true,
277277
});
278278
} catch (err) {
279-
if (retries <= 1) {
280-
throw new Error(err);
279+
if (err?.status !== 429 || retries <= 3) {
280+
$?.export?.("response", err);
281+
throw new Error("Error response");
281282
}
282283
// if rate limit is exceeded, Retry-After will contain the # of seconds
283284
// to wait before retrying
284-
const delay = (err && err.status == 429)
285-
? (err.headers["Retry-After"] * 1000)
286-
: 500;
285+
const delay = err?.headers?.["Retry-After"]
286+
? err.headers["Retry-After"] * 1000
287+
: (retries * 3000);
287288
await pause(delay);
288-
return this.retry($, config, retries - 1);
289+
return this.retry($, config, retries + 1);
289290
}
290291
},
291292
async getItems(types, q, limit, offset) {
@@ -329,71 +330,53 @@ export default {
329330
return item.name;
330331
}
331332
},
332-
async getPlaylist({
333-
$, playlistId, ...args
334-
}) {
335-
const res = await this._makeRequest({
336-
$,
333+
async getPlaylist(playlistId, args) {
334+
const { data } = await this._makeRequest({
337335
url: `/playlists/${playlistId}`,
338336
...args,
339337
});
340-
return res.data ?? {};
338+
return data;
341339
},
342-
async getPlaylists({
343-
$, ...args
344-
}) {
345-
const res = await this._makeRequest({
346-
$,
340+
async getPlaylists(args) {
341+
const { data } = await this._makeRequest({
347342
url: "/me/playlists",
348343
...args,
349344
});
350-
return res.data?.items ?? null;
345+
return data?.items ?? [];
351346
},
352-
async getCategories({
353-
$, ...args
354-
}) {
355-
const res = await this._makeRequest({
356-
$,
347+
async getCategories(args) {
348+
const { data } = await this._makeRequest({
357349
url: "/browse/categories",
358350
...args,
359351
});
360-
return res.data?.categories?.items ?? [];
352+
return data?.categories?.items ?? [];
361353
},
362-
async getUserTracks({
363-
$, ...args
364-
}) {
365-
const res = await this._makeRequest({
366-
$,
354+
async getUserTracks(args) {
355+
const { data } = await this._makeRequest({
367356
url: "/me/tracks",
368357
...args,
369358
});
370-
return res.data?.items ?? [];
359+
return data?.items ?? [];
371360
},
372361
async getPlaylistItems({
373362
$, playlistId, ...args
374363
}) {
375-
const res = await this._makeRequest({
364+
const { data } = await this._makeRequest({
376365
$,
377366
url: `/playlists/${playlistId}/tracks`,
378367
...args,
379368
});
380-
return res.data?.items ?? [];
369+
return data?.items ?? [];
381370
},
382-
async getGenres({
383-
$, ...args
384-
} = {}) {
371+
async getGenres(args) {
385372
const { data } = await this._makeRequest({
386-
$,
387373
url: "/recommendations/available-genre-seeds",
388374
...args,
389375
});
390-
return data.genres;
376+
return data?.genres;
391377
},
392-
async getRecommendations({
393-
$, ...args
394-
}) {
378+
async getRecommendations(args) {
395379
const { data } = await this._makeRequest({
396-
$,
397380
url: "/recommendations",
398381
...args,
399382
});

0 commit comments

Comments
 (0)