diff --git a/packages/client/src/scripts/request.ts b/packages/client/src/scripts/request.ts new file mode 100644 index 000000000..f8cfa928e --- /dev/null +++ b/packages/client/src/scripts/request.ts @@ -0,0 +1,51 @@ +import { $i } from '@/account'; + +export const request = async (url: string, body?: Record, options?: Record, 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, token?: string) => { + return request(url, undefined, { method: 'GET' }, token); +}; + +export const post = async (url: string, body: Record, options?: Record, token?: string) => { + return request(url, body, { method: 'POST' }, token); +}; + +export const put = async (url: string, body: Record, options?: Record, token?: string) => { + return request(url, body, { method: 'PUT' }, token); +}; + +export const del = async (url: string, options?: Record, token?: string) => { + return request(url, undefined, { method: 'DELETE' }, token); +};