mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
feat: add sandbox
This commit is contained in:
+25
@@ -0,0 +1,25 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user