Skip to content

Parameter binding

Parameter decorators bind arguments by index, not by their declared TypeScript type. Explicit names survive minification and are the reliable choice for path/query/header fields.

Binding matrix

parameter(type: ParameterType, name = '') returns a legacy method-parameter decorator. ParameterMetadata stores type, optional name, and index; PARAMETER_METADATA_KEY is the Symbol used on the target/property.

Factory / ParameterTypeScalar argumentObject argumentNullish behavior
path(name = '') / PATHBind named path fieldMerge enumerable entries, ignoring provided nameSkip null/undefined
query(name = '') / QUERYBind named query fieldMerge entriesSkip null/undefined
header(name = '') / HEADERSet named headerMerge entries case-insensitivelySkip entire null/undefined argument; an undefined object value deletes a header
body() / BODYWhole request bodyWhole request bodyAssigned as supplied before request merging
request() / REQUESTExpect ParameterRequestMerge into resolved request lastFalsy argument becomes empty request
attribute(name = '') / ATTRIBUTENamed Map entry, skip undefinedMerge record/Map entries; null adds nothingNull is ignored by record merge

Arguments are processed left-to-right; later bindings win. Body and request bindings are single selected values, so the last such parameter wins rather than merging multiple request arguments. Unannotated ordinary arguments are ignored.

ParameterRequest<BODY> extends FetchRequestInit<BODY> and PathCapable. Its path changes the endpoint path; use urlParams.path for replacement values. It can override method, headers, body, timeout, signal, and native Fetch fields, following mergeRequest, including its nullish fallback rules. An empty-string parameter path does not override a nonempty endpoint path.

The replacement method receives only the actual argument list; default parameter expressions in the original placeholder method do not execute. Pass defaults explicitly or configure urlParams metadata.

Names and reflection

getParameterNames(func): string[] parses Function.toString(), caches by function in a WeakMap, and returns an empty array on parsing failure; non-functions throw TypeError before parsing. It uses simple comma splitting and annotation/default stripping, so complex syntax or minification is not a stable naming contract.

getParameterName(target, propertyKey, index, providedName?) returns a truthy explicit name first, then an inferred name, otherwise undefined. Bound scalars without a resolved name fall back to param${index}. Missing path bindings can issue a diagnostic warning; the underlying URL resolver may subsequently throw for missing values, so do not rely on a warning as successful execution.

Cancellation and inherited metadata

An AbortSignal or AbortController argument is recognized before decorator metadata, even without a decorator. If multiple are supplied the last of each kind wins; a request parameter can override them. A signal bypasses the Fetcher timeout as described in cancellation.

Parameter metadata uses copy-on-write when inherited, so decorating an override does not mutate the parent's Map. Class binding walks inherited string-named methods. Symbol-named methods and static methods are not part of that binding traversal. Keep explicit parameter names on inherited/overridden endpoints too.

Complete example

ts
import {
  api,
  post,
  path,
  query,
  body,
  request,
  autoGeneratedError,
  type ParameterRequest,
} from '@ahoo-wang/fetcher-decorator';

type User = { id: string; name: string };
@api('/users')
class Users {
  @post('/{id}')
  update(
    @path('id') id: string,
    @query('notify') notify: boolean,
    @body() value: { name: string },
    @request() options?: ParameterRequest,
    signal?: AbortSignal,
  ): Promise<User> {
    throw autoGeneratedError(id, notify, value, options, signal);
  }
}
const users = new Users();
async function updateUser() {
  const controller = new AbortController();
  return users.update(
    '1',
    true,
    { name: 'Ada' },
    { headers: { 'X-Trace-Id': 'demo' } },
    controller.signal,
  );
}
void updateUser;

Public symbols and source

SymbolImplementation
ParameterTypeparameterDecorator.ts:19
ParameterMetadataparameterDecorator.ts:136
PARAMETER_METADATA_KEYparameterDecorator.ts:161
parameterparameterDecorator.ts:199
pathparameterDecorator.ts:265
queryparameterDecorator.ts:297
headerparameterDecorator.ts:329
bodyparameterDecorator.ts:347
ParameterRequestparameterDecorator.ts:359
requestparameterDecorator.ts:379
attributeparameterDecorator.ts:415
getParameterNamesreflection.ts:46
getParameterNamereflection.ts:92

Package index

Released under the Apache License 2.0.