From 751db324d6c27f0631bb6c6ba6de89c3f47ad317 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Fri, 26 Jun 2026 01:40:04 -0500 Subject: [PATCH] feat: handler argument decorators (needs/ctx/param/query/body/headers) --- src/params.ts | 76 +++++++++++++++++++++++++++++++++++++++++++++ test/params.test.ts | 47 ++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 src/params.ts create mode 100644 test/params.test.ts diff --git a/src/params.ts b/src/params.ts new file mode 100644 index 0000000..bdfb9ca --- /dev/null +++ b/src/params.ts @@ -0,0 +1,76 @@ +import 'reflect-metadata'; +import type { Ctor } from './metadata'; + +export type Query = Record; +export type Headers = Record; +export type Context = Record; + +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 = 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 = 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; + if (s.keys) { + const out: Record = {}; + for (const k of s.keys) out[k] = bag[k]; + return out; + } + if (s.key !== undefined) return bag[s.key]; + return bag; +} diff --git a/test/params.test.ts b/test/params.test.ts new file mode 100644 index 0000000..558949d --- /dev/null +++ b/test/params.test.ts @@ -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([]); +});