|
1 | | -# FastFetch API Fetch Enhancer |
| 1 | +# FastFetch API Enhancer |
2 | 2 |
|
3 | | -## Description |
4 | | -FastFetch is an NPM package designed to enhance the `fetch()` function by providing a smarter wrapper with features like auto-retry, deduplication, and minimal boilerplate for efficient API requests. With FastFetch, you can eliminate redundant API calls and seamlessly handle failures thanks to the built-in retry logic using exponential backoff strategy.. |
| 3 | +FastFetch is a production-grade, enterprise-ready HTTP client built around the native `fetch` API. It transforms simple HTTP requests into robust network architecture by introducing advanced resilience patterns, high-concurrency request management, and real-time streaming capabilities without excessive boilerplate. |
5 | 4 |
|
6 | | -## Features |
7 | | -- **Auto-Retry**: Automatic retries for failed API requests using an exponential backoff strategy. |
8 | | -- **Deduplication**: Avoid duplicate API calls by caching requests and returning the same response when multiple requests for the same resource are made simultaneously. |
9 | | -- **Efficiency**: Minimal boilerplate code needed to make API requests, making the process faster and easier. |
10 | | -- **Reliability**: Built-in retry logic ensures that failed requests are retried intelligently without manual intervention. |
| 5 | +## Architecture & Features |
11 | 6 |
|
12 | | -## Topics |
13 | | -api, api-fetch, api-rest, auto-retry, axios, cache, deduplication, exponential-backoff, fast-fetch, fetch, fetch-api, fetch-data, frontend, ghost-cache, node-package-manager, nodejs, npm, npm-package, retry, smart-api |
| 7 | +FastFetch v2 provides seven core execution pipelines directly into your fetch workflow: |
14 | 8 |
|
15 | | -## Repository Link |
16 | | -[](https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases "Needs to be launched from the link") |
| 9 | +- **Strict Typed Error Hierarchy:** Explicit errors for `TimeoutError`, `NetworkError`, `HttpError`, and `CircuitOpenError` enabling robust try/catch control flow. |
| 10 | +- **Koa-Style Middleware Pipeline:** A powerful `.use(async (ctx, next) => {})` onion-model interceptor architecture for modifying request metadata or observing response execution duration. |
| 11 | +- **Finite State Machine Circuit Breaker:** Stops cascading failures to downstream services by automatically failing fast when failure thresholds are breached, entering a `HALF_OPEN` state after the configured timeout. |
| 12 | +- **Priority-Aware Concurrency Queue:** Protects API rate limits by enforcing a strict `maxConcurrent` ceiling across all ongoing requests while maintaining execution priority. |
| 13 | +- **Offline Mutation Buffering:** Automatically buffers mutating requests (`POST`, `PUT`, `PATCH`, `DELETE`) internally if the browser detects an offline state, sequentially replaying them upon reconnection. |
| 14 | +- **Real-Time Telemetry & Metrics:** Maintains rolling windows on p50, p95, and p99 latency percentiles alongside exact endpoint success and failure ratios. |
| 15 | +- **Server-Sent Events (SSE) Native Streaming:** A built-in high-performance stream consumer utilizing `api.stream()` for interacting with AI models and event-driven backends natively over HTTP. |
17 | 16 |
|
18 | | -For more information, please visit the repository's [Releases](https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases) section. |
| 17 | +## Installation |
19 | 18 |
|
20 | | ---- |
21 | | - |
22 | | -Welcome to the FastFetch API Fetch Enhancer repository! FastFetch is your go-to solution for optimizing API requests in your https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases projects. Whether you are building web applications, frontend interfaces, or working with backend services, FastFetch has got you covered. |
23 | | - |
24 | | -### Getting Started |
25 | | -To start using FastFetch in your project, follow these simple steps: |
26 | | -1. Install FastFetch via NPM: |
27 | 19 | ```bash |
28 | | -npm install fast-fetch |
29 | | -``` |
30 | | -2. Incorporate FastFetch in your script: |
31 | | -```javascript |
32 | | -const fastFetch = require('fast-fetch'); |
33 | | -``` |
34 | | -3. Begin making efficient API calls using FastFetch: |
35 | | -```javascript |
36 | | -fastFetch('https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases') |
37 | | - .then(response => https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases(response)) |
38 | | - .catch(err => https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases(err)); |
| 20 | +npm install fastfetch-api-fetch-enhancer |
39 | 21 | ``` |
40 | 22 |
|
41 | | -### Advanced Features |
42 | | -FastFetch comes with a set of advanced features to enhance your API request handling: |
43 | | -- **Auto-Retry**: Don't worry about failed requests, FastFetch will automatically retry with exponential backoff. |
44 | | -- **Deduplication**: Avoid duplicate API calls by fetching the same response when multiple requests for the same resource are made. |
45 | | -- **Caching**: Ghost-caching ensures that responses are stored for quicker access in subsequent requests. |
46 | | -- **Error Handling**: Easily manage and catch errors in your API requests with built-in retry logic. |
47 | | -
|
48 | | -### Example Usage |
49 | | -```javascript |
50 | | -const fetchData = async () => { |
51 | | - try { |
52 | | - const data = await fastFetch('https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases'); |
53 | | - https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases(data); |
54 | | - } catch (error) { |
55 | | - https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases(error); |
| 23 | +## Basic Usage |
| 24 | + |
| 25 | +A minimal example utilizing the `createClient` factory method: |
| 26 | + |
| 27 | +```typescript |
| 28 | +import { createClient } from 'fastfetch-api-fetch-enhancer'; |
| 29 | + |
| 30 | +// Instantiate your global HTTP client |
| 31 | +const api = createClient({ |
| 32 | + baseURL: 'https://api.example.com/v1', |
| 33 | + headers: { 'Authorization': 'Bearer <token>' }, |
| 34 | + timeout: 10000, |
| 35 | + retries: 3, |
| 36 | + retryDelay: 1000, |
| 37 | + maxConcurrent: 50, |
| 38 | + circuitBreaker: { |
| 39 | + threshold: 5, |
| 40 | + timeout: 30000 |
| 41 | + } |
| 42 | +}); |
| 43 | + |
| 44 | +// Koa-style middleware injection |
| 45 | +api.use(async (ctx, next) => { |
| 46 | + ctx.init.headers['X-Request-Id'] = crypto.randomUUID(); |
| 47 | + await next(); |
| 48 | + console.log(`[HTTP ${ctx.response?.status}] ${ctx.method} ${ctx.url} - ${ctx.duration}ms`); |
| 49 | +}); |
| 50 | + |
| 51 | +// Start making requests |
| 52 | +async function execute() { |
| 53 | + try { |
| 54 | + // Standard JSON shorthand |
| 55 | + const user = await api.json('/users/me'); |
| 56 | + |
| 57 | + // Auto-serialization for payload delivery |
| 58 | + await api.post('/events', { action: 'login', timestamp: Date.now() }); |
| 59 | + |
| 60 | + } catch (error) { |
| 61 | + // Typed catch boundaries |
| 62 | + if (error instanceof HttpError) { |
| 63 | + console.error('API Rejected Request:', error.status); |
| 64 | + } else if (error instanceof TimeoutError) { |
| 65 | + console.error('Request timed out at:', error.timeout); |
| 66 | + } else { |
| 67 | + console.error('Catastrophic failure:', error); |
56 | 68 | } |
57 | | -}; |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Advanced Streaming (SSE) |
| 74 | + |
| 75 | +Consume AI generation pipelines directly over HTTP using the `stream` handler: |
58 | 76 |
|
59 | | -fetchData(); |
| 77 | +```typescript |
| 78 | +const signal = new AbortController(); |
| 79 | + |
| 80 | +await api.stream( |
| 81 | + '/ai/generate-text', |
| 82 | + (event) => { |
| 83 | + process.stdout.write(event.data); |
| 84 | + }, |
| 85 | + { signal: signal.signal } |
| 86 | +); |
60 | 87 | ``` |
61 | 88 |
|
62 | | -### Contribution |
63 | | -We welcome contributions to FastFetch! If you have ideas for improvements, new features, or bug fixes, feel free to submit a pull request. Together we can make FastFetch even better for the community. |
| 89 | +## Monitoring & Telemetry |
| 90 | + |
| 91 | +Evaluate application stability at any time by accessing the built-in system telemetry map: |
64 | 92 |
|
65 | | -### License |
66 | | -FastFetch is licensed under the MIT License. See the [LICENSE](https://github.com/keshav-005/FastFetch-API-Fetch-Enhancer/releases) file for more details. |
| 93 | +```typescript |
| 94 | +const telemetry = api.metrics.snapshot(); |
| 95 | +console.table(telemetry.byEndpoint); |
| 96 | +console.log('p99 global latency:', telemetry.globalLatencies.p99); |
| 97 | +``` |
67 | 98 |
|
68 | | ---- |
| 99 | +## License |
69 | 100 |
|
70 | | -Thank you for choosing FastFetch as your API fetch enhancer. Make your API requests smarter, faster, and more reliable with FastFetch! 🌟🚀 |
| 101 | +FastFetch is open-source software licensed under the MIT License. |
0 commit comments