@codingaryan/smoothapi 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -0
- package/package.json +11 -3
package/README.md
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# @codingaryan/smoothapi
|
|
2
|
+
|
|
3
|
+
API resilience library for TypeScript/JavaScript. It wraps the native `fetch` API with **exponential backoff, full jitter, and a finite-state machine circuit breaker** to protect against cascading failures.
|
|
4
|
+
|
|
5
|
+
Zero dependencies. Small bundle size. Built for modern ESM.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @codingaryan/smoothapi
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Features
|
|
14
|
+
|
|
15
|
+
- **Exponential Backoff with Full Jitter:** Prevents the "thundering herd" problem by randomizing retry delays.
|
|
16
|
+
- **Circuit Breaker (FSM):** Isolated per-domain state machine (`CLOSED` → `OPEN` → `HALF_OPEN`).
|
|
17
|
+
- **Smart Retries:** Automatically retries on specific HTTP status codes (e.g., 429, 500, 502, 503, 504) while throwing immediately on client errors (400, 401, 404).
|
|
18
|
+
- **Graceful Fallbacks:** Optionally serve cached or default data instantly when the circuit is `OPEN`.
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
Create your resilient fetch wrapper once and use it throughout your application:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import { createResilientFetch } from '@codingaryan/smoothapi';
|
|
26
|
+
|
|
27
|
+
const fetchWithRetry = createResilientFetch({
|
|
28
|
+
backoff: {
|
|
29
|
+
baseDelay: 100, // ms to wait before first retry
|
|
30
|
+
maxDelay: 30000, // cap on exponential growth
|
|
31
|
+
maxRetries: 3 // max number of retry attempts
|
|
32
|
+
},
|
|
33
|
+
circuitBreaker: {
|
|
34
|
+
failureThreshold: 3, // trip OPEN after 3 consecutive failures
|
|
35
|
+
cooldownMs: 10000 // stay OPEN for 10 seconds before probing
|
|
36
|
+
},
|
|
37
|
+
// Optional: Return this instead of throwing when the circuit is OPEN
|
|
38
|
+
fallback: { error: "Service degraded, returning stale data." },
|
|
39
|
+
// Optional: Custom status codes to retry on
|
|
40
|
+
retryOn: [429, 500, 502, 503, 504]
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
async function main() {
|
|
44
|
+
try {
|
|
45
|
+
// Drop-in replacement for native fetch
|
|
46
|
+
const response = await fetchWithRetry('https://api.example.com/data');
|
|
47
|
+
|
|
48
|
+
// If fallback triggered, it returns your fallback object directly
|
|
49
|
+
if ('error' in response) {
|
|
50
|
+
console.log("Fallback triggered:", response.error);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Otherwise it's a standard Response object
|
|
55
|
+
const data = await response.json();
|
|
56
|
+
console.log(data);
|
|
57
|
+
} catch (err) {
|
|
58
|
+
console.error("Request failed completely:", err);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## How It Works
|
|
64
|
+
|
|
65
|
+
1. **Host Extraction:** The domain is automatically extracted from the URL. The circuit breaker state is isolated per host (e.g., `api.github.com` failing won't trip the circuit for `api.stripe.com`).
|
|
66
|
+
2. **Circuit Check:** Before making a network request, the breaker checks the state. If it's `OPEN`, the request is blocked instantly (returning your fallback, or throwing a `CircuitOpenError`).
|
|
67
|
+
3. **Execution & Retries:** If the response status is in your `retryOn` list, it's counted as a failure and retried with backoff.
|
|
68
|
+
4. **Recovery:** After `cooldownMs`, the breaker enters `HALF_OPEN` state. The next request acts as a probe. If it succeeds, the circuit closes. If it fails, it snaps back to `OPEN` immediately.
|
|
69
|
+
|
|
70
|
+
## License
|
|
71
|
+
|
|
72
|
+
MIT
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codingaryan/smoothapi",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "API resilience library — exponential backoff and circuit breaker for fetch",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -11,7 +11,9 @@
|
|
|
11
11
|
"types": "./dist/index.d.ts"
|
|
12
12
|
}
|
|
13
13
|
},
|
|
14
|
-
"files": [
|
|
14
|
+
"files": [
|
|
15
|
+
"dist"
|
|
16
|
+
],
|
|
15
17
|
"scripts": {
|
|
16
18
|
"build": "tsc",
|
|
17
19
|
"build:watch": "tsc --watch",
|
|
@@ -25,6 +27,12 @@
|
|
|
25
27
|
"engines": {
|
|
26
28
|
"node": ">=18.0.0"
|
|
27
29
|
},
|
|
28
|
-
"keywords": [
|
|
30
|
+
"keywords": [
|
|
31
|
+
"resilience",
|
|
32
|
+
"circuit-breaker",
|
|
33
|
+
"retry",
|
|
34
|
+
"backoff",
|
|
35
|
+
"fetch"
|
|
36
|
+
],
|
|
29
37
|
"license": "MIT"
|
|
30
38
|
}
|