import { validateAndGetAccountId } from '@repo/jwt' import type { Context } from 'hono' import type { App } from './context' /** * Resolve the account id from a Bearer token, mirroring the repeated * auth-header check. Returns `null` when the header is missing, * the token is invalid, or the `sub` claim isn't an integer. */ export async function authedId(c: Context): Promise { return validateAndGetAccountId(c.req.raw, await c.env.JWT_SECRET.get()) } /** Results.Unauthorized() equivalent — 401 with empty body. */ export function unauthorized(c: Context) { return c.body(null, 401) } /** Reads the `Ids` form field into a list of integer ids. */ export async function parseFormIds(c: Context): Promise { const body = await c.req.parseBody().catch(() => ({}) as Record) const ids = body.Ids if (typeof ids !== 'string') return [] return ids .split(',') .map((s) => Number.parseInt(s.trim(), 10)) .filter((n) => !Number.isNaN(n)) } /** Read integer ids from repeated/comma-separated `id` query params. The 2023 * client passes these to the bulk GET endpoints (e.g. `?id=1&id=2`). */ export function queryIds(c: Context): number[] { return ( c.req .queries('id') ?.flatMap((v) => v.split(',')) .map((s) => Number.parseInt(s.trim(), 10)) .filter((n) => !Number.isNaN(n)) ?? [] ) }