payment-retry-kit 0.0.0-stage → 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 +13 -2
- package/index.d.ts +10 -0
- package/index.js +47 -0
- package/package.json +24 -4
package/README.md
CHANGED
|
@@ -1,3 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
# payment-retry-kit
|
|
4
|
+
|
|
5
|
+
A zero-dependency utility for resilient retry logic with exponential backoff, specifically tailored for payment APIs.
|
|
6
|
+
|
|
7
|
+
## Why use this?
|
|
8
|
+
When a payment API (like Stripe, PayPal, or Adyen) fails with a `500 Server Error` or a `429 Too Many Requests`, your server shouldn't just give up. It should wait and try again.
|
|
9
|
+
|
|
10
|
+
But you shouldn't retry a `400 Bad Request` (like a declined credit card), because retrying will just fail again.
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
```bash
|
|
14
|
+
npm install payment-retry-kit
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retries an async function with exponential backoff.
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} fn - The async function to retry.
|
|
5
|
+
* @param {object} [options] - Configuration options.
|
|
6
|
+
* @param {number} [options.maxRetries=3] - Maximum number of retries.
|
|
7
|
+
* @param {number} [options.baseDelayMs=1000] - Initial delay in ms (default 1 second).
|
|
8
|
+
* @param {Function} [options.shouldRetry] - Custom function to decide if an error is retryable.
|
|
9
|
+
* @returns {Promise<any>} - The result of the successful function call.
|
|
10
|
+
*/
|
|
11
|
+
async function retry(fn, options = {}) {
|
|
12
|
+
const maxRetries = options.maxRetries || 3;
|
|
13
|
+
const baseDelayMs = options.baseDelayMs || 1000;
|
|
14
|
+
|
|
15
|
+
// By default, retry on network errors or 5xx/429 status codes
|
|
16
|
+
const shouldRetry = options.shouldRetry || ((error) => {
|
|
17
|
+
if (error.status >= 500 || error.status === 429) return true;
|
|
18
|
+
if (error.code === 'ECONNRESET' || error.code === 'ETIMEDOUT') return true;
|
|
19
|
+
return false; // Don't retry 400s (Bad Request), 401s (Unauthorized), etc.
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
let attempt = 0;
|
|
23
|
+
|
|
24
|
+
while (true) {
|
|
25
|
+
try {
|
|
26
|
+
// Try to run the function
|
|
27
|
+
return await fn();
|
|
28
|
+
} catch (error) {
|
|
29
|
+
attempt++;
|
|
30
|
+
|
|
31
|
+
// If we've reached the max retries, or the error isn't retryable, throw it.
|
|
32
|
+
if (attempt > maxRetries || !shouldRetry(error)) {
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Calculate exponential backoff: 1s, 2s, 4s, 8s...
|
|
37
|
+
// We add a bit of random "jitter" so multiple servers don't retry at the exact same millisecond.
|
|
38
|
+
const jitter = Math.random() * 100;
|
|
39
|
+
const delay = (baseDelayMs * Math.pow(2, attempt - 1)) + jitter;
|
|
40
|
+
|
|
41
|
+
// Wait before the next attempt
|
|
42
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
module.exports = { retry };
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "payment-retry-kit",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Resilient retry logic with exponential backoff for payment APIs.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"types": "index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"index.js",
|
|
9
|
+
"index.d.ts"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "vitest run"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"retry",
|
|
16
|
+
"payment",
|
|
17
|
+
"backoff",
|
|
18
|
+
"resilience",
|
|
19
|
+
"stripe"
|
|
20
|
+
],
|
|
21
|
+
"author": "Deepasri Kanagaraj",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"vitest": "^5.1.2"
|
|
25
|
+
}
|
|
26
|
+
}
|