Kind: Class
Source: packages/microservices/factories/rpc-params-factory.ts
Part of: Microservices
RpcParamsFactory resolves RPC handler parameter values from the arguments supplied by a microservice transport. Its exchangeKeyForValue() method maps an RPC parameter type—such as payload or context—to the appropriate value, including selecting a named property from the payload.
Methods
| Method | Signature | Returns |
|---|---|---|
exchangeKeyForValue | `exchangeKeyForValue(type: number, data: string | undefined, args: unknown[])` |
Where it refuses work
RpcParamsFactorystops the work with an early return when!args.
Diagram
mermaidgraph LR A[Transport invokes RPC handler] --> B[Handler arguments array] B --> C[RpcParamsFactory.exchangeKeyForValue] C --> D{RPC parameter type} D -->|PAYLOAD| E[Full payload or payload property] D -->|CONTEXT| F[Transport context] D -->|Unknown type| G[null]
Usage
tsimport { RpcParamsFactory } from '@nestjs/microservices/factories/rpc-params-factory';
import { RpcParamtype } from '@nestjs/microservices/enums/rpc-paramtype.enum';
const paramsFactory = new RpcParamsFactory();
const payload = { id: 'user-123', action: 'created' };
const context = { pattern: 'users.create' };
const args = [payload, context];
// Resolve the complete message payload.
const message = paramsFactory.exchangeKeyForValue(
RpcParamtype.PAYLOAD,
undefined,
args,
);
// Resolve a property from the message payload.
const userId = paramsFactory.exchangeKeyForValue(
RpcParamtype.PAYLOAD,
'id',
args,
);
// Resolve the RPC transport context.
const rpcContext = paramsFactory.exchangeKeyForValue(
RpcParamtype.CONTEXT,
undefined,
args,
);
AI Coding Instructions
- Pass handler arguments in their expected order: payload at index
0and RPC context at index1. - Use
RpcParamtype.PAYLOADwith a property key to extract a specific payload field; omit the key to return the full payload. - Use
RpcParamtype.CONTEXTonly when the transport provides context as the second handler argument. - Preserve the
nullfallback for unsupported parameter types or missing argument arrays so parameter resolution fails safely.
How it works
RpcParamsFactory is a class with one method, exchangeKeyForValue(type, data, args), that selects an RPC handler parameter value from an invocation-argument array. Its parameters are a numeric type, optional string data, and unknown[] arguments. [packages/microservices/factories/rpc-params-factory.ts:3-8]
- For
RpcParamtype.PAYLOAD, it returnsargs[0]whendatais falsy; whendatais truthy, it returns the property named bydatafromargs[0]. Optional chaining means a missing or nullish first argument yieldsundefinedfor property extraction. [packages/microservices/factories/rpc-params-factory.ts:12-15] - For
RpcParamtype.CONTEXT, it returnsargs[1];datais ignored. [packages/microservices/factories/rpc-params-factory.ts:12-17] - For
RpcParamtype.GRPC_CALL, it returnsargs[2];datais ignored. [packages/microservices/factories/rpc-params-factory.ts:12-19] - The enum defines these three kinds as
PAYLOAD,CONTEXT, andGRPC_CALL. [packages/microservices/enums/rpc-paramtype.enum.ts:3-7] - If
argsis falsy, or iftypedoes not match one of those enum values, the method returnsnull. [packages/microservices/factories/rpc-params-factory.ts:9-11] [packages/microservices/factories/rpc-params-factory.ts:19-20] - It does not check whether the selected array slot or requested payload property exists. Thus, with a truthy
argsarray, missing slots or properties can result inundefined. [packages/microservices/factories/rpc-params-factory.ts:14-18] - The method only reads from
argsand does not write to it or other state. [packages/microservices/factories/rpc-params-factory.ts:9-21]
RpcContextCreator owns an instance of this class and creates extraction callbacks that pass the callback’s received arguments into exchangeKeyForValue. [packages/microservices/context/rpc-context-creator.ts:41-45] [packages/microservices/context/rpc-context-creator.ts:237-241] The default non-gRPC metadata maps the payload parameter to argument slot 0; the gRPC defaults additionally map context and gRPC call parameters to slots 1 and 2. [packages/microservices/context/rpc-metadata-constants.ts:3-10]
Was this page helpful?