We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
形似 koa 的中间件无法使用 inject 进行注入,使用上与 service 无法对齐;故而新增中间件类型:
@Middleware({ enable: true })
@MV({ middlewares: [] })
需要考虑一下是否在 @artus/pipeline 中添加对类中间件支持?
@artus/pipeline
对于全局中间件,参考 nestjs 的语法,支持 Middleware 注解,参数为是否启用,通过 pipeline.run(ctx) 运行
pipeline.run(ctx)
import { ArtusInjectEnum, Inject } from '@artus/core'; import { ArtusxContext, ArtusxNext, Middleware } from '@artusx/core'; @Middleware({ enable: true, }) export default class CheckAuthMiddleware { @Inject(ArtusInjectEnum.Config) config: Record<string, string | number>; async use(ctx: ArtusxContext, next: ArtusxNext): Promise<void> { const { data } = ctx.context.output; data.authed = false; console.log('middleware - checkAuth', ctx.context, this.config); await next(); } }
函数中间件,在 http method 中使用,接收参数为函数中间件数组,通过 koa.use(middlewares) 方式运行
koa.use(middlewares)
middleware.ts
import { ArtusxContext, ArtusxNext } from '@artusx/core'; export default async function traceTime(_ctx: ArtusxContext, next: ArtusxNext): Promise<void> { console.time('trace'); await next(); console.timeEnd('trace'); }
controller.ts
import { ArtusInjectEnum, Inject } from '@artus/core'; import { GET, POST, Controller, MW } from '@artusx/core'; import type { ArtusxContext } from '@artusx/core'; import traceTime from '../middleware/traceTime'; @Controller() export default class HomeController { @Inject(ArtusInjectEnum.Config) config: Record<string, string | number>; @MW([traceTime]) @GET('/') async home(ctx: ArtusxContext) { ctx.body = 'home'; } }
The text was updated successfully, but these errors were encountered:
配置中增加 config.artusx.middleware 可选参数,对已配置的中间件根据数组顺序排序,并优先于未配置中间件执行。
config.artusx.middleware
import traceTime from '../middleware/traceTime'; import checkAuth from '../middleware/checkAuth'; export default { artusx: { middlewares: [TraceTime, checkAuth], } };
Sorry, something went wrong.
No branches or pull requests
背景
形似 koa 的中间件无法使用 inject 进行注入,使用上与 service 无法对齐;故而新增中间件类型:
@Middleware({ enable: true })
注解@MV({ middlewares: [] })
注解需要考虑一下是否在
@artus/pipeline
中添加对类中间件支持?实现
示例
类中间件
对于全局中间件,参考 nestjs 的语法,支持 Middleware 注解,参数为是否启用,通过
pipeline.run(ctx)
运行函数中间件
函数中间件,在 http method 中使用,接收参数为函数中间件数组,通过
koa.use(middlewares)
方式运行middleware.ts
controller.ts
The text was updated successfully, but these errors were encountered: