payment-retry-kit 0.0.0-stage → 0.1.0

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.
Files changed (4) hide show
  1. package/README.md +11 -2
  2. package/index.d.ts +10 -0
  3. package/index.js +47 -0
  4. package/package.json +21 -4
package/README.md CHANGED
@@ -1,3 +1,12 @@
1
- # Temporary Holding Version
1
+ # payment-retry-kit
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A zero-dependency utility for resilient retry logic with exponential backoff, specifically tailored for payment APIs.
4
+
5
+ ## Why use this?
6
+ 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.
7
+
8
+ But you shouldn't retry a `400 Bad Request` (like a declined credit card), because retrying will just fail again.
9
+
10
+ ## Install
11
+ ```bash
12
+ npm install payment-retry-kit
package/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export interface RetryOptions {
2
+ maxRetries?: number;
3
+ baseDelayMs?: number;
4
+ shouldRetry?: (error: any) => boolean;
5
+ }
6
+
7
+ export function retry<T>(
8
+ fn: () => Promise<T>,
9
+ options?: RetryOptions
10
+ ): Promise<T>;
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,23 @@
1
1
  {
2
2
  "name": "payment-retry-kit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
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": "node test.js"
13
+ },
14
+ "keywords": [
15
+ "retry",
16
+ "payment",
17
+ "backoff",
18
+ "resilience",
19
+ "stripe"
20
+ ],
21
+ "author": "Deepasri Kanagaraj",
22
+ "license": "MIT"
23
+ }