forked from mirrors/homebox
move to nuxt
This commit is contained in:
parent
890eb55d27
commit
26ecb5a9d4
93 changed files with 5273 additions and 4749 deletions
100
frontend/lib/requests/requests.ts
Normal file
100
frontend/lib/requests/requests.ts
Normal file
|
@ -0,0 +1,100 @@
|
|||
export enum Method {
|
||||
GET = 'GET',
|
||||
POST = 'POST',
|
||||
PUT = 'PUT',
|
||||
DELETE = 'DELETE',
|
||||
}
|
||||
|
||||
export interface TResponse<T> {
|
||||
status: number;
|
||||
error: boolean;
|
||||
data: T;
|
||||
response: Response;
|
||||
}
|
||||
|
||||
export class Requests {
|
||||
private baseUrl: string;
|
||||
private token: () => string;
|
||||
private headers: Record<string, string> = {};
|
||||
private logger?: (response: Response) => void;
|
||||
|
||||
private url(rest: string): string {
|
||||
return this.baseUrl + rest;
|
||||
}
|
||||
|
||||
constructor(
|
||||
baseUrl: string,
|
||||
token: string | (() => string) = '',
|
||||
headers: Record<string, string> = {},
|
||||
logger?: (response: Response) => void
|
||||
) {
|
||||
this.baseUrl = baseUrl;
|
||||
this.token = typeof token === 'string' ? () => token : token;
|
||||
this.headers = headers;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public get<T>(url: string): Promise<TResponse<T>> {
|
||||
return this.do<T>(Method.GET, url);
|
||||
}
|
||||
|
||||
public post<T, U>(url: string, payload: T): Promise<TResponse<U>> {
|
||||
return this.do<U>(Method.POST, url, payload);
|
||||
}
|
||||
|
||||
public put<T, U>(url: string, payload: T): Promise<TResponse<U>> {
|
||||
return this.do<U>(Method.PUT, url, payload);
|
||||
}
|
||||
|
||||
public delete<T>(url: string): Promise<TResponse<T>> {
|
||||
return this.do<T>(Method.DELETE, url);
|
||||
}
|
||||
|
||||
private methodSupportsBody(method: Method): boolean {
|
||||
return method === Method.POST || method === Method.PUT;
|
||||
}
|
||||
|
||||
private async do<T>(method: Method, url: string, payload: Object = {}): Promise<TResponse<T>> {
|
||||
const args: RequestInit = {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...this.headers,
|
||||
},
|
||||
};
|
||||
|
||||
const token = this.token();
|
||||
if (token !== '' && args.headers !== undefined) {
|
||||
args.headers['Authorization'] = token;
|
||||
}
|
||||
|
||||
if (this.methodSupportsBody(method)) {
|
||||
args.body = JSON.stringify(payload);
|
||||
}
|
||||
|
||||
const response = await fetch(this.url(url), args);
|
||||
|
||||
if (this.logger) {
|
||||
this.logger(response);
|
||||
}
|
||||
|
||||
const data: T = await (async () => {
|
||||
if (response.status === 204) {
|
||||
return {} as T;
|
||||
}
|
||||
|
||||
try {
|
||||
return await response.json();
|
||||
} catch (e) {
|
||||
return {} as T;
|
||||
}
|
||||
})();
|
||||
|
||||
return {
|
||||
status: response.status,
|
||||
error: !response.ok,
|
||||
data,
|
||||
response,
|
||||
};
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue