From c9787a521ba993115d42112766bdd1ea2faff107 Mon Sep 17 00:00:00 2001 From: Azalea <22280294+hykilpikonna@users.noreply.github.com> Date: Tue, 20 Feb 2024 18:41:59 -0500 Subject: [PATCH] [+] SDK --- AquaNet/src/libs/sdk.ts | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 AquaNet/src/libs/sdk.ts diff --git a/AquaNet/src/libs/sdk.ts b/AquaNet/src/libs/sdk.ts new file mode 100644 index 00000000..28a0eaf9 --- /dev/null +++ b/AquaNet/src/libs/sdk.ts @@ -0,0 +1,72 @@ +import { AQUA_HOST } from "./config"; + +interface RequestInitWithParams extends RequestInit { + params?: { [index: string]: string } +} + +/** + * Modify a fetch url + * + * @param input Fetch url input + * @param callback Callback for modification + */ +export function reconstructUrl(input: URL | RequestInfo, callback: (url: URL) => URL | void): RequestInfo | URL { + let u = new URL((input instanceof Request) ? input.url : input); + const result = callback(u) + if (result) u = result + if (input instanceof Request) { + // @ts-ignore + return { url: u, ...input } + } + return u +} + +/** + * Fetch with url parameters + */ +export function fetchWithParams(input: URL | RequestInfo, init?: RequestInitWithParams): Promise { + return fetch(reconstructUrl(input, u => { + u.search = new URLSearchParams(init?.params ?? {}).toString() + }), init) +} + +export async function post(endpoint: string, params: any, init?: RequestInitWithParams): Promise { + // Add token if exists + const token = localStorage.getItem('token') + if (token) params = { ...(params ?? {}), token } + + let res = await fetchWithParams(AQUA_HOST + endpoint, { + method: 'POST', + headers: { + 'Content-Type': 'application/json' + }, + params, + ...init + }).catch(e => { + console.error(e) + throw new Error('Network error') + }) + + if (!res.ok) { + const text = await res.text() + console.error(`${res.status}: ${text}`) + throw new Error(`${text}`) + } + + return res.json() +} + +/** + * aqua.net.UserRegistrar + * + * @param user + */ +export async function register(user: { username: string, email: string, password: string, turnstile: string }) { + return await post('/api/v2/user/register', user) +} +export async function login(user: { email: string, password: string, turnstile: string }) { + const data = await post('/api/v2/user/login', user) + + // Put token into local storage + localStorage.setItem('token', data.token) +} \ No newline at end of file