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[];
|
||||
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
@@ -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
@@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user