homebox/frontend/lib/requests/requests.ts

125 lines
3.2 KiB
TypeScript
Raw Permalink Normal View History

2022-08-31 00:06:57 +00:00
export enum Method {
GET = "GET",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
PATCH = "PATCH",
2022-08-31 00:06:57 +00:00
}
export type ResponseInterceptor = (r: Response, rq?: RequestInit) => void;
2022-09-04 06:19:13 +00:00
2022-08-31 00:06:57 +00:00
export interface TResponse<T> {
2022-08-31 02:11:36 +00:00
status: number;
error: boolean;
data: T;
response: Response;
2022-08-31 00:06:57 +00:00
}
2022-09-09 06:05:23 +00:00
export type RequestArgs<T> = {
url: string;
body?: T;
data?: FormData;
headers?: Record<string, string>;
};
2022-08-31 00:06:57 +00:00
export class Requests {
2022-08-31 02:11:36 +00:00
private baseUrl: string;
private token: () => string;
private headers: Record<string, string> = {};
2022-09-04 06:19:13 +00:00
private responseInterceptors: ResponseInterceptor[] = [];
addResponseInterceptor(interceptor: ResponseInterceptor) {
this.responseInterceptors.push(interceptor);
}
private callResponseInterceptors(response: Response, request?: RequestInit) {
this.responseInterceptors.forEach(i => i(response, request));
2022-09-04 06:19:13 +00:00
}
2022-08-31 02:11:36 +00:00
private url(rest: string): string {
return this.baseUrl + rest;
}
constructor(baseUrl: string, token: string | (() => string) = "", headers: Record<string, string> = {}) {
2022-08-31 02:11:36 +00:00
this.baseUrl = baseUrl;
this.token = typeof token === "string" ? () => token : token;
2022-08-31 02:11:36 +00:00
this.headers = headers;
}
2022-09-09 06:05:23 +00:00
public get<T>(args: RequestArgs<T>): Promise<TResponse<T>> {
return this.do<T>(Method.GET, args);
2022-08-31 02:11:36 +00:00
}
2022-09-09 06:05:23 +00:00
public post<T, U>(args: RequestArgs<T>): Promise<TResponse<U>> {
return this.do<U>(Method.POST, args);
2022-08-31 02:11:36 +00:00
}
2022-09-09 06:05:23 +00:00
public put<T, U>(args: RequestArgs<T>): Promise<TResponse<U>> {
return this.do<U>(Method.PUT, args);
2022-08-31 02:11:36 +00:00
}
public patch<T, U>(args: RequestArgs<T>): Promise<TResponse<U>> {
return this.do<U>(Method.PATCH, args);
}
2022-09-09 06:05:23 +00:00
public delete<T>(args: RequestArgs<T>): Promise<TResponse<T>> {
return this.do<T>(Method.DELETE, args);
2022-08-31 02:11:36 +00:00
}
private methodSupportsBody(method: Method): boolean {
return method === Method.POST || method === Method.PUT || method === Method.PATCH;
2022-08-31 02:11:36 +00:00
}
2022-09-09 06:05:23 +00:00
private async do<T>(method: Method, rargs: RequestArgs<unknown>): Promise<TResponse<T>> {
const payload: RequestInit = {
2022-08-31 02:11:36 +00:00
method,
headers: {
2022-09-09 06:05:23 +00:00
...rargs.headers,
2022-08-31 02:11:36 +00:00
...this.headers,
} as Record<string, string>,
2022-08-31 02:11:36 +00:00
};
const token = this.token();
if (token !== "" && payload.headers !== undefined) {
// @ts-expect-error - we know that the header is there
payload.headers["Authorization"] = token; // eslint-disable-line dot-notation
2022-08-31 02:11:36 +00:00
}
if (this.methodSupportsBody(method)) {
2022-09-09 06:05:23 +00:00
if (rargs.data) {
payload.body = rargs.data;
} else {
// @ts-expect-error - we know that the header is there
payload.headers["Content-Type"] = "application/json";
2022-09-09 06:05:23 +00:00
payload.body = JSON.stringify(rargs.body);
}
2022-08-31 02:11:36 +00:00
}
2022-09-09 06:05:23 +00:00
const response = await fetch(this.url(rargs.url), payload);
this.callResponseInterceptors(response, payload);
2022-08-31 02:11:36 +00:00
const data: T = await (async () => {
2022-09-01 22:32:03 +00:00
if (response.status === 204) {
return {} as T;
}
if (response.headers.get("Content-Type")?.startsWith("application/json")) {
try {
return await response.json();
} catch (e) {
return {} as T;
}
2022-08-31 02:11:36 +00:00
}
return response.body as unknown as T;
2022-08-31 02:11:36 +00:00
})();
return {
status: response.status,
error: !response.ok,
data,
response,
};
}
2022-08-31 00:06:57 +00:00
}