@catbee/utils 0.0.8-rc.3 → 1.0.1
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/README.md +3 -2
- package/build/index.cjs +6912 -0
- package/build/index.d.ts +5284 -0
- package/build/index.mjs +6635 -0
- package/package.json +13 -292
- package/build/cjs/config.d.ts +0 -121
- package/build/cjs/config.js +0 -137
- package/build/cjs/index.d.ts +0 -26
- package/build/cjs/index.js +0 -68
- package/build/cjs/servers/server.builder.d.ts +0 -507
- package/build/cjs/servers/server.builder.js +0 -658
- package/build/cjs/servers/server.d.ts +0 -255
- package/build/cjs/servers/server.js +0 -970
- package/build/cjs/types/api-response.d.ts +0 -151
- package/build/cjs/types/api-response.js +0 -36
- package/build/cjs/types/index.d.ts +0 -124
- package/build/cjs/types/index.js +0 -25
- package/build/cjs/types/server.d.ts +0 -267
- package/build/cjs/types/server.js +0 -25
- package/build/cjs/utils/array.utils.d.ts +0 -167
- package/build/cjs/utils/array.utils.js +0 -363
- package/build/cjs/utils/async.utils.d.ts +0 -264
- package/build/cjs/utils/async.utils.js +0 -640
- package/build/cjs/utils/cache.utils.d.ts +0 -152
- package/build/cjs/utils/cache.utils.js +0 -298
- package/build/cjs/utils/context-store.utils.d.ts +0 -188
- package/build/cjs/utils/context-store.utils.js +0 -297
- package/build/cjs/utils/crypto.utils.d.ts +0 -159
- package/build/cjs/utils/crypto.utils.js +0 -295
- package/build/cjs/utils/date.utils.d.ts +0 -158
- package/build/cjs/utils/date.utils.js +0 -395
- package/build/cjs/utils/decorators.utils.d.ts +0 -511
- package/build/cjs/utils/decorators.utils.js +0 -1035
- package/build/cjs/utils/dir.utils.d.ts +0 -195
- package/build/cjs/utils/dir.utils.js +0 -500
- package/build/cjs/utils/env.utils.d.ts +0 -376
- package/build/cjs/utils/env.utils.js +0 -786
- package/build/cjs/utils/exception.utils.d.ts +0 -229
- package/build/cjs/utils/exception.utils.js +0 -408
- package/build/cjs/utils/fs.utils.d.ts +0 -163
- package/build/cjs/utils/fs.utils.js +0 -371
- package/build/cjs/utils/http-status-codes.d.ts +0 -265
- package/build/cjs/utils/http-status-codes.js +0 -297
- package/build/cjs/utils/id.utils.d.ts +0 -35
- package/build/cjs/utils/id.utils.js +0 -90
- package/build/cjs/utils/logger.utils.d.ts +0 -159
- package/build/cjs/utils/logger.utils.js +0 -350
- package/build/cjs/utils/middleware.utils.d.ts +0 -99
- package/build/cjs/utils/middleware.utils.js +0 -243
- package/build/cjs/utils/obj.utils.d.ts +0 -123
- package/build/cjs/utils/obj.utils.js +0 -428
- package/build/cjs/utils/performance.utils.d.ts +0 -135
- package/build/cjs/utils/performance.utils.js +0 -280
- package/build/cjs/utils/request.utils.d.ts +0 -85
- package/build/cjs/utils/request.utils.js +0 -199
- package/build/cjs/utils/response.utils.d.ts +0 -162
- package/build/cjs/utils/response.utils.js +0 -274
- package/build/cjs/utils/stream.utils.d.ts +0 -87
- package/build/cjs/utils/stream.utils.js +0 -217
- package/build/cjs/utils/string.utils.d.ts +0 -92
- package/build/cjs/utils/string.utils.js +0 -178
- package/build/cjs/utils/type.utils.d.ts +0 -89
- package/build/cjs/utils/type.utils.js +0 -195
- package/build/cjs/utils/url.utils.d.ts +0 -140
- package/build/cjs/utils/url.utils.js +0 -316
- package/build/cjs/utils/validate.utils.d.ts +0 -176
- package/build/cjs/utils/validate.utils.js +0 -344
- package/build/esm/config.d.ts +0 -121
- package/build/esm/config.js +0 -132
- package/build/esm/index.d.ts +0 -26
- package/build/esm/index.js +0 -49
- package/build/esm/servers/server.builder.d.ts +0 -507
- package/build/esm/servers/server.builder.js +0 -654
- package/build/esm/servers/server.d.ts +0 -255
- package/build/esm/servers/server.js +0 -963
- package/build/esm/types/api-response.d.ts +0 -151
- package/build/esm/types/api-response.js +0 -33
- package/build/esm/types/index.d.ts +0 -124
- package/build/esm/types/index.js +0 -24
- package/build/esm/types/server.d.ts +0 -267
- package/build/esm/types/server.js +0 -24
- package/build/esm/utils/array.utils.d.ts +0 -167
- package/build/esm/utils/array.utils.js +0 -344
- package/build/esm/utils/async.utils.d.ts +0 -264
- package/build/esm/utils/async.utils.js +0 -619
- package/build/esm/utils/cache.utils.d.ts +0 -152
- package/build/esm/utils/cache.utils.js +0 -294
- package/build/esm/utils/context-store.utils.d.ts +0 -188
- package/build/esm/utils/context-store.utils.js +0 -290
- package/build/esm/utils/crypto.utils.d.ts +0 -159
- package/build/esm/utils/crypto.utils.js +0 -278
- package/build/esm/utils/date.utils.d.ts +0 -158
- package/build/esm/utils/date.utils.js +0 -383
- package/build/esm/utils/decorators.utils.d.ts +0 -511
- package/build/esm/utils/decorators.utils.js +0 -1013
- package/build/esm/utils/dir.utils.d.ts +0 -195
- package/build/esm/utils/dir.utils.js +0 -476
- package/build/esm/utils/env.utils.d.ts +0 -376
- package/build/esm/utils/env.utils.js +0 -782
- package/build/esm/utils/exception.utils.d.ts +0 -229
- package/build/esm/utils/exception.utils.js +0 -382
- package/build/esm/utils/fs.utils.d.ts +0 -163
- package/build/esm/utils/fs.utils.js +0 -348
- package/build/esm/utils/http-status-codes.d.ts +0 -265
- package/build/esm/utils/http-status-codes.js +0 -294
- package/build/esm/utils/id.utils.d.ts +0 -35
- package/build/esm/utils/id.utils.js +0 -83
- package/build/esm/utils/logger.utils.d.ts +0 -159
- package/build/esm/utils/logger.utils.js +0 -304
- package/build/esm/utils/middleware.utils.d.ts +0 -99
- package/build/esm/utils/middleware.utils.js +0 -235
- package/build/esm/utils/obj.utils.d.ts +0 -123
- package/build/esm/utils/obj.utils.js +0 -412
- package/build/esm/utils/performance.utils.d.ts +0 -135
- package/build/esm/utils/performance.utils.js +0 -273
- package/build/esm/utils/request.utils.d.ts +0 -85
- package/build/esm/utils/request.utils.js +0 -190
- package/build/esm/utils/response.utils.d.ts +0 -162
- package/build/esm/utils/response.utils.js +0 -261
- package/build/esm/utils/stream.utils.d.ts +0 -87
- package/build/esm/utils/stream.utils.js +0 -209
- package/build/esm/utils/string.utils.d.ts +0 -92
- package/build/esm/utils/string.utils.js +0 -164
- package/build/esm/utils/type.utils.d.ts +0 -89
- package/build/esm/utils/type.utils.js +0 -186
- package/build/esm/utils/url.utils.d.ts +0 -140
- package/build/esm/utils/url.utils.js +0 -303
- package/build/esm/utils/validate.utils.d.ts +0 -176
- package/build/esm/utils/validate.utils.js +0 -319
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* The MIT License
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
6
|
-
*
|
|
7
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
* in the Software without restriction, including without limitation the rights
|
|
10
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
* furnished to do so, subject to the following conditions:
|
|
13
|
-
*
|
|
14
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
* copies or substantial portions of the Software.
|
|
16
|
-
*
|
|
17
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
* SOFTWARE.
|
|
24
|
-
*/
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.isPrimitiveType = isPrimitiveType;
|
|
27
|
-
exports.getTypeOf = getTypeOf;
|
|
28
|
-
exports.isArrayOf = isArrayOf;
|
|
29
|
-
exports.toStr = toStr;
|
|
30
|
-
exports.toNum = toNum;
|
|
31
|
-
exports.toBool = toBool;
|
|
32
|
-
exports.ensureType = ensureType;
|
|
33
|
-
/**
|
|
34
|
-
* Check if a value is of a specific primitive type.
|
|
35
|
-
*
|
|
36
|
-
* @param value - Value to check
|
|
37
|
-
* @param type - Type to check against
|
|
38
|
-
* @returns Whether the value is of the specified type
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```typescript
|
|
42
|
-
* isPrimitiveType('hello', 'string'); // true
|
|
43
|
-
* isPrimitiveType(42, 'number'); // true
|
|
44
|
-
* isPrimitiveType(true, 'boolean'); // true
|
|
45
|
-
* isPrimitiveType(null, 'null'); // true
|
|
46
|
-
* isPrimitiveType(undefined, 'undefined'); // true
|
|
47
|
-
* isPrimitiveType({}, 'object'); // true
|
|
48
|
-
* isPrimitiveType([], 'array'); // true
|
|
49
|
-
* ```
|
|
50
|
-
*/
|
|
51
|
-
function isPrimitiveType(value, type) {
|
|
52
|
-
if (type === 'array') {
|
|
53
|
-
return Array.isArray(value);
|
|
54
|
-
}
|
|
55
|
-
if (type === 'null') {
|
|
56
|
-
return value === null;
|
|
57
|
-
}
|
|
58
|
-
if (type === 'undefined') {
|
|
59
|
-
return value === undefined;
|
|
60
|
-
}
|
|
61
|
-
if (type === 'object') {
|
|
62
|
-
// Only plain objects, not arrays or null
|
|
63
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
64
|
-
}
|
|
65
|
-
return typeof value === type;
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Get the primitive type of a value as a string.
|
|
69
|
-
*
|
|
70
|
-
* @param value - Value to get the type of
|
|
71
|
-
* @returns String representing the type
|
|
72
|
-
*
|
|
73
|
-
* @example
|
|
74
|
-
* ```typescript
|
|
75
|
-
* getTypeOf('hello'); // 'string'
|
|
76
|
-
* getTypeOf(42); // 'number'
|
|
77
|
-
* getTypeOf([]); // 'array'
|
|
78
|
-
* getTypeOf(null); // 'null'
|
|
79
|
-
* ```
|
|
80
|
-
*/
|
|
81
|
-
function getTypeOf(value) {
|
|
82
|
-
if (value === null)
|
|
83
|
-
return 'null';
|
|
84
|
-
if (value === undefined)
|
|
85
|
-
return 'undefined';
|
|
86
|
-
if (Array.isArray(value))
|
|
87
|
-
return 'array';
|
|
88
|
-
return typeof value;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Type guard for checking if a value is an array of a specific type.
|
|
92
|
-
*
|
|
93
|
-
* @param value - Value to check
|
|
94
|
-
* @param itemTypeGuard - Function that checks if items are of the expected type
|
|
95
|
-
* @returns True if the value is an array with items of the expected type
|
|
96
|
-
*
|
|
97
|
-
* @example
|
|
98
|
-
* ```typescript
|
|
99
|
-
* isArrayOf([1, 2, 3], (item): item is number => typeof item === 'number'); // true
|
|
100
|
-
* isArrayOf(['a', 'b', 'c'], (item): item is string => typeof item === 'string'); // true
|
|
101
|
-
* isArrayOf([1, '2', 3], (item): item is number => typeof item === 'number'); // false
|
|
102
|
-
* ```
|
|
103
|
-
*/
|
|
104
|
-
function isArrayOf(value, itemTypeGuard) {
|
|
105
|
-
if (!Array.isArray(value))
|
|
106
|
-
return false;
|
|
107
|
-
return value.every(item => itemTypeGuard(item));
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* Convert a value to a string.
|
|
111
|
-
*
|
|
112
|
-
* @param value - Value to convert
|
|
113
|
-
* @param defaultValue - Default value if conversion fails
|
|
114
|
-
* @returns String representation of the value
|
|
115
|
-
*/
|
|
116
|
-
function toStr(value, defaultValue = '') {
|
|
117
|
-
if (value === null || value === undefined)
|
|
118
|
-
return defaultValue;
|
|
119
|
-
try {
|
|
120
|
-
if (typeof value === 'object') {
|
|
121
|
-
return JSON.stringify(value);
|
|
122
|
-
}
|
|
123
|
-
return String(value);
|
|
124
|
-
}
|
|
125
|
-
catch {
|
|
126
|
-
return defaultValue;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
/**
|
|
130
|
-
* Convert a value to a number.
|
|
131
|
-
*
|
|
132
|
-
* @param value - Value to convert
|
|
133
|
-
* @param defaultValue - Default value if conversion fails
|
|
134
|
-
* @returns Numeric representation of the value
|
|
135
|
-
*/
|
|
136
|
-
function toNum(value, defaultValue = 0) {
|
|
137
|
-
if (value === null || value === undefined)
|
|
138
|
-
return defaultValue;
|
|
139
|
-
if (typeof value === 'number')
|
|
140
|
-
return value;
|
|
141
|
-
try {
|
|
142
|
-
const num = Number(value);
|
|
143
|
-
return isNaN(num) ? defaultValue : num;
|
|
144
|
-
}
|
|
145
|
-
catch {
|
|
146
|
-
return defaultValue;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Convert a value to a boolean.
|
|
151
|
-
*
|
|
152
|
-
* @param value - Value to convert
|
|
153
|
-
* @param defaultValue - Default value if conversion fails
|
|
154
|
-
* @returns Boolean representation of the value
|
|
155
|
-
*/
|
|
156
|
-
function toBool(value, defaultValue = false) {
|
|
157
|
-
if (value === null || value === undefined)
|
|
158
|
-
return defaultValue;
|
|
159
|
-
if (typeof value === 'boolean')
|
|
160
|
-
return value;
|
|
161
|
-
if (typeof value === 'string') {
|
|
162
|
-
const lowercased = value.toLowerCase();
|
|
163
|
-
if (lowercased === 'true' || lowercased === 'yes' || lowercased === 'y' || lowercased === '1') {
|
|
164
|
-
return true;
|
|
165
|
-
}
|
|
166
|
-
if (lowercased === 'false' || lowercased === 'no' || lowercased === 'n' || lowercased === '0') {
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
if (typeof value === 'number') {
|
|
171
|
-
return value !== 0;
|
|
172
|
-
}
|
|
173
|
-
return defaultValue;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Ensure a value matches the expected type, or provide a default.
|
|
177
|
-
*
|
|
178
|
-
* @param value - Value to check
|
|
179
|
-
* @param expectedType - Expected primitive type
|
|
180
|
-
* @param defaultValue - Default value to use if type doesn't match
|
|
181
|
-
* @returns The value if it matches the type, otherwise the default
|
|
182
|
-
*
|
|
183
|
-
* @example
|
|
184
|
-
* ```typescript
|
|
185
|
-
* ensureType(42, 'number', 0); // 42
|
|
186
|
-
* ensureType('42', 'number', 0); // 0
|
|
187
|
-
* ensureType(undefined, 'string', 'default'); // 'default'
|
|
188
|
-
* ```
|
|
189
|
-
*/
|
|
190
|
-
function ensureType(value, expectedType, defaultValue) {
|
|
191
|
-
if (getTypeOf(value) === expectedType) {
|
|
192
|
-
return value;
|
|
193
|
-
}
|
|
194
|
-
return defaultValue;
|
|
195
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Appends query parameters to a given URL.
|
|
3
|
-
*
|
|
4
|
-
* @param {string} url - The base URL to which query parameters will be appended.
|
|
5
|
-
* @param {Record<string, string | number>} params - Key-value pairs to add as query parameters.
|
|
6
|
-
* @returns {string} The new URL string with query parameters appended.
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* appendQueryParams('https://example.com', { page: 1, limit: 10 });
|
|
10
|
-
* // → 'https://example.com/?page=1&limit=10'
|
|
11
|
-
*/
|
|
12
|
-
export declare function appendQueryParams(url: string, params: Record<string, string | number>): string;
|
|
13
|
-
/**
|
|
14
|
-
* Parses a query string into a key-value object.
|
|
15
|
-
*
|
|
16
|
-
* @param {string} query - The query string (with or without leading '?').
|
|
17
|
-
* @returns {Record<string, string>} Object representing parsed query parameters.
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* parseQueryString('?page=1&limit=10');
|
|
21
|
-
* // → { page: '1', limit: '10' }
|
|
22
|
-
*/
|
|
23
|
-
export declare function parseQueryString(query: string): Record<string, string>;
|
|
24
|
-
/**
|
|
25
|
-
* Validates if a string is a valid URL.
|
|
26
|
-
*
|
|
27
|
-
* @param {string} url - The URL string to validate.
|
|
28
|
-
* @param {boolean} [requireHttps=false] - If true, requires the URL to use HTTPS.
|
|
29
|
-
* @returns {boolean} True if the URL is valid, false otherwise.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isValidUrl(url: string, requireHttps?: boolean): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Extracts the domain name from a URL.
|
|
34
|
-
*
|
|
35
|
-
* @param {string} url - The URL to extract the domain from.
|
|
36
|
-
* @param {boolean} [removeSubdomains=false] - If true, removes subdomains (returns root domain only).
|
|
37
|
-
* @returns {string} The domain name.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* getDomain('https://api.example.com/path');
|
|
41
|
-
* // → 'api.example.com'
|
|
42
|
-
*
|
|
43
|
-
* getDomain('https://api.example.com/path', true);
|
|
44
|
-
* // → 'example.com'
|
|
45
|
-
*/
|
|
46
|
-
export declare function getDomain(url: string, removeSubdomains?: boolean): string;
|
|
47
|
-
/**
|
|
48
|
-
* Joins URL paths properly handling slashes.
|
|
49
|
-
*
|
|
50
|
-
* @param {...string[]} segments - URL path segments to join.
|
|
51
|
-
* @returns {string} Joined URL path.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* joinPaths('https://example.com/', '/api/', '/users');
|
|
55
|
-
* // → 'https://example.com/api/users'
|
|
56
|
-
*/
|
|
57
|
-
export declare function joinPaths(...segments: string[]): string;
|
|
58
|
-
/**
|
|
59
|
-
* Normalizes a URL by resolving relative paths, handling protocol-relative URLs, etc.
|
|
60
|
-
*
|
|
61
|
-
* @param {string} url - The URL to normalize.
|
|
62
|
-
* @param {string} [base] - Optional base URL for resolving relative URLs.
|
|
63
|
-
* @returns {string} Normalized URL.
|
|
64
|
-
*
|
|
65
|
-
* @example
|
|
66
|
-
* normalizeUrl('HTTP://Example.COM/foo/../bar');
|
|
67
|
-
* // → 'http://example.com/bar'
|
|
68
|
-
*/
|
|
69
|
-
export declare function normalizeUrl(url: string, base?: string): string;
|
|
70
|
-
/**
|
|
71
|
-
* Creates a URL builder for constructing URLs with a base URL.
|
|
72
|
-
*
|
|
73
|
-
* @param {string} baseUrl - The base URL to build upon.
|
|
74
|
-
* @returns {Object} URL builder methods.
|
|
75
|
-
*
|
|
76
|
-
* @example
|
|
77
|
-
* const api = createUrlBuilder('https://api.example.com');
|
|
78
|
-
* api.path('/users', { active: true });
|
|
79
|
-
* // → 'https://api.example.com/users?active=true'
|
|
80
|
-
*/
|
|
81
|
-
export declare function createUrlBuilder(baseUrl: string): {
|
|
82
|
-
/**
|
|
83
|
-
* Creates a full URL with the given path and query parameters.
|
|
84
|
-
*
|
|
85
|
-
* @param {string} path - The path to append to the base URL.
|
|
86
|
-
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
87
|
-
* @returns {string} The complete URL.
|
|
88
|
-
*/
|
|
89
|
-
path(path: string, params?: Record<string, any>): string;
|
|
90
|
-
/**
|
|
91
|
-
* Creates a full URL with query parameters but no additional path.
|
|
92
|
-
*
|
|
93
|
-
* @param {Record<string, any>} params - Query parameters to add.
|
|
94
|
-
* @returns {string} The complete URL with query parameters.
|
|
95
|
-
*/
|
|
96
|
-
query(params: Record<string, any>): string;
|
|
97
|
-
};
|
|
98
|
-
/**
|
|
99
|
-
* Extracts specific query parameters from a URL.
|
|
100
|
-
*
|
|
101
|
-
* @param {string} url - The URL to extract parameters from.
|
|
102
|
-
* @param {string[]} paramNames - Names of parameters to extract.
|
|
103
|
-
* @returns {Record<string, string>} Object containing the extracted parameters.
|
|
104
|
-
*/
|
|
105
|
-
export declare function extractQueryParams(url: string, paramNames: string[]): Record<string, string>;
|
|
106
|
-
/**
|
|
107
|
-
* Removes specified query parameters from a URL.
|
|
108
|
-
*
|
|
109
|
-
* @param {string} url - The URL to modify.
|
|
110
|
-
* @param {string[]} paramsToRemove - Names of parameters to remove.
|
|
111
|
-
* @returns {string} URL with parameters removed.
|
|
112
|
-
*/
|
|
113
|
-
export declare function removeQueryParams(url: string, paramsToRemove: string[]): string;
|
|
114
|
-
/**
|
|
115
|
-
* Gets the file extension from a URL path.
|
|
116
|
-
*
|
|
117
|
-
* @param {string} url - The URL to examine.
|
|
118
|
-
* @returns {string} The file extension (without dot) or empty string if none found.
|
|
119
|
-
*
|
|
120
|
-
* @example
|
|
121
|
-
* getExtension('https://example.com/document.pdf?v=1');
|
|
122
|
-
* // → 'pdf'
|
|
123
|
-
*/
|
|
124
|
-
export declare function getExtension(url: string): string;
|
|
125
|
-
/**
|
|
126
|
-
* Parses URL query parameters into a strongly-typed object.
|
|
127
|
-
*
|
|
128
|
-
* @template T Expected type of the query parameters
|
|
129
|
-
* @param {string} url - The URL to parse.
|
|
130
|
-
* @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.
|
|
131
|
-
* @returns {Partial<T>} Typed query parameters.
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {
|
|
135
|
-
* page: Number,
|
|
136
|
-
* q: String
|
|
137
|
-
* });
|
|
138
|
-
* // → { page: 2, q: 'test' }
|
|
139
|
-
*/
|
|
140
|
-
export declare function parseTypedQueryParams<T extends Record<string, any>>(url: string, converters?: Record<keyof T, (val: string) => any>): Partial<T>;
|
|
@@ -1,316 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* The MIT License
|
|
4
|
-
*
|
|
5
|
-
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
|
|
6
|
-
*
|
|
7
|
-
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
-
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
-
* in the Software without restriction, including without limitation the rights
|
|
10
|
-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
-
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
-
* furnished to do so, subject to the following conditions:
|
|
13
|
-
*
|
|
14
|
-
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
-
* copies or substantial portions of the Software.
|
|
16
|
-
*
|
|
17
|
-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
-
* SOFTWARE.
|
|
24
|
-
*/
|
|
25
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
-
exports.appendQueryParams = appendQueryParams;
|
|
27
|
-
exports.parseQueryString = parseQueryString;
|
|
28
|
-
exports.isValidUrl = isValidUrl;
|
|
29
|
-
exports.getDomain = getDomain;
|
|
30
|
-
exports.joinPaths = joinPaths;
|
|
31
|
-
exports.normalizeUrl = normalizeUrl;
|
|
32
|
-
exports.createUrlBuilder = createUrlBuilder;
|
|
33
|
-
exports.extractQueryParams = extractQueryParams;
|
|
34
|
-
exports.removeQueryParams = removeQueryParams;
|
|
35
|
-
exports.getExtension = getExtension;
|
|
36
|
-
exports.parseTypedQueryParams = parseTypedQueryParams;
|
|
37
|
-
const url_1 = require("url");
|
|
38
|
-
/**
|
|
39
|
-
* Appends query parameters to a given URL.
|
|
40
|
-
*
|
|
41
|
-
* @param {string} url - The base URL to which query parameters will be appended.
|
|
42
|
-
* @param {Record<string, string | number>} params - Key-value pairs to add as query parameters.
|
|
43
|
-
* @returns {string} The new URL string with query parameters appended.
|
|
44
|
-
*
|
|
45
|
-
* @example
|
|
46
|
-
* appendQueryParams('https://example.com', { page: 1, limit: 10 });
|
|
47
|
-
* // → 'https://example.com/?page=1&limit=10'
|
|
48
|
-
*/
|
|
49
|
-
function appendQueryParams(url, params) {
|
|
50
|
-
const urlObj = new url_1.URL(url);
|
|
51
|
-
for (const [key, value] of Object.entries(params)) {
|
|
52
|
-
urlObj.searchParams.set(key, value.toString());
|
|
53
|
-
}
|
|
54
|
-
return urlObj.toString();
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Parses a query string into a key-value object.
|
|
58
|
-
*
|
|
59
|
-
* @param {string} query - The query string (with or without leading '?').
|
|
60
|
-
* @returns {Record<string, string>} Object representing parsed query parameters.
|
|
61
|
-
*
|
|
62
|
-
* @example
|
|
63
|
-
* parseQueryString('?page=1&limit=10');
|
|
64
|
-
* // → { page: '1', limit: '10' }
|
|
65
|
-
*/
|
|
66
|
-
function parseQueryString(query) {
|
|
67
|
-
const cleanQuery = query.startsWith('?') ? query.slice(1) : query;
|
|
68
|
-
// Object.fromEntries guarantees string values in result
|
|
69
|
-
return Object.fromEntries(new url_1.URLSearchParams(cleanQuery));
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Validates if a string is a valid URL.
|
|
73
|
-
*
|
|
74
|
-
* @param {string} url - The URL string to validate.
|
|
75
|
-
* @param {boolean} [requireHttps=false] - If true, requires the URL to use HTTPS.
|
|
76
|
-
* @returns {boolean} True if the URL is valid, false otherwise.
|
|
77
|
-
*/
|
|
78
|
-
function isValidUrl(url, requireHttps = false) {
|
|
79
|
-
try {
|
|
80
|
-
const parsedUrl = new url_1.URL(url);
|
|
81
|
-
if (!parsedUrl.protocol || !parsedUrl.hostname)
|
|
82
|
-
return false;
|
|
83
|
-
if (requireHttps && parsedUrl.protocol !== 'https:')
|
|
84
|
-
return false;
|
|
85
|
-
return true;
|
|
86
|
-
}
|
|
87
|
-
catch {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
/**
|
|
92
|
-
* Extracts the domain name from a URL.
|
|
93
|
-
*
|
|
94
|
-
* @param {string} url - The URL to extract the domain from.
|
|
95
|
-
* @param {boolean} [removeSubdomains=false] - If true, removes subdomains (returns root domain only).
|
|
96
|
-
* @returns {string} The domain name.
|
|
97
|
-
*
|
|
98
|
-
* @example
|
|
99
|
-
* getDomain('https://api.example.com/path');
|
|
100
|
-
* // → 'api.example.com'
|
|
101
|
-
*
|
|
102
|
-
* getDomain('https://api.example.com/path', true);
|
|
103
|
-
* // → 'example.com'
|
|
104
|
-
*/
|
|
105
|
-
function getDomain(url, removeSubdomains = false) {
|
|
106
|
-
try {
|
|
107
|
-
const { hostname } = new url_1.URL(url);
|
|
108
|
-
if (!removeSubdomains) {
|
|
109
|
-
return hostname;
|
|
110
|
-
}
|
|
111
|
-
// Extract root domain (remove subdomains)
|
|
112
|
-
const parts = hostname.split('.');
|
|
113
|
-
if (parts.length <= 2)
|
|
114
|
-
return hostname;
|
|
115
|
-
// Handle special cases like co.in, com.au
|
|
116
|
-
const secondLevelDomains = ['co', 'com', 'org', 'net', 'gov', 'edu', 'ac'];
|
|
117
|
-
const sld = parts[parts.length - 2];
|
|
118
|
-
if (secondLevelDomains.includes(sld) && parts.length > 2) {
|
|
119
|
-
return parts.slice(-3).join('.');
|
|
120
|
-
}
|
|
121
|
-
return parts.slice(-2).join('.');
|
|
122
|
-
}
|
|
123
|
-
catch {
|
|
124
|
-
return '';
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Joins URL paths properly handling slashes.
|
|
129
|
-
*
|
|
130
|
-
* @param {...string[]} segments - URL path segments to join.
|
|
131
|
-
* @returns {string} Joined URL path.
|
|
132
|
-
*
|
|
133
|
-
* @example
|
|
134
|
-
* joinPaths('https://example.com/', '/api/', '/users');
|
|
135
|
-
* // → 'https://example.com/api/users'
|
|
136
|
-
*/
|
|
137
|
-
function joinPaths(...segments) {
|
|
138
|
-
let result = segments
|
|
139
|
-
.filter(s => s !== undefined && s !== null)
|
|
140
|
-
.map((segment, index) => {
|
|
141
|
-
if (index === 0)
|
|
142
|
-
return segment.replace(/\/+$/, '');
|
|
143
|
-
return segment.replace(/^\/+|\/+$/g, '');
|
|
144
|
-
})
|
|
145
|
-
.filter(Boolean)
|
|
146
|
-
.join('/');
|
|
147
|
-
if (segments[0] === '' && !result.startsWith('/'))
|
|
148
|
-
result = '/' + result;
|
|
149
|
-
return result;
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Normalizes a URL by resolving relative paths, handling protocol-relative URLs, etc.
|
|
153
|
-
*
|
|
154
|
-
* @param {string} url - The URL to normalize.
|
|
155
|
-
* @param {string} [base] - Optional base URL for resolving relative URLs.
|
|
156
|
-
* @returns {string} Normalized URL.
|
|
157
|
-
*
|
|
158
|
-
* @example
|
|
159
|
-
* normalizeUrl('HTTP://Example.COM/foo/../bar');
|
|
160
|
-
* // → 'http://example.com/bar'
|
|
161
|
-
*/
|
|
162
|
-
function normalizeUrl(url, base) {
|
|
163
|
-
try {
|
|
164
|
-
// Handle protocol-relative URLs
|
|
165
|
-
if (url.startsWith('//')) {
|
|
166
|
-
url = `https:${url}`;
|
|
167
|
-
}
|
|
168
|
-
// Resolve relative URLs against a base
|
|
169
|
-
const parsedUrl = base ? new url_1.URL(url, base) : new url_1.URL(url);
|
|
170
|
-
// Normalize
|
|
171
|
-
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+/g, '/'); // Collapse multiple slashes
|
|
172
|
-
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, ''); // Remove trailing slash
|
|
173
|
-
parsedUrl.hostname = parsedUrl.hostname.toLowerCase();
|
|
174
|
-
return parsedUrl.toString();
|
|
175
|
-
}
|
|
176
|
-
catch {
|
|
177
|
-
return url; // Return original if invalid
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Creates a URL builder for constructing URLs with a base URL.
|
|
182
|
-
*
|
|
183
|
-
* @param {string} baseUrl - The base URL to build upon.
|
|
184
|
-
* @returns {Object} URL builder methods.
|
|
185
|
-
*
|
|
186
|
-
* @example
|
|
187
|
-
* const api = createUrlBuilder('https://api.example.com');
|
|
188
|
-
* api.path('/users', { active: true });
|
|
189
|
-
* // → 'https://api.example.com/users?active=true'
|
|
190
|
-
*/
|
|
191
|
-
function createUrlBuilder(baseUrl) {
|
|
192
|
-
return {
|
|
193
|
-
/**
|
|
194
|
-
* Creates a full URL with the given path and query parameters.
|
|
195
|
-
*
|
|
196
|
-
* @param {string} path - The path to append to the base URL.
|
|
197
|
-
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
198
|
-
* @returns {string} The complete URL.
|
|
199
|
-
*/
|
|
200
|
-
path(path, params) {
|
|
201
|
-
const url = joinPaths(baseUrl, path);
|
|
202
|
-
return params ? appendQueryParams(url, params) : url;
|
|
203
|
-
},
|
|
204
|
-
/**
|
|
205
|
-
* Creates a full URL with query parameters but no additional path.
|
|
206
|
-
*
|
|
207
|
-
* @param {Record<string, any>} params - Query parameters to add.
|
|
208
|
-
* @returns {string} The complete URL with query parameters.
|
|
209
|
-
*/
|
|
210
|
-
query(params) {
|
|
211
|
-
return appendQueryParams(baseUrl, params);
|
|
212
|
-
}
|
|
213
|
-
};
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Extracts specific query parameters from a URL.
|
|
217
|
-
*
|
|
218
|
-
* @param {string} url - The URL to extract parameters from.
|
|
219
|
-
* @param {string[]} paramNames - Names of parameters to extract.
|
|
220
|
-
* @returns {Record<string, string>} Object containing the extracted parameters.
|
|
221
|
-
*/
|
|
222
|
-
function extractQueryParams(url, paramNames) {
|
|
223
|
-
try {
|
|
224
|
-
const parsedUrl = new url_1.URL(url);
|
|
225
|
-
const result = {};
|
|
226
|
-
for (const name of paramNames) {
|
|
227
|
-
const value = parsedUrl.searchParams.get(name);
|
|
228
|
-
if (value !== null) {
|
|
229
|
-
result[name] = value;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return result;
|
|
233
|
-
}
|
|
234
|
-
catch {
|
|
235
|
-
return {};
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
/**
|
|
239
|
-
* Removes specified query parameters from a URL.
|
|
240
|
-
*
|
|
241
|
-
* @param {string} url - The URL to modify.
|
|
242
|
-
* @param {string[]} paramsToRemove - Names of parameters to remove.
|
|
243
|
-
* @returns {string} URL with parameters removed.
|
|
244
|
-
*/
|
|
245
|
-
function removeQueryParams(url, paramsToRemove) {
|
|
246
|
-
try {
|
|
247
|
-
const parsedUrl = new url_1.URL(url);
|
|
248
|
-
for (const param of paramsToRemove) {
|
|
249
|
-
parsedUrl.searchParams.delete(param);
|
|
250
|
-
}
|
|
251
|
-
return parsedUrl.toString();
|
|
252
|
-
}
|
|
253
|
-
catch {
|
|
254
|
-
return url;
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
/**
|
|
258
|
-
* Gets the file extension from a URL path.
|
|
259
|
-
*
|
|
260
|
-
* @param {string} url - The URL to examine.
|
|
261
|
-
* @returns {string} The file extension (without dot) or empty string if none found.
|
|
262
|
-
*
|
|
263
|
-
* @example
|
|
264
|
-
* getExtension('https://example.com/document.pdf?v=1');
|
|
265
|
-
* // → 'pdf'
|
|
266
|
-
*/
|
|
267
|
-
function getExtension(url) {
|
|
268
|
-
try {
|
|
269
|
-
const { pathname } = new url_1.URL(url);
|
|
270
|
-
const match = pathname.match(/\.([^./\\?#]+)$/);
|
|
271
|
-
return match ? match[1].toLowerCase() : '';
|
|
272
|
-
}
|
|
273
|
-
catch {
|
|
274
|
-
// If URL parsing fails, try a simpler approach
|
|
275
|
-
const match = url.match(/\.([^./\\?#]+)$/);
|
|
276
|
-
return match ? match[1].toLowerCase() : '';
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
/**
|
|
280
|
-
* Parses URL query parameters into a strongly-typed object.
|
|
281
|
-
*
|
|
282
|
-
* @template T Expected type of the query parameters
|
|
283
|
-
* @param {string} url - The URL to parse.
|
|
284
|
-
* @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.
|
|
285
|
-
* @returns {Partial<T>} Typed query parameters.
|
|
286
|
-
*
|
|
287
|
-
* @example
|
|
288
|
-
* parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {
|
|
289
|
-
* page: Number,
|
|
290
|
-
* q: String
|
|
291
|
-
* });
|
|
292
|
-
* // → { page: 2, q: 'test' }
|
|
293
|
-
*/
|
|
294
|
-
function parseTypedQueryParams(url, converters) {
|
|
295
|
-
try {
|
|
296
|
-
const { searchParams } = new url_1.URL(url);
|
|
297
|
-
const result = {};
|
|
298
|
-
for (const [key, value] of searchParams.entries()) {
|
|
299
|
-
if (converters && key in converters) {
|
|
300
|
-
try {
|
|
301
|
-
result[key] = converters[key](value);
|
|
302
|
-
}
|
|
303
|
-
catch {
|
|
304
|
-
// Skip on conversion error
|
|
305
|
-
}
|
|
306
|
-
}
|
|
307
|
-
else {
|
|
308
|
-
result[key] = value;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
return result;
|
|
312
|
-
}
|
|
313
|
-
catch {
|
|
314
|
-
return {};
|
|
315
|
-
}
|
|
316
|
-
}
|