Files
suwayomi-material-you-webui/src/lib/requests/client/RestClient.ts
T

121 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-05-18 13:25:19 +02:00
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2023-08-15 14:39:45 +02:00
import { BaseClient } from '@/lib/requests/client/BaseClient.ts';
2023-05-18 13:25:19 +02:00
export enum HttpMethod {
2024-03-05 02:07:05 +01:00
GET = 'GET',
POST = 'POST',
PATCH = 'PATCH',
DELETE = 'DELETE',
2023-05-18 13:25:19 +02:00
}
export interface IRestClient {
2024-03-05 02:07:05 +01:00
get(url: string): Promise<Response>;
delete(url: string): Promise<Response>;
post(url: string, data?: any): Promise<Response>;
put(url: string, data?: any): Promise<Response>;
patch(url: string, data?: any): Promise<Response>;
2023-05-18 13:25:19 +02:00
}
2023-08-15 14:39:45 +02:00
export class RestClient
2024-03-05 02:07:05 +01:00
extends BaseClient<typeof fetch, RequestInit, (url: string, data: any) => Promise<Response>>
2023-08-15 14:39:45 +02:00
implements IRestClient
{
protected client!: typeof fetch;
2024-04-09 18:04:30 -04:00
private config: RequestInit = {
credentials: 'include',
};
2024-03-05 02:07:05 +01:00
public readonly fetcher = async (
2023-05-18 13:25:19 +02:00
url: string,
{
data,
httpMethod = HttpMethod.GET,
config,
checkResponseIsJson = true,
}: {
data?: any;
httpMethod?: HttpMethod;
2024-03-05 02:07:05 +01:00
config?: RequestInit;
2023-05-18 13:25:19 +02:00
checkResponseIsJson?: boolean;
} = {},
2024-03-05 02:07:05 +01:00
): Promise<Response> => {
const updatedUrl = url.startsWith('http') ? url : `${this.getBaseUrl()}${url}`;
let result: Response;
2023-05-18 13:25:19 +02:00
switch (httpMethod) {
case HttpMethod.GET:
2024-03-05 02:07:05 +01:00
result = await this.client(updatedUrl, { ...this.config, ...config, method: httpMethod });
2023-05-18 13:25:19 +02:00
break;
case HttpMethod.POST:
case HttpMethod.PATCH:
case HttpMethod.DELETE:
2024-03-05 02:07:05 +01:00
result = await this.client(updatedUrl, {
...this.config,
...config,
method: httpMethod,
body: JSON.stringify(data),
});
2023-05-18 13:25:19 +02:00
break;
default:
throw new Error(`Unexpected HttpMethod "${httpMethod}"`);
}
if (result.status !== 200) {
throw new Error(result.statusText);
}
2024-03-05 02:07:05 +01:00
if (checkResponseIsJson && result.headers.get('content-type') !== 'application/json') {
2023-05-18 13:25:19 +02:00
throw new Error('Response is not json');
}
2024-03-05 02:07:05 +01:00
return result;
2023-05-18 13:25:19 +02:00
};
constructor() {
super();
this.createClient();
}
private createClient(): void {
2024-03-05 02:07:05 +01:00
this.client = fetch.bind(window);
2023-05-18 13:25:19 +02:00
}
2024-03-05 02:07:05 +01:00
public updateConfig(config: RequestInit): void {
this.config = { ...this.config, ...config };
2023-05-18 13:25:19 +02:00
}
2024-03-05 02:07:05 +01:00
public getClient(): typeof fetch {
2023-05-18 13:25:19 +02:00
return this.client;
}
get get() {
2024-03-05 02:07:05 +01:00
return (url: string) => this.fetcher(url);
2023-05-18 13:25:19 +02:00
}
get post() {
2024-03-05 02:07:05 +01:00
return (url: string, data?: any) => this.fetcher(url, { data, httpMethod: HttpMethod.POST });
2023-05-18 13:25:19 +02:00
}
get put() {
2024-03-05 02:07:05 +01:00
return (url: string, data?: any) => this.fetcher(url, { data, httpMethod: HttpMethod.POST });
2023-05-18 13:25:19 +02:00
}
get patch() {
2024-03-05 02:07:05 +01:00
return (url: string, data?: any) => this.fetcher(url, { data, httpMethod: HttpMethod.PATCH });
2023-05-18 13:25:19 +02:00
}
get delete() {
2024-03-05 02:07:05 +01:00
return (url: string) => this.fetcher(url, { httpMethod: HttpMethod.DELETE });
2023-05-18 13:25:19 +02:00
}
}