feat: @inbound and @abort handler argument decorators
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
2 files changed
+30
-3
No files matched your search
+11
-1
@@ -5,7 +5,7 @@ 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';
|
||||
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<string, ArgSpec[]> = 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<string, unknown>;
|
||||
|
||||
+19
-2
@@ -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<unknown>, @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<unknown>, @abort() _a: AbortSignal) {} }
|
||||
expect(getHandlerNeeds(getArgs(Ctl, 'handle'))).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user