@soundify/api
Advanced tools
+27
-32
| import { objectToSearchParams, } from "@soundify/shared"; | ||
| /** | ||
| * Client will throw this error if spotify will return error | ||
| * The Spotify client will throw this error if the api response is not "ok" (status >= 400) | ||
| */ | ||
| export class SpotifyError extends Error { | ||
| status; | ||
| constructor(message, status) { | ||
| super(message); | ||
| constructor(message, status, options) { | ||
| super(message, options); | ||
| this.status = status; | ||
@@ -27,5 +27,5 @@ this.name = "SpotifyError" + status; | ||
| /** | ||
| * Access token or object that implements `Accessor` | ||
| * Access token or object that implements `IAccessProvider` | ||
| */ | ||
| #accessor; | ||
| #accessProvider; | ||
| #retry5xx = { | ||
@@ -45,4 +45,4 @@ times: 0, | ||
| */ | ||
| accessor, opts = {}) { | ||
| this.#accessor = accessor; | ||
| accessProvider, opts = {}) { | ||
| this.#accessProvider = accessProvider; | ||
| if (opts.retry5xx) | ||
@@ -53,4 +53,4 @@ this.#retry5xx = opts.retry5xx; | ||
| } | ||
| setAccessor(accessor) { | ||
| this.#accessor = accessor; | ||
| setAccessProvider(accessor) { | ||
| this.#accessProvider = accessor; | ||
| } | ||
@@ -66,5 +66,5 @@ async fetch(baseURL, responseType, { body, query, method } = {}) { | ||
| let retry429 = this.#retry429.times; | ||
| let accessToken = typeof this.#accessor === "string" | ||
| ? this.#accessor | ||
| : await this.#accessor.getAccessToken(); | ||
| let accessToken = typeof this.#accessProvider === "string" | ||
| ? this.#accessProvider | ||
| : await this.#accessProvider.getAccessToken(); | ||
| const call = async () => { | ||
@@ -82,31 +82,26 @@ const res = await fetch(url, { | ||
| return res; | ||
| if (res.status === 401 && typeof this.#accessor !== "string" && | ||
| if (res.status === 401 && typeof this.#accessProvider !== "string" && | ||
| !isTriedRefresh) { | ||
| const newToken = await this.#accessor.getAccessToken(true); | ||
| accessToken = newToken; | ||
| isTriedRefresh = true; | ||
| return call(); | ||
| try { | ||
| accessToken = await this.#accessProvider.getAccessToken(true); | ||
| isTriedRefresh = true; | ||
| return call(); | ||
| } | ||
| catch (e) { | ||
| throw new SpotifyError((await res.json()).error.message, res.status, { cause: e }); | ||
| } | ||
| } | ||
| if (res.status === 429 && retry429 > 0) { | ||
| if (this.#retry429.delay !== 0) { | ||
| if (res.status === 429 && retry429) { | ||
| if (this.#retry429.delay) | ||
| await wait(this.#retry429.delay); | ||
| } | ||
| retry429--; | ||
| return call(); | ||
| } | ||
| if (res.status.toString().startsWith("5") && retry5xx > 0) { | ||
| if (this.#retry5xx.delay !== 0) { | ||
| if (res.status.toString().startsWith("5") && retry5xx) { | ||
| if (this.#retry5xx.delay) | ||
| await wait(this.#retry5xx.delay); | ||
| } | ||
| retry5xx--; | ||
| return call(); | ||
| } | ||
| let error; | ||
| try { | ||
| error = await res.json(); | ||
| } | ||
| catch (_) { | ||
| throw new SpotifyError("Unable to read response body (not a json value)", res.status); | ||
| } | ||
| throw new SpotifyError(error.error.message, res.status); | ||
| throw new SpotifyError((await res.json()).error.message, res.status); | ||
| }; | ||
@@ -118,4 +113,4 @@ const res = await call(); | ||
| if (res.body) | ||
| await res.body.cancel(); | ||
| res.body.cancel(); | ||
| } | ||
| } |
+2
-2
@@ -5,3 +5,3 @@ { | ||
| "name": "@soundify/api", | ||
| "version": "0.0.34", | ||
| "version": "0.0.35", | ||
| "description": "Modern Spotify api wrapper for Node, Deno, and browser 🎧", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
| "dependencies": { | ||
| "@soundify/shared": "0.0.34" | ||
| "@soundify/shared": "0.0.35" | ||
| }, | ||
@@ -16,0 +16,0 @@ "packageManager": "pnpm@7.29.1", |
@@ -1,2 +0,2 @@ | ||
| import { Accessor, FetchOpts, HTTPClient, JSONValue } from "@soundify/shared"; | ||
| import { FetchOpts, HTTPClient, IAccessProvider, JSONValue } from "@soundify/shared"; | ||
| type Retry = { | ||
@@ -17,7 +17,7 @@ /** | ||
| /** | ||
| * Client will throw this error if spotify will return error | ||
| * The Spotify client will throw this error if the api response is not "ok" (status >= 400) | ||
| */ | ||
| export declare class SpotifyError extends Error { | ||
| readonly status: number; | ||
| constructor(message: string, status: number); | ||
| constructor(message: string, status: number, options?: ErrorOptions); | ||
| } | ||
@@ -50,4 +50,4 @@ /** | ||
| */ | ||
| accessor: Accessor | string, opts?: SpotifyClientOpts); | ||
| setAccessor(accessor: Accessor | string): void; | ||
| accessProvider: IAccessProvider | string, opts?: SpotifyClientOpts); | ||
| setAccessProvider(accessor: IAccessProvider | string): void; | ||
| fetch(baseURL: string, responseType: "void", opts?: FetchOpts): Promise<void>; | ||
@@ -54,0 +54,0 @@ fetch<R extends JSONValue = JSONValue>(baseURL: string, responseType: "json", opts?: FetchOpts): Promise<R>; |
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
93331
0.08%2692
-0.19%+ Added
- Removed
Updated