UNPKG

@opra/common

Version:
99 lines (98 loc) 3.33 kB
import { omitUndefined } from '@jsopen/objects'; import { asMutable } from 'ts-gems'; import { ResponsiveMap } from '../../helpers/index.js'; 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 { MQControllerDecoratorFactory } from '../decorators/mq-controller.decorator.js'; import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom, } from '../utils/inspect.util.js'; /** * MQController */ export const MQController = function (...args) { // ClassDecorator if (!this) return MQController[DECORATOR].apply(undefined, args); // Constructor const [owner, initArgs] = args; DocumentElement.call(this, owner); if (!CLASS_NAME_PATTERN.test(initArgs.name)) throw new TypeError(`Invalid resource name (${initArgs.name})`); const _this = asMutable(this); _this.kind = OpraSchema.MQController.Kind; _this.types = _this.node[kDataTypeMap] = new DataTypeMap(); _this.operations = new 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 MQController */ class MQControllerClass extends 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 MQController) { return this.node.parent.element.findHeader(paramName, location); } } /** * */ toString() { return `[MQController ${this.name}]`; } /** * */ toJSON() { const out = 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; } /** * */ [nodeInspectCustom]() { return `[${colorFgYellow}MQController${colorFgMagenta + this.name + colorReset}]`; } } MQController.prototype = MQControllerClass.prototype; Object.assign(MQController, MQControllerDecoratorFactory); MQController[DECORATOR] = MQControllerDecoratorFactory;