mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
26 lines
625 B
TypeScript
26 lines
625 B
TypeScript
const PREFIX = "/api/admin/v1";
|
|||
|
|
|
||
|
|
export class ApiFailure extends Error {
|
||
|
|
constructor(
|
||
|
|
public status: number,
|
||
|
|
message: string,
|
||
|
|
) {
|
||
|
|
super(message);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export async function request<T>(path: string): Promise<T> {
|
||
|
|
const response = await fetch(`${PREFIX}${path}`, {
|
||
|
|
credentials: "same-origin",
|
||
|
|
headers: { accept: "application/json" },
|
||
|
|
});
|
||
|
|
if (!response.ok) {
|
||
|
|
const envelope = await response.json().catch(() => null);
|
||
|
|
throw new ApiFailure(
|
||
|
|
response.status,
|
||
|
|
envelope?.error?.message ?? `Request failed (${response.status})`,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
return response.json() as Promise<T>;
|
||
|
|
}
|