feat(routes): per-endpoint duplicates override via @Post/@Get opts

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-06 20:16:46 -05:00
1 parent ec2e3a0e62
commit 34017f6ab2
4 files changed
+23 -6

No files matched your search

+3 -1
View File
@@ -45,6 +45,7 @@ interface RoutePlan {
needs: string[];
run: (ctx: any) => unknown;
transformer: typeof JsonTransformer;
duplicates?: 'array' | 'last';
}
export interface MeshConfig {
@@ -115,6 +116,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
needs: getHandlerNeeds(argSpecs),
run: (c: any) => inst[route.handlerName](...resolveArgs(argSpecs, c)),
transformer: getTransformer(C, route.handlerName) ?? JsonTransformer,
duplicates: route.duplicates,
});
}
}
@@ -319,7 +321,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
...routePlans
.filter((plan) => plan.transport !== 'ws') // buffer | sse | ndjson | negotiate
.map((plan): RouteDef => ({
method: plan.method, pattern: plan.pattern, transport: plan.transport,
method: plan.method, pattern: plan.pattern, transport: plan.transport, bodyDuplicates: plan.duplicates,
handler: async (req) => {
const provided = await providedSeed(plan);
return runPipeline({
+2 -2
View File
@@ -31,7 +31,7 @@ export type RouteHandler = (req: {
ip: string;
}) => Promise<PipelineResult>;
export interface RouteDef { method: string; pattern: string; transport: Transport; handler: RouteHandler }
export interface RouteDef { method: string; pattern: string; transport: Transport; handler: RouteHandler; bodyDuplicates?: 'array' | 'last' }
export interface MatchedRoute { params: Record<string, string>; def: RouteDef }
export interface WsOpenCtx {
@@ -258,7 +258,7 @@ export function createHttpServer(
}
let body: unknown;
const ct = String(req.headers['content-type'] ?? '');
const duplicates = opts?.bodyDuplicates ?? 'last';
const duplicates = matched.def.bodyDuplicates ?? opts?.bodyDuplicates ?? 'last';
if (buf !== undefined && ct.includes('application/json')) {
try { body = JSON.parse(buf.toString('utf8')); }
catch {
+3 -3
View File
@@ -7,7 +7,7 @@ export interface ProviderMeta { provides: string; needs: string[]; optional: boo
export interface StepMeta { provides: string; needs: string[]; optional: boolean; export: boolean }
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
export type Transport = 'buffer' | 'sse' | 'ndjson' | 'negotiate' | 'ws';
export interface RouteMeta { method: HttpMethod; path: string; handlerName: string; transport: Transport; export: boolean }
export interface RouteMeta { method: HttpMethod; path: string; handlerName: string; transport: Transport; export: boolean; duplicates?: 'array' | 'last' }
export interface ModuleOptions {
mountpoint: string; providers?: Ctor[]; steps?: Ctor[]; controllers?: Ctor[];
}
@@ -40,11 +40,11 @@ export function Route(prefix: string): ClassDecorator {
}
function routeDecorator(method: HttpMethod, transport: Transport) {
return (path: string, opts?: { export?: boolean }): MethodDecorator =>
return (path: string, opts?: { export?: boolean; duplicates?: 'array' | 'last' }): MethodDecorator =>
(target, propertyKey) => {
const ctor = target.constructor;
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
routes.push({ method, path, handlerName: String(propertyKey), transport, export: opts?.export ?? false });
routes.push({ method, path, handlerName: String(propertyKey), transport, export: opts?.export ?? false, duplicates: opts?.duplicates });
Reflect.defineMetadata(K.routes, routes, ctor);
};
}
+15
View File
@@ -72,4 +72,19 @@ describe('bodyDuplicates', () => {
method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: 'x=1&x=2' });
expect(await r.json()).toEqual({ x: '2' });
});
it('per-endpoint override beats app default', async () => {
@Route('/') class C {
@Post('/a', { duplicates: 'last' }) a(@body() b: any) { return b; }
@Post('/b', { duplicates: 'array' }) b2(@body() b: any) { return b; }
}
@Module({ mountpoint: '/', controllers: [C] }) class CMod {}
app = createApp({ modules: [CMod], bodyDuplicates: 'last' });
const server = await app.listen(0);
const port = (server.address() as any).port;
const post = (p: string) => fetch(`http://127.0.0.1:${port}${p}`, {
method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: 'x=1&x=2' }).then((r) => r.json());
expect(await post('/a')).toEqual({ x: '2' });
expect(await post('/b')).toEqual({ x: ['1', '2'] });
});
});