From dbdb2b70f15aa23854c1923970068b027029d936 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Thu, 22 Feb 2024 22:26:32 +0100 Subject: [PATCH] client: refactor API calling --- packages/client/src/os.ts | 12 +++++------- packages/client/src/scripts/initialize-sw.ts | 2 +- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/packages/client/src/os.ts b/packages/client/src/os.ts index c465d3211..cf89f65f3 100644 --- a/packages/client/src/os.ts +++ b/packages/client/src/os.ts @@ -27,7 +27,7 @@ export const api = ((endpoint: string, data: Record = {}, token?: s const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined; const promise = new Promise((resolve, reject) => { - fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, { + fetch(`${apiUrl}/${endpoint}`, { method: 'POST', body: JSON.stringify(data), credentials: 'omit', @@ -37,13 +37,12 @@ export const api = ((endpoint: string, data: Record = {}, token?: s ...(authorization ? { authorization } : {}), }, }).then(async (res) => { - const body = res.status === 204 ? null : await res.json(); - if (res.status === 200) { - resolve(body); + resolve(await res.json()); } else if (res.status === 204) { resolve(); } else { + const body = await res.json(); reject(body.error); } }).catch(reject); @@ -77,13 +76,12 @@ export const apiGet = ((endpoint: string, data: Record = {}, token? ...(authorization ? { authorization } : {}), }, }).then(async (res) => { - const body = res.status === 204 ? null : await res.json(); - if (res.status === 200) { - resolve(body); + resolve(await res.json()); } else if (res.status === 204) { resolve(); } else { + const body = await res.json(); reject(body.error); } }).catch(reject); diff --git a/packages/client/src/scripts/initialize-sw.ts b/packages/client/src/scripts/initialize-sw.ts index 584a023ef..cfcbe3414 100644 --- a/packages/client/src/scripts/initialize-sw.ts +++ b/packages/client/src/scripts/initialize-sw.ts @@ -23,7 +23,7 @@ export async function initializeSw() { function encode(buffer: ArrayBuffer | null) { return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer))); } - + // Register api('sw/register', { endpoint: subscription.endpoint,