add request script for API requests

This commit is contained in:
Andy 2022-12-29 03:02:11 +01:00
parent 76a8e000a3
commit ef0f6af97f

View file

@ -0,0 +1,51 @@
import { $i } from '@/account';
export const request = async (url: string, body?: Record<string, any>, options?: Record<string, any>, token?: string) => {
let requestOptions = {};
const authorizationToken = token ?? $i?.token ?? undefined;
const authorization = authorizationToken ? `Bearer ${authorizationToken}` : undefined;
requestOptions = {
...{
headers: {
'content-type': 'application/json',
...(authorization ? { authorization } : {}),
},
},
...options,
};
const response = await fetch(url, {
...requestOptions,
body: body ? JSON.stringify(body) : undefined,
});
const responseBody = response.status === 204 ? null : await response.json();
if (response.status === 200) {
return responseBody;
}
else if (response.status === 204) {
return null;
}
else {
throw new Error(responseBody.error);
}
};
export const get = async (url: string, options?: Record<string, any>, token?: string) => {
return request(url, undefined, { method: 'GET' }, token);
};
export const post = async (url: string, body: Record<string, any>, options?: Record<string, any>, token?: string) => {
return request(url, body, { method: 'POST' }, token);
};
export const put = async (url: string, body: Record<string, any>, options?: Record<string, any>, token?: string) => {
return request(url, body, { method: 'PUT' }, token);
};
export const del = async (url: string, options?: Record<string, any>, token?: string) => {
return request(url, undefined, { method: 'DELETE' }, token);
};