feat(openapi): document constrained route params
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+56
-13
No files matched your search
+2
-2
@@ -1,6 +1,6 @@
|
||||
import type { RouteDef, MatchedRoute } from './types';
|
||||
|
||||
type CompiledSegment =
|
||||
export type CompiledSegment =
|
||||
| { kind: 'static'; value: string }
|
||||
| { kind: 'param'; name: string; constraint?: RegExp; constraintSource?: string }
|
||||
| { kind: 'catchAll'; name: string };
|
||||
@@ -147,7 +147,7 @@ export function compilePattern(pattern: string): CompiledPattern {
|
||||
if (pattern.includes('//')) throw new Error(`invalid route pattern '${pattern}': repeated slash`);
|
||||
|
||||
const normalized = pattern.length > 1 && pattern.endsWith('/') ? pattern.slice(0, -1) : pattern;
|
||||
const rawSegments = normalized.split('/').slice(1);
|
||||
const rawSegments = normalized === '/' ? [] : normalized.split('/').slice(1);
|
||||
const segments = compileSegments(pattern, rawSegments);
|
||||
const compiled: CompiledPattern = {
|
||||
source: pattern,
|
||||
|
||||
+24
-9
@@ -1,5 +1,6 @@
|
||||
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 {
|
||||
@@ -23,23 +24,37 @@ export interface OpenApiDoc {
|
||||
paths: Record<string, Record<string, unknown>>;
|
||||
}
|
||||
|
||||
type Parameter = { name: string; in: 'path' | 'query' | 'header'; required: boolean; schema: { type: 'string' } };
|
||||
type Parameter = {
|
||||
name: string;
|
||||
in: 'path' | 'query' | 'header';
|
||||
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 = pattern
|
||||
.split('/')
|
||||
.filter(Boolean)
|
||||
.map((seg) => (seg.startsWith(':') ? `{${seg.slice(1).replace(/\*$/, '')}}` : seg));
|
||||
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 pattern
|
||||
.split('/')
|
||||
.filter((seg) => seg.startsWith(':'))
|
||||
.map((seg) => ({ name: seg.slice(1).replace(/\*$/, ''), in: 'path', required: true, schema: { type: 'string' } }));
|
||||
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 } : {}),
|
||||
},
|
||||
},
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/** Query and header parameters declared by the handler's `@query` / `@headers` / `@header` argument decorators. */
|
||||
|
||||
Reference in new issue
Block a user