UNPKG

@mvx/mvc

Version:

@mvx/mvc is mvc framework on server, base on koa, @tsdi frameworks

122 lines (120 loc) 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CompositeMiddleware = exports.MvcMiddleware = void 0; const tslib_1 = require("tslib"); const ioc_1 = require("@tsdi/ioc"); const boot_1 = require("@tsdi/boot"); /** * middleware * * @export * @abstract * @class Middleware * @extends {Handle<MvcContext>} * @implements {IMiddleware} */ class MvcMiddleware extends boot_1.Handle { /** * get injector of current message queue. */ getInjector() { return this._injector(); } static ρAnn() { return { "name": "MvcMiddleware" }; } } tslib_1.__decorate([ ioc_1.Inject(ioc_1.InjectorProxyToken), tslib_1.__metadata("design:type", Function) ], MvcMiddleware.prototype, "_injector", void 0); exports.MvcMiddleware = MvcMiddleware; /** * composite middleware. * * @export * @class CompositeMiddleware * @extends {Handles<IContext>} */ let CompositeMiddleware = class CompositeMiddleware extends boot_1.Handles { /** * get injector of current message queue. */ getInjector() { return this._injector(); } find(filter) { return this.handles.find(item => filter(item)); } /** * use handle before * * @param {HandleType} handle * @param {HandleType} [before] * @returns {this} * @memberof LifeScope */ useBefore(handle, before) { if (ioc_1.isString(before)) { let beforehdl = this.find((h) => h.middleName === before); super.useBefore(handle, beforehdl); } else { super.useBefore(handle, before); } return this; } /** * use handle after. * * @param {HandleType} handle * @param {HandleType} [after] * @returns {this} * @memberof LifeScope */ useAfter(handle, after) { if (ioc_1.isString(after)) { let afterhdl = this.find((h) => h.middleName === after); super.useAfter(handle, afterhdl); } else { super.useAfter(handle, after); } return this; } registerHandle(HandleType) { if (ioc_1.isClass(HandleType)) { this.getInjector().registerType(HandleType); } return this; } toHandle(handleType) { var _a, _b, _c, _d; if (handleType instanceof ioc_1.Action) { return handleType.toAction(); } else if (ioc_1.isClass(handleType)) { const ist = (_a = this.getInjector().get(handleType)) !== null && _a !== void 0 ? _a : this.getInjector().getContainer().getTypeReflects().getInjector(handleType).getInstance(handleType); return (_b = ist === null || ist === void 0 ? void 0 : ist.toAction) === null || _b === void 0 ? void 0 : _b.call(ist); } else if (ioc_1.isToken(handleType)) { return (_d = (_c = this.getInjector().get(handleType)) === null || _c === void 0 ? void 0 : _c.toAction) === null || _d === void 0 ? void 0 : _d.call(_c); } else if (ioc_1.isFunction(handleType)) { return handleType; } return null; } static ρAnn() { return { "name": "CompositeMiddleware", "params": { "find": ["filter"], "useBefore": ["handle", "before"], "useAfter": ["handle", "after"], "registerHandle": ["HandleType"], "toHandle": ["handleType"] } }; } }; tslib_1.__decorate([ ioc_1.Inject(ioc_1.InjectorProxyToken), tslib_1.__metadata("design:type", Function) ], CompositeMiddleware.prototype, "_injector", void 0); CompositeMiddleware = tslib_1.__decorate([ ioc_1.Injectable() ], CompositeMiddleware); exports.CompositeMiddleware = CompositeMiddleware; //# sourceMappingURL=../sourcemaps/middlewares/MvcMiddleware.js.map