feat: handler argument decorators (needs/ctx/param/query/body/headers)
This commit is contained in:
2 files changed
+123
No files matched your search
@@ -0,0 +1,76 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import type { Ctor } from './metadata';
|
||||||
|
|
||||||
|
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';
|
||||||
|
export interface ArgSpec { index: number; source: ArgSource; key?: string; keys?: string[] }
|
||||||
|
|
||||||
|
const ARGS = Symbol('gt:args');
|
||||||
|
|
||||||
|
function record(target: object, methodKey: string | symbol | undefined, spec: ArgSpec): void {
|
||||||
|
if (methodKey === undefined) return; // constructor params not supported
|
||||||
|
const ctor = target.constructor as Ctor;
|
||||||
|
const map: Record<string, ArgSpec[]> = Reflect.getMetadata(ARGS, ctor) ?? {};
|
||||||
|
const list = map[String(methodKey)] ?? [];
|
||||||
|
list.push(spec);
|
||||||
|
map[String(methodKey)] = list;
|
||||||
|
Reflect.defineMetadata(ARGS, map, ctor);
|
||||||
|
}
|
||||||
|
|
||||||
|
function envelope(source: ArgSource) {
|
||||||
|
return (arg?: string | string[]): ParameterDecorator =>
|
||||||
|
(target, methodKey, index) => {
|
||||||
|
const spec: ArgSpec = Array.isArray(arg)
|
||||||
|
? { index, source, keys: arg }
|
||||||
|
: typeof arg === 'string'
|
||||||
|
? { index, source, key: arg }
|
||||||
|
: { index, source };
|
||||||
|
record(target, methodKey, spec);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export const param = envelope('params');
|
||||||
|
export const query = envelope('query');
|
||||||
|
export const body = envelope('body');
|
||||||
|
export const headers = envelope('headers');
|
||||||
|
|
||||||
|
export function needs(key: string): ParameterDecorator {
|
||||||
|
return (target, methodKey, index) => record(target, methodKey, { index, source: 'needs', key });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ctx(): ParameterDecorator {
|
||||||
|
return (target, methodKey, index) => record(target, methodKey, { index, source: 'ctx' });
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getArgs(cls: Ctor, methodName: string): ArgSpec[] {
|
||||||
|
const map: Record<string, ArgSpec[]> = Reflect.getMetadata(ARGS, cls) ?? {};
|
||||||
|
return [...(map[methodName] ?? [])].sort((a, b) => a.index - b.index);
|
||||||
|
}
|
||||||
|
|
||||||
|
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[] {
|
||||||
|
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);
|
||||||
|
return args;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveOne(s: ArgSpec, context: Context): unknown {
|
||||||
|
if (s.source === 'ctx') return context;
|
||||||
|
if (s.source === 'needs') return context[s.key as string];
|
||||||
|
const bag = (context[s.source] ?? {}) as Record<string, unknown>;
|
||||||
|
if (s.keys) {
|
||||||
|
const out: Record<string, unknown> = {};
|
||||||
|
for (const k of s.keys) out[k] = bag[k];
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
if (s.key !== undefined) return bag[s.key];
|
||||||
|
return bag;
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import 'reflect-metadata';
|
||||||
|
import { expect, test } from 'vitest';
|
||||||
|
import { needs, ctx, param, query, getArgs, getHandlerNeeds, resolveArgs } from '../src/params';
|
||||||
|
|
||||||
|
class Ctl {
|
||||||
|
handler(
|
||||||
|
@needs('user') _user: unknown,
|
||||||
|
@param('id') _id: unknown,
|
||||||
|
@query() _q: unknown,
|
||||||
|
@query(['a', 'b']) _sub: unknown,
|
||||||
|
@ctx() _c: unknown,
|
||||||
|
) { return null; }
|
||||||
|
}
|
||||||
|
|
||||||
|
test('getArgs returns specs sorted by parameter index', () => {
|
||||||
|
const specs = getArgs(Ctl, 'handler');
|
||||||
|
expect(specs.map((s) => [s.index, s.source, s.key ?? s.keys])).toEqual([
|
||||||
|
[0, 'needs', 'user'],
|
||||||
|
[1, 'params', 'id'],
|
||||||
|
[2, 'query', undefined],
|
||||||
|
[3, 'query', ['a', 'b']],
|
||||||
|
[4, 'ctx', undefined],
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getHandlerNeeds extracts only @needs keys', () => {
|
||||||
|
expect(getHandlerNeeds(getArgs(Ctl, 'handler'))).toEqual(['user']);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('resolveArgs builds positional args from context', () => {
|
||||||
|
const context = {
|
||||||
|
user: { id: 'u1' },
|
||||||
|
params: { id: '7' },
|
||||||
|
query: { a: '1', b: '2', c: '3' },
|
||||||
|
};
|
||||||
|
const args = resolveArgs(getArgs(Ctl, 'handler'), context);
|
||||||
|
expect(args[0]).toEqual({ id: 'u1' });
|
||||||
|
expect(args[1]).toBe('7');
|
||||||
|
expect(args[2]).toEqual({ a: '1', b: '2', c: '3' });
|
||||||
|
expect(args[3]).toEqual({ a: '1', b: '2' });
|
||||||
|
expect(args[4]).toBe(context);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('a handler with no arg decorators resolves to no args', () => {
|
||||||
|
class Bare { ping() { return 1; } }
|
||||||
|
expect(resolveArgs(getArgs(Bare, 'ping'), {})).toEqual([]);
|
||||||
|
});
|
||||||
Reference in new issue
Block a user