reqforge 0.1.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
@@ -8,6 +8,40 @@ A lightweight, flexible HTTP client library with request interception, automatic
8
8
  npm install reqforge
9
9
  ```
10
10
 
11
+ ## Quick Start
12
+
13
+ ```javascript
14
+ const { config, utils } = require('reqforge');
15
+
16
+ // Get the base URL for API requests
17
+ const baseURL = config.getBaseURL();
18
+ console.log(baseURL); // https://api.example.com/v1
19
+
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
23
+ ```
24
+
25
+ ## Configuration
26
+
27
+ The `config` module provides:
28
+
29
+ - `getBaseURL()` - Get the base URL for API requests
30
+ - `API_VERSION` - Current API version
31
+ - `defaultConfig` - Default configuration object
32
+ - `mergeConfig(customConfig)` - Merge custom configuration with defaults
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
+
11
45
  ## License
12
46
 
13
47
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reqforge",
3
- "version": "0.1.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/config.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Configuration module for ReqForge
3
+ * @module config
4
+ */
5
+
6
+ const API_VERSION = 'v1';
7
+
8
+ /**
9
+ * Get the base URL for API requests
10
+ * @returns {string} The base URL with API version
11
+ */
12
+ function getBaseURL() {
13
+ return `https://api.example.com/${API_VERSION}`;
14
+ }
15
+
16
+ /**
17
+ * Default configuration for requests
18
+ */
19
+ const defaultConfig = {
20
+ baseURL: getBaseURL(),
21
+ timeout: 5000,
22
+ headers: {
23
+ 'Content-Type': 'application/json'
24
+ }
25
+ };
26
+
27
+ /**
28
+ * Merge custom configuration with defaults
29
+ * @param {Object} customConfig - Custom configuration to merge
30
+ * @returns {Object} Merged configuration
31
+ */
32
+ function mergeConfig(customConfig = {}) {
33
+ return {
34
+ ...defaultConfig,
35
+ ...customConfig,
36
+ headers: {
37
+ ...defaultConfig.headers,
38
+ ...(customConfig.headers || {})
39
+ }
40
+ };
41
+ }
42
+
43
+ module.exports = {
44
+ getBaseURL,
45
+ API_VERSION,
46
+ defaultConfig,
47
+ mergeConfig
48
+ };
package/src/index.js CHANGED
@@ -3,4 +3,10 @@
3
3
  * @module reqforge
4
4
  */
5
5
 
6
- module.exports = {};
6
+ const config = require('./config');
7
+ const utils = require('./utils');
8
+
9
+ module.exports = {
10
+ config,
11
+ utils
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
+ };