2024-06-07 03:51:33 +02:00
|
|
|
import { DEFAULT_UA } from "./constants";
|
|
|
|
|
|
|
|
|
|
type HttpVerb = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
|
2024-06-07 06:30:08 +02:00
|
|
|
type ConvertibleObject = {
|
|
|
|
|
[key: string]:
|
|
|
|
|
| string
|
|
|
|
|
| number
|
|
|
|
|
| boolean
|
|
|
|
|
| File
|
|
|
|
|
| undefined
|
|
|
|
|
| null
|
|
|
|
|
| ConvertibleObject[]
|
|
|
|
|
| ConvertibleObject;
|
|
|
|
|
};
|
2024-06-07 03:51:33 +02:00
|
|
|
|
2024-06-08 03:01:47 +02:00
|
|
|
/**
|
|
|
|
|
* Output of a request. Contains the data and headers.
|
|
|
|
|
* @template ReturnType The type of the data returned by the request.
|
|
|
|
|
*/
|
2024-06-07 03:51:33 +02:00
|
|
|
export interface Output<ReturnType> {
|
|
|
|
|
data: ReturnType;
|
|
|
|
|
headers: Headers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const objectToFormData = (obj: ConvertibleObject): FormData => {
|
|
|
|
|
return Object.keys(obj).reduce((formData, key) => {
|
|
|
|
|
if (obj[key] === undefined || obj[key] === null) return formData;
|
2024-06-07 06:30:08 +02:00
|
|
|
if (obj[key] instanceof File) {
|
|
|
|
|
formData.append(key, obj[key] as Blob);
|
|
|
|
|
return formData;
|
|
|
|
|
}
|
|
|
|
|
if (Array.isArray(obj[key])) {
|
|
|
|
|
(obj[key] as ConvertibleObject[]).forEach((item, index) => {
|
|
|
|
|
if (item instanceof File) {
|
|
|
|
|
formData.append(`${key}[${index}]`, item as Blob);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
formData.append(`${key}[${index}]`, String(item));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
|
}
|
|
|
|
|
if (typeof obj[key] === "object") {
|
|
|
|
|
const nested = objectToFormData(obj[key] as ConvertibleObject);
|
|
|
|
|
|
|
|
|
|
for (const [nestedKey, value] of nested.entries()) {
|
|
|
|
|
formData.append(`${key}[${nestedKey}]`, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return formData;
|
|
|
|
|
}
|
2024-06-07 03:51:33 +02:00
|
|
|
formData.append(key, String(obj[key]));
|
|
|
|
|
return formData;
|
|
|
|
|
}, new FormData());
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-08 03:01:47 +02:00
|
|
|
/**
|
|
|
|
|
* Wrapper around Error, useful for detecting if an error
|
|
|
|
|
* is due to a failed request.
|
|
|
|
|
*
|
|
|
|
|
* Throws if the request returns invalid or unexpected data.
|
|
|
|
|
*/
|
2024-06-07 03:51:33 +02:00
|
|
|
export class ResponseError extends Error {}
|
|
|
|
|
|
|
|
|
|
export class BaseClient {
|
|
|
|
|
constructor(
|
|
|
|
|
protected baseUrl: URL,
|
|
|
|
|
private accessToken?: string,
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
get url(): URL {
|
|
|
|
|
return this.baseUrl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get token(): string | undefined {
|
|
|
|
|
return this.accessToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async request<ReturnType>(
|
|
|
|
|
request: Request,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
const result = await fetch(request);
|
|
|
|
|
|
|
|
|
|
if (!result.ok) {
|
|
|
|
|
const error = await result.json();
|
|
|
|
|
throw new ResponseError(
|
|
|
|
|
`Request failed (${result.status}): ${
|
|
|
|
|
error.error || error.message || result.statusText
|
|
|
|
|
}`,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
data: await result.json(),
|
|
|
|
|
headers: result.headers,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async constructRequest(
|
|
|
|
|
path: string,
|
|
|
|
|
method: HttpVerb,
|
|
|
|
|
body?: object | FormData,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Request> {
|
2024-06-08 01:04:25 +02:00
|
|
|
const headers = new Headers({
|
|
|
|
|
"User-Agent": DEFAULT_UA,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (this.accessToken) {
|
|
|
|
|
headers.set("Authorization", `Bearer ${this.accessToken}`);
|
|
|
|
|
}
|
|
|
|
|
if (body) {
|
|
|
|
|
if (!(body instanceof FormData)) {
|
|
|
|
|
headers.set("Content-Type", "application/json; charset=utf-8");
|
|
|
|
|
} // else: let FormData set the content type, as it knows best (boundary, etc.)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const [key, value] of Object.entries(extra?.headers || {})) {
|
|
|
|
|
headers.set(key, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-07 03:51:33 +02:00
|
|
|
return new Request(new URL(path, this.baseUrl).toString(), {
|
|
|
|
|
method,
|
2024-06-08 01:04:25 +02:00
|
|
|
headers,
|
2024-06-07 03:51:33 +02:00
|
|
|
body: body
|
|
|
|
|
? body instanceof FormData
|
|
|
|
|
? body
|
|
|
|
|
: JSON.stringify(body)
|
|
|
|
|
: undefined,
|
|
|
|
|
...extra,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async get<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(path, "GET", undefined, extra),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async post<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body?: object,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(path, "POST", body, extra),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async postForm<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body: FormData | ConvertibleObject,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(
|
|
|
|
|
path,
|
|
|
|
|
"POST",
|
|
|
|
|
body instanceof FormData ? body : objectToFormData(body),
|
|
|
|
|
extra,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async put<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body?: object,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(path, "PUT", body, extra),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async putForm<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body: FormData | ConvertibleObject,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(
|
|
|
|
|
path,
|
|
|
|
|
"PUT",
|
|
|
|
|
body instanceof FormData ? body : objectToFormData(body),
|
|
|
|
|
extra,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async patch<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body?: object,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(path, "PATCH", body, extra),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async patchForm<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body: FormData | ConvertibleObject,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(
|
|
|
|
|
path,
|
|
|
|
|
"PATCH",
|
|
|
|
|
body instanceof FormData ? body : objectToFormData(body),
|
|
|
|
|
extra,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async delete<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body?: object,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(path, "DELETE", body, extra),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected async deleteForm<ReturnType>(
|
|
|
|
|
path: string,
|
|
|
|
|
body: FormData | ConvertibleObject,
|
|
|
|
|
extra?: RequestInit,
|
|
|
|
|
): Promise<Output<ReturnType>> {
|
|
|
|
|
return await this.request(
|
|
|
|
|
await this.constructRequest(
|
|
|
|
|
path,
|
|
|
|
|
"DELETE",
|
|
|
|
|
body instanceof FormData ? body : objectToFormData(body),
|
|
|
|
|
extra,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|