aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared/customFetch.ts
blob: 18b4e1339fe9c690353884632220da2ffb9b5243 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import serverConfig from "./config";

// Generic fetch function type that works across environments
type FetchFunction = (
  input: RequestInfo | URL | string,
  init?: RequestInit,
) => Promise<Response>;

// 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<typeof fetchImpl>[0],
    init?: Parameters<typeof fetchImpl>[1],
  ): ReturnType<typeof fetchImpl> {
    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();