reqforge 0.8.0 → 0.10.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/cache.js +100 -0
- package/src/index.js +5 -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.10.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/cache.js
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache module for ReqForge
|
|
3
|
+
* @module cache
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Simple in-memory cache with TTL support
|
|
8
|
+
*/
|
|
9
|
+
class MemoryCache {
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.store = new Map();
|
|
12
|
+
this.defaultTTL = options.defaultTTL || 60000;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Get value from cache
|
|
17
|
+
* @param {string} key - Cache key
|
|
18
|
+
* @returns {*|null} Cached value or null
|
|
19
|
+
*/
|
|
20
|
+
get(key) {
|
|
21
|
+
const entry = this.store.get(key);
|
|
22
|
+
if (!entry) return null;
|
|
23
|
+
|
|
24
|
+
if (Date.now() > entry.expiry) {
|
|
25
|
+
this.store.delete(key);
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return entry.value;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Set value in cache
|
|
34
|
+
* @param {string} key - Cache key
|
|
35
|
+
* @param {*} value - Value to cache
|
|
36
|
+
* @param {number} ttl - Time to live in ms
|
|
37
|
+
*/
|
|
38
|
+
set(key, value, ttl) {
|
|
39
|
+
this.store.set(key, {
|
|
40
|
+
value,
|
|
41
|
+
expiry: Date.now() + (ttl || this.defaultTTL)
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Check if key exists and is not expired
|
|
47
|
+
* @param {string} key - Cache key
|
|
48
|
+
* @returns {boolean} True if valid
|
|
49
|
+
*/
|
|
50
|
+
has(key) {
|
|
51
|
+
return this.get(key) !== null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Delete key from cache
|
|
56
|
+
* @param {string} key - Cache key
|
|
57
|
+
*/
|
|
58
|
+
delete(key) {
|
|
59
|
+
this.store.delete(key);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Clear all cached entries
|
|
64
|
+
*/
|
|
65
|
+
clear() {
|
|
66
|
+
this.store.clear();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Get cache size
|
|
71
|
+
* @returns {number} Number of entries
|
|
72
|
+
*/
|
|
73
|
+
size() {
|
|
74
|
+
let count = 0;
|
|
75
|
+
this.store.forEach((entry, key) => {
|
|
76
|
+
if (Date.now() <= entry.expiry) {
|
|
77
|
+
count++;
|
|
78
|
+
} else {
|
|
79
|
+
this.store.delete(key);
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
return count;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Create cache key from URL and params
|
|
88
|
+
* @param {string} url - Request URL
|
|
89
|
+
* @param {Object} params - Request params
|
|
90
|
+
* @returns {string} Cache key
|
|
91
|
+
*/
|
|
92
|
+
function createCacheKey(url, params = {}) {
|
|
93
|
+
const normalized = { url, ...params };
|
|
94
|
+
return JSON.stringify(normalized);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
module.exports = {
|
|
98
|
+
MemoryCache,
|
|
99
|
+
createCacheKey
|
|
100
|
+
};
|
package/src/index.js
CHANGED
|
@@ -10,6 +10,8 @@ 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');
|
|
14
|
+
const cache = require('./cache');
|
|
13
15
|
|
|
14
16
|
module.exports = {
|
|
15
17
|
config,
|
|
@@ -18,5 +20,7 @@ module.exports = {
|
|
|
18
20
|
Logger,
|
|
19
21
|
auth,
|
|
20
22
|
errors,
|
|
21
|
-
retry
|
|
23
|
+
retry,
|
|
24
|
+
interceptor,
|
|
25
|
+
cache
|
|
22
26
|
};
|
|
@@ -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
|
+
};
|