@mvx/mvc
Version:
@mvx/mvc is mvc framework on server, base on koa, @tsdi frameworks
132 lines (130 loc) • 4.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompositeMiddleware = exports.MvcMiddleware = void 0;
var tslib_1 = require("tslib");
var ioc_1 = require("@tsdi/ioc");
var boot_1 = require("@tsdi/boot");
/**
* middleware
*
* @export
* @abstract
* @class Middleware
* @extends {Handle<MvcContext>}
* @implements {IMiddleware}
*/
var MvcMiddleware = /** @class */ (function (_super) {
tslib_1.__extends(MvcMiddleware, _super);
function MvcMiddleware() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* get injector of current message queue.
*/
MvcMiddleware.prototype.getInjector = function () {
return this._injector();
};
MvcMiddleware.ρAnn = function () {
return { "name": "MvcMiddleware" };
};
tslib_1.__decorate([
ioc_1.Inject(ioc_1.InjectorProxyToken),
tslib_1.__metadata("design:type", Function)
], MvcMiddleware.prototype, "_injector", void 0);
return MvcMiddleware;
}(boot_1.Handle));
exports.MvcMiddleware = MvcMiddleware;
/**
* composite middleware.
*
* @export
* @class CompositeMiddleware
* @extends {Handles<IContext>}
*/
var CompositeMiddleware = /** @class */ (function (_super) {
tslib_1.__extends(CompositeMiddleware, _super);
function CompositeMiddleware() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* get injector of current message queue.
*/
CompositeMiddleware.prototype.getInjector = function () {
return this._injector();
};
CompositeMiddleware.prototype.find = function (filter) {
return this.handles.find(function (item) { return filter(item); });
};
/**
* use handle before
*
* @param {HandleType} handle
* @param {HandleType} [before]
* @returns {this}
* @memberof LifeScope
*/
CompositeMiddleware.prototype.useBefore = function (handle, before) {
if (ioc_1.isString(before)) {
var beforehdl = this.find(function (h) { return h.middleName === before; });
_super.prototype.useBefore.call(this, handle, beforehdl);
}
else {
_super.prototype.useBefore.call(this, handle, before);
}
return this;
};
/**
* use handle after.
*
* @param {HandleType} handle
* @param {HandleType} [after]
* @returns {this}
* @memberof LifeScope
*/
CompositeMiddleware.prototype.useAfter = function (handle, after) {
if (ioc_1.isString(after)) {
var afterhdl = this.find(function (h) { return h.middleName === after; });
_super.prototype.useAfter.call(this, handle, afterhdl);
}
else {
_super.prototype.useAfter.call(this, handle, after);
}
return this;
};
CompositeMiddleware.prototype.registerHandle = function (HandleType) {
if (ioc_1.isClass(HandleType)) {
this.getInjector().registerType(HandleType);
}
return this;
};
CompositeMiddleware.prototype.toHandle = function (handleType) {
var _a, _b, _c, _d;
if (handleType instanceof ioc_1.Action) {
return handleType.toAction();
}
else if (ioc_1.isClass(handleType)) {
var 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;
};
CompositeMiddleware.ρAnn = function () {
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);
return CompositeMiddleware;
}(boot_1.Handles));
exports.CompositeMiddleware = CompositeMiddleware;
//# sourceMappingURL=../sourcemaps/middlewares/MvcMiddleware.js.map