Files
core/src/openapi.ts
T
chrnx 47743fdf1f
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 27s
CI / runtimes (deno) (pull_request) Successful in 20s
CI / runtimes (edge) (pull_request) Successful in 14s
feat(http)!: cookie decorators, setCookie, and string | string[] response headers (CARD-14, #113)
Reading a cookie meant parsing the Cookie header by hand, and a response
could not carry more than one Set-Cookie: response header values were
Record<string, string>, and the fetch adapters would have stringified an
array into one broken "a,b" cookie anyway.

- @cookie('name') / @cookies() sit next to @header/@headers as a new
  'cookies' arg source. The header is parsed on first use per request and
  cached on the context, so a route without a cookie argument never parses
  it. RFC 6265: first value wins, %-sequences decoded, malformed pairs
  skipped (never a 400), undecodable values kept raw. Several cookie lines
  are joined with "; ". The map has no prototype. @needs('cookies') reads
  the same map, and 'cookies' joins the seed-key allow-list.
- setCookie(name, value, options) serialises one Set-Cookie value with
  HttpOnly, SameSite=Lax, Path=/ and Secure by default (D10), and throws,
  naming the field, on anything that could inject a header or that browsers
  reject (sameSite none / partitioned without secure).
- Response header values widen to string | string[] (D11) across
  TransformerFn, ErrorResponse, HttpError(Like).headers, ResponseShape and
  the neutral outcome. Node writes a set-cookie array as one line per
  element and joins every other array with ", " before writeHead; the
  fetch adapters build a Headers and append each set-cookie element.
  mergeInjectedHeaders keeps arrays by reference and joins a vary array
  before merging it.
- OpenAPI maps a cookie argument to an in: cookie parameter.

BREAKING CHANGE: code that reads a header value from TransformerFn,
ErrorResponse, HttpError or ResponseShape as a string must handle string[].

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
2026-10-23 00:02:49 -05:00

143 lines
5.1 KiB
TypeScript

import type { ArgSpec } from './params';
import type { Transport } from './metadata';
import { compilePattern } from './http/router';
/** Minimal per-route input the OpenAPI projection needs, derived from a route plan. */
export interface OpenApiRoute {
method: string;
pattern: string;
transport: Transport;
args: ArgSpec[];
}
/** `info` block overrides for the generated document. */
export interface OpenApiInfo {
title?: string;
version?: string;
description?: string;
}
/** A generated OpenAPI 3.1 document (structural: paths, params, and error responses; body/response schemas are generic). */
export interface OpenApiDoc {
openapi: string;
info: { title: string; version: string; description?: string };
paths: Record<string, Record<string, unknown>>;
}
type Parameter = {
name: string;
in: 'path' | 'query' | 'header' | 'cookie';
required: boolean;
schema: { type: 'string'; pattern?: string };
};
/** Converts a route pattern to an OpenAPI path template: `/users/:id` → `/users/{id}`, `:rest*` → `{rest}`. */
function templatePath(pattern: string): string {
const segments = compilePattern(pattern).segments.map((segment) =>
segment.kind === 'static' ? segment.value : `{${segment.name}}`,
);
return `/${segments.join('/')}`;
}
/** Every `:name` (and `:name*`) segment of a pattern becomes a required path parameter. */
function pathParams(pattern: string): Parameter[] {
return compilePattern(pattern).segments.flatMap((segment): Parameter[] => {
if (segment.kind === 'static') return [];
return [
{
name: segment.name,
in: 'path',
required: true,
schema: {
type: 'string',
...(segment.kind === 'param' && segment.constraintSource ? { pattern: segment.constraintSource } : {}),
},
},
];
});
}
/** Where each argument source sits in an OpenAPI parameter list; sources absent here are not parameters. */
const ARG_LOCATION: Partial<Record<ArgSpec['source'], Parameter['in']>> = {
query: 'query',
headers: 'header',
cookies: 'cookie',
};
/** Query, header and cookie parameters declared by `@query` / `@headers` / `@header` / `@cookies` / `@cookie`. */
function argParams(args: ArgSpec[]): Parameter[] {
const params: Parameter[] = [];
for (const arg of args) {
const location = ARG_LOCATION[arg.source];
if (!location) continue;
const names = arg.keys ?? (arg.key ? [arg.key] : []);
for (const name of names) params.push({ name, in: location, required: false, schema: { type: 'string' } });
}
return params;
}
/**
* The documented `200` per transport. `ws` is absent by construction — ws routes are not
* HTTP request/response and are filtered out before this runs. A new transport adds a row,
* and the compiler fails until it does.
*/
type HttpTransport = Exclude<Transport, 'ws'>;
const TRANSPORT_OK: Record<HttpTransport, Record<string, unknown>> = {
buffer: { description: 'OK', content: { 'application/json': { schema: {} } } },
sse: { description: 'Event stream', content: { 'text/event-stream': {} } },
ndjson: { description: 'Stream', content: { 'application/x-ndjson': {} } },
negotiate: { description: 'Stream', content: { 'application/x-ndjson': {} } },
};
/** The success/error responses documented for a route, keyed by transport and whether any input is validated. */
function responses(route: OpenApiRoute, transport: HttpTransport): Record<string, unknown> {
const result: Record<string, unknown> = { '200': TRANSPORT_OK[transport] };
if (route.args.some((arg) => arg.schema)) result['422'] = { description: 'Validation failed' };
result['500'] = { description: 'Internal Server Error' };
return result;
}
/** Builds a single OpenAPI operation object for a route. */
function operation(route: OpenApiRoute, transport: HttpTransport): Record<string, unknown> {
const parameters = [...pathParams(route.pattern), ...argParams(route.args)];
const op: Record<string, unknown> = { responses: responses(route, transport) };
if (parameters.length) op.parameters = parameters;
if (route.args.some((arg) => arg.source === 'body')) {
op.requestBody = { content: { 'application/json': { schema: {} } } };
}
return op;
}
/**
* Projects the route graph into a structural OpenAPI 3.1 document: paths, methods, path/query/header
* parameters, which routes take a body, and the systematic error responses. Body and response *schemas*
* are generic — Standard Schema doesn't expose JSON Schema (see the roadmap). WebSocket routes are omitted.
*/
export function buildOpenApi(routes: OpenApiRoute[], info?: OpenApiInfo): OpenApiDoc {
const paths: Record<string, Record<string, unknown>> = {};
for (const route of routes) {
const transport = route.transport;
if (transport === 'ws') continue; // not an HTTP request/response; narrows to HttpTransport below
const path = templatePath(route.pattern);
(paths[path] ??= {})[route.method.toLowerCase()] = operation(route, transport);
}
return {
openapi: '3.1.0',
info: {
title: info?.title ?? 'green-tea API',
version: info?.version ?? '0.0.0',
...(info?.description ? { description: info.description } : {}),
},
paths,
};
}