import { Env } from "../types.js";

/**
 * Wrapper around env.KBDB.fetch that automatically injects
 * the KBDB_INTERNAL_TOKEN Authorization header.
 */
export function kbdbFetch(env: Env, path: string, init?: RequestInit): Promise<Response> {
  const headers = new Headers((init?.headers as HeadersInit) || {});
  if (env.KBDB_INTERNAL_TOKEN) {
    headers.set("Authorization", `Bearer ${env.KBDB_INTERNAL_TOKEN}`);
  }
  return env.KBDB.fetch(`http://kbdb${path}`, { ...init, headers });
}
