UNPKG

tedi

Version:

Express wrappper written in typescript with dependency injection capabilities

41 lines (40 loc) 1.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_1 = require("lodash"); const core_1 = require("../core"); const utils_1 = require("../core/utils"); const METADATA_KEY = "tedi:web"; class WebDecoratorError extends core_1.TediError { constructor(target, methodName, err) { super(`${utils_1.getClassName(target)}#${methodName}`, err); } } function getMetadataKey(httpMethodName) { return `${METADATA_KEY}:${httpMethodName}`; } function setMetadata(httpMethodName, metadata, target) { Reflect.defineMetadata(getMetadataKey(httpMethodName), metadata, target); } function getMetadata(httpMethodName, target) { return Reflect.getMetadata(getMetadataKey(httpMethodName), target); } function WebDecoratorBuilder(httpMethodName) { return function () { return function (target, propertyKey, descriptor) { if (getMetadata(httpMethodName, target)) { throw new WebDecoratorError(target, propertyKey, `duplicate decoration found for @${httpMethodName}`); } setMetadata(httpMethodName, { httpMethodName: httpMethodName, methodName: propertyKey, }, target); }; }; } let webDecorator = { $getMetadata: (httpMethodName, target) => getMetadata(httpMethodName, target), }; lodash_1.keys(utils_1.HTTP_METHODS_NAMES).forEach(httpMethodName => { webDecorator[httpMethodName] = WebDecoratorBuilder(httpMethodName); }); exports.web = webDecorator;