| 1 | import createFetchClient from "openapi-fetch"; |
| 2 | import type { paths |
| 3 | import { API_URL } |
| 4 | |
| 5 | /** Typed API client for client components. Unlike the server one, the browser carries the session cookie itself - it only has to be asked to. */ |
| 6 | export const apiClient = |
| 7 | baseUrl: API_URL, |
| 8 | credentials: "include", |
| 9 | }); |
| 10 | |
| 11 | /** Resolves an API call to its data, throwing the API's error message. */ |
| 12 | export async function unwrap |
| 13 | request: Promise |
| 14 | ): Promise<T |
| 15 | const { data, |
| 16 | if (error !== |
| 17 | return data as |
| 18 | } |
| 19 | |
| 20 | /** The message the API's error filter returns, or a status-code fallback. */ |
| 21 | export function apiErrorMessage( |
| 22 | error: unknown, |
| 23 | fallback = "Something went wrong" |
| 24 | ) { |
| 25 | const message = |
| 26 | if (typeof message |
| 27 | // ValidationPipe answers with one message per failed constraint. |
| 28 | if (Array.isArray |
| 29 | return fallback; |
| 30 | } |