Makes every call to the API succeed or fail the same way. It reads the JSON envelope, and turns the errors a request can end in into messages. Every component and hook that calls the API goes through it.

Functions

f
dataOr<T>(
res: Response,
fallback: T
): Promise<T>

Returns the payload of res, or fallback when the response says success: false. readFirstPage reads the profile pages' lists with it, and the profile its invitees, so that a list the server refuses shows empty rather than failing the page.

f
expectOk<T>(res: Response): Promise<T>

Returns the payload of res. In a form, the error it throws reaches useApiForm's submit, which shows it as the form's error.

f
friendlyFetchError(err: unknown): string

Returns the message to show for err, an error caught around a request. A TypeError means the request never reached the server (no network, CORS), and gets a message saying so. The server's own messages come through as they are.

f
parseAPIResponse<T>(raw: unknown): APIResponse<T>

Returns raw as an API envelope. Only the success field is checked, so the payload's type is the caller's word.