diff options
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; +} |
