Function: doFetch()
@providerprotocol/ai / http / doFetch
Function: doFetch()
Section titled “Function: 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
Parameters
Section titled “Parameters”string
The URL to fetch
RequestInit
Standard fetch RequestInit options (method, headers, body, etc.)
config
Section titled “config”Provider configuration containing fetch customization, timeout, and retry strategy
provider
Section titled “provider”string
Provider identifier for error context (e.g., ‘openai’, ‘anthropic’)
modality
Section titled “modality”Request modality for error context (e.g., ‘llm’, ‘embedding’, ‘image’)
Returns
Section titled “Returns”Promise<Response>
The successful Response object
Throws
Section titled “Throws”RATE_LIMITED - When rate limited and retries exhausted
Throws
Section titled “Throws”NETWORK_ERROR - When a network failure occurs
Throws
Section titled “Throws”TIMEOUT - When the request times out
Throws
Section titled “Throws”CANCELLED - When the request is aborted via signal
Throws
Section titled “Throws”Various codes based on HTTP status (see statusToErrorCode)
Example
Section titled “Example”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');