reqforge 0.8.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 +25 -17
- package/package.json +1 -1
- package/src/index.js +3 -1
- package/src/interceptor.js +88 -0
package/README.md
CHANGED
|
@@ -11,16 +11,34 @@ npm install reqforge
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```javascript
|
|
14
|
-
const {
|
|
15
|
-
|
|
16
|
-
//
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
14
|
+
const { request, interceptor } = require('reqforge');
|
|
15
|
+
|
|
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
|
|
24
|
+
request.get('/users')
|
|
25
|
+
.then(res => res.json())
|
|
21
26
|
.then(data => console.log(data));
|
|
22
27
|
```
|
|
23
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
|
+
|
|
24
42
|
## Retry Mechanism
|
|
25
43
|
|
|
26
44
|
The `retry` module provides:
|
|
@@ -28,15 +46,6 @@ The `retry` module provides:
|
|
|
28
46
|
- `withRetry(fn, options)` - Execute function with automatic retry
|
|
29
47
|
- `calculateBackoff(attempt, baseDelay, maxDelay)` - Calculate exponential backoff delay
|
|
30
48
|
|
|
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
|
-
|
|
40
49
|
## Error Handling
|
|
41
50
|
|
|
42
51
|
The `errors` module provides:
|
|
@@ -47,7 +56,6 @@ The `errors` module provides:
|
|
|
47
56
|
- `AuthError` - Authentication failures
|
|
48
57
|
- `ValidationError` - Validation errors
|
|
49
58
|
- `ErrorCodes` - Error code constants
|
|
50
|
-
- `createErrorFromResponse(response)` - Create error from response
|
|
51
59
|
|
|
52
60
|
## Authentication
|
|
53
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
|
@@ -10,6 +10,7 @@ const Logger = require('./logger');
|
|
|
10
10
|
const auth = require('./auth');
|
|
11
11
|
const errors = require('./errors');
|
|
12
12
|
const retry = require('./retry');
|
|
13
|
+
const interceptor = require('./interceptor');
|
|
13
14
|
|
|
14
15
|
module.exports = {
|
|
15
16
|
config,
|
|
@@ -18,5 +19,6 @@ module.exports = {
|
|
|
18
19
|
Logger,
|
|
19
20
|
auth,
|
|
20
21
|
errors,
|
|
21
|
-
retry
|
|
22
|
+
retry,
|
|
23
|
+
interceptor
|
|
22
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
|
+
};
|