refactor(hifi): update localStorage and token handling
Modified token encoding to use globalThis and added a setToken method.
This commit is contained in:
parent
cc2f28a798
commit
4f0d95bf42
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 {
|
export class HiFiClient {
|
||||||
private static tokenPromise: Promise<string> | null = null;
|
private static tokenPromise: Promise<string> | null = null;
|
||||||
private static albumTracksMax = 20;
|
private static albumTracksMax = 20;
|
||||||
|
|
@ -31,8 +29,7 @@ export class HiFiClient {
|
||||||
private static _localStorage: Record<string, string> = {};
|
private static _localStorage: Record<string, string> = {};
|
||||||
private static get localStorage() {
|
private static get localStorage() {
|
||||||
return (
|
return (
|
||||||
globalThis?.localStorage ??
|
globalThis?.localStorage ?? {
|
||||||
window?.localStorage ?? {
|
|
||||||
getItem: (key) => HiFiClient._localStorage[key],
|
getItem: (key) => HiFiClient._localStorage[key],
|
||||||
setItem: (key, value) => {
|
setItem: (key, value) => {
|
||||||
HiFiClient._localStorage[key] = String(value);
|
HiFiClient._localStorage[key] = String(value);
|
||||||
|
|
@ -94,13 +91,18 @@ export class HiFiClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static encodeBasic(id: string, secret: string) {
|
private static encodeBasic(id: string, secret: string) {
|
||||||
if (typeof window !== 'undefined' && typeof window.btoa === 'function') {
|
if (typeof globalThis.btoa === 'function') {
|
||||||
return window.btoa(`${id}:${secret}`);
|
return btoa(`${id}:${secret}`);
|
||||||
}
|
}
|
||||||
// Node fallback
|
// Node fallback
|
||||||
return Buffer.from(`${id}:${secret}`).toString('base64');
|
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(
|
private static async fetchAppToken(
|
||||||
signal: AbortSignal = new AbortController().signal,
|
signal: AbortSignal = new AbortController().signal,
|
||||||
clientId: string,
|
clientId: string,
|
||||||
|
|
@ -747,10 +749,6 @@ export class HiFiClient {
|
||||||
offset: qp.offset ? Number(qp.offset) : undefined,
|
offset: qp.offset ? Number(qp.offset) : undefined,
|
||||||
});
|
});
|
||||||
default:
|
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}`);
|
throw new Error(`Unknown route: ${pathname}`);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue