mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-12 22:26:13 +00:00
refactor(api): read the remaining settings and admin route bodies through parseJsonBody
Admin, coach, consent, chart-overlay, device registration, insights feedback and provider chain, ECG ingest, onboarding, search and the AI provider settings move to parseJsonBody with the same message, error code, status and wide-event action they answered with. A refusal whose annotate meta is computed from the issues, or puts its keys in another order than the helper would, keeps that annotate verbatim in onInvalid. The routes that still call safeJson do something between the read and the schema: an optimistic-concurrency token split off the body, a count cap answered first, a refunded quota slot, a conditional read, a non-Zod check, or the body handed to a service.
This commit is contained in:
1 parent
af14d0aa7e
commit
ca04ef01b3
19 files changed
+203
-411
No files matched your search
+15
-23
@@ -28,12 +28,8 @@
|
||||
import { NextRequest } from "next/server";
|
||||
|
||||
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
|
||||
import {
|
||||
apiError,
|
||||
apiSuccess,
|
||||
returnAllZodIssues,
|
||||
safeJson,
|
||||
} from "@/lib/api-response";
|
||||
import { apiError, apiSuccess } from "@/lib/api-response";
|
||||
import { parseJsonBody } from "@/lib/api/body";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { defaultLocale, locales, type Locale } from "@/lib/i18n/config";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
@@ -71,24 +67,20 @@ export const POST = apiHandler(async (request: NextRequest) => {
|
||||
return apiError("Too many searches. Please retry shortly.", 429);
|
||||
}
|
||||
|
||||
const { data: body, error: jsonError } = await safeJson<{
|
||||
q?: unknown;
|
||||
limit?: unknown;
|
||||
}>(request, { maxBytes: SEARCH_BODY_MAX_BYTES });
|
||||
if (jsonError) return jsonError;
|
||||
const parsed = searchQuerySchema.safeParse({
|
||||
q: body?.q ?? undefined,
|
||||
limit: body?.limit ?? undefined,
|
||||
const parsed = await parseJsonBody(request, searchQuerySchema, {
|
||||
maxBytes: SEARCH_BODY_MAX_BYTES,
|
||||
input: (raw) => {
|
||||
const body = raw as { q?: unknown; limit?: unknown };
|
||||
return {
|
||||
q: body?.q ?? undefined,
|
||||
limit: body?.limit ?? undefined,
|
||||
};
|
||||
},
|
||||
action: "search.query.invalid",
|
||||
countIssues: true,
|
||||
errorCode: "search.invalid",
|
||||
});
|
||||
if (!parsed.success) {
|
||||
annotate({
|
||||
action: { name: "search.query.invalid" },
|
||||
meta: { issue_count: parsed.error.issues.length },
|
||||
});
|
||||
return returnAllZodIssues(parsed.error, 422, {
|
||||
errorCode: "search.invalid",
|
||||
});
|
||||
}
|
||||
if (!parsed.ok) return parsed.error;
|
||||
|
||||
const slot = beginSearch(actor.id, user.id);
|
||||
try {
|
||||
|
||||
Reference in new issue
Block a user