mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-29 00:59:37 +07:00
26 lines
662 B
TypeScript
26 lines
662 B
TypeScript
import { BASE_URL } from "./consts";
|
|
|
|
export const api = async (url: string, options?: RequestInit) => {
|
|
const res = await fetch(BASE_URL + "/api" + url, options);
|
|
if (!res.ok) {
|
|
const body = await res.text().catch(() => null);
|
|
throw new APIError(body || res.statusText, res.status);
|
|
}
|
|
|
|
const contentType = res.headers.get("content-type");
|
|
if (contentType?.includes("application/json")) {
|
|
return res.json();
|
|
}
|
|
|
|
return res.text();
|
|
};
|
|
|
|
export class APIError extends Error {
|
|
code: number;
|
|
|
|
constructor(message: string, statusCode?: number) {
|
|
super(message);
|
|
this.code = typeof statusCode === "number" ? statusCode : 400;
|
|
}
|
|
}
|