fix(route): fix Spotify TypeError (#15732)
* fix(route): fix Spotify TypeError * fix(route): use builtin ofetch
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+22
-22
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user