diff --git a/src/index.ts b/src/index.ts index c2609fc..b0628d5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,17 +23,21 @@ declare type SpotifyPluginOptions = { type Falsy = undefined | null | false | 0 | ""; const isTruthy = (x: T | Falsy): x is T => Boolean(x); +const refreshAPIToken = async () => { + if (expirationTime <= Date.now() - 60000) { + const res = await API.refreshAccessToken().catch(() => API.clientCredentialsGrant()); + expirationTime = Date.now() + res.body.expires_in * 1000; + API.setAccessToken(res.body.access_token); + } +}; + const getItems = async (data: any): Promise => { if (!data.tracks.items) return data.tracks; const items: any[] = data.tracks.items; if (!["playlist", "album"].includes(data.type)) return items; while (data.tracks.next) { if (!expirationTime) break; - if (expirationTime <= Date.now() - 60000) { - const res = await API.refreshAccessToken().catch(() => API.clientCredentialsGrant()); - expirationTime = Date.now() + res.body.expires_in * 1000; - API.setAccessToken(res.body.access_token); - } + await refreshAPIToken(); try { data.tracks = ( await API[data.type === "playlist" ? "getPlaylistTracks" : "getAlbumTracks"](data.id, { @@ -58,6 +62,7 @@ const getDataWithAPI = async (url: string) => { let data: any; const id = (parsedURL).id; if (!id) throw new DisTubeError("SPOTIFY_PLUGIN_UNSUPPORTED_LINK", "This link is not supported."); + await refreshAPIToken(); try { switch (parsedURL.type) { case "track": diff --git a/tsup.config.ts b/tsup.config.ts index 729264c..58785d1 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -4,7 +4,7 @@ export const tsup: Options = { clean: true, dts: true, entry: ["src/index.ts"], - format: ["esm", "cjs"], + format: "cjs", minify: false, keepNames: true, skipNodeModulesBundle: true,