UNPKG

@opra/common

Version:
102 lines (101 loc) 3.75 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RpcController = void 0; const objects_1 = require("@jsopen/objects"); const ts_gems_1 = require("ts-gems"); const index_js_1 = require("../../helpers/index.js"); const index_js_2 = 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 rpc_controller_decorator_js_1 = require("../decorators/rpc-controller.decorator.js"); const inspect_util_js_1 = require("../utils/inspect.util.js"); /** * RpcController */ exports.RpcController = function (...args) { // ClassDecorator if (!this) return exports.RpcController[constants_js_1.DECORATOR].apply(undefined, args); // Constructor const [owner, initArgs] = args; document_element_js_1.DocumentElement.call(this, owner); if (!constants_js_1.CLASS_NAME_PATTERN.test(initArgs.name)) throw new TypeError(`Invalid resource name (${initArgs.name})`); const _this = (0, ts_gems_1.asMutable)(this); _this.kind = index_js_2.OpraSchema.RpcController.Kind; _this.types = _this.node[constants_js_1.kDataTypeMap] = new data_type_map_js_1.DataTypeMap(); _this.operations = new index_js_1.ResponsiveMap(); _this.headers = []; _this.name = initArgs.name; _this.description = initArgs.description; _this.instance = initArgs.instance; _this.ctor = initArgs.ctor; _this._controllerReverseMap = new WeakMap(); _this._initialize?.(initArgs); }; /** * * @class RpcController */ class RpcControllerClass extends document_element_js_1.DocumentElement { findHeader(paramName, location) { 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; } if (this.node.parent && this.node.parent.element instanceof exports.RpcController) { return this.node.parent.element.findHeader(paramName, location); } } /** * */ toString() { return `[RpcController ${this.name}]`; } /** * */ toJSON() { const out = (0, objects_1.omitUndefined)({ kind: this.kind, description: this.description, }); if (this.operations.size) { out.operations = {}; for (const v of this.operations.values()) { out.operations[v.name] = v.toJSON(); } } if (this.types.size) { out.types = {}; for (const v of this.types.values()) { out.types[v.name] = v.toJSON(); } } if (this.headers.length) { out.headers = []; for (const prm of this.headers) { out.headers.push(prm.toJSON()); } } return out; } /** * */ [inspect_util_js_1.nodeInspectCustom]() { return `[${inspect_util_js_1.colorFgYellow}RpcController${inspect_util_js_1.colorFgMagenta + this.name + inspect_util_js_1.colorReset}]`; } } exports.RpcController.prototype = RpcControllerClass.prototype; Object.assign(exports.RpcController, rpc_controller_decorator_js_1.RpcControllerDecoratorFactory); exports.RpcController[constants_js_1.DECORATOR] = rpc_controller_decorator_js_1.RpcControllerDecoratorFactory;