import { NextResponse } from "next/server"; import { z } from "zod"; import { withAuth } from "@/lib/api-auth"; import { apiError, handleApiError } from "@/lib/api-errors"; import { createToken, listTokens, TokenError } from "@/lib/api-tokens"; export const dynamic = "force-dynamic"; /** API tokens for machine clients (Settings → API tokens). Never includes the tokens themselves. */ export const GET = withAuth(async () => NextResponse.json(await listTokens())); const Body = z.object({ name: z.string().max(40), scopes: z.array(z.string()).min(1).max(10) }); /** Create a token. The response is the only time the token is shown. */ export const POST = withAuth(async (req) => { try { const body = Body.parse(await req.json()); return NextResponse.json(await createToken(body.name, body.scopes), { status: 201, headers: { "Cache-Control": "no-store" } }); } catch (err) { if (err instanceof TokenError) return apiError(err.status, err.code, err.message); return handleApiError(err); } });