UNPKG

@opra/common

Version:
160 lines (159 loc) 5.91 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpController = void 0; const tslib_1 = require("tslib"); const node_path_1 = tslib_1.__importDefault(require("node:path")); 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 http_controller_decorator_js_1 = require("../decorators/http-controller.decorator.js"); const inspect_util_js_1 = require("../utils/inspect.util.js"); /** * HttpController */ exports.HttpController = function (...args) { // ClassDecorator if (!this) return exports.HttpController[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.HttpController.Kind; _this.types = _this.node[constants_js_1.kDataTypeMap] = new data_type_map_js_1.DataTypeMap(); _this.operations = new index_js_1.ResponsiveMap(); _this.controllers = new index_js_1.ResponsiveMap(); _this.parameters = []; _this.name = initArgs.name; _this.description = initArgs.description; _this.path = initArgs.path ?? initArgs.name; _this.instance = initArgs.instance; _this.ctor = initArgs.ctor; _this._controllerReverseMap = new WeakMap(); _this._initialize?.(initArgs); }; /** * * @class HttpController */ class HttpControllerClass extends document_element_js_1.DocumentElement { /** * @property isRoot */ get isRoot() { return !(this.owner instanceof exports.HttpController); } findController(arg0) { if (typeof arg0 === 'function') { /** Check for cached mapping */ let controller = this._controllerReverseMap.get(arg0); if (controller != null) return controller; /** Lookup for ctor in all controllers */ for (const c of this.controllers.values()) { if (c.ctor === arg0) { this._controllerReverseMap.set(arg0, c); return c; } if (c.controllers.size) { controller = c.findController(arg0); if (controller) { this._controllerReverseMap.set(arg0, controller); return controller; } } } this._controllerReverseMap.set(arg0, null); return; } if (arg0.startsWith('/')) arg0 = arg0.substring(1); if (arg0.includes('/')) { const a = arg0.split('/'); let r = this; while (r && a.length > 0) { r = r.controllers.get(a.shift()); } return r; } return this.controllers.get(arg0); } findParameter(paramName, location) { const paramNameLower = paramName.toLowerCase(); let prm; for (prm of this.parameters) { if (location && location !== prm.location) continue; 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.HttpController) { return this.node.parent.element.findParameter(paramName, location); } } getFullUrl() { return node_path_1.default.posix.join(this.owner instanceof exports.HttpController ? this.owner.getFullUrl() : '/', this.path); } /** * */ toString() { return `[HttpController ${this.name}]`; } /** * */ toJSON(options) { const out = (0, objects_1.omitUndefined)({ kind: this.kind, description: this.description, path: this.path, }); if (this.operations.size) { out.operations = {}; for (const v of this.operations.values()) { out.operations[v.name] = v.toJSON(options); } } if (this.controllers.size) { out.controllers = {}; for (const v of this.controllers.values()) { out.controllers[v.name] = v.toJSON(options); } } if (this.types.size) { out.types = {}; for (const v of this.types.values()) { out.types[v.name] = v.toJSON(options); } } if (this.parameters.length) { out.parameters = []; for (const prm of this.parameters) { out.parameters.push(prm.toJSON(options)); } } return out; } /** * */ [inspect_util_js_1.nodeInspectCustom]() { return `[${inspect_util_js_1.colorFgYellow}HttpController${inspect_util_js_1.colorFgMagenta + this.name + inspect_util_js_1.colorReset}]`; } } exports.HttpController.prototype = HttpControllerClass.prototype; Object.assign(exports.HttpController, http_controller_decorator_js_1.HttpControllerDecoratorFactory); exports.HttpController[constants_js_1.DECORATOR] = http_controller_decorator_js_1.HttpControllerDecoratorFactory;