Merge pull request #368 from DanTheMan827/patch-1
refactor(hifi): update localStorage and token handling
This commit is contained in:
commit
f9a58b1cac
1 changed files with 8 additions and 10 deletions
18
js/HiFi.ts
18
js/HiFi.ts
|
|
@ -18,8 +18,6 @@ export class TidalResponse extends Response {
|
|||
}
|
||||
}
|
||||
|
||||
/** A container for the mock localStorage */
|
||||
|
||||
export class HiFiClient {
|
||||
private static tokenPromise: Promise<string> | null = null;
|
||||
private static albumTracksMax = 20;
|
||||
|
|
@ -31,8 +29,7 @@ export class HiFiClient {
|
|||
private static _localStorage: Record<string, string> = {};
|
||||
private static get localStorage() {
|
||||
return (
|
||||
globalThis?.localStorage ??
|
||||
window?.localStorage ?? {
|
||||
globalThis?.localStorage ?? {
|
||||
getItem: (key) => HiFiClient._localStorage[key],
|
||||
setItem: (key, value) => {
|
||||
HiFiClient._localStorage[key] = String(value);
|
||||
|
|
@ -94,13 +91,18 @@ export class HiFiClient {
|
|||
}
|
||||
|
||||
private static encodeBasic(id: string, secret: string) {
|
||||
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
|
||||
return window.btoa(`${id}:${secret}`);
|
||||
if (typeof globalThis.btoa === 'function') {
|
||||
return btoa(`${id}:${secret}`);
|
||||
}
|
||||
// Node fallback
|
||||
return Buffer.from(`${id}:${secret}`).toString('base64');
|
||||
}
|
||||
|
||||
static setToken(token: string, expiry: number = Date.now() + 60000) {
|
||||
HiFiClient.token = token;
|
||||
HiFiClient.appTokenExpiry = expiry
|
||||
}
|
||||
|
||||
private static async fetchAppToken(
|
||||
signal: AbortSignal = new AbortController().signal,
|
||||
clientId: string,
|
||||
|
|
@ -747,10 +749,6 @@ export class HiFiClient {
|
|||
offset: qp.offset ? Number(qp.offset) : undefined,
|
||||
});
|
||||
default:
|
||||
// unknown local route => treat as raw upstream path (forward)
|
||||
if (pathOrUrl.startsWith('http')) {
|
||||
return await this.fetchJson(pathOrUrl);
|
||||
}
|
||||
throw new Error(`Unknown route: ${pathname}`);
|
||||
}
|
||||
} catch (err) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue