Router/Action.js

"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var object_hash_1 = __importDefault(require("object-hash"));
var ExpressTS_1 = __importDefault(require("../app/ExpressTS"));
/**
 * This decorator is used to define an action.
 * Actions are **automatically** injected.
 *
 * All the actions must be in ``src/actions`` and to have the ``@Action`` decorator.
 *
 * The middlewares can be [Class Middlewares (Injected)]{@link module:Injector~Middleware}
 * as strings or function middlewares.
 * @static
 * @return {any}
 * @method Action
 * @memberof module:Router
 * @param {!string} path The path of the router
 * @param {?Array.<string | Function>} middlewares Middlewares
 * @example <caption>Example usage of [\@Action]{@link Action}.</caption>
 * [\@Action]{@link Action}('/path', ['ClassMiddleware', functionMiddleware])
 * export default class ActionName {
 *
 * }
 */
exports.default = (function (path, middlewares) { return function (constructor) {
    var actionHash = (0, object_hash_1.default)(constructor);
    var action = ExpressTS_1.default.getData(actionHash, 'actions');
    if (!action) {
        ExpressTS_1.default.setData(actionHash, {
            target: constructor,
            instance: new constructor(),
            path: path,
            functions: [],
            middlewares: middlewares
        }, 'actions');
    }
    else {
        ExpressTS_1.default.setData(actionHash, __assign(__assign({}, action), { target: constructor, instance: new constructor(), path: path, middlewares: middlewares }), 'actions');
    }
    var _injectedAction = function () { };
    ExpressTS_1.default.inject(_injectedAction, constructor, 'action', actionHash);
    return _injectedAction;
}; });
//# sourceMappingURL=Action.js.map