| | | 1 | | import { ApiProblemError, isApiProblem, type ProblemBody } from "~/api/problem"; |
| | | 2 | | |
| | | 3 | | export type FieldErrors = Record<string, string[]>; |
| | | 4 | | |
| | | 5 | | export type ApiActionResult<T> = |
| | | 6 | | | { ok: true; data: T } |
| | | 7 | | | { ok: false; error: string; fieldErrors?: FieldErrors }; |
| | | 8 | | |
| | | 9 | | /** |
| | | 10 | | * Run a backend call from a route action and translate validation/auth |
| | | 11 | | * problems into a structured result so the form can re-render in place. |
| | | 12 | | * |
| | | 13 | | * Returns `{ ok: false }` for 4xx (except 404, which bubbles to the nearest |
| | | 14 | | * ErrorBoundary because the resource itself is missing). 5xx and network |
| | | 15 | | * failures rethrow so the ErrorBoundary catches them with full ProblemDetails. |
| | | 16 | | */ |
| | 8 | 17 | | export async function tryApi<T>(fn: () => Promise<T>): Promise<ApiActionResult<T>> { |
| | 8 | 18 | | try { |
| | 8 | 19 | | return { ok: true, data: await fn() }; |
| | | 20 | | } catch (e) { |
| | 7 | 21 | | const problem = await coerceProblem(e); |
| | 7 | 22 | | if (problem && problem.status >= 400 && problem.status < 500 && problem.status !== 404) { |
| | 3 | 23 | | return { |
| | | 24 | | ok: false, |
| | | 25 | | error: problem.detail ?? problem.title, |
| | | 26 | | fieldErrors: problem.errors, |
| | | 27 | | }; |
| | | 28 | | } |
| | 4 | 29 | | throw e; |
| | | 30 | | } |
| | | 31 | | } |
| | | 32 | | |
| | 7 | 33 | | async function coerceProblem(e: unknown): Promise<ApiProblemError | null> { |
| | 7 | 34 | | if (isApiProblem(e)) return e; |
| | | 35 | | // The typed wrapper throws a Response with the ProblemDetails body so the |
| | | 36 | | // payload survives RR7's SSR serialization on the loader path. Actions need |
| | | 37 | | // to read it back here. |
| | 3 | 38 | | if (e instanceof Response) { |
| | | 39 | | let body: ProblemBody; |
| | 2 | 40 | | try { |
| | 2 | 41 | | body = (await e.clone().json()) as ProblemBody; |
| | | 42 | | } catch { |
| | 0 | 43 | | body = { title: e.statusText, status: e.status }; |
| | | 44 | | } |
| | 2 | 45 | | return new ApiProblemError(e.status, body); |
| | | 46 | | } |
| | 1 | 47 | | return null; |
| | | 48 | | } |