// Thin Prodigi v4 API client. Reads PRODIGI_API_KEY and PRODIGI_API_BASE_URL // from the environment. Server-only — never import from a client component. const DEFAULT_BASE = 'https://api.prodigi.com/v4.0/'; function baseUrl(): string { const raw = process.env.PRODIGI_API_BASE_URL || DEFAULT_BASE; return raw.endsWith('/') ? raw : `${raw}/`; } function apiKey(): string { // When pointed at the sandbox, prefer the sandbox key so the live key is // never clobbered just to run a test — flip PRODIGI_API_BASE_URL alone. const isSandbox = baseUrl().includes('sandbox'); const key = isSandbox ? process.env.PRODIGI_API_KEY_SANDBOX || process.env.PRODIGI_API_KEY : process.env.PRODIGI_API_KEY; if (!key) throw new Error('PRODIGI_API_KEY is not set'); return key; } export async function prodigiFetch( path: string, init: RequestInit = {}, ): Promise { const url = new URL(path.replace(/^\//, ''), baseUrl()); const headers = new Headers(init.headers); headers.set('X-API-Key', apiKey()); if (init.body && !headers.has('Content-Type')) { headers.set('Content-Type', 'application/json'); } return fetch(url, { ...init, headers }); }