Overview

Generate and propagate request IDs for logging correlation.

Features

  • Auto-generate request IDs
  • Propagate from headers
  • Logging correlation
  • Custom generators

Installation

npx hanma add elysia-request-id --framework elysia

Code

import { Elysia } from "elysia";

export interface RequestIdOptions {
  /** Header name to read/write request ID (default: X-Request-ID) */
  headerName?: string;
  /** Custom ID generator (default: crypto.randomUUID) */
  generator?: () => string;
  /** Whether to set response header (default: true) */
  setResponseHeader?: boolean;
}

/**
 * Request ID middleware for Elysia
 * Generates or propagates request IDs for tracing and logging correlation
 */
export const requestId = (options: RequestIdOptions = {}) => {
  const config = {
    headerName: options.headerName ?? "X-Request-ID",
    generator: options.generator ?? (() => crypto.randomUUID()),
    setResponseHeader: options.setResponseHeader ?? true,
  };

  return new Elysia({ name: "request-id" })
    .derive({ as: "global" }, ({ request }) => {
      // Use existing request ID from header or generate new one
      const existingId = request.headers.get(config.headerName);
      const requestId = existingId || config.generator();

      return { requestId };
    })
    .onAfterHandle(({ requestId, set }) => {
      if (config.setResponseHeader) {
        set.headers[config.headerName] = requestId;
      }
    });
};

/**
 * Helper to create a logger that includes request ID
 */
export const createRequestLogger = (requestId: string) => ({
  info: (message: string, data?: Record<string, unknown>) => {
    console.log(JSON.stringify({ level: "info", requestId, message, ...data }));
  },
  warn: (message: string, data?: Record<string, unknown>) => {
    console.warn(JSON.stringify({ level: "warn", requestId, message, ...data }));
  },
  error: (message: string, data?: Record<string, unknown>) => {
    console.error(JSON.stringify({ level: "error", requestId, message, ...data }));
  },
  debug: (message: string, data?: Record<string, unknown>) => {
    if (process.env.NODE_ENV !== "production") {
      console.debug(JSON.stringify({ level: "debug", requestId, message, ...data }));
    }
  },
});

/* USAGE:

import { Elysia } from "elysia";
import { requestId, createRequestLogger } from "./middleware/request-id";

const app = new Elysia()
  .use(requestId())
  .get("/", ({ requestId }) => {
    const log = createRequestLogger(requestId);
    log.info("Processing request");
    
    return { message: "Hello", requestId };
  })
  .listen(3000);

// With custom options
const app = new Elysia()
  .use(requestId({
    headerName: "X-Correlation-ID",
    generator: () => `req_${Date.now()}_${Math.random().toString(36).slice(2, 9)}`,
  }))
  .listen(3000);

*/

Usage

import { requestId } from './middleware/request-id';

app.use(requestId());

// Access in routes
app.get('/', ({ requestId }) => ({ requestId }));

Dependencies

No external dependencies required.

Frequently Asked Questions

How do I install Request ID?
Run the following command: npx hanma add elysia-request-id --framework elysia
How do I use this snippet?
Copy the code and paste it into your project. The snippet will be saved to src/middleware/request-id.ts.
Can I customize this snippet?
Yes! All snippets are copy-paste code that you own. Feel free to modify them to fit your needs.
Does this work with other frameworks?
This snippet is designed for the specific framework. Check the other framework pages for equivalent implementations.