advanced-client-fetch
v1.1.8
Published
π Modern HTTP client with fetch-first design, plugin architecture, and security features. Works across all platforms with automatic cookie management and SSRF protection.
Maintainers
Readme
Advanced Client Fetch
π Modern HTTP client with fetch-first design, plugin architecture, and security features. Works across all platforms with automatic cookie management and SSRF protection.
β¨ Features
- π Universal: Works on Node.js, Browser, Edge runtimes, Deno, and Bun
- π Plugin Architecture: Extensible middleware system with built-in plugins
- β‘ Performance: Fetch-first design with optimized request handling
- π‘οΈ Security: Built-in SSRF protection and security features
- πͺ Cookie Management: Automatic cookie handling across requests
- π Axios Compatible: Drop-in replacement for Axios
- π Metrics: Built-in performance monitoring and metrics
- π Retry Logic: Smart retry with exponential backoff and jitter
- β±οΈ Timeout: Configurable timeouts with per-attempt and total timeout
- πΎ Caching: RFC 9111 compliant HTTP caching
- π¦ Rate Limiting: Token bucket and sliding window rate limiting
- π Circuit Breaker: Fault tolerance with circuit breaker pattern
- π Deduplication: Request deduplication to prevent duplicate calls
- π Monitoring: Comprehensive metrics and performance monitoring
π Quick Start
Installation
# npm
npm install advanced-client-fetch
# pnpm
pnpm add advanced-client-fetch
# yarn
yarn add advanced-client-fetch
# bun
bun add advanced-client-fetchBasic Usage
import { createClient } from 'advanced-client-fetch';
// Create a client
const client = createClient({
baseURL: 'https://api.example.com',
headers: {
'Authorization': 'Bearer your-token'
}
});
// Make requests
const users = await client.get('/users');
const user = await client.post('/users', { name: 'John', email: '[email protected]' });With Plugins
import { createClient, retry, timeout, cache, metrics } from 'advanced-client-fetch';
const client = createClient({
baseURL: 'https://api.example.com',
plugins: [
retry({ retries: 3, minDelay: 100, maxDelay: 2000 }),
timeout(30000),
cache({ ttl: 300000 }),
metrics({ enabled: true })
]
});Platform-Specific Presets
import {
createNodeClient,
createEdgeClient,
createBrowserClient,
createDenoClient,
createBunClient
} from 'advanced-client-fetch';
// Node.js optimized
const nodeClient = createNodeClient({
baseURL: 'https://api.example.com'
});
// Edge runtime optimized
const edgeClient = createEdgeClient({
baseURL: 'https://api.example.com'
});
// Browser optimized
const browserClient = createBrowserClient({
baseURL: 'https://api.example.com'
});Axios Compatibility
import { createAxiosAdapter } from 'advanced-client-fetch';
const axios = createAxiosAdapter({
baseURL: 'https://api.example.com',
timeout: 5000
});
// Use exactly like Axios
const response = await axios.get('/users');π Documentation
Core Concepts
- Client API - Core client methods and options
- Plugins - Built-in and custom plugins
- Presets - Platform-specific configurations
- Axios Adapter - Axios compatibility layer
- Error Handling - Error types and handling
- Security - Security features and best practices
Plugins
- Retry - Smart retry with exponential backoff
- Timeout - Configurable timeouts
- Cache - HTTP caching with RFC 9111 compliance
- Rate Limiting - Request rate limiting
- Circuit Breaker - Fault tolerance
- Deduplication - Request deduplication
- Metrics - Performance monitoring
Presets
- Node.js - Node.js runtime optimizations
- Edge - Edge runtime optimizations
- Browser - Browser environment optimizations
- Deno - Deno runtime optimizations
- Bun - Bun runtime optimizations
π Plugins
Built-in Plugins
import {
retry,
timeout,
cache,
rateLimit,
circuitBreaker,
dedupe,
metrics
} from 'advanced-client-fetch';
const client = createClient({
plugins: [
// Retry failed requests
retry({
retries: 3,
minDelay: 100,
maxDelay: 2000,
jitter: true
}),
// Set request timeout
timeout(30000),
// Cache responses
cache({
ttl: 300000, // 5 minutes
cacheOnlyGET: true
}),
// Rate limiting
rateLimit({
requests: 100,
window: 60000 // 1 minute
}),
// Circuit breaker
circuitBreaker({
failureThreshold: 5,
window: 60000,
resetTimeout: 30000
}),
// Request deduplication
dedupe({
maxAge: 30000
}),
// Performance metrics
metrics({
enabled: true
})
]
});Custom Plugins
import { createClient, type Middleware } from 'advanced-client-fetch';
const customPlugin: Middleware = async (ctx, next) => {
console.log(`Making request to ${ctx.req.url}`);
try {
await next();
console.log(`Request completed with status ${ctx.res?.status}`);
} catch (error) {
console.error(`Request failed: ${error.message}`);
throw error;
}
};
const client = createClient({
plugins: [customPlugin]
});π Platform Support
Node.js
import { createNodeClient } from 'advanced-client-fetch';
const client = createNodeClient({
baseURL: 'https://api.example.com'
});Edge Runtimes
import { createEdgeClient } from 'advanced-client-fetch';
const client = createEdgeClient({
baseURL: 'https://api.example.com'
});Browser
import { createBrowserClient } from 'advanced-client-fetch';
const client = createBrowserClient({
baseURL: 'https://api.example.com'
});Deno
import { createDenoClient } from 'advanced-client-fetch';
const client = createDenoClient({
baseURL: 'https://api.example.com'
});Bun
import { createBunClient } from 'advanced-client-fetch';
const client = createBunClient({
baseURL: 'https://api.example.com'
});π Migration from Axios
Advanced Client Fetch provides a drop-in replacement for Axios:
// Before (Axios)
import axios from 'axios';
const response = await axios.get('/api/users');
// After (Advanced Client Fetch)
import { createAxiosAdapter } from 'advanced-client-fetch';
const axios = createAxiosAdapter();
const response = await axios.get('/api/users');π‘οΈ Security Features
- SSRF Protection: Prevents Server-Side Request Forgery attacks
- Host Validation: Validates allowed/blocked hosts
- Request Size Limits: Configurable request and response size limits
- Redirect Limits: Prevents infinite redirect loops
- Cookie Security: Secure cookie handling with proper flags
π Performance
- Fetch-First: Uses native fetch API for optimal performance
- Tree Shaking: Optimized bundle size with tree shaking
- Lazy Loading: Plugins are loaded only when needed
- Connection Pooling: Efficient connection management
- Request Deduplication: Prevents duplicate requests
π§ͺ Testing
# Run tests
pnpm test
# Run tests in watch mode
pnpm test:watch
# Run tests with coverage
pnpm test:coverageπ¦ Bundle Analysis
# Analyze bundle size
pnpm build
pnpm analyzeπ€ Contributing
We welcome contributions! Please see our Contributing Guide for details.
π License
MIT License - see LICENSE for details.
π Acknowledgments
- Inspired by Axios for its excellent API design
- Built on top of the native Fetch API
- Plugin architecture inspired by Koa
π Support
- π Documentation
- π Issues
- π¬ Discussions
- π§ Email
Made with β€οΈ by Yasar Tahir Kose# CI Test
