diff --git a/AquaNet/src/libs/sdk.ts b/AquaNet/src/libs/sdk.ts index 93d76a98..d40203cc 100644 --- a/AquaNet/src/libs/sdk.ts +++ b/AquaNet/src/libs/sdk.ts @@ -4,6 +4,7 @@ import type { GameName } from "./scoring"; interface RequestInitWithParams extends RequestInit { params?: { [index: string]: string } + localCache?: boolean } /** @@ -32,11 +33,18 @@ export function fetchWithParams(input: URL | RequestInfo, init?: RequestInitWith }), init) } +let cache: { [index: string]: any } = {} + export async function post(endpoint: string, params: any, init?: RequestInitWithParams): Promise { // Add token if exists const token = localStorage.getItem('token') if (token && !('token' in params)) params = { ...(params ?? {}), token } + if (init?.localCache) { + const cached = cache[endpoint + JSON.stringify(params) + JSON.stringify(init)] + if (cached) return cached + } + let res = await fetchWithParams(AQUA_HOST + endpoint, { method: 'POST', headers: { @@ -64,7 +72,10 @@ export async function post(endpoint: string, params: any, init?: RequestInitWith throw new Error(`${text}`) } - return res.json() + const ret = res.json() + cache[endpoint + JSON.stringify(params) + JSON.stringify(init)] = ret + + return ret } /** @@ -89,7 +100,9 @@ export const USER = { post('/api/v2/user/confirm-email', { token }), me: (): Promise => post('/api/v2/user/me', {}), - isLoggedIn: () => !!localStorage.getItem('token') + isLoggedIn: () => !!localStorage.getItem('token'), + keychip: (): Promise => + post('/api/v2/user/keychip', {}).then(it => it.keychip), } export const CARD = {