Skip to content

covenix / createParamDecorator

Function: createParamDecorator()

createParamDecorator<T, D>(resolve): (data?) => ParameterDecorator

Defined in: src/parameters.ts:39

Builds a custom parameter decorator from a resolver. The resolver runs at request time with the ParamContext ({ req, res }) plus any data passed where the decorator is applied, and may be sync or async — its resolved value is injected as the handler argument. This is the extension point for injecting values the built-in decorators don't cover (a cookie, req.ip, a tenant resolved from a header, an awaited per-request value). A resolver that throws is routed through the normal error pipeline (so throw createError.X() picks the status).

Type safety note: TypeScript's legacy parameter decorators can't constrain the annotated parameter type, so (as with @Principal()) the handler's parameter type is developer-asserted — keep it in sync with the resolver's return type.

Type Parameters

T

T

D

D = undefined

Parameters

resolve

(ctx, data) => T | Promise<T>

Computes the value from { req, res } and the decorator data.

Returns

A decorator factory; call it (optionally with data) on a parameter.

(data?) => ParameterDecorator

Example

ts
const ClientIp = createParamDecorator(({ req }) => req.ip);
const Cookie = createParamDecorator(({ req }, name: string) => req.cookies?.[name]);

@Get()
handler(@ClientIp() ip: string | undefined, @Cookie('sid') sid: string | undefined) {}