mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-27 12:36:20 -04:00
fix changes in node-fetch v3 re: response.json() return types
This commit is contained in:
parent
3a69932c03
commit
445674cc69
@ -6,7 +6,13 @@ import { VercelRequest, VercelResponse } from "@vercel/node";
|
||||
import fetch from "node-fetch";
|
||||
import * as queryString from "query-string";
|
||||
|
||||
import type { Track, SpotifyTrackSchema, SpotifyActivitySchema } from "./types/tracks";
|
||||
import type {
|
||||
Track,
|
||||
SpotifyTrackSchema,
|
||||
SpotifyActivitySchema,
|
||||
SpotifyTokenSchema,
|
||||
SpotifyTopSchema,
|
||||
} from "./types/tracks";
|
||||
|
||||
const { SPOTIFY_CLIENT_ID, SPOTIFY_CLIENT_SECRET, SPOTIFY_REFRESH_TOKEN } = process.env;
|
||||
|
||||
@ -74,7 +80,7 @@ export default async (req: VercelRequest, res: VercelResponse) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getAccessToken = async () => {
|
||||
const getAccessToken = async (): Promise<SpotifyTokenSchema> => {
|
||||
const response = await fetch(TOKEN_ENDPOINT, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
@ -87,11 +93,10 @@ const getAccessToken = async () => {
|
||||
}),
|
||||
});
|
||||
|
||||
return response.json();
|
||||
return response.json() as Promise<SpotifyTokenSchema>;
|
||||
};
|
||||
|
||||
const getNowPlaying = async (): Promise<Track> => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const { access_token } = await getAccessToken();
|
||||
|
||||
const response = await fetch(NOW_PLAYING_ENDPOINT, {
|
||||
@ -107,8 +112,7 @@ const getNowPlaying = async (): Promise<Track> => {
|
||||
return { isPlaying: false };
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const active: SpotifyActivitySchema = await response.json();
|
||||
const active = (await response.json()) as SpotifyActivitySchema;
|
||||
|
||||
if (active.is_playing === true && active.item) {
|
||||
return {
|
||||
@ -138,7 +142,7 @@ const getTopTracks = async (): Promise<Track[]> => {
|
||||
});
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
const { items } = await response.json();
|
||||
const { items } = (await response.json()) as SpotifyTopSchema;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
||||
const tracks: Track[] = items.map((track: Readonly<SpotifyTrackSchema>) => ({
|
||||
|
14
api/types/tracks.d.ts
vendored
14
api/types/tracks.d.ts
vendored
@ -20,8 +20,20 @@ export type SpotifyActivitySchema = {
|
||||
item?: SpotifyTrackSchema;
|
||||
};
|
||||
|
||||
export type SpotifyTokenSchema = {
|
||||
access_token: string;
|
||||
token_type: string;
|
||||
scope: string;
|
||||
expires_in: number;
|
||||
refresh_token: string;
|
||||
};
|
||||
|
||||
export type SpotifyTopSchema = {
|
||||
items: SpotifyTrackSchema[];
|
||||
};
|
||||
|
||||
export type Track = {
|
||||
isPlaying: boolean;
|
||||
isPlaying?: boolean;
|
||||
artist?: string;
|
||||
title?: string;
|
||||
album?: string;
|
||||
|
Loading…
x
Reference in New Issue
Block a user