refactor: close type holes on the transport and error axes
- OpenApiRoute.transport was typed `string`, so the compiler could not check the transport branching at all. Type it Transport and key the documented 200 off a Record, so a new transport fails the build instead of silently going undocumented. - pickEncoder fell through to ndjson for unknown transports; make the mapping exhaustive so a new transport cannot inherit the wrong wire format by accident. - HttpError carries optional headers, removing the instanceof Redirect special case that coupled the error renderer to one subclass. Custom errors needing headers (retry-after, etag) no longer require touching transformers. - Drop the module-global connSeq in mesh/link: `pending` is already per-connection, so a per-link counter suffices and connection ids stop depending on test order. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+55
-36
No files matched your search
+23
-15
@@ -1,10 +1,11 @@
|
||||
import type { ArgSpec } from './params';
|
||||
import type { Transport } from './metadata';
|
||||
|
||||
/** Minimal per-route input the OpenAPI projection needs, derived from a route plan. */
|
||||
export interface OpenApiRoute {
|
||||
method: string;
|
||||
pattern: string;
|
||||
transport: string;
|
||||
transport: Transport;
|
||||
args: ArgSpec[];
|
||||
}
|
||||
|
||||
@@ -55,17 +56,23 @@ function argParams(args: ArgSpec[]): Parameter[] {
|
||||
return params;
|
||||
}
|
||||
|
||||
/** The success/error responses documented for a route, keyed by transport and whether any input is validated. */
|
||||
function responses(route: OpenApiRoute): Record<string, unknown> {
|
||||
const result: Record<string, unknown> = {};
|
||||
/**
|
||||
* 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'>;
|
||||
|
||||
if (route.transport === 'sse') {
|
||||
result['200'] = { description: 'Event stream', content: { 'text/event-stream': {} } };
|
||||
} else if (route.transport === 'ndjson' || route.transport === 'negotiate') {
|
||||
result['200'] = { description: 'Stream', content: { 'application/x-ndjson': {} } };
|
||||
} else {
|
||||
result['200'] = { description: 'OK', content: { 'application/json': { schema: {} } } };
|
||||
}
|
||||
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' };
|
||||
@@ -73,9 +80,9 @@ function responses(route: OpenApiRoute): Record<string, unknown> {
|
||||
}
|
||||
|
||||
/** Builds a single OpenAPI operation object for a route. */
|
||||
function operation(route: OpenApiRoute): Record<string, unknown> {
|
||||
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) };
|
||||
const op: Record<string, unknown> = { responses: responses(route, transport) };
|
||||
|
||||
if (parameters.length) op.parameters = parameters;
|
||||
|
||||
@@ -95,9 +102,10 @@ export function buildOpenApi(routes: OpenApiRoute[], info?: OpenApiInfo): OpenAp
|
||||
const paths: Record<string, Record<string, unknown>> = {};
|
||||
|
||||
for (const route of routes) {
|
||||
if (route.transport === 'ws') continue; // not an HTTP request/response
|
||||
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);
|
||||
(paths[path] ??= {})[route.method.toLowerCase()] = operation(route, transport);
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user