@digitalcloud.no/smart-fetch-client
v0.1.1
Published
Fetch-based HTTP client with retries, timeouts, and typed responses.
Downloads
192
Maintainers
Readme
@digitalcloud.no/smart-fetch-client
Fetch-based HTTP client with retries, timeouts, and typed responses.
Installation
npm install @digitalcloud.no/smart-fetch-clientFeatures
- ✅ Built on native
fetchAPI - ✅ Automatic retries with exponential backoff
- ✅ Request timeouts
- ✅ TypeScript support with typed responses
- ✅ Query parameter handling
- ✅ Base URL configuration
- ✅ Custom headers and interceptors
Usage
import { createClient } from '@digitalcloud.no/smart-fetch-client';
const client = createClient({
baseUrl: 'https://api.example.com',
timeout: 5000,
retry: { attempts: 3, delay: 1000 },
headers: { 'Authorization': 'Bearer token' }
});
// GET request with typed response
const user = await client.get<{ id: number; name: string }>('/users/1');
console.log(user.id, user.name);
// POST request
const newUser = await client.post('/users', {
body: { name: 'John Doe', email: '[email protected]' }
});
// With query parameters
const users = await client.get('/users', {
query: { page: 1, limit: 10 }
});API
createClient(options)
Creates a new HTTP client instance.
Options:
baseUrl: Base URL for all requeststimeout: Request timeout in millisecondsretry: Retry configuration{ attempts, delay }headers: Default headers for all requestsfetch: Custom fetch implementation
Methods:
get<T>(path, options?): GET requestpost<T>(path, options?): POST requestput<T>(path, options?): PUT requestpatch<T>(path, options?): PATCH requestdelete<T>(path, options?): DELETE request
Contributing
Contributions are welcome! This package is part of the npm-packages monorepo.
License
MIT © digitalcloud.no
