UNPKG

@tsed/common

Version:
84 lines 3.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HandlerMetadata = void 0; const HandlerType_1 = require("../interfaces/HandlerType"); const ParamMetadata_1 = require("../models/ParamMetadata"); const ParamTypes_1 = require("./ParamTypes"); class HandlerMetadata { constructor(options) { this.injectable = false; this.type = HandlerType_1.HandlerType.RAW_FN; this.hasNextFunction = false; const { target, token, propertyKey, type, routeOptions } = options; this.type = type || target.type || HandlerType_1.HandlerType.RAW_FN; this.routeOptions = routeOptions || {}; this.handler = propertyKey ? target.prototype[propertyKey] : target; if (propertyKey) { this.target = target; this.token = token; this.propertyKey = propertyKey; this.hasNextFunction = this.hasParamType(ParamTypes_1.ParamTypes.NEXT_FN); if (this.hasParamType(ParamTypes_1.ParamTypes.ERR)) { this.type = HandlerType_1.HandlerType.ERR_MIDDLEWARE; } this.injectable = ParamMetadata_1.ParamMetadata.getParams(target, propertyKey).length > 0; } if (!this.injectable) { if (this.handler.length === 4) { this.type = HandlerType_1.HandlerType.RAW_ERR_FN; } this.hasNextFunction = this.handler.length >= 3; } } get hasErrorParam() { return this.type === HandlerType_1.HandlerType.ERR_MIDDLEWARE || this.type === HandlerType_1.HandlerType.RAW_ERR_FN; } get parameters() { if (this.injectable) { return this.getParams(); } // Emulate ParamMetadata const parameters = []; if (this.hasErrorParam) { parameters.push(new ParamMetadata_1.ParamMetadata({ target: this.target, propertyKey: this.propertyKey, index: 0, paramType: ParamTypes_1.ParamTypes.ERR })); } parameters.push(new ParamMetadata_1.ParamMetadata({ target: this.target, propertyKey: this.propertyKey, index: parameters.length, paramType: ParamTypes_1.ParamTypes.REQUEST })); parameters.push(new ParamMetadata_1.ParamMetadata({ target: this.target, propertyKey: this.propertyKey, index: parameters.length, paramType: ParamTypes_1.ParamTypes.RESPONSE })); if (this.hasNextFunction) { parameters.push(new ParamMetadata_1.ParamMetadata({ target: this.target, propertyKey: this.propertyKey, index: parameters.length, paramType: ParamTypes_1.ParamTypes.NEXT_FN })); } return parameters; } getParams() { return ParamMetadata_1.ParamMetadata.getParams(this.target, this.propertyKey) || []; } hasParamType(paramType) { return this.getParams().findIndex((p) => p.paramType === paramType) > -1; } isFinal() { var _a; return ((_a = this.routeOptions) === null || _a === void 0 ? void 0 : _a.isFinal) || false; } } exports.HandlerMetadata = HandlerMetadata; //# sourceMappingURL=HandlerMetadata.js.map