Skip to main content

Middleware

A Middleware is a special Component that can interrupt the request processing and return a response.

Definition

  • modifying response headers:
import { HttpContext, Middleware } from "@tymber/core";

export class MyMiddleware extends Middleware {
async handle(ctx: HttpContext) {
ctx.responseHeaders.set("some-header", "1");
}
}
  • redirecting:
import { HttpContext, Middleware } from "@tymber/core";

export class MyMiddleware extends Middleware {
async handle(ctx: HttpContext) {
return ctx.redirect("/login");
}
}
  • aborting the request:
import { HttpContext, Middleware } from "@tymber/core";

export class MyMiddleware extends Middleware {
async handle(ctx: HttpContext) {
return new Response(null, { status: 403 });
}
}

Registration

import { type Module, type AppInit } from "@tymber/core";
import { MyMiddleware } from "./middlewares/MyMiddleware";

export const MyModule: Module = {
name: "my-module",
version: "1.2.3",

init(app: AppInit) {
app.middleware(MyMiddleware);
},
};