import serverConfig from "./config"; // Generic fetch function type that works across environments type FetchFunction = ( input: RequestInfo | URL | string, init?: RequestInit, ) => Promise; // Factory function to create a custom fetch with timeout for any fetch implementation export function createCustomFetch(fetchImpl: FetchFunction = globalThis.fetch) { return function customFetch( input: Parameters[0], init?: Parameters[1], ): ReturnType { const timeout = serverConfig.inference.fetchTimeoutSec * 1000; // Convert to milliseconds return fetchImpl(input, { signal: AbortSignal.timeout(timeout), ...init, }); }; } // Default export for backward compatibility - uses global fetch export const customFetch = createCustomFetch();