diff --git a/api/tracks.ts b/api/tracks.ts index 5b2b6887..a77598ba 100644 --- a/api/tracks.ts +++ b/api/tracks.ts @@ -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 => { const response = await fetch(TOKEN_ENDPOINT, { method: "POST", headers: { @@ -87,11 +93,10 @@ const getAccessToken = async () => { }), }); - return response.json(); + return response.json() as Promise; }; const getNowPlaying = async (): Promise => { - // 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 => { 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 => { }); // 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) => ({ diff --git a/api/types/tracks.d.ts b/api/types/tracks.d.ts index 80b13420..e706b83d 100644 --- a/api/types/tracks.d.ts +++ b/api/types/tracks.d.ts @@ -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;