Skip to content

Commit

Permalink
fix(route): fix Spotify TypeError (#15732)
Browse files Browse the repository at this point in the history
* fix(route): fix Spotify TypeError

* fix(route): use builtin ofetch
  • Loading branch information
ttl97 authored May 27, 2024
1 parent 2e839e6 commit 9a0ef60
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 78 deletions.
29 changes: 13 additions & 16 deletions lib/routes/spotify/artist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import utils from './utils';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/artist/:id',
Expand Down Expand Up @@ -38,21 +38,18 @@ export const route: Route = {
async function handler(ctx) {
const token = await utils.getPublicToken();
const id = ctx.req.param('id');
const meta = await got
.get(`https://api.spotify.com/v1/artists/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();

const itemsResponse = await got
.get(`https://api.spotify.com/v1/artists/${id}/albums`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();
const meta = await ofetch(`https://api.spotify.com/v1/artists/${id}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});
const itemsResponse = await ofetch(`https://api.spotify.com/v1/artists/${id}/albums`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});
const albums = itemsResponse.items;

return {
Expand Down
15 changes: 7 additions & 8 deletions lib/routes/spotify/artists-top.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
import utils from './utils';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/top/artists',
Expand Down Expand Up @@ -41,13 +41,12 @@ export const route: Route = {

async function handler() {
const token = await utils.getPrivateToken();
const itemsResponse = await got
.get(`https://api.spotify.com/v1/me/top/artists`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();
const itemsResponse = await ofetch(`https://api.spotify.com/v1/me/top/artists`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});
const items = itemsResponse.items;

return {
Expand Down
15 changes: 7 additions & 8 deletions lib/routes/spotify/playlist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import utils from './utils';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/playlist/:id',
Expand Down Expand Up @@ -38,13 +38,12 @@ export const route: Route = {
async function handler(ctx) {
const token = await utils.getPublicToken();
const id = ctx.req.param('id');
const meta = await got
.get(`https://api.spotify.com/v1/playlists/${id}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();
const meta = await ofetch(`https://api.spotify.com/v1/playlists/${id}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});
const tracks = meta.tracks.items;

return {
Expand Down
15 changes: 7 additions & 8 deletions lib/routes/spotify/saved.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route } from '@/types';
import utils from './utils';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/saved/:limit?',
Expand Down Expand Up @@ -47,13 +47,12 @@ async function handler(ctx) {
const limit = ctx.req.param('limit');
const pageSize = isNaN(Number.parseInt(limit)) ? 50 : Number.parseInt(limit);

const itemsResponse = await got
.get(`https://api.spotify.com/v1/me/tracks?limit=${pageSize}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();
const itemsResponse = await ofetch(`https://api.spotify.com/v1/me/tracks?limit=${pageSize}`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});
const tracks = itemsResponse.items;

return {
Expand Down
15 changes: 7 additions & 8 deletions lib/routes/spotify/show.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
import utils from './utils';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
Expand Down Expand Up @@ -39,13 +39,12 @@ async function handler(ctx) {
const token = await utils.getPublicToken();
const id = ctx.req.param('id');

const meta = await got
.get(`https://api.spotify.com/v1/shows/${id}?market=US`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();
const meta = await ofetch(`https://api.spotify.com/v1/shows/${id}?market=US`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});

const episodes = meta.episodes.items;

Expand Down
15 changes: 7 additions & 8 deletions lib/routes/spotify/tracks-top.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Route } from '@/types';
import utils from './utils';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';

export const route: Route = {
path: '/top/tracks',
Expand Down Expand Up @@ -41,13 +41,12 @@ export const route: Route = {

async function handler() {
const token = await utils.getPrivateToken();
const itemsResponse = await got
.get(`https://api.spotify.com/v1/me/top/tracks`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.json();
const itemsResponse = await ofetch(`https://api.spotify.com/v1/me/top/tracks`, {
method: 'GET',
headers: {
Authorization: `Bearer ${token}`,
},
});
const items = itemsResponse.items;

return {
Expand Down
44 changes: 22 additions & 22 deletions lib/routes/spotify/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';

// Token used to retrieve public information.
async function getPublicToken() {
Expand All @@ -10,16 +10,16 @@ async function getPublicToken() {

const { clientId, clientSecret } = config.spotify;

const tokenResponse = await got
.post('https://accounts.spotify.com/api/token', {
headers: {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
},
form: {
grant_type: 'client_credentials',
},
})
.json();
const tokenResponse = await ofetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'client_credentials',
}).toString(),
});
return tokenResponse.access_token;
}

Expand All @@ -32,17 +32,17 @@ async function getPrivateToken() {

const { clientId, clientSecret, refreshToken } = config.spotify;

const tokenResponse = await got
.post('https://accounts.spotify.com/api/token', {
headers: {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
},
form: {
grant_type: 'refresh_token',
refresh_token: refreshToken,
},
})
.json();
const tokenResponse = await ofetch('https://accounts.spotify.com/api/token', {
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(`${clientId}:${clientSecret}`).toString('base64')}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: new URLSearchParams({
grant_type: 'refresh_token',
refresh_token: refreshToken,
}).toString(),
});
return tokenResponse.access_token;
}

Expand Down

0 comments on commit 9a0ef60

Please sign in to comment.