diff options
| author | navewindre <boneyaard@gmail.com> | 2025-11-11 08:11:24 +0100 |
|---|---|---|
| committer | navewindre <boneyaard@gmail.com> | 2025-11-11 08:11:24 +0100 |
| commit | f5e29189f70c5c8532916504a1a22f8c586f6e73 (patch) | |
| tree | 9bf42144e608260527766e128268b380231ed95b /web/src/api.tsx | |
| parent | 6442494822d12c23cdd609031c4039d3309b64f6 (diff) | |
new web
Diffstat (limited to 'web/src/api.tsx')
| -rw-r--r-- | web/src/api.tsx | 39 |
1 files changed, 39 insertions, 0 deletions
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; +} |
