Skip to content

Function: doFetch()

@providerprotocol/ai


@providerprotocol/ai / http / doFetch

doFetch(url, init, config, provider, modality): Promise<Response>

Defined in: src/http/fetch.ts:182

Executes an HTTP fetch request with automatic retry, timeout handling, and error normalization.

This function wraps the standard fetch API with additional capabilities:

  • Configurable timeout with automatic request cancellation (per attempt)
  • Retry strategy support (exponential backoff, linear, token bucket, etc.)
  • Pre-request delay support for rate limiting strategies
  • Automatic Retry-After header parsing and handling
  • Error normalization to UPPError format

string

The URL to fetch

RequestInit

Standard fetch RequestInit options (method, headers, body, etc.)

ProviderConfig

Provider configuration containing fetch customization, timeout, and retry strategy

string

Provider identifier for error context (e.g., ‘openai’, ‘anthropic’)

Modality

Request modality for error context (e.g., ‘llm’, ‘embedding’, ‘image’)

Promise<Response>

The successful Response object

RATE_LIMITED - When rate limited and retries exhausted

NETWORK_ERROR - When a network failure occurs

TIMEOUT - When the request times out

CANCELLED - When the request is aborted via signal

Various codes based on HTTP status (see statusToErrorCode)

const response = await doFetch(
'https://api.openai.com/v1/chat/completions',
{
method: 'POST',
headers: { 'Authorization': 'Bearer sk-...' },
body: JSON.stringify({ model: 'gpt-4', messages: [] })
},
{ timeout: 30000, retryStrategy: exponentialBackoff() },
'openai',
'llm'
);