forked from FoundKeyGang/FoundKey
client: refactor API calling
This commit is contained in:
parent
d4a5ed29db
commit
dbdb2b70f1
2 changed files with 6 additions and 8 deletions
|
@ -27,7 +27,7 @@ export const api = ((endpoint: string, data: Record<string, any> = {}, token?: s
|
||||||
const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined;
|
const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined;
|
||||||
|
|
||||||
const promise = new Promise<void>((resolve, reject) => {
|
const promise = new Promise<void>((resolve, reject) => {
|
||||||
fetch(endpoint.indexOf('://') > -1 ? endpoint : `${apiUrl}/${endpoint}`, {
|
fetch(`${apiUrl}/${endpoint}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify(data),
|
body: JSON.stringify(data),
|
||||||
credentials: 'omit',
|
credentials: 'omit',
|
||||||
|
@ -37,13 +37,12 @@ export const api = ((endpoint: string, data: Record<string, any> = {}, token?: s
|
||||||
...(authorization ? { authorization } : {}),
|
...(authorization ? { authorization } : {}),
|
||||||
},
|
},
|
||||||
}).then(async (res) => {
|
}).then(async (res) => {
|
||||||
const body = res.status === 204 ? null : await res.json();
|
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
resolve(body);
|
resolve(await res.json());
|
||||||
} else if (res.status === 204) {
|
} else if (res.status === 204) {
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
|
const body = await res.json();
|
||||||
reject(body.error);
|
reject(body.error);
|
||||||
}
|
}
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
@ -77,13 +76,12 @@ export const apiGet = ((endpoint: string, data: Record<string, any> = {}, token?
|
||||||
...(authorization ? { authorization } : {}),
|
...(authorization ? { authorization } : {}),
|
||||||
},
|
},
|
||||||
}).then(async (res) => {
|
}).then(async (res) => {
|
||||||
const body = res.status === 204 ? null : await res.json();
|
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (res.status === 200) {
|
||||||
resolve(body);
|
resolve(await res.json());
|
||||||
} else if (res.status === 204) {
|
} else if (res.status === 204) {
|
||||||
resolve();
|
resolve();
|
||||||
} else {
|
} else {
|
||||||
|
const body = await res.json();
|
||||||
reject(body.error);
|
reject(body.error);
|
||||||
}
|
}
|
||||||
}).catch(reject);
|
}).catch(reject);
|
||||||
|
|
|
@ -23,7 +23,7 @@ export async function initializeSw() {
|
||||||
function encode(buffer: ArrayBuffer | null) {
|
function encode(buffer: ArrayBuffer | null) {
|
||||||
return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
|
return btoa(String.fromCharCode.apply(null, new Uint8Array(buffer)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Register
|
// Register
|
||||||
api('sw/register', {
|
api('sw/register', {
|
||||||
endpoint: subscription.endpoint,
|
endpoint: subscription.endpoint,
|
||||||
|
|
Loading…
Reference in a new issue