Skip to content

DetectorFunction

reference
1 min readUpdated

Kind: Type

Source: src/middleware/language/language.ts

Part of: Middleware

Type for detector functions

DetectorFunction defines a function that detects the language associated with an incoming request or execution context. Language middleware calls this function to select the language value used by downstream handlers.

Definition

ts
(c: Context, options: DetectorOptions) => string | undefined

Diagram

mermaid
graph LR
  Request[Incoming request] --> Detector[DetectorFunction]
  Detector --> Language[Detected language]
  Language --> Middleware[Language middleware]
  Middleware --> Handler[Downstream handler]

Usage

ts
import type { DetectorFunction } from "./middleware/language/language";

const detectLanguage: DetectorFunction = (request) => {
  const header = request.headers.get("accept-language");

  if (!header) {
    return "en";
  }

  return header.split(",")[0].trim();
};

// Pass detectLanguage to the language middleware configuration.

AI Coding Instructions

  • Keep detector functions focused on reading request or context data and returning a language value.
  • Return a fallback language when the expected request data is missing.
  • Parse headers defensively because language headers can contain multiple values and quality markers.
  • Keep custom detector logic compatible with the parameter and return types defined by DetectorFunction.

Was this page helpful?

Download as PDF
DetectorFunction — Hono (narrator proof)