interface ILifecycleApi
Since 0.1.0

Lifecycle hook registration surface. Hooks run in registration order within each phase.

Methods

onRegister(fn: () => void | Promise<void>): void

Runs during the owning plugin's registration.

onInit(fn: () => void | Promise<void>): void

Runs after all plugins have registered.

onBootstrap(fn: () => void | Promise<void>): void

Runs immediately before the server starts listening.

onRequest(fn: (ctx: IRequestContext) => void | Promise<void>): void

Runs at the start of every request.

onResponse(fn: (ctx: IRequestContext) => void | Promise<void>): void

Runs after every response is produced.

onError(fn: (
error: Error,
) => void | Promise<void>
): void

Runs when an error escapes middleware or a handler.

Since 0.2.0
onStopping(fn: () => void | Promise<void>): void

Runs at the very start of stop(), before the application begins refusing new requests.

This is the only hook that runs while the application is still serving normally, which is what makes it the right home for "tell the outside world to stop sending me traffic" work — deregistering from a service registry, for instance. Deregistering in onShutdown instead means the socket is already closed by the time the registry hears about it, so callers keep being routed to a dead port for up to one health-check interval on every rolling deploy.

Hooks run LIFO and are awaited, so a slow hook delays the whole shutdown and a rejecting one surfaces from stop() — but only after the shutdown has completed, because a hook failure must not be able to prevent the application stopping. With no hooks registered this window has zero width and stop() behaves exactly as it did before.

onShutdown(fn: () => void | Promise<void>): void

Runs when shutdown begins — close connections, flush buffers here.

By this point the application is already refusing new requests and the server socket is closed; use onStopping for work that must happen while traffic is still being served.

onClose(fn: () => void | Promise<void>): void

Runs after shutdown completes.

Usage

import { type ILifecycleApi } from "common/src/index.ts";