feat(params): schema-aware arg decorators; async validating resolveArgs
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
3 files changed
+83
-18
No files matched your search
+23
-9
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user