Skip to content

Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors

995 B
Raw Blame
import type { SupportedBatchEndpoint } from "@/shared/constants/batchEndpoints";
import { getRuntimePorts } from "@/lib/runtime/ports";
import { normalizeBasePath } from "@/shared/utils/basePath";

async function dispatchBatchApiRequest({
  endpoint,
  body,
  apiKey,
}: {
  endpoint: SupportedBatchEndpoint;
  body: Record<string, unknown>;
  apiKey?: string | null;
}): Promise<Response> {
  const headers = new Headers({ "Content-Type": "application/json" });
  if (apiKey) {
    headers.set("Authorization", `Bearer ${apiKey}`);
  }

  const { dashboardPort } = getRuntimePorts();
  const basePath = normalizeBasePath(process.env.OMNIROUTE_BASE_PATH);
  const url = `http://127.0.0.1:${dashboardPort}${basePath}${endpoint}`;

  return await globalThis.fetch(url, {
    method: "POST",
    headers,
    body: JSON.stringify(body),
    // Never follow a redirect while carrying the stored batch API key.
    redirect: "error",
  });
}

export const dispatch = {
  dispatchBatchApiRequest,
};