From f5e29189f70c5c8532916504a1a22f8c586f6e73 Mon Sep 17 00:00:00 2001 From: navewindre Date: Tue, 11 Nov 2025 08:11:24 +0100 Subject: new web --- web/src/api.tsx | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 web/src/api.tsx (limited to 'web/src/api.tsx') diff --git a/web/src/api.tsx b/web/src/api.tsx new file mode 100644 index 0000000..7233d89 --- /dev/null +++ b/web/src/api.tsx @@ -0,0 +1,39 @@ +export const url = "https://networkheaven.net"; + +export interface ReqParams { + method: string, + body?: string, +} + +export async function post( endpoint: string, body: Object ) { + return await req( endpoint, { + method: "POST", + body: JSON.stringify( body ), + } ); +} + +export async function req( endpoint: string, params: ReqParams ) { + const res = await fetch( `${url}/${endpoint}`, { + method: params.method, + headers: { + "Content-Type": "application/json", + }, + body: params.body, + } ); + + if( !res.ok ) { + let json = null; + try { + json = await res.json(); + } catch( e: any ) { + throw new Error( "error contacting server" ); + } + + throw new Error( json.msg ); + } + + const json = await res.json(); + if( json.status != 'ok' ) + throw new Error( json.msg ); + return json; +} -- cgit v1.2.3