reqforge 0.3.0 → 0.4.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/.claude/settings.local.json +4 -1
- package/README.md +20 -4
- package/package.json +1 -1
- package/src/index.js +3 -1
- package/src/request.js +79 -0
package/README.md
CHANGED
|
@@ -11,17 +11,33 @@ npm install reqforge
|
|
|
11
11
|
## Quick Start
|
|
12
12
|
|
|
13
13
|
```javascript
|
|
14
|
-
const { config, utils } = require('reqforge');
|
|
14
|
+
const { config, utils, request } = require('reqforge');
|
|
15
15
|
|
|
16
16
|
// Get the base URL for API requests
|
|
17
17
|
const baseURL = config.getBaseURL();
|
|
18
18
|
console.log(baseURL); // https://api.example.com/v1
|
|
19
19
|
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
// Make a GET request
|
|
21
|
+
request.get('/users')
|
|
22
|
+
.then(res => res.json())
|
|
23
|
+
.then(data => console.log(data));
|
|
24
|
+
|
|
25
|
+
// Make a POST request
|
|
26
|
+
request.post('/users', { name: 'John', email: 'john@example.com' })
|
|
27
|
+
.then(res => res.json())
|
|
28
|
+
.then(data => console.log(data));
|
|
23
29
|
```
|
|
24
30
|
|
|
31
|
+
## Request Methods
|
|
32
|
+
|
|
33
|
+
The `request` module provides:
|
|
34
|
+
|
|
35
|
+
- `sendRequest(endpoint, data, options)` - Generic request method
|
|
36
|
+
- `get(endpoint, options)` - GET request
|
|
37
|
+
- `post(endpoint, data, options)` - POST request
|
|
38
|
+
- `put(endpoint, data, options)` - PUT request
|
|
39
|
+
- `del(endpoint, options)` - DELETE request
|
|
40
|
+
|
|
25
41
|
## Configuration
|
|
26
42
|
|
|
27
43
|
The `config` module provides:
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "reqforge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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
package/src/request.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core request module for ReqForge
|
|
3
|
+
* @module request
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const { getBaseURL } = require('./config');
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Send HTTP request
|
|
10
|
+
* @param {string} endpoint - API endpoint
|
|
11
|
+
* @param {Object} data - Request body data
|
|
12
|
+
* @param {Object} options - Request options
|
|
13
|
+
* @returns {Promise<Response>} Fetch response promise
|
|
14
|
+
*/
|
|
15
|
+
function sendRequest(endpoint, data, options = {}) {
|
|
16
|
+
const url = getBaseURL() + endpoint;
|
|
17
|
+
|
|
18
|
+
const fetchOptions = {
|
|
19
|
+
method: options.method || 'GET',
|
|
20
|
+
headers: options.headers || {}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
if (data) {
|
|
24
|
+
fetchOptions.body = JSON.stringify(data);
|
|
25
|
+
fetchOptions.headers['Content-Type'] = 'application/json';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return fetch(url, fetchOptions);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* GET request helper
|
|
33
|
+
* @param {string} endpoint - API endpoint
|
|
34
|
+
* @param {Object} options - Request options
|
|
35
|
+
* @returns {Promise<Response>} Fetch response promise
|
|
36
|
+
*/
|
|
37
|
+
function get(endpoint, options = {}) {
|
|
38
|
+
return sendRequest(endpoint, null, { ...options, method: 'GET' });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* POST request helper
|
|
43
|
+
* @param {string} endpoint - API endpoint
|
|
44
|
+
* @param {Object} data - Request body data
|
|
45
|
+
* @param {Object} options - Request options
|
|
46
|
+
* @returns {Promise<Response>} Fetch response promise
|
|
47
|
+
*/
|
|
48
|
+
function post(endpoint, data, options = {}) {
|
|
49
|
+
return sendRequest(endpoint, data, { ...options, method: 'POST' });
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* PUT request helper
|
|
54
|
+
* @param {string} endpoint - API endpoint
|
|
55
|
+
* @param {Object} data - Request body data
|
|
56
|
+
* @param {Object} options - Request options
|
|
57
|
+
* @returns {Promise<Response>} Fetch response promise
|
|
58
|
+
*/
|
|
59
|
+
function put(endpoint, data, options = {}) {
|
|
60
|
+
return sendRequest(endpoint, data, { ...options, method: 'PUT' });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* DELETE request helper
|
|
65
|
+
* @param {string} endpoint - API endpoint
|
|
66
|
+
* @param {Object} options - Request options
|
|
67
|
+
* @returns {Promise<Response>} Fetch response promise
|
|
68
|
+
*/
|
|
69
|
+
function del(endpoint, options = {}) {
|
|
70
|
+
return sendRequest(endpoint, null, { ...options, method: 'DELETE' });
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
module.exports = {
|
|
74
|
+
sendRequest,
|
|
75
|
+
get,
|
|
76
|
+
post,
|
|
77
|
+
put,
|
|
78
|
+
del
|
|
79
|
+
};
|