UNPKG

@halsp/core

Version:

面向云的现代渐进式轻量 Node.js 框架

37 lines 966 B
import { LambdaMiddleware } from "./lambda.middleware.mjs"; import { Middleware, invokeMiddlewares, } from "./middleware.mjs"; export class ComposeMiddleware extends Middleware { enable; constructor(enable) { super(); this.enable = enable; } #mds = []; async invoke() { if (this.enable && !(await this.enable(this.ctx))) { await this.next(); return; } const mds = [ ...this.#mds, new LambdaMiddleware(async () => { await this.next(); }), ]; await invokeMiddlewares(this.ctx, mds); } use(lambda) { this.#mds.push(() => new LambdaMiddleware(lambda)); return this; } add(md, type) { if (type) { this.#mds.push([md, type]); } else { this.#mds.push(md); } return this; } } //# sourceMappingURL=compose.middleware.js.map