Skip to content
File

Blob: src/client/lib/http.ts

typescript19 lines
1export const apiRequest = (input: RequestInfo | URL, init: RequestInit = {}): Promise<Response> =>
2 fetch(input, { ...init, credentials: init.credentials ?? "same-origin" });
3 
4export const apiJsonRequest = (
5 input: RequestInfo | URL,
6 body: unknown,
7 init: Omit<RequestInit, "body"> = {},
8): Promise<Response> =>
9 apiRequest(input, {
10 ...init,
11 headers: { "content-type": "application/json", ...init.headers },
12 body: JSON.stringify(body),
13 });
14 
15export const responseMessage = async (res: Response, fallback: string): Promise<string> => {
16 const body = (await res.json().catch(() => null)) as { message?: unknown } | null;
17 return typeof body?.message === "string" && body.message.length > 0 ? body.message : fallback;
18};