diff --git a/lib/routes/spotify/artist.ts b/lib/routes/spotify/artist.ts index e25f7884206091..fef440bd0c4d54 100644 --- a/lib/routes/spotify/artist.ts +++ b/lib/routes/spotify/artist.ts @@ -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', @@ -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 { diff --git a/lib/routes/spotify/artists-top.ts b/lib/routes/spotify/artists-top.ts index 0a8497d713a319..eff23670538158 100644 --- a/lib/routes/spotify/artists-top.ts +++ b/lib/routes/spotify/artists-top.ts @@ -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', @@ -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 { diff --git a/lib/routes/spotify/playlist.ts b/lib/routes/spotify/playlist.ts index b1888db12c6a6e..dffc8aae60d042 100644 --- a/lib/routes/spotify/playlist.ts +++ b/lib/routes/spotify/playlist.ts @@ -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', @@ -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 { diff --git a/lib/routes/spotify/saved.ts b/lib/routes/spotify/saved.ts index 7aeafbc7998081..a288c36ef0cec1 100644 --- a/lib/routes/spotify/saved.ts +++ b/lib/routes/spotify/saved.ts @@ -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?', @@ -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 { diff --git a/lib/routes/spotify/show.ts b/lib/routes/spotify/show.ts index 8302fa48460357..64540eabd30330 100644 --- a/lib/routes/spotify/show.ts +++ b/lib/routes/spotify/show.ts @@ -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 = { @@ -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; diff --git a/lib/routes/spotify/tracks-top.ts b/lib/routes/spotify/tracks-top.ts index 3ab80540b1178b..c9b732bba43f2b 100644 --- a/lib/routes/spotify/tracks-top.ts +++ b/lib/routes/spotify/tracks-top.ts @@ -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', @@ -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 { diff --git a/lib/routes/spotify/utils.ts b/lib/routes/spotify/utils.ts index ea460fba9fce45..d2777414fb2959 100644 --- a/lib/routes/spotify/utils.ts +++ b/lib/routes/spotify/utils.ts @@ -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() { @@ -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; } @@ -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; }