mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-12 10:48:58 +08:00
[+] Keychip sdk
This commit is contained in:
@@ -4,6 +4,7 @@ import type { GameName } from "./scoring";
|
|||||||
|
|
||||||
interface RequestInitWithParams extends RequestInit {
|
interface RequestInitWithParams extends RequestInit {
|
||||||
params?: { [index: string]: string }
|
params?: { [index: string]: string }
|
||||||
|
localCache?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -32,11 +33,18 @@ export function fetchWithParams(input: URL | RequestInfo, init?: RequestInitWith
|
|||||||
}), init)
|
}), init)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let cache: { [index: string]: any } = {}
|
||||||
|
|
||||||
export async function post(endpoint: string, params: any, init?: RequestInitWithParams): Promise<any> {
|
export async function post(endpoint: string, params: any, init?: RequestInitWithParams): Promise<any> {
|
||||||
// Add token if exists
|
// Add token if exists
|
||||||
const token = localStorage.getItem('token')
|
const token = localStorage.getItem('token')
|
||||||
if (token && !('token' in params)) params = { ...(params ?? {}), 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, {
|
let res = await fetchWithParams(AQUA_HOST + endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -64,7 +72,10 @@ export async function post(endpoint: string, params: any, init?: RequestInitWith
|
|||||||
throw new Error(`${text}`)
|
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 }),
|
post('/api/v2/user/confirm-email', { token }),
|
||||||
me: (): Promise<UserMe> =>
|
me: (): Promise<UserMe> =>
|
||||||
post('/api/v2/user/me', {}),
|
post('/api/v2/user/me', {}),
|
||||||
isLoggedIn: () => !!localStorage.getItem('token')
|
isLoggedIn: () => !!localStorage.getItem('token'),
|
||||||
|
keychip: (): Promise<string> =>
|
||||||
|
post('/api/v2/user/keychip', {}).then(it => it.keychip),
|
||||||
}
|
}
|
||||||
|
|
||||||
export const CARD = {
|
export const CARD = {
|
||||||
|
|||||||
Reference in New Issue
Block a user