Files
sendou.ink/app/features/dev-tools/core/dev-api.ts
Kalle f7372324fe
Some checks failed
E2E Tests / e2e (push) Has been cancelled
Tests and checks on push / run-checks-and-tests (push) Has been cancelled
Updates translation progress / update-translation-progress-issue (push) Has been cancelled
E2E Tests / publish-docs-screenshots (push) Has been cancelled
Dev tools (#3471)
2026-10-25 11:43:36 +02:00

28 lines
759 B
TypeScript

/** Posts JSON to a dev tools action. Failures resolve to the message the server gave (`{ error }`) or a generic one. */
export async function postDev<T>(
url: string,
body: unknown = {},
): Promise<{ ok: true; data: T } | { ok: false; error: string }> {
const response = await fetch(url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!response.ok) {
const message = await response
.json()
.then((json: { error?: unknown }) =>
typeof json?.error === "string" ? json.error : null,
)
.catch(() => null);
return {
ok: false,
error: message ?? `Failed with ${response.status}, see the server log`,
};
}
return { ok: true, data: (await response.json()) as T };
}