Files
HealthLog/src/app/api/auth/me/ai-usage/route.ts
T
Marc-André Bombeck 3786deb503 feat(api): publish the v1.43 contract with routes that answer 501 until filled
Every new endpoint of the contract exists, wrapped in apiHandler, with its
authentication, module gate and request validation, and answers
501 not_implemented until its behaviour lands. The OpenAPI document carries
the new operations and schemas and the additive response fields on the
changed routes. The four server feature flags are published as false.

contract-stub-inventory.test.ts registers the stubbed routes; each one
leaves the register with the change that fills it. The route, sharing,
session, security, module-gate and outcome inventories name the new routes
deliberately, and the error-code catalogue lists the codes the routes emit
today.
2026-10-15 21:49:02 +02:00

43 lines
1.5 KiB
TypeScript

/**
* `PUT /api/auth/me/ai-usage` — the account's own AI usage level (#1217).
*
* `{ level: "frugal" | "standard" | "full" | null }`; `null` follows the
* instance default. A level above the operator's ceiling is refused with 422
* `ai.usage.aboveCeiling`, never capped silently. Answers the resolved
* `AiUsageState`, the same block `GET /api/auth/me` publishes as `ai.usage`.
* Own account only (cookie or a full-scope token), at most 30 writes an hour.
*/
import { NextRequest } from "next/server";
import { apiHandler, requireAuth } from "@/lib/api-handler";
import { apiError, returnAllZodIssues, safeJson } from "@/lib/api-response";
import { notImplemented } from "@/lib/http/not-implemented";
import { checkRateLimit } from "@/lib/rate-limit";
import { aiUsagePutSchema } from "@/lib/validations/ai-usage";
export const dynamic = "force-dynamic";
export const PUT = apiHandler(async (request: NextRequest) => {
const { user } = await requireAuth();
const rl = await checkRateLimit(`ai-usage:${user.id}`, 30, 60 * 60 * 1000);
if (!rl.allowed) {
return apiError("Too many changes, try again later", 429, {
errorCode: "ai.usage.rateLimited",
});
}
const { data: body, error: jsonError } = await safeJson(request, {
maxBytes: 1024,
});
if (jsonError) return jsonError;
const parsed = aiUsagePutSchema.safeParse(body);
if (!parsed.success) {
return returnAllZodIssues(parsed.error, 422, {
errorCode: "ai.usage.invalid",
});
}
return notImplemented("auth.me.ai_usage.put");
});