Open History to see the last commit
1/** Admin-key gating + the JSON error envelope used by every API response. */
2
3export function jsonError(
4 status: number,
5 code: string,
6 message: string,
7): Response {
8 return Response.json({ error: { code, message } }, { status })
9}
10
11/**
12 * Admin requests present the key via `X-Admin-Key` or `Authorization:
13 * Bearer`. Always false when no ADMIN_KEY is configured — the admin surface
14 * fails closed.
15 */
16export function isAdminRequest(
17 request: Request,
18 adminKey: string | undefined,
19): boolean {
20 if (!adminKey) return false
21 const authorization = request.headers.get('authorization')
22 const bearer = authorization?.startsWith('Bearer ')
23 ? authorization.slice('Bearer '.length)
24 : null
25 const presented = request.headers.get('x-admin-key') ?? bearer
26 return presented === adminKey
27}