API reference
Public exports and commonly used options from the package source. For the complete TypeScript surface, see the linked declarations.
Client creation
createClient returns an isolated client configured with the supplied defaults. The default export is a pre-created client and also exposes .create() and .createClient().
import client, { createClient } from 'react-native-fetch-client';
const api = createClient({
baseURL: 'https://api.example.com',
timeout: 10_000,
});
const response = await api.get<{ id: number }>('/users/1');
console.log(response.status, response.data.id);
const anotherApi = client.create({ baseURL: 'https://other.example.com' });Common client options
All listed configuration properties are optional. The defaults below are documented in the package’s public type declarations.
| Option | Type | Default | Description |
|---|---|---|---|
baseURL | string | No default | Prefix for relative request URLs. `baseUrl` is also accepted as an alias. |
timeout | number | 0 (disabled) | Request timeout in milliseconds. |
retry.attempts | number | 0 | Maximum retries after the initial request. |
retry.backoff | 'exponential' | 'linear' | 'exponential' | Delay strategy between retries. |
dedup.enabled | boolean | false | Enable sharing of matching concurrent requests. |
cache.enabled | boolean | false | Enable the response cache. |
cache.defaultTtl | number | 60,000 ms | Default cache lifetime. `ttl` is also accepted as an alias. |
cache.strategy | 'cache-first' | 'stale-while-revalidate' | 'cache-first' | Default strategy for cacheable requests. |
circuitBreaker.enabled | boolean | false | Enable the circuit breaker. |
maxConcurrentRequests | number | Unlimited | Maximum number of concurrent requests when set to a positive value. |
fetchImpl | typeof globalThis.fetch | globalThis.fetch | Fetch implementation used by this client. |
When you provide auth, its getToken callback is required. See the authentication guide.
Request methods
| Method | Purpose | Returns |
|---|---|---|
get(url, config?) | Send a GET request. | Promise<HttpResponse<T>> |
post(url, body?, config?) | Send a POST request. | Promise<HttpResponse<T>> |
put / patch(url, body?, config?) | Send a PUT or PATCH request. | Promise<HttpResponse<T>> |
delete(url, config?) | Send a DELETE request. | Promise<HttpResponse<T>> |
request(config) | Send a request with an explicit URL and method. | Promise<HttpResponse<T>> |
safe.* | Use method helpers that return a [data, error] tuple. | Promise<SafeResult<T>> |
stream(url, options?) | Read SSE or chunked response events. | AsyncGenerator<StreamEvent> |
upload(url, file, options?) | Upload supported file or form data. | Promise<HttpResponse<T>> |
paginate(url, options?) | Iterate over items across pages. Use .pages() for response pages and .all() to collect items. | AsyncGenerator or Promise<T[]> |
poll(url, options) | Repeat a request until the required until predicate succeeds. | Promise<HttpResponse<T>> |
Request and response types
Request options are passed as the last argument to method helpers, or alongside url and method to request(). Common fields include headers, params, pathParams, signal, responseType, schema, retry, cache, and unwrap.
By default, methods return an HttpResponse<T> envelope with the parsed body in data, plus status and response metadata. At runtime, unwrap: true returns the body directly; the current TypeScript declarations still describe the method result as HttpResponse<T>.
Errors and exports
The package exports FetchNativeError and specialized errors for HTTP responses, network failures, timeouts, cancellation, parsing, configuration, retry exhaustion, validation, and circuit-breaker rejection. Type guards include isHttpError, isNetworkError, isTimeoutError, and isCancelError.