MQOperation
Package: @opra/common
MQOperation is the runtime class that represents a single message-queue operation within an MQController. An operation maps a channel pattern to a payload type and optional key type, and carries its own set of headers and a response descriptor.
Inheritance
DocumentElement
└── MQOperation
Properties
| Property | Type | Description |
|---|---|---|
name | string | Operation name as registered on the controller. |
description | string | undefined | Human-readable description. |
channel | string | RegExp | (string | RegExp)[] | Channel or topic pattern this operation handles. A single string, a regular expression, or an array of either. |
type | DataType | The data type of the incoming message payload. |
keyType | DataType | undefined | Optional data type for the message key (e.g. a Kafka partition key). |
types | DataTypeMap | Local data types scoped to this operation. |
headers | MQHeader[] | Headers declared directly on this operation. |
response | MQOperationResponse | Descriptor for the outgoing response message. |
Methods
findHeader(paramName)
Searches for a header by name on this operation. Returns undefined if not found.
const header = operation.findHeader('x-correlation-id');
findHeader(paramName: string): MQHeader | undefined
generateCodec(codec, options?)
Compiles a validation/transformation function for the operation's payload type. Use this to validate incoming or outgoing message bodies.
const decoder = operation.generateCodec('decode');
const result = decoder(rawPayload);
const encoder = operation.generateCodec('encode');
generateCodec(
codec: 'encode' | 'decode',
options?: DataType.GenerateCodecOptions,
): Validator
generateKeyCodec(codec, options?)
Same as generateCodec but targets the message key type (keyType). Throws or returns an isAny validator if keyType is not defined.
const keyDecoder = operation.generateKeyCodec('decode');
generateKeyCodec(
codec: 'encode' | 'decode',
options?: DataType.GenerateCodecOptions,
): Validator
toJSON(options?)
Returns a plain OpraSchema.MQOperation object for schema export.
toJSON(options?: ApiDocument.ExportOptions): OpraSchema.MQOperation
Obtaining an MQOperation instance
import { MQApi } from '@opra/common';
const document = await ApiDocumentFactory.createDocument({ ... });
if (document.api instanceof MQApi) {
const op = document.api.findOperation('CustomerQueue', 'processOrder');
if (op) {
console.log(op.name); // 'processOrder'
console.log(op.channel); // 'orders.*' or /^orders\..+$/
const decoder = op.generateCodec('decode');
const validated = decoder(rawMessage);
}
}