Middleware

The Middleware class registers inbound or outbound middleware into the global router. Inbound middleware runs before route handlers; outbound middleware runs after. Both variants receive the request context and can return a Res to short-circuit the request, or void to continue.

Contents
  1. Usage
  2. Constructor Parameters
  3. Properties

Usage

As with many modules, the Middleware class can be instantiated directly with new or extended using the abstract class. The middlewares are automatically registered and inserted into the lifecycle inside the constructor.

With new

import { C } from "@ozanarslan/corpus";

const server = new C.Server();

new C.Middleware({
	variant: "inbound",
	useOn: "*",
	handler: async (c) => {
		if (!c.headers.get("authorization")) {
			return new C.Res("Unauthorized", { status: 401 });
		}
	},
});

void server.listen(3000);

Extending

import { C } from "@ozanarslan/corpus";
import { SomeService } from "./SomeService";

class SomeMiddleware extends C.MiddlewareAbstract {
	constructor(
		private readonly someService: SomeService,
		override readonly useOn: C.MiddlewareUseOn,
	) {
		super();
		// this method needs to be called to register it to the router
		// here or where you instantiate
		this.register();
	}

	override handler: C.MiddlewareHandler = (c) => {
		return this.someService.fn(c.headers);
	};
}

const server = new C.Server();
const someRoute = new C.Route("/", () => "ok");
const someService = new SomeService();
new SomeMiddleware(someService, { useOn: [someRoute] });
void server.listen(3000);

Constructor Parameters

variant

Either "inbound" or "outbound". Defaults to "inbound". Inbound middlewares run before the handlers and outbound middlewares run after. Mental model if coming from NestJS:

CorpusNestJS
Inbound Middleware returning voidMiddleware (calling next())
Outbound Middleware returning voidInterceptor (post-handler, returning observable)
Inbound Middleware returning ResGuard returning false or throwing
Outbound Middleware returning ResException Filter catching and transforming
Inbound Middleware throwingGuard or Middleware throwing
Outbound Middleware throwingInterceptor or Exception Filter throwing

useOn

The route(s) and controller(s) this middleware applies to. Pass "*" to apply globally to all routes.

export type MiddlewareUseOn =
	| Array<RouteInterface | Controller>
	| RouteInterface
	| Controller
	| "*";

handler

The middleware function. Receives the request context and can return a Res or throw and error to halt further processing, or void to pass through.

Properties

All constructor options are stored as readonly properties: variant, useOn, and handler.