reqforge 0.2.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.
@@ -7,7 +7,11 @@
7
7
  "Bash(git add *)",
8
8
  "Bash(git commit *)",
9
9
  "Bash(git push *)",
10
- "Bash(npm publish *)"
10
+ "Bash(npm publish *)",
11
+ "Bash(npm view *)",
12
+ "Bash(git checkout *)",
13
+ "Bash(curl *)",
14
+ "Bash(git pull *)"
11
15
  ]
12
16
  }
13
17
  }
package/README.md CHANGED
@@ -11,21 +11,33 @@ npm install reqforge
11
11
  ## Quick Start
12
12
 
13
13
  ```javascript
14
- const { config } = 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
- // Merge custom configuration
21
- const customConfig = config.mergeConfig({
22
- timeout: 10000,
23
- headers: {
24
- 'Authorization': 'Bearer your-token'
25
- }
26
- });
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));
27
29
  ```
28
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
+
29
41
  ## Configuration
30
42
 
31
43
  The `config` module provides:
@@ -35,6 +47,17 @@ The `config` module provides:
35
47
  - `defaultConfig` - Default configuration object
36
48
  - `mergeConfig(customConfig)` - Merge custom configuration with defaults
37
49
 
50
+ ## Utilities
51
+
52
+ The `utils` module provides:
53
+
54
+ - `buildURL(baseURL, params)` - Build URL with query parameters
55
+ - `serializeQuery(obj)` - Serialize object to query string
56
+ - `parseQuery(queryString)` - Parse query string to object
57
+ - `deepMerge(target, source)` - Deep merge objects
58
+ - `isPlainObject(value)` - Check if value is a plain object
59
+ - `delay(ms)` - Delay execution
60
+
38
61
  ## License
39
62
 
40
63
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.2.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
@@ -4,7 +4,11 @@
4
4
  */
5
5
 
6
6
  const config = require('./config');
7
+ const utils = require('./utils');
8
+ const request = require('./request');
7
9
 
8
10
  module.exports = {
9
- config
11
+ config,
12
+ utils,
13
+ request
10
14
  };
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
+ };
package/src/utils.js ADDED
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Utility functions for ReqForge
3
+ * @module utils
4
+ */
5
+
6
+ /**
7
+ * Build URL with query parameters
8
+ * @param {string} baseURL - The base URL
9
+ * @param {Object} params - Query parameters
10
+ * @returns {string} URL with query string
11
+ */
12
+ function buildURL(baseURL, params = {}) {
13
+ const url = new URL(baseURL);
14
+ Object.keys(params).forEach(key => {
15
+ if (params[key] !== undefined && params[key] !== null) {
16
+ url.searchParams.append(key, params[key]);
17
+ }
18
+ });
19
+ return url.toString();
20
+ }
21
+
22
+ /**
23
+ * Serialize object to query string
24
+ * @param {Object} obj - Object to serialize
25
+ * @returns {string} Query string
26
+ */
27
+ function serializeQuery(obj) {
28
+ return Object.keys(obj)
29
+ .filter(key => obj[key] !== undefined && obj[key] !== null)
30
+ .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`)
31
+ .join('&');
32
+ }
33
+
34
+ /**
35
+ * Parse query string to object
36
+ * @param {string} queryString - Query string to parse
37
+ * @returns {Object} Parsed query object
38
+ */
39
+ function parseQuery(queryString) {
40
+ const params = {};
41
+ const searchParams = new URLSearchParams(queryString);
42
+ searchParams.forEach((value, key) => {
43
+ params[key] = value;
44
+ });
45
+ return params;
46
+ }
47
+
48
+ /**
49
+ * Deep merge objects
50
+ * @param {Object} target - Target object
51
+ * @param {Object} source - Source object
52
+ * @returns {Object} Merged object
53
+ */
54
+ function deepMerge(target, source) {
55
+ const result = { ...target };
56
+ Object.keys(source).forEach(key => {
57
+ if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) {
58
+ result[key] = deepMerge(result[key] || {}, source[key]);
59
+ } else {
60
+ result[key] = source[key];
61
+ }
62
+ });
63
+ return result;
64
+ }
65
+
66
+ /**
67
+ * Check if value is a plain object
68
+ * @param {*} value - Value to check
69
+ * @returns {boolean} True if plain object
70
+ */
71
+ function isPlainObject(value) {
72
+ return Object.prototype.toString.call(value) === '[object Object]';
73
+ }
74
+
75
+ /**
76
+ * Delay execution
77
+ * @param {number} ms - Milliseconds to delay
78
+ * @returns {Promise} Promise that resolves after delay
79
+ */
80
+ function delay(ms) {
81
+ return new Promise(resolve => setTimeout(resolve, ms));
82
+ }
83
+
84
+ module.exports = {
85
+ buildURL,
86
+ serializeQuery,
87
+ parseQuery,
88
+ deepMerge,
89
+ isPlainObject,
90
+ delay
91
+ };