Something went wrong. Try again.
Simple shell implementation.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132import { type Result, err, ok } from "../lib/result";
export type ApiError = | { readonly status: number; readonly type: "bad-response"; } | { readonly error: unknown; readonly type: "request-failed"; };
export async function request<T>(path: string, init?: RequestInit): Promise<Result<T, ApiError>> { try { const headers = new Headers(init?.headers); headers.set("content-type", "application/json");
const response = await fetch(path, { ...init, headers, });
if (!response.ok) { return err({ status: response.status, type: "bad-response" }); }
return ok((await response.json()) as T); } catch (error) { return err({ error, type: "request-failed" }); }}