type alias MiddlewareFunction
Since 0.1.0

A middleware function: pre-process, call next(), post-process. May short-circuit by returning a response without calling next().

Examples

Example 1

const timing: MiddlewareFunction = async (ctx, next) => {
  const runtime = ctx.services.get<IRuntimeServices>(CAPABILITIES.RUNTIME);
  await next();
  // ctx.startTime is a monotonic runtime.hrtime() reading; subtract it from
  // another monotonic reading — never from a wall-clock epoch.
  ctx.response.header('X-Duration', String(runtime.hrtime() - ctx.startTime));
};

Definition

() => void | HandlerResult | Promise<void | HandlerResult>

Usage

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