@opra/common
Version:
Opra common package
156 lines (155 loc) • 5.41 kB
JavaScript
import nodePath from 'node:path';
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 { HttpControllerDecoratorFactory } from '../decorators/http-controller.decorator.js';
import { colorFgMagenta, colorFgYellow, colorReset, nodeInspectCustom, } from '../utils/inspect.util.js';
/**
* HttpController
*/
export const HttpController = function (...args) {
// ClassDecorator
if (!this)
return HttpController[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.HttpController.Kind;
_this.types = _this.node[kDataTypeMap] = new DataTypeMap();
_this.operations = new ResponsiveMap();
_this.controllers = new 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 DocumentElement {
/**
* @property isRoot
*/
get isRoot() {
return !(this.owner instanceof 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 HttpController) {
return this.node.parent.element.findParameter(paramName, location);
}
}
getFullUrl() {
return nodePath.posix.join(this.owner instanceof HttpController ? this.owner.getFullUrl() : '/', this.path);
}
/**
*
*/
toString() {
return `[HttpController ${this.name}]`;
}
/**
*
*/
toJSON(options) {
const out = 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;
}
/**
*
*/
[nodeInspectCustom]() {
return `[${colorFgYellow}HttpController${colorFgMagenta + this.name + colorReset}]`;
}
}
HttpController.prototype = HttpControllerClass.prototype;
Object.assign(HttpController, HttpControllerDecoratorFactory);
HttpController[DECORATOR] = HttpControllerDecoratorFactory;