Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Helicone Helicone BuildRouter

From Leeroopedia
Knowledge Sources
Domains Proxy Architecture, Request Routing, API Gateway
Last Updated 2026-02-14 00:00 GMT

Overview

Concrete router factory for dispatching intercepted LLM requests to the correct provider handler, provided by the routerFactory module in the Helicone Cloudflare Worker.

Description

buildRouter is a synchronous factory function that constructs a fully configured itty-router instance (or OpenAPIRouter for the Helicone API worker). It consults a WORKER_MAP dispatch table that maps each WORKER_TYPE string to a provider-specific router builder function. Before provider-specific routes are registered, the function adds shared base routes: a /healthcheck GET endpoint and a /v1/feedback POST endpoint. When includeCors is true, a wildcard CORS handler is prepended to allow browser-based clients.

The supported worker types are: OPENAI_PROXY, ANTHROPIC_PROXY, HELICONE_API, GATEWAY_API, GENERATE_API, AI_GATEWAY_API, and CUSTOMER_GATEWAY. The HELICONE_API type uses @cloudflare/itty-router-openapi for OpenAPI schema generation, while all other types use the standard itty-router.

Usage

Call buildRouter once during worker initialization, passing the WORKER_TYPE from the environment and a boolean indicating whether CORS headers should be injected. The returned router is then used to handle all incoming requests for the lifetime of the worker.

Code Reference

Source Location

  • Repository: Helicone
  • File: worker/src/routers/routerFactory.ts (lines 124-155)

Signature

export function buildRouter(
  provider: Env["WORKER_TYPE"],
  includeCors: boolean
): BaseRouter | BaseOpenAPIRouter

Import

import { buildRouter } from "./routers/routerFactory";

I/O Contract

Inputs

Name Type Required Description
provider Env["WORKER_TYPE"] Yes The worker type string from the deployment environment. Valid values: "OPENAI_PROXY", "ANTHROPIC_PROXY", "HELICONE_API", "GATEWAY_API", "GENERATE_API", "AI_GATEWAY_API", "CUSTOMER_GATEWAY".
includeCors boolean Yes When true, a wildcard CORS middleware is prepended to the router that responds to all methods and origins with permissive Access-Control-Allow-* headers.

Outputs

Name Type Description
router BaseOpenAPIRouter A fully composed router instance with base routes (healthcheck, feedback) and provider-specific routes registered. BaseRouter is RouterType<Route, [RequestWrapper, Env, ExecutionContext]>. BaseOpenAPIRouter is the OpenAPI variant used only for HELICONE_API.

Usage Examples

Basic Usage

import { buildRouter } from "./routers/routerFactory";
import { RequestWrapper } from "./lib/RequestWrapper";

export default {
  async fetch(request: Request, env: Env, ctx: ExecutionContext) {
    const router = buildRouter(env.WORKER_TYPE, request.method === "OPTIONS");

    const { data: requestWrapper, error } = await RequestWrapper.create(
      request,
      env
    );
    if (error !== null) {
      return new Response(error, { status: 401 });
    }

    return router.handle(request, requestWrapper, env, ctx);
  },
};

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment