reqforge 0.7.0 → 0.9.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 +30 -9
- package/package.json +1 -1
- package/src/index.js +5 -1
- package/src/interceptor.js +88 -0
- package/src/retry.js +56 -0
package/README.md
CHANGED
|
@@ -11,19 +11,41 @@ npm install reqforge
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```javascript
|
|
14
|
-
const {
|
|
14
|
+
const { request, interceptor } = require('reqforge');
|
|
15
15
|
|
|
16
|
-
//
|
|
16
|
+
// Add a request interceptor
|
|
17
|
+
const reqInterceptor = interceptor.createRequestInterceptor();
|
|
18
|
+
reqInterceptor.use((config) => {
|
|
19
|
+
console.log('Request:', config.url);
|
|
20
|
+
return config;
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// Make a request
|
|
17
24
|
request.get('/users')
|
|
18
25
|
.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
|
-
});
|
|
26
|
+
.then(data => console.log(data));
|
|
25
27
|
```
|
|
26
28
|
|
|
29
|
+
## Interceptors
|
|
30
|
+
|
|
31
|
+
The `interceptor` module provides request and response interception:
|
|
32
|
+
|
|
33
|
+
- `createRequestInterceptor()` - Create request interceptor manager
|
|
34
|
+
- `createResponseInterceptor()` - Create response interceptor manager
|
|
35
|
+
|
|
36
|
+
### InterceptorManager Methods
|
|
37
|
+
|
|
38
|
+
- `use(fulfilled, rejected)` - Add interceptor handler
|
|
39
|
+
- `eject(id)` - Remove interceptor handler
|
|
40
|
+
- `clear()` - Clear all handlers
|
|
41
|
+
|
|
42
|
+
## Retry Mechanism
|
|
43
|
+
|
|
44
|
+
The `retry` module provides:
|
|
45
|
+
|
|
46
|
+
- `withRetry(fn, options)` - Execute function with automatic retry
|
|
47
|
+
- `calculateBackoff(attempt, baseDelay, maxDelay)` - Calculate exponential backoff delay
|
|
48
|
+
|
|
27
49
|
## Error Handling
|
|
28
50
|
|
|
29
51
|
The `errors` module provides:
|
|
@@ -34,7 +56,6 @@ The `errors` module provides:
|
|
|
34
56
|
- `AuthError` - Authentication failures
|
|
35
57
|
- `ValidationError` - Validation errors
|
|
36
58
|
- `ErrorCodes` - Error code constants
|
|
37
|
-
- `createErrorFromResponse(response)` - Create error from response
|
|
38
59
|
|
|
39
60
|
## Authentication
|
|
40
61
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reqforge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.9.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,8 @@ 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');
|
|
13
|
+
const interceptor = require('./interceptor');
|
|
12
14
|
|
|
13
15
|
module.exports = {
|
|
14
16
|
config,
|
|
@@ -16,5 +18,7 @@ module.exports = {
|
|
|
16
18
|
request,
|
|
17
19
|
Logger,
|
|
18
20
|
auth,
|
|
19
|
-
errors
|
|
21
|
+
errors,
|
|
22
|
+
retry,
|
|
23
|
+
interceptor
|
|
20
24
|
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interceptor module for ReqForge
|
|
3
|
+
* @module interceptor
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Interceptor manager for request and response hooks
|
|
8
|
+
*/
|
|
9
|
+
class InterceptorManager {
|
|
10
|
+
constructor() {
|
|
11
|
+
this.handlers = [];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Add an interceptor handler
|
|
16
|
+
* @param {Function} fulfilled - Handler for fulfilled promise
|
|
17
|
+
* @param {Function} rejected - Handler for rejected promise
|
|
18
|
+
* @returns {number} Handler ID
|
|
19
|
+
*/
|
|
20
|
+
use(fulfilled, rejected) {
|
|
21
|
+
this.handlers.push({
|
|
22
|
+
fulfilled,
|
|
23
|
+
rejected,
|
|
24
|
+
id: this.handlers.length
|
|
25
|
+
});
|
|
26
|
+
return this.handlers.length - 1;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Remove an interceptor handler
|
|
31
|
+
* @param {number} id - Handler ID
|
|
32
|
+
*/
|
|
33
|
+
eject(id) {
|
|
34
|
+
if (this.handlers[id]) {
|
|
35
|
+
this.handlers[id] = null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Execute all handlers
|
|
41
|
+
* @param {*} data - Data to pass through handlers
|
|
42
|
+
* @returns {Promise} Processed data
|
|
43
|
+
*/
|
|
44
|
+
forEach(data) {
|
|
45
|
+
const chain = [Promise.resolve(data)];
|
|
46
|
+
this.handlers.forEach(handler => {
|
|
47
|
+
if (handler) {
|
|
48
|
+
chain.push(handler.fulfilled, handler.rejected);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
let promise = Promise.resolve(data);
|
|
52
|
+
this.handlers.forEach(handler => {
|
|
53
|
+
if (handler) {
|
|
54
|
+
promise = promise.then(handler.fulfilled, handler.rejected);
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
return promise;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Clear all handlers
|
|
62
|
+
*/
|
|
63
|
+
clear() {
|
|
64
|
+
this.handlers = [];
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Create request interceptor
|
|
70
|
+
* @returns {InterceptorManager} Request interceptor
|
|
71
|
+
*/
|
|
72
|
+
function createRequestInterceptor() {
|
|
73
|
+
return new InterceptorManager();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Create response interceptor
|
|
78
|
+
* @returns {InterceptorManager} Response interceptor
|
|
79
|
+
*/
|
|
80
|
+
function createResponseInterceptor() {
|
|
81
|
+
return new InterceptorManager();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
module.exports = {
|
|
85
|
+
InterceptorManager,
|
|
86
|
+
createRequestInterceptor,
|
|
87
|
+
createResponseInterceptor
|
|
88
|
+
};
|
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
|
+
};
|