@opra/common
Version:
Opra common package
99 lines (98 loc) • 3.53 kB
JavaScript
import { omitUndefined } from '@jsopen/objects';
import { asMutable } from 'ts-gems';
import { vg } from 'valgen';
import { OpraSchema } from '../../schema/index.js';
import { DataTypeMap } from '../common/data-type-map.js';
import { DocumentElement } from '../common/document-element.js';
import { CLASS_NAME_PATTERN, DECORATOR, kDataTypeMap } from '../constants.js';
import { DataType } from '../data-type/data-type.js';
import { MQOperationDecoratorFactory, } from '../decorators/mq-operation.decorator.js';
/**
* MQOperation
*/
export const MQOperation = function (...args) {
// Decorator
if (!this) {
const [type, options] = args;
const decoratorChain = [];
return MQOperation[DECORATOR].call(undefined, decoratorChain, type, options);
}
// Constructor
const [resource, initArgs] = args;
DocumentElement.call(this, resource);
if (!CLASS_NAME_PATTERN.test(initArgs.name))
throw new TypeError(`Invalid operation name (${initArgs.name})`);
const _this = asMutable(this);
_this.headers = [];
_this.types = _this.node[kDataTypeMap] = new DataTypeMap();
_this.name = initArgs.name;
_this.description = initArgs.description;
_this.channel = initArgs.channel;
if (initArgs?.type) {
_this.type =
initArgs?.type instanceof DataType
? initArgs.type
: _this.owner.node.getDataType(initArgs.type);
}
if (initArgs?.keyType) {
_this.keyType =
initArgs?.keyType instanceof DataType
? initArgs.keyType
: _this.owner.node.getDataType(initArgs.keyType);
}
_this.designType = initArgs.designType;
_this.keyDesignType = initArgs.keyDesignType;
};
/**
* @class MQOperation
*/
class MQOperationClass extends DocumentElement {
findHeader(paramName) {
const paramNameLower = paramName.toLowerCase();
let prm;
for (prm of this.headers) {
if (typeof prm.name === 'string') {
prm._nameLower = prm._nameLower || prm.name.toLowerCase();
if (prm._nameLower === paramNameLower)
return prm;
}
if (prm.name instanceof RegExp && prm.name.test(paramName))
return prm;
}
}
toJSON() {
const out = omitUndefined({
kind: OpraSchema.MQOperation.Kind,
description: this.description,
channel: this.channel,
type: this.type.name ? this.type.name : this.type.toJSON(),
keyType: this.keyType
? this.keyType.name
? this.keyType.name
: this.keyType.toJSON()
: undefined,
response: this.response?.toJSON(),
});
if (this.headers.length) {
out.headers = [];
for (const prm of this.headers) {
out.headers.push(prm.toJSON());
}
}
return out;
}
generateCodec(codec, options, properties) {
return (this.type?.generateCodec(codec, options, {
...properties,
designType: this.designType,
}) || vg.isAny());
}
generateKeyCodec(codec, options, properties) {
return (this.keyType?.generateCodec(codec, options, {
...properties,
designType: this.keyDesignType,
}) || vg.isAny());
}
}
MQOperation.prototype = MQOperationClass.prototype;
MQOperation[DECORATOR] = MQOperationDecoratorFactory;