From 34017f6ab2b39fe9d24cf49898332f6366f2e9d9 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Mon, 6 Jul 2026 20:16:46 -0500 Subject: [PATCH] feat(routes): per-endpoint duplicates override via @Post/@Get opts Signed-off-by: Diego Resendez --- src/app.ts | 4 +++- src/http.ts | 4 ++-- src/metadata.ts | 6 +++--- test/multipart-http.test.ts | 15 +++++++++++++++ 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/app.ts b/src/app.ts index 4a3f30d..2161e75 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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({ diff --git a/src/http.ts b/src/http.ts index 1c0c97c..4b28355 100644 --- a/src/http.ts +++ b/src/http.ts @@ -31,7 +31,7 @@ export type RouteHandler = (req: { ip: string; }) => Promise; -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; 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 { diff --git a/src/metadata.ts b/src/metadata.ts index 33e48dc..be4efce 100644 --- a/src/metadata.ts +++ b/src/metadata.ts @@ -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); }; } diff --git a/test/multipart-http.test.ts b/test/multipart-http.test.ts index cd5d550..871d157 100644 --- a/test/multipart-http.test.ts +++ b/test/multipart-http.test.ts @@ -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'] }); + }); });