diff --git a/src/params.ts b/src/params.ts index 41c5e96..02f8a20 100644 --- a/src/params.ts +++ b/src/params.ts @@ -5,7 +5,7 @@ export type Query = Record; export type Headers = Record; export type Context = Record; -type ArgSource = 'ctx' | 'needs' | 'params' | 'query' | 'body' | 'headers'; +type ArgSource = 'ctx' | 'needs' | 'params' | 'query' | 'body' | 'headers' | 'inbound' | 'abort'; export interface ArgSpec { index: number; source: ArgSource; key?: string; keys?: string[] } const ARGS = Symbol('gt:args'); @@ -45,6 +45,14 @@ export function ctx(): ParameterDecorator { return (target, methodKey, index) => record(target, methodKey, { index, source: 'ctx' }); } +export function inbound(): ParameterDecorator { + return (target, methodKey, index) => record(target, methodKey, { index, source: 'inbound' }); +} + +export function abort(): ParameterDecorator { + return (target, methodKey, index) => record(target, methodKey, { index, source: 'abort' }); +} + 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); @@ -65,6 +73,8 @@ export function resolveArgs(specs: ArgSpec[], context: Context): unknown[] { function resolveOne(s: ArgSpec, context: Context): unknown { if (s.source === 'ctx') return context; if (s.source === 'needs') return context[s.key as string]; + if (s.source === 'inbound') return context.inbound; + if (s.source === 'abort') return context.abort; const bag = context[s.source]; if (typeof bag !== 'object' || bag === null) return bag; // can't pick from a non-object; return as-is const record = bag as Record; diff --git a/test/params.test.ts b/test/params.test.ts index 558949d..833ffb9 100644 --- a/test/params.test.ts +++ b/test/params.test.ts @@ -1,6 +1,6 @@ import 'reflect-metadata'; -import { expect, test } from 'vitest'; -import { needs, ctx, param, query, getArgs, getHandlerNeeds, resolveArgs } from '../src/params'; +import { expect, test, describe, it } from 'vitest'; +import { needs, ctx, param, query, inbound, abort, getArgs, getHandlerNeeds, resolveArgs } from '../src/params'; class Ctl { handler( @@ -45,3 +45,20 @@ test('a handler with no arg decorators resolves to no args', () => { class Bare { ping() { return 1; } } expect(resolveArgs(getArgs(Bare, 'ping'), {})).toEqual([]); }); + +describe('@inbound / @abort', () => { + it('resolves inbound and abort from reserved context keys, not as bags', () => { + class Ctl { handle(@inbound() inc: AsyncIterable, @abort() sig: AbortSignal) { return [inc, sig]; } } + const specs = getArgs(Ctl, 'handle'); + const ac = new AbortController(); + const inc = (async function* () {})(); + const args = resolveArgs(specs, { inbound: inc, abort: ac.signal }); + expect(args[0]).toBe(inc); + expect(args[1]).toBe(ac.signal); + }); + + it('inbound/abort do not count as graph needs', () => { + class Ctl { handle(@inbound() _i: AsyncIterable, @abort() _a: AbortSignal) {} } + expect(getHandlerNeeds(getArgs(Ctl, 'handle'))).toEqual([]); + }); +});