reqforge 0.2.0 → 0.3.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,8 @@
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 *)"
11
12
  ]
12
13
  }
13
14
  }
package/README.md CHANGED
@@ -11,19 +11,15 @@ npm install reqforge
11
11
  ## Quick Start
12
12
 
13
13
  ```javascript
14
- const { config } = require('reqforge');
14
+ const { config, utils } = 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
+ // Build URL with query parameters
21
+ const url = utils.buildURL('https://api.example.com/v1/users', { page: 1, limit: 10 });
22
+ console.log(url); // https://api.example.com/v1/users?page=1&limit=10
27
23
  ```
28
24
 
29
25
  ## Configuration
@@ -35,6 +31,17 @@ The `config` module provides:
35
31
  - `defaultConfig` - Default configuration object
36
32
  - `mergeConfig(customConfig)` - Merge custom configuration with defaults
37
33
 
34
+ ## Utilities
35
+
36
+ The `utils` module provides:
37
+
38
+ - `buildURL(baseURL, params)` - Build URL with query parameters
39
+ - `serializeQuery(obj)` - Serialize object to query string
40
+ - `parseQuery(queryString)` - Parse query string to object
41
+ - `deepMerge(target, source)` - Deep merge objects
42
+ - `isPlainObject(value)` - Check if value is a plain object
43
+ - `delay(ms)` - Delay execution
44
+
38
45
  ## License
39
46
 
40
47
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.2.0",
3
+ "version": "0.3.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,9 @@
4
4
  */
5
5
 
6
6
  const config = require('./config');
7
+ const utils = require('./utils');
7
8
 
8
9
  module.exports = {
9
- config
10
+ config,
11
+ utils
10
12
  };
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
+ };