Skip to main content

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

PropertyTypeDescription
namestringOperation name as registered on the controller.
descriptionstring | undefinedHuman-readable description.
channelstring | RegExp | (string | RegExp)[]Channel or topic pattern this operation handles. A single string, a regular expression, or an array of either.
typeDataTypeThe data type of the incoming message payload.
keyTypeDataType | undefinedOptional data type for the message key (e.g. a Kafka partition key).
typesDataTypeMapLocal data types scoped to this operation.
headersMQHeader[]Headers declared directly on this operation.
responseMQOperationResponseDescriptor 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);
}
}

MQController · MQHeader · MQOperationResponse