mirror of
https://github.com/block/buzz.git
synced 2026-08-18 06:50:31 +02:00
Signed-off-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1ntr8jjcqq6gt06q5avvqttfjgpwshmra22pcmcagdnukw4ja4nqqsa9g54 <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1dccv64krpcpse5cmkzfeh998cftungyatw3djt8jwdw6g43f7fyqzzmrf7 <6e30cd56c30e030cd31bb0939b94a7c257c9a09d5ba2d92cf2735da45629f248@sprout-oss.stage.blox.sqprod.co>
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>;
|
|
}
|