import { hc, type ClientRequestOptions } from 'hono/client' import type * as App from './App.js' /** Default hosted Tempo API URL. */ export const defaultUrl = 'https://api.tempo.xyz' /** Creates a typed client for the hosted Tempo API. */ export function create(options: create.Options = {}): create.ReturnType { const { apiKey, url = defaultUrl, ...client } = options const mergedHeaders = headers(client.headers, apiKey) if (!mergedHeaders) return hc(url, client) return hc(url, { ...client, headers: mergedHeaders }) } /** Typed Hono client for the Tempo API. */ export type Client = ReturnType> export declare namespace create { /** Typed Hono client returned by {@link create}. */ type ReturnType = Client /** Options for creating a typed Tempo API client. */ type Options = ClientRequestOptions & { /** API key sent on the canonical `tempo-api-key` header. */ apiKey?: string | undefined /** Tempo API URL. Defaults to {@link defaultUrl}. */ url?: string | undefined } } function headers( base: ClientRequestOptions['headers'], apiKey: string | undefined, ): ClientRequestOptions['headers'] { if (!apiKey) return base if (typeof base === 'function') return async () => mergeHeaders(await base(), apiKey) return mergeHeaders(base, apiKey) } function mergeHeaders(base: Record | undefined, apiKey: string) { const headers = new Headers(base) headers.set('tempo-api-key', apiKey) return Object.fromEntries(headers.entries()) }