0.0.3 โข Published 5 months ago
fetch-smart v0.0.3
FetchSmart
A powerful, type-safe HTTP client for modern JavaScript applications. Built on top of the native Fetch API with enhanced features for a better developer experience.
โจ Features
- ๐ Smart Retries: Automatic retry with exponential backoff
- โฑ๏ธ Timeout Control: Configurable request timeouts
- ๐ Progress Tracking: Monitor upload and download progress
- ๐ฏ Type Safety: Full TypeScript support with comprehensive types
- ๐ Response Parsing: Automatic content-type handling
- ๐ซ Cancellation: Built-in request cancellation support
- ๐ Rich Response: Detailed response metadata
- ๐ CORS Ready: Built-in CORS handling
- ๐ Security: Configurable request credentials
- ๐ฆ Zero Dependencies: No external runtime dependencies
๐ฅ Installation
npm install fetch-smart
# or
yarn add fetch-smart
# or
pnpm add fetch-smart
๐ Quick Start
import fetchClient from 'fetch-smart';
// Simple GET request
const { data } = await fetchClient.get('https://api.example.com/users');
// POST with JSON
const response = await fetchClient.post('https://api.example.com/users', {
name: 'John Doe',
email: 'john@example.com'
});
// PUT with retry and timeout
const updated = await fetchClient.put(
'https://api.example.com/users/1',
{ name: 'Jane Doe' },
{
retry: 3,
timeout: 5000
}
);
๐ ๏ธ Advanced Usage
Creating an Instance
import { FetchSmart } from 'fetch-smart';
const client = new FetchSmart('https://api.example.com', {
'Authorization': 'Bearer your-token'
});
Progress Monitoring
const response = await client.post(
'/upload',
formData,
{
onUploadProgress: (progress) => {
console.log(`Upload: ${progress.progress}%`);
},
onDownloadProgress: (progress) => {
console.log(`Download: ${progress.progress}%`);
}
}
);
Retry Configuration
const response = await client.get('/data', {
retry: 3,
retryDelay: 1000,
exponentialDelay: true,
retryCondition: () => true
});
Request Cancellation
const controller = new AbortController();
const promise = client.get('/data', {
signal: controller.signal
});
// Cancel request
controller.abort();
๐ API Reference
Request Configuration
Option | Type | Default | Description |
---|---|---|---|
body | unknown | - | Request body data |
params | Record<string, string> | - | URL query parameters |
timeout | number | - | Request timeout (ms) |
headers | HeadersInit | {} | Custom headers |
retry | number | 0 | Retry attempts |
retryDelay | number | 1000 | Delay between retries |
exponentialDelay | boolean | true | Use exponential backoff |
retryCondition | () => boolean | - | Custom retry logic |
cache | RequestCache | 'default' | Cache strategy |
mode | RequestMode | 'cors' | CORS mode |
credentials | RequestCredentials | 'same-origin' | Credentials mode |
signal | AbortSignal | - | Cancellation signal |
responseType | ResponseType | 'json' | Response format |
onUploadProgress | (progress: ProgressEvent) => void | - | Upload progress |
onDownloadProgress | (progress: ProgressEvent) => void | - | Download progress |
priority | 'high'๏ฝ'low'๏ฝ'auto' | 'auto' | Request priority |
Response Structure
Property | Type | Description |
---|---|---|
data | T ๏ฝ null | Parsed response data |
status | number | HTTP status code |
statusText | string | Status message |
headers | Headers | Response headers |
ok | boolean | Status in 200-299 range |
url | string | Final URL after redirects |
requestTime | number | Request start time |
responseTime | number | Response received time |
duration | number | Total request time (ms) |
error | string ๏ฝ null | Error message if failed |
retries | number | Retry attempts made |
maxRetries | number | Maximum retries |
isCached | boolean | Response from cache |
๐งช TypeScript Support
FetchSmart is written in TypeScript and provides full type definitions:
interface User {
id: number;
name: string;
email: string;
}
// Types are inferred automatically
const { data } = await client.get<User>('/user/1');
console.log(data.name); // TypeScript knows this is a string
// POST with type checking
const newUser = await client.post<User>('/users', {
name: 'John',
email: 'john@example.com'
});
๐ค Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
npm run commit
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
๐ License
This project is licensed under the MIT License - see the LICENSE file for details.
๐ Acknowledgments
- Built with TypeScript for better developer experience
- Inspired by Axios and Ky
- Follows modern JavaScript best practices
๐ฌ Support
- ๐ซ Report issues on GitHub Issues
- ๐ Star the repo if you find it useful
๐ Changelog
See CHANGELOG.md for release history.