Axeom Logo
Axeom.

Lifecycle Hooks

Mastering the request-response flow with Interceptors.

Axeom replaces traditional middleware with a dedicated Hook System. Hooks are specialized interceptors that allow you to attach logic to specific phases of the request execution lifecycle.


The Request Lifecycle

Understanding the sequence of hooks is key to building complex logic in a weightless framework.

  1. onBeforeMatch: Global firewall. Runs before the router.
  2. onRequest: Match confirmation. Runs once a route is identified.
  3. onBeforeHandle: Handler guard. The last check before your logic runs.
  4. Handler: Your core business logic.
  5. onAfterHandle: Success inspector. Runs after the handler returns a value.
  6. onResponse: Response courier. Modifies the final outgoing Response object.

Global vs Local Hooks

Hooks can be registered globally on the main app instance or locally within a group.

// Global Hook: Runs for EVERY request
.(() => {
  .(`[GLOBAL]: ${..}`);
});

.("/api", () => {
  // Local Hook: Runs only for routes inside /api
  .(() => {
    const  = ..("Authorization");
    if (!) return new ("Forbidden", { : 403 });
  });

  .("/data", () => "Sensitive data");
});

Detailed Hook Breakdown

onBeforeMatch

Use this for early-exit logic like maintenance modes or global redirects. It only has access to the raw Request object.

.(() => {
  if (..("X-Maintenance")) {
    return new ("Service Unavailable", { : 503 });
  }
});

onBeforeHandle & onAfterHandle

These are the most common hooks for authorization and logging. onBeforeHandle can stop the request by returning a Response.

.(() => {
  ( as any).timeStart = .();
});

.(() => {
  const  = .() - ( as any).timeStart;
  .(`Handler took ${}ms`);
});

onResponse

This hook receives both the generated Response and the Context. Use it to inject custom headers or cookies globally.

.((, ) => {
  ..("X-Axeom-Version", "1.0.0");
  return ; // You must return a Response!
});

Performance Tip

Axeom flattens hooks into an optimized array at startup. This means that having 10 hooks is significantly faster than 10 nested middleware functions in traditional frameworks, as there is no recursive "next()" call stack to manage.

On this page