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:
4 files changed
+23
-6
No files matched your search
+3
-1
@@ -45,6 +45,7 @@ interface RoutePlan {
|
|||||||
needs: string[];
|
needs: string[];
|
||||||
run: (ctx: any) => unknown;
|
run: (ctx: any) => unknown;
|
||||||
transformer: typeof JsonTransformer;
|
transformer: typeof JsonTransformer;
|
||||||
|
duplicates?: 'array' | 'last';
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface MeshConfig {
|
export interface MeshConfig {
|
||||||
@@ -115,6 +116,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
|
|||||||
needs: getHandlerNeeds(argSpecs),
|
needs: getHandlerNeeds(argSpecs),
|
||||||
run: (c: any) => inst[route.handlerName](...resolveArgs(argSpecs, c)),
|
run: (c: any) => inst[route.handlerName](...resolveArgs(argSpecs, c)),
|
||||||
transformer: getTransformer(C, route.handlerName) ?? JsonTransformer,
|
transformer: getTransformer(C, route.handlerName) ?? JsonTransformer,
|
||||||
|
duplicates: route.duplicates,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -319,7 +321,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
|
|||||||
...routePlans
|
...routePlans
|
||||||
.filter((plan) => plan.transport !== 'ws') // buffer | sse | ndjson | negotiate
|
.filter((plan) => plan.transport !== 'ws') // buffer | sse | ndjson | negotiate
|
||||||
.map((plan): RouteDef => ({
|
.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) => {
|
handler: async (req) => {
|
||||||
const provided = await providedSeed(plan);
|
const provided = await providedSeed(plan);
|
||||||
return runPipeline({
|
return runPipeline({
|
||||||
|
|||||||
+2
-2
@@ -31,7 +31,7 @@ export type RouteHandler = (req: {
|
|||||||
ip: string;
|
ip: string;
|
||||||
}) => Promise<PipelineResult>;
|
}) => 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 MatchedRoute { params: Record<string, string>; def: RouteDef }
|
||||||
|
|
||||||
export interface WsOpenCtx {
|
export interface WsOpenCtx {
|
||||||
@@ -258,7 +258,7 @@ export function createHttpServer(
|
|||||||
}
|
}
|
||||||
let body: unknown;
|
let body: unknown;
|
||||||
const ct = String(req.headers['content-type'] ?? '');
|
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')) {
|
if (buf !== undefined && ct.includes('application/json')) {
|
||||||
try { body = JSON.parse(buf.toString('utf8')); }
|
try { body = JSON.parse(buf.toString('utf8')); }
|
||||||
catch {
|
catch {
|
||||||
|
|||||||
+3
-3
@@ -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 interface StepMeta { provides: string; needs: string[]; optional: boolean; export: boolean }
|
||||||
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE';
|
||||||
export type Transport = 'buffer' | 'sse' | 'ndjson' | 'negotiate' | 'ws';
|
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 {
|
export interface ModuleOptions {
|
||||||
mountpoint: string; providers?: Ctor[]; steps?: Ctor[]; controllers?: Ctor[];
|
mountpoint: string; providers?: Ctor[]; steps?: Ctor[]; controllers?: Ctor[];
|
||||||
}
|
}
|
||||||
@@ -40,11 +40,11 @@ export function Route(prefix: string): ClassDecorator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function routeDecorator(method: HttpMethod, transport: Transport) {
|
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) => {
|
(target, propertyKey) => {
|
||||||
const ctor = target.constructor;
|
const ctor = target.constructor;
|
||||||
const routes = (Reflect.getMetadata(K.routes, ctor) as RouteMeta[]) ?? [];
|
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);
|
Reflect.defineMetadata(K.routes, routes, ctor);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -72,4 +72,19 @@ describe('bodyDuplicates', () => {
|
|||||||
method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: 'x=1&x=2' });
|
method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body: 'x=1&x=2' });
|
||||||
expect(await r.json()).toEqual({ 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'] });
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user