Custom behaviors
A behavior implements IPipelineBehavior: one method, handle(context, next). It runs
code before next(), after it, or answers without calling it.
A behavior is a plain class. Give the pipeline an instance when its constructor needs arguments; a behavior placed without an instance is constructed with none.
Check required constructor arguments in the constructor and throw a TypeError, so a
missing dependency fails when the pipeline is created, not at the first call.
Options per operation
Section titled “Options per operation”A call site passes options as a [Behavior, options] tuple; the behavior reads the merged
options with context.getBehaviorOptions(Behavior). Export a helper that builds the tuple,
as every package does:
Options given both globally and at a call site are merged shallowly, the call site’s keys winning; see Execution order.
Sharing values with other behaviors
Section titled “Sharing values with other behaviors”context.items carries values from one behavior to the next. Export a typed token for
each value another behavior may read:
Replacing the input
Section titled “Replacing the input”A behavior that produces a new input, such as a parsed copy, hands it to the function with
replaceRequest(context, value) instead of changing the caller’s object. Later behaviors
then see the replacement as context.request.
A contract
Section titled “A contract”A behavior class can declare a contract under the well-known symbol
PIPELINE_BEHAVIOR_CONTRACT: ordering rules against other behaviors, and a validate
function that checks the effective options of each operation. The pipeline runs the
contract once per operation, before its first execution; see
How an operation is wrapped.
An ordering rule names other behaviors by class or by identity string and applies only
when they are present. To require a peer, check context.effectiveBehaviorTypes in
validate.
A stable identity
Section titled “A stable identity”A behavior is identified by its class. When two copies of a package can be loaded, give
the class a stable string under PIPELINE_BEHAVIOR_ID, so both copies are recognized as
one behavior:
Logging
Section titled “Logging”Write through a PipelineLogger taken as an optional constructor argument, defaulting to
console. A NestJS LoggerService and most structured loggers satisfy the interface.