homebox/frontend/lib/requests/requests.ts

119 lines
2.9 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",
2022-08-31 00:06:57 +00:00
}
2022-09-04 06:19:13 +00:00
export type RequestInterceptor = (r: Response) => void;
export type ResponseInterceptor = (r: Response) => void;
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) {
this.responseInterceptors.forEach(i => i(response));
}
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
}
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;
}
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) {
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 {
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);
2022-09-04 06:19:13 +00:00
this.callResponseInterceptors(response);
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
}