reqforge 0.7.0 → 0.8.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.
package/README.md CHANGED
@@ -11,19 +11,32 @@ npm install reqforge
11
11
  ## Quick Start
12
12
 
13
13
  ```javascript
14
- const { config, request, auth, errors } = require('reqforge');
15
-
16
- // Make a request with error handling
17
- request.get('/users')
18
- .then(res => res.json())
19
- .then(data => console.log(data))
20
- .catch(err => {
21
- if (err instanceof errors.NetworkError) {
22
- console.error('Network issue:', err.message);
23
- }
24
- });
14
+ const { config, request, retry } = require('reqforge');
15
+
16
+ // Make a request with automatic retry
17
+ retry.withRetry(() => {
18
+ return request.get('/users')
19
+ .then(res => res.json());
20
+ }, { maxRetries: 3, delay: 1000 })
21
+ .then(data => console.log(data));
25
22
  ```
26
23
 
24
+ ## Retry Mechanism
25
+
26
+ The `retry` module provides:
27
+
28
+ - `withRetry(fn, options)` - Execute function with automatic retry
29
+ - `calculateBackoff(attempt, baseDelay, maxDelay)` - Calculate exponential backoff delay
30
+
31
+ ### Retry Options
32
+
33
+ | Option | Default | Description |
34
+ |--------|---------|-------------|
35
+ | `maxRetries` | 3 | Maximum number of retry attempts |
36
+ | `delay` | 1000 | Initial delay in milliseconds |
37
+ | `backoff` | 2 | Backoff multiplier |
38
+ | `shouldRetry` | `() => true` | Function to determine if retry should occur |
39
+
27
40
  ## Error Handling
28
41
 
29
42
  The `errors` module provides:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "A lightweight, flexible HTTP client library with request interception, automatic retry, and logging capabilities, designed for modern web applications.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -9,6 +9,7 @@ const request = require('./request');
9
9
  const Logger = require('./logger');
10
10
  const auth = require('./auth');
11
11
  const errors = require('./errors');
12
+ const retry = require('./retry');
12
13
 
13
14
  module.exports = {
14
15
  config,
@@ -16,5 +17,6 @@ module.exports = {
16
17
  request,
17
18
  Logger,
18
19
  auth,
19
- errors
20
+ errors,
21
+ retry
20
22
  };
package/src/retry.js ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Retry mechanism module for ReqForge
3
+ * @module retry
4
+ */
5
+
6
+ /**
7
+ * Execute function with retry
8
+ * @param {Function} fn - Function to execute
9
+ * @param {Object} options - Retry options
10
+ * @returns {Promise} Result of the function
11
+ */
12
+ async function withRetry(fn, options = {}) {
13
+ const {
14
+ maxRetries = 3,
15
+ delay = 1000,
16
+ backoff = 2,
17
+ shouldRetry = () => true
18
+ } = options;
19
+
20
+ let lastError;
21
+ let currentDelay = delay;
22
+
23
+ for (let attempt = 0; attempt <= maxRetries; attempt++) {
24
+ try {
25
+ return await fn();
26
+ } catch (error) {
27
+ lastError = error;
28
+
29
+ if (attempt === maxRetries || !shouldRetry(error)) {
30
+ throw error;
31
+ }
32
+
33
+ await new Promise(resolve => setTimeout(resolve, currentDelay));
34
+ currentDelay *= backoff;
35
+ }
36
+ }
37
+
38
+ throw lastError;
39
+ }
40
+
41
+ /**
42
+ * Calculate exponential backoff delay
43
+ * @param {number} attempt - Current attempt number
44
+ * @param {number} baseDelay - Base delay in ms
45
+ * @param {number} maxDelay - Maximum delay in ms
46
+ * @returns {number} Delay in ms
47
+ */
48
+ function calculateBackoff(attempt, baseDelay = 1000, maxDelay = 30000) {
49
+ const delay = baseDelay * Math.pow(2, attempt);
50
+ return Math.min(delay, maxDelay);
51
+ }
52
+
53
+ module.exports = {
54
+ withRetry,
55
+ calculateBackoff
56
+ };