Docs: every v2 route documents its body, answer and parameters (1.52.1)

Most routes answered by a v1 handler showed an empty body and
`data: unknown` in /docs/v2. Each now documents what its handler reads
and answers, and every path and query parameter is described.

- `documented: { body?, response? }` on v1Route/defineRoute, with shared
  shapes in src/v2/docs.ts. Documentation only: not enforced, so nothing
  that works can start failing; outside production a body or answer that
  doesn't fit is logged ("doesn't match its documented schema").
- QUERY_HELP / PATH_HELP describe common parameters; empty bodies say
  "No body needed".
- Declared the funnel's eventIds and webhook deliveries' status/before.
- tests/v2Docs.test.ts keeps the document complete; frontend types
  regenerated, three call sites' types tightened (no payload change).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-02 13:26:15 +02:00
co-authored by Claude Opus 5.5
parent 6e64e64bff
commit d74af96f1d
27 changed files with 34727 additions and 1036 deletions
+18
View File
@@ -57,6 +57,13 @@ export type RouteDef<P extends Schema | undefined, Q extends Schema | undefined,
* and `response` is `fileResponse`. `contentType` here is what the docs show.
*/
file?: { contentType: string };
/**
* Documentation only (v1Route, 1.52.1): the body and the answer's `data` as the v1 handler takes
* and gives them, for the OpenAPI document. Not enforced — the handler gets what was sent and its
* answer goes out as it is — but outside production anything that doesn't fit is logged
* (`[api/v2] … doesn't match its documented …`), so the docs can't drift unnoticed.
*/
documented?: { body?: Schema; response?: Schema };
handler: (ctx: Ctx<Infer<P>, Infer<Q>, Infer<B>>) => Promise<z.input<R>> | z.input<R>;
};
@@ -89,6 +96,15 @@ export function defineRoute<P extends Schema | undefined, Q extends Schema | und
const parse = <S extends Schema | undefined>(schema: S, value: unknown): Infer<S> =>
(schema ? schema.parse(value) : undefined) as Infer<S>;
/** Outside production: says when a documented-only body or answer doesn't fit its documentation. */
function warnUndocumented(def: AnyRoute, what: string, schema: Schema, value: unknown) {
if (process.env.NODE_ENV === 'production') return;
const checked = schema.safeParse(value);
if (!checked.success) {
console.warn(`[api/v2] ${def.method.toUpperCase()} ${def.path}: ${what} doesn't match its documented schema`, JSON.stringify(checked.error.issues.slice(0, 5)));
}
}
/** Adds every defined route to the router. */
export function mount(router: Router, list: AnyRoute[] = routes): void {
for (const def of list) {
@@ -102,7 +118,9 @@ export function mount(router: Router, list: AnyRoute[] = routes): void {
req,
res,
};
if (def.documented?.body) warnUndocumented(def, 'request body', def.documented.body, req.body ?? {});
const result = await def.handler(ctx);
if (def.documented?.response) warnUndocumented(def, 'answer', def.documented.response, (result as { data?: unknown })?.data);
const checked = def.response.safeParse(result);
if (!checked.success) {
console.error(`[api/v2] ${def.method.toUpperCase()} ${def.path}: response doesn't match its schema`, checked.error.issues);