Middleware

Middleware interface for TypeScript and Python

Middleware Interface

Middleware has one method, use(). Call next() to run the rest of the pipeline and get its result.

interface MiddlewareInterface {
  use(
    context: ExecutionContext,
    next: () => Promise<any>
  ): Promise<any>;
}
async def use(self, context: ExecutionContext, next_fn)
from nitrostack import use_middleware

@use_middleware(AuditMiddleware)
async def handler(...):
    ...

Call await next_fn() to continue the pipeline.

Example

A middleware that runs code before and after the handler:

@Middleware()
export class MyMiddleware implements MiddlewareInterface {
  async use(context: ExecutionContext, next: () => Promise<any>): Promise<any> {
    // Before
    const result = await next();
    // After
    return result;
  }
}

On this page