@opra/common
Version:
Opra common package
89 lines (88 loc) • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RpcOperation = void 0;
const objects_1 = require("@jsopen/objects");
const ts_gems_1 = require("ts-gems");
const index_js_1 = require("../../schema/index.js");
const data_type_map_js_1 = require("../common/data-type-map.js");
const document_element_js_1 = require("../common/document-element.js");
const constants_js_1 = require("../constants.js");
const data_type_js_1 = require("../data-type/data-type.js");
const rpc_operation_decorator_js_1 = require("../decorators/rpc-operation.decorator.js");
/**
* RpcOperation
*/
exports.RpcOperation = function (...args) {
// Decorator
if (!this) {
const [payloadType, options] = args;
const decoratorChain = [];
return exports.RpcOperation[constants_js_1.DECORATOR].call(undefined, decoratorChain, payloadType, options);
}
// Constructor
const [resource, initArgs] = args;
document_element_js_1.DocumentElement.call(this, resource);
if (!constants_js_1.CLASS_NAME_PATTERN.test(initArgs.name))
throw new TypeError(`Invalid operation name (${initArgs.name})`);
const _this = (0, ts_gems_1.asMutable)(this);
_this.headers = [];
_this.types = _this.node[constants_js_1.kDataTypeMap] = new data_type_map_js_1.DataTypeMap();
_this.name = initArgs.name;
_this.description = initArgs.description;
_this.channel = initArgs.channel;
if (initArgs?.payloadType) {
_this.payloadType =
initArgs?.payloadType instanceof data_type_js_1.DataType
? initArgs.payloadType
: _this.owner.node.getDataType(initArgs.payloadType);
}
if (initArgs?.keyType) {
_this.keyType =
initArgs?.keyType instanceof data_type_js_1.DataType
? initArgs.keyType
: _this.owner.node.getDataType(initArgs.keyType);
}
};
/**
* @class RpcOperation
*/
class RpcOperationClass extends document_element_js_1.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 = (0, objects_1.omitUndefined)({
kind: index_js_1.OpraSchema.RpcOperation.Kind,
description: this.description,
channel: this.channel,
payloadType: this.payloadType.name
? this.payloadType.name
: this.payloadType.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;
}
}
exports.RpcOperation.prototype = RpcOperationClass.prototype;
exports.RpcOperation[constants_js_1.DECORATOR] = rpc_operation_decorator_js_1.RpcOperationDecoratorFactory;