feat(params): schema-aware arg decorators; async validating resolveArgs

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-06 21:33:13 -05:00
1 parent 101833d52d
commit eb494ff254
3 files changed
+83 -18

No files matched your search

+2 -2
View File
@@ -114,7 +114,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
steps: [],
handlerName: route.handlerName,
needs: getHandlerNeeds(argSpecs),
run: (c: any) => inst[route.handlerName](...resolveArgs(argSpecs, c)),
run: async (c: any) => inst[route.handlerName](...(await resolveArgs(argSpecs, c))),
transformer: getTransformer(C, route.handlerName) ?? JsonTransformer,
duplicates: route.duplicates,
});
@@ -358,7 +358,7 @@ export function createApp(opts: { modules: Ctor[]; plugins?: Plugin[]; mesh?: Me
headers: req.headers, inbound, abort,
protocol: (req.socket as any).encrypted ? 'https' : 'http', ip: req.socket.remoteAddress ?? '' };
for (const s of planSteps(plan)) { ctx = { ...ctx, ...(await s.run(ctx)) }; }
const out = plan.run(ctx);
const out = await plan.run(ctx);
if (!isAsyncIterable(out)) throw new Error(`@Ws handler '${plan.handlerName}' must return an AsyncIterable`);
return out as AsyncIterable<unknown>;
},
+23 -9
View File
@@ -1,12 +1,14 @@
import 'reflect-metadata';
import type { Ctor } from './metadata';
import { isStandardSchema, type StandardSchemaV1 } from './standard-schema';
import { ValidationError } from './signals';
export type Query = Record<string, string>;
export type Headers = Record<string, string | string[] | undefined>;
export type Context = Record<string, unknown>;
type ArgSource = 'ctx' | 'needs' | 'params' | 'query' | 'body' | 'headers' | 'inbound' | 'abort';
export interface ArgSpec { index: number; source: ArgSource; key?: string; keys?: string[] }
export interface ArgSpec { index: number; source: ArgSource; key?: string; keys?: string[]; schema?: StandardSchemaV1 }
const ARGS = Symbol('gt:args');
@@ -21,13 +23,17 @@ function record(target: object, methodKey: string | symbol | undefined, spec: Ar
}
function envelope(source: ArgSource) {
return (arg?: string | string[]): ParameterDecorator =>
return (arg?: string | string[] | StandardSchemaV1, schema?: StandardSchemaV1): ParameterDecorator =>
(target, methodKey, index) => {
const spec: ArgSpec = Array.isArray(arg)
? { index, source, keys: arg }
: typeof arg === 'string'
? { index, source, key: arg }
: { index, source };
const spec: ArgSpec = { index, source };
if (Array.isArray(arg)) spec.keys = arg;
else if (typeof arg === 'string') spec.key = arg;
else if (isStandardSchema(arg)) spec.schema = arg;
else if (arg !== undefined) throw new Error(`@${source}: expected a Standard Schema or a string/array key`);
if (schema !== undefined) {
if (!isStandardSchema(schema)) throw new Error(`@${source}: second argument must be a Standard Schema`);
spec.schema = schema;
}
record(target, methodKey, spec);
};
}
@@ -62,11 +68,19 @@ export function getHandlerNeeds(specs: ArgSpec[]): string[] {
return specs.filter((s) => s.source === 'needs' && s.key).map((s) => s.key as string);
}
export function resolveArgs(specs: ArgSpec[], context: Context): unknown[] {
export async function resolveArgs(specs: ArgSpec[], context: Context): Promise<unknown[]> {
if (specs.length === 0) return [];
const max = Math.max(...specs.map((s) => s.index));
const args: unknown[] = new Array(max + 1).fill(undefined);
for (const s of specs) args[s.index] = resolveOne(s, context);
for (const s of specs) {
let value = resolveOne(s, context);
if (s.schema) {
const result = await s.schema['~standard'].validate(value);
if (result.issues) throw new ValidationError([...result.issues], s.source);
value = result.value;
}
args[s.index] = value;
}
return args;
}