+91
| /** | ||
| * Utility functions for ReqForge | ||
| * @module utils | ||
| */ | ||
| /** | ||
| * Build URL with query parameters | ||
| * @param {string} baseURL - The base URL | ||
| * @param {Object} params - Query parameters | ||
| * @returns {string} URL with query string | ||
| */ | ||
| function buildURL(baseURL, params = {}) { | ||
| const url = new URL(baseURL); | ||
| Object.keys(params).forEach(key => { | ||
| if (params[key] !== undefined && params[key] !== null) { | ||
| url.searchParams.append(key, params[key]); | ||
| } | ||
| }); | ||
| return url.toString(); | ||
| } | ||
| /** | ||
| * Serialize object to query string | ||
| * @param {Object} obj - Object to serialize | ||
| * @returns {string} Query string | ||
| */ | ||
| function serializeQuery(obj) { | ||
| return Object.keys(obj) | ||
| .filter(key => obj[key] !== undefined && obj[key] !== null) | ||
| .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`) | ||
| .join('&'); | ||
| } | ||
| /** | ||
| * Parse query string to object | ||
| * @param {string} queryString - Query string to parse | ||
| * @returns {Object} Parsed query object | ||
| */ | ||
| function parseQuery(queryString) { | ||
| const params = {}; | ||
| const searchParams = new URLSearchParams(queryString); | ||
| searchParams.forEach((value, key) => { | ||
| params[key] = value; | ||
| }); | ||
| return params; | ||
| } | ||
| /** | ||
| * Deep merge objects | ||
| * @param {Object} target - Target object | ||
| * @param {Object} source - Source object | ||
| * @returns {Object} Merged object | ||
| */ | ||
| function deepMerge(target, source) { | ||
| const result = { ...target }; | ||
| Object.keys(source).forEach(key => { | ||
| if (source[key] && typeof source[key] === 'object' && !Array.isArray(source[key])) { | ||
| result[key] = deepMerge(result[key] || {}, source[key]); | ||
| } else { | ||
| result[key] = source[key]; | ||
| } | ||
| }); | ||
| return result; | ||
| } | ||
| /** | ||
| * Check if value is a plain object | ||
| * @param {*} value - Value to check | ||
| * @returns {boolean} True if plain object | ||
| */ | ||
| function isPlainObject(value) { | ||
| return Object.prototype.toString.call(value) === '[object Object]'; | ||
| } | ||
| /** | ||
| * Delay execution | ||
| * @param {number} ms - Milliseconds to delay | ||
| * @returns {Promise} Promise that resolves after delay | ||
| */ | ||
| function delay(ms) { | ||
| return new Promise(resolve => setTimeout(resolve, ms)); | ||
| } | ||
| module.exports = { | ||
| buildURL, | ||
| serializeQuery, | ||
| parseQuery, | ||
| deepMerge, | ||
| isPlainObject, | ||
| delay | ||
| }; |
@@ -10,5 +10,6 @@ { | ||
| "Bash(git push *)", | ||
| "Bash(npm publish *)" | ||
| "Bash(npm publish *)", | ||
| "Bash(npm view *)" | ||
| ] | ||
| } | ||
| } |
+1
-1
| { | ||
| "name": "reqforge", | ||
| "version": "0.2.0", | ||
| "version": "0.3.0", | ||
| "description": "A lightweight, flexible HTTP client library with request interception, automatic retry, and logging capabilities, designed for modern web applications.", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
+15
-8
@@ -14,3 +14,3 @@ # ReqForge | ||
| ```javascript | ||
| const { config } = require('reqforge'); | ||
| const { config, utils } = require('reqforge'); | ||
@@ -21,9 +21,5 @@ // Get the base URL for API requests | ||
| // Merge custom configuration | ||
| const customConfig = config.mergeConfig({ | ||
| timeout: 10000, | ||
| headers: { | ||
| 'Authorization': 'Bearer your-token' | ||
| } | ||
| }); | ||
| // Build URL with query parameters | ||
| const url = utils.buildURL('https://api.example.com/v1/users', { page: 1, limit: 10 }); | ||
| console.log(url); // https://api.example.com/v1/users?page=1&limit=10 | ||
| ``` | ||
@@ -40,4 +36,15 @@ | ||
| ## Utilities | ||
| The `utils` module provides: | ||
| - `buildURL(baseURL, params)` - Build URL with query parameters | ||
| - `serializeQuery(obj)` - Serialize object to query string | ||
| - `parseQuery(queryString)` - Parse query string to object | ||
| - `deepMerge(target, source)` - Deep merge objects | ||
| - `isPlainObject(value)` - Check if value is a plain object | ||
| - `delay(ms)` - Delay execution | ||
| ## License | ||
| MIT |
+3
-1
@@ -7,5 +7,7 @@ /** | ||
| const config = require('./config'); | ||
| const utils = require('./utils'); | ||
| module.exports = { | ||
| config | ||
| config, | ||
| utils | ||
| }; |
6658
68.05%8
14.29%151
135.94%48
17.07%