Files
accounted/lib/api/v1/body.ts
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

36 lines
1.0 KiB
TypeScript

/**
* JSON body reader for v1 routes.
*
* Every v1 write route parses its body the same way: read JSON, and on a
* malformed body answer with the VALIDATION_ERROR envelope pointing at
* `body`. This helper is that block, so routes do:
*
* const raw = await readV1JsonBody(request, ctx)
* if (!raw.ok) return raw.response
* const parsed = Schema.safeParse(raw.body)
*/
import type { NextResponse } from 'next/server'
import { v1ErrorResponseFromCode, type V1ValidationContext } from './errors'
export type V1JsonBodyResult =
| { ok: true; body: unknown }
| { ok: false; response: NextResponse }
export async function readV1JsonBody(
request: Request,
ctx: V1ValidationContext,
): Promise<V1JsonBodyResult> {
try {
return { ok: true, body: await request.json() }
} catch {
return {
ok: false,
response: await v1ErrorResponseFromCode('VALIDATION_ERROR', ctx.log, {
requestId: ctx.requestId,
details: { field: 'body', message: 'Body is not valid JSON.' },
}),
}
}
}