@catbee/utils 1.1.0 → 2.0.0-next.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/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
package/types/index.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
// src/types/api-response.ts
|
|
26
|
+
var SortDirection = /* @__PURE__ */ (function(SortDirection2) {
|
|
27
|
+
SortDirection2["ASC"] = "asc";
|
|
28
|
+
SortDirection2["DESC"] = "desc";
|
|
29
|
+
return SortDirection2;
|
|
30
|
+
})({});
|
|
31
|
+
|
|
32
|
+
export { SortDirection };
|
package/url/index.cjs
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var url = require('url');
|
|
28
|
+
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
30
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
function appendQueryParams(url$1, params) {
|
|
32
|
+
const urlObj = new url.URL(url$1);
|
|
33
|
+
for (const [key, value] of Object.entries(params)) {
|
|
34
|
+
urlObj.searchParams.set(key, value.toString());
|
|
35
|
+
}
|
|
36
|
+
return urlObj.toString();
|
|
37
|
+
}
|
|
38
|
+
__name(appendQueryParams, "appendQueryParams");
|
|
39
|
+
function parseQueryString(query) {
|
|
40
|
+
const cleanQuery = query.startsWith("?") ? query.slice(1) : query;
|
|
41
|
+
return Object.fromEntries(new url.URLSearchParams(cleanQuery));
|
|
42
|
+
}
|
|
43
|
+
__name(parseQueryString, "parseQueryString");
|
|
44
|
+
function isValidUrl(url$1, requireHttps = false) {
|
|
45
|
+
try {
|
|
46
|
+
const parsedUrl = new url.URL(url$1);
|
|
47
|
+
if (!parsedUrl.protocol || !parsedUrl.hostname) return false;
|
|
48
|
+
if (requireHttps && parsedUrl.protocol !== "https:") return false;
|
|
49
|
+
return true;
|
|
50
|
+
} catch {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
__name(isValidUrl, "isValidUrl");
|
|
55
|
+
function getDomain(url$1, removeSubdomains = false) {
|
|
56
|
+
try {
|
|
57
|
+
const { hostname } = new url.URL(url$1);
|
|
58
|
+
if (!removeSubdomains) {
|
|
59
|
+
return hostname;
|
|
60
|
+
}
|
|
61
|
+
const parts = hostname.split(".");
|
|
62
|
+
if (parts.length <= 2) return hostname;
|
|
63
|
+
const secondLevelDomains = [
|
|
64
|
+
"co",
|
|
65
|
+
"com",
|
|
66
|
+
"org",
|
|
67
|
+
"net",
|
|
68
|
+
"gov",
|
|
69
|
+
"edu",
|
|
70
|
+
"ac"
|
|
71
|
+
];
|
|
72
|
+
const sld = parts[parts.length - 2];
|
|
73
|
+
if (secondLevelDomains.includes(sld) && parts.length > 2) {
|
|
74
|
+
return parts.slice(-3).join(".");
|
|
75
|
+
}
|
|
76
|
+
return parts.slice(-2).join(".");
|
|
77
|
+
} catch {
|
|
78
|
+
return "";
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
__name(getDomain, "getDomain");
|
|
82
|
+
function joinPaths(...segments) {
|
|
83
|
+
let result = segments.filter((s) => s !== void 0 && s !== null).map((segment, index) => {
|
|
84
|
+
if (index === 0) return segment.replace(/\/+$/, "");
|
|
85
|
+
return segment.replace(/^\/+|\/+$/g, "");
|
|
86
|
+
}).filter(Boolean).join("/");
|
|
87
|
+
if (segments[0] === "" && !result.startsWith("/")) result = "/" + result;
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
__name(joinPaths, "joinPaths");
|
|
91
|
+
function normalizeUrl(url$1, base) {
|
|
92
|
+
try {
|
|
93
|
+
let normalized = url$1.startsWith("//") ? `https:${url$1}` : url$1;
|
|
94
|
+
const parsedUrl = base ? new url.URL(normalized, base) : new url.URL(normalized);
|
|
95
|
+
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+/g, "/");
|
|
96
|
+
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, "");
|
|
97
|
+
parsedUrl.hostname = parsedUrl.hostname.toLowerCase();
|
|
98
|
+
return parsedUrl.toString();
|
|
99
|
+
} catch {
|
|
100
|
+
return url$1;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
__name(normalizeUrl, "normalizeUrl");
|
|
104
|
+
function createUrlBuilder(baseUrl) {
|
|
105
|
+
return {
|
|
106
|
+
/**
|
|
107
|
+
* Creates a full URL with the given path and query parameters.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} path - The path to append to the base URL.
|
|
110
|
+
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
111
|
+
* @returns {string} The complete URL.
|
|
112
|
+
*/
|
|
113
|
+
path(path, params) {
|
|
114
|
+
const url = joinPaths(baseUrl, path);
|
|
115
|
+
return params ? appendQueryParams(url, params) : url;
|
|
116
|
+
},
|
|
117
|
+
/**
|
|
118
|
+
* Creates a full URL with query parameters but no additional path.
|
|
119
|
+
*
|
|
120
|
+
* @param {Record<string, any>} params - Query parameters to add.
|
|
121
|
+
* @returns {string} The complete URL with query parameters.
|
|
122
|
+
*/
|
|
123
|
+
query(params) {
|
|
124
|
+
return appendQueryParams(baseUrl, params);
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
__name(createUrlBuilder, "createUrlBuilder");
|
|
129
|
+
function extractQueryParams(url$1, paramNames) {
|
|
130
|
+
try {
|
|
131
|
+
const parsedUrl = new url.URL(url$1);
|
|
132
|
+
const result = {};
|
|
133
|
+
for (const name of paramNames) {
|
|
134
|
+
const value = parsedUrl.searchParams.get(name);
|
|
135
|
+
if (value !== null) {
|
|
136
|
+
result[name] = value;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
} catch {
|
|
141
|
+
return {};
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
__name(extractQueryParams, "extractQueryParams");
|
|
145
|
+
function removeQueryParams(url$1, paramsToRemove) {
|
|
146
|
+
try {
|
|
147
|
+
const parsedUrl = new url.URL(url$1);
|
|
148
|
+
for (const param of paramsToRemove) {
|
|
149
|
+
parsedUrl.searchParams.delete(param);
|
|
150
|
+
}
|
|
151
|
+
return parsedUrl.toString();
|
|
152
|
+
} catch {
|
|
153
|
+
return url$1;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
__name(removeQueryParams, "removeQueryParams");
|
|
157
|
+
function getExtension(url$1) {
|
|
158
|
+
try {
|
|
159
|
+
const { pathname } = new url.URL(url$1);
|
|
160
|
+
const match = pathname.match(/\.([^./\\?#]+)$/);
|
|
161
|
+
return match ? match[1].toLowerCase() : "";
|
|
162
|
+
} catch {
|
|
163
|
+
const match = url$1.match(/\.([^./\\?#]+)$/);
|
|
164
|
+
return match ? match[1].toLowerCase() : "";
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
__name(getExtension, "getExtension");
|
|
168
|
+
function parseTypedQueryParams(url$1, converters) {
|
|
169
|
+
try {
|
|
170
|
+
const { searchParams } = new url.URL(url$1);
|
|
171
|
+
const result = {};
|
|
172
|
+
for (const [key, value] of searchParams.entries()) {
|
|
173
|
+
if (converters && key in converters) {
|
|
174
|
+
try {
|
|
175
|
+
result[key] = converters[key](value);
|
|
176
|
+
} catch {
|
|
177
|
+
}
|
|
178
|
+
} else {
|
|
179
|
+
result[key] = value;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return result;
|
|
183
|
+
} catch {
|
|
184
|
+
return {};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
__name(parseTypedQueryParams, "parseTypedQueryParams");
|
|
188
|
+
|
|
189
|
+
exports.appendQueryParams = appendQueryParams;
|
|
190
|
+
exports.createUrlBuilder = createUrlBuilder;
|
|
191
|
+
exports.extractQueryParams = extractQueryParams;
|
|
192
|
+
exports.getDomain = getDomain;
|
|
193
|
+
exports.getExtension = getExtension;
|
|
194
|
+
exports.isValidUrl = isValidUrl;
|
|
195
|
+
exports.joinPaths = joinPaths;
|
|
196
|
+
exports.normalizeUrl = normalizeUrl;
|
|
197
|
+
exports.parseQueryString = parseQueryString;
|
|
198
|
+
exports.parseTypedQueryParams = parseTypedQueryParams;
|
|
199
|
+
exports.removeQueryParams = removeQueryParams;
|
package/url/index.d.ts
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Appends query parameters to a given URL.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} url - The base URL to which query parameters will be appended.
|
|
29
|
+
* @param {Record<string, string | number>} params - Key-value pairs to add as query parameters.
|
|
30
|
+
* @returns {string} The new URL string with query parameters appended.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* appendQueryParams('https://example.com', { page: 1, limit: 10 });
|
|
34
|
+
* // -> 'https://example.com/?page=1&limit=10'
|
|
35
|
+
*/
|
|
36
|
+
declare function appendQueryParams(url: string, params: Record<string, string | number>): string;
|
|
37
|
+
/**
|
|
38
|
+
* Parses a query string into a key-value object.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} query - The query string (with or without leading '?').
|
|
41
|
+
* @returns {Record<string, string>} Object representing parsed query parameters.
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
* parseQueryString('?page=1&limit=10');
|
|
45
|
+
* // -> { page: '1', limit: '10' }
|
|
46
|
+
*/
|
|
47
|
+
declare function parseQueryString(query: string): Record<string, string>;
|
|
48
|
+
/**
|
|
49
|
+
* Validates if a string is a valid URL.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} url - The URL string to validate.
|
|
52
|
+
* @param {boolean} [requireHttps=false] - If true, requires the URL to use HTTPS.
|
|
53
|
+
* @returns {boolean} True if the URL is valid, false otherwise.
|
|
54
|
+
*/
|
|
55
|
+
declare function isValidUrl(url: string, requireHttps?: boolean): boolean;
|
|
56
|
+
/**
|
|
57
|
+
* Extracts the domain name from a URL.
|
|
58
|
+
*
|
|
59
|
+
* @param {string} url - The URL to extract the domain from.
|
|
60
|
+
* @param {boolean} [removeSubdomains=false] - If true, removes subdomains (returns root domain only).
|
|
61
|
+
* @returns {string} The domain name.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* getDomain('https://api.example.com/path');
|
|
65
|
+
* // -> 'api.example.com'
|
|
66
|
+
*
|
|
67
|
+
* getDomain('https://api.example.com/path', true);
|
|
68
|
+
* // -> 'example.com'
|
|
69
|
+
*/
|
|
70
|
+
declare function getDomain(url: string, removeSubdomains?: boolean): string;
|
|
71
|
+
/**
|
|
72
|
+
* Joins URL paths properly handling slashes.
|
|
73
|
+
*
|
|
74
|
+
* @param {...string[]} segments - URL path segments to join.
|
|
75
|
+
* @returns {string} Joined URL path.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
* joinPaths('https://example.com/', '/api/', '/users');
|
|
79
|
+
* // -> 'https://example.com/api/users'
|
|
80
|
+
*/
|
|
81
|
+
declare function joinPaths(...segments: string[]): string;
|
|
82
|
+
/**
|
|
83
|
+
* Normalizes a URL by resolving relative paths, handling protocol-relative URLs, etc.
|
|
84
|
+
*
|
|
85
|
+
* @param {string} url - The URL to normalize.
|
|
86
|
+
* @param {string} [base] - Optional base URL for resolving relative URLs.
|
|
87
|
+
* @returns {string} Normalized URL.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* normalizeUrl('HTTP://Example.COM/foo/../bar');
|
|
91
|
+
* // -> 'http://example.com/bar'
|
|
92
|
+
*/
|
|
93
|
+
declare function normalizeUrl(url: string, base?: string): string;
|
|
94
|
+
/**
|
|
95
|
+
* Creates a URL builder for constructing URLs with a base URL.
|
|
96
|
+
*
|
|
97
|
+
* @param {string} baseUrl - The base URL to build upon.
|
|
98
|
+
* @returns {Object} URL builder methods.
|
|
99
|
+
*
|
|
100
|
+
* @example
|
|
101
|
+
* const api = createUrlBuilder('https://api.example.com');
|
|
102
|
+
* api.path('/users', { active: true });
|
|
103
|
+
* // -> 'https://api.example.com/users?active=true'
|
|
104
|
+
*/
|
|
105
|
+
declare function createUrlBuilder(baseUrl: string): {
|
|
106
|
+
/**
|
|
107
|
+
* Creates a full URL with the given path and query parameters.
|
|
108
|
+
*
|
|
109
|
+
* @param {string} path - The path to append to the base URL.
|
|
110
|
+
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
111
|
+
* @returns {string} The complete URL.
|
|
112
|
+
*/
|
|
113
|
+
path(path: string, params?: Record<string, any>): string;
|
|
114
|
+
/**
|
|
115
|
+
* Creates a full URL with query parameters but no additional path.
|
|
116
|
+
*
|
|
117
|
+
* @param {Record<string, any>} params - Query parameters to add.
|
|
118
|
+
* @returns {string} The complete URL with query parameters.
|
|
119
|
+
*/
|
|
120
|
+
query(params: Record<string, any>): string;
|
|
121
|
+
};
|
|
122
|
+
/**
|
|
123
|
+
* Extracts specific query parameters from a URL.
|
|
124
|
+
*
|
|
125
|
+
* @param {string} url - The URL to extract parameters from.
|
|
126
|
+
* @param {string[]} paramNames - Names of parameters to extract.
|
|
127
|
+
* @returns {Record<string, string>} Object containing the extracted parameters.
|
|
128
|
+
*/
|
|
129
|
+
declare function extractQueryParams(url: string, paramNames: string[]): Record<string, string>;
|
|
130
|
+
/**
|
|
131
|
+
* Removes specified query parameters from a URL.
|
|
132
|
+
*
|
|
133
|
+
* @param {string} url - The URL to modify.
|
|
134
|
+
* @param {string[]} paramsToRemove - Names of parameters to remove.
|
|
135
|
+
* @returns {string} URL with parameters removed.
|
|
136
|
+
*/
|
|
137
|
+
declare function removeQueryParams(url: string, paramsToRemove: string[]): string;
|
|
138
|
+
/**
|
|
139
|
+
* Gets the file extension from a URL path.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} url - The URL to examine.
|
|
142
|
+
* @returns {string} The file extension (without dot) or empty string if none found.
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* getExtension('https://example.com/document.pdf?v=1');
|
|
146
|
+
* // -> 'pdf'
|
|
147
|
+
*/
|
|
148
|
+
declare function getExtension(url: string): string;
|
|
149
|
+
/**
|
|
150
|
+
* Parses URL query parameters into a strongly-typed object.
|
|
151
|
+
*
|
|
152
|
+
* @template T Expected type of the query parameters
|
|
153
|
+
* @param {string} url - The URL to parse.
|
|
154
|
+
* @param {Record<keyof T, (val: string) => any>} [converters] - Type converters for params.
|
|
155
|
+
* @returns {Partial<T>} Typed query parameters.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* parseTypedQueryParams<{page: number, q: string}>('https://example.com?page=2&q=test', {
|
|
159
|
+
* page: Number,
|
|
160
|
+
* q: String
|
|
161
|
+
* });
|
|
162
|
+
* // -> { page: 2, q: 'test' }
|
|
163
|
+
*/
|
|
164
|
+
declare function parseTypedQueryParams<T extends Record<string, any>>(url: string, converters?: Record<keyof T, (val: string) => any>): Partial<T>;
|
|
165
|
+
|
|
166
|
+
export { appendQueryParams, createUrlBuilder, extractQueryParams, getDomain, getExtension, isValidUrl, joinPaths, normalizeUrl, parseQueryString, parseTypedQueryParams, removeQueryParams };
|
package/url/index.mjs
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { URL, URLSearchParams } from 'url';
|
|
26
|
+
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
29
|
+
function appendQueryParams(url, params) {
|
|
30
|
+
const urlObj = new URL(url);
|
|
31
|
+
for (const [key, value] of Object.entries(params)) {
|
|
32
|
+
urlObj.searchParams.set(key, value.toString());
|
|
33
|
+
}
|
|
34
|
+
return urlObj.toString();
|
|
35
|
+
}
|
|
36
|
+
__name(appendQueryParams, "appendQueryParams");
|
|
37
|
+
function parseQueryString(query) {
|
|
38
|
+
const cleanQuery = query.startsWith("?") ? query.slice(1) : query;
|
|
39
|
+
return Object.fromEntries(new URLSearchParams(cleanQuery));
|
|
40
|
+
}
|
|
41
|
+
__name(parseQueryString, "parseQueryString");
|
|
42
|
+
function isValidUrl(url, requireHttps = false) {
|
|
43
|
+
try {
|
|
44
|
+
const parsedUrl = new URL(url);
|
|
45
|
+
if (!parsedUrl.protocol || !parsedUrl.hostname) return false;
|
|
46
|
+
if (requireHttps && parsedUrl.protocol !== "https:") return false;
|
|
47
|
+
return true;
|
|
48
|
+
} catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
__name(isValidUrl, "isValidUrl");
|
|
53
|
+
function getDomain(url, removeSubdomains = false) {
|
|
54
|
+
try {
|
|
55
|
+
const { hostname } = new URL(url);
|
|
56
|
+
if (!removeSubdomains) {
|
|
57
|
+
return hostname;
|
|
58
|
+
}
|
|
59
|
+
const parts = hostname.split(".");
|
|
60
|
+
if (parts.length <= 2) return hostname;
|
|
61
|
+
const secondLevelDomains = [
|
|
62
|
+
"co",
|
|
63
|
+
"com",
|
|
64
|
+
"org",
|
|
65
|
+
"net",
|
|
66
|
+
"gov",
|
|
67
|
+
"edu",
|
|
68
|
+
"ac"
|
|
69
|
+
];
|
|
70
|
+
const sld = parts[parts.length - 2];
|
|
71
|
+
if (secondLevelDomains.includes(sld) && parts.length > 2) {
|
|
72
|
+
return parts.slice(-3).join(".");
|
|
73
|
+
}
|
|
74
|
+
return parts.slice(-2).join(".");
|
|
75
|
+
} catch {
|
|
76
|
+
return "";
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
__name(getDomain, "getDomain");
|
|
80
|
+
function joinPaths(...segments) {
|
|
81
|
+
let result = segments.filter((s) => s !== void 0 && s !== null).map((segment, index) => {
|
|
82
|
+
if (index === 0) return segment.replace(/\/+$/, "");
|
|
83
|
+
return segment.replace(/^\/+|\/+$/g, "");
|
|
84
|
+
}).filter(Boolean).join("/");
|
|
85
|
+
if (segments[0] === "" && !result.startsWith("/")) result = "/" + result;
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
__name(joinPaths, "joinPaths");
|
|
89
|
+
function normalizeUrl(url, base) {
|
|
90
|
+
try {
|
|
91
|
+
let normalized = url.startsWith("//") ? `https:${url}` : url;
|
|
92
|
+
const parsedUrl = base ? new URL(normalized, base) : new URL(normalized);
|
|
93
|
+
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+/g, "/");
|
|
94
|
+
parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, "");
|
|
95
|
+
parsedUrl.hostname = parsedUrl.hostname.toLowerCase();
|
|
96
|
+
return parsedUrl.toString();
|
|
97
|
+
} catch {
|
|
98
|
+
return url;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
__name(normalizeUrl, "normalizeUrl");
|
|
102
|
+
function createUrlBuilder(baseUrl) {
|
|
103
|
+
return {
|
|
104
|
+
/**
|
|
105
|
+
* Creates a full URL with the given path and query parameters.
|
|
106
|
+
*
|
|
107
|
+
* @param {string} path - The path to append to the base URL.
|
|
108
|
+
* @param {Record<string, any>} [params] - Query parameters to add.
|
|
109
|
+
* @returns {string} The complete URL.
|
|
110
|
+
*/
|
|
111
|
+
path(path, params) {
|
|
112
|
+
const url = joinPaths(baseUrl, path);
|
|
113
|
+
return params ? appendQueryParams(url, params) : url;
|
|
114
|
+
},
|
|
115
|
+
/**
|
|
116
|
+
* Creates a full URL with query parameters but no additional path.
|
|
117
|
+
*
|
|
118
|
+
* @param {Record<string, any>} params - Query parameters to add.
|
|
119
|
+
* @returns {string} The complete URL with query parameters.
|
|
120
|
+
*/
|
|
121
|
+
query(params) {
|
|
122
|
+
return appendQueryParams(baseUrl, params);
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
__name(createUrlBuilder, "createUrlBuilder");
|
|
127
|
+
function extractQueryParams(url, paramNames) {
|
|
128
|
+
try {
|
|
129
|
+
const parsedUrl = new URL(url);
|
|
130
|
+
const result = {};
|
|
131
|
+
for (const name of paramNames) {
|
|
132
|
+
const value = parsedUrl.searchParams.get(name);
|
|
133
|
+
if (value !== null) {
|
|
134
|
+
result[name] = value;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return result;
|
|
138
|
+
} catch {
|
|
139
|
+
return {};
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
__name(extractQueryParams, "extractQueryParams");
|
|
143
|
+
function removeQueryParams(url, paramsToRemove) {
|
|
144
|
+
try {
|
|
145
|
+
const parsedUrl = new URL(url);
|
|
146
|
+
for (const param of paramsToRemove) {
|
|
147
|
+
parsedUrl.searchParams.delete(param);
|
|
148
|
+
}
|
|
149
|
+
return parsedUrl.toString();
|
|
150
|
+
} catch {
|
|
151
|
+
return url;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
__name(removeQueryParams, "removeQueryParams");
|
|
155
|
+
function getExtension(url) {
|
|
156
|
+
try {
|
|
157
|
+
const { pathname } = new URL(url);
|
|
158
|
+
const match = pathname.match(/\.([^./\\?#]+)$/);
|
|
159
|
+
return match ? match[1].toLowerCase() : "";
|
|
160
|
+
} catch {
|
|
161
|
+
const match = url.match(/\.([^./\\?#]+)$/);
|
|
162
|
+
return match ? match[1].toLowerCase() : "";
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
__name(getExtension, "getExtension");
|
|
166
|
+
function parseTypedQueryParams(url, converters) {
|
|
167
|
+
try {
|
|
168
|
+
const { searchParams } = new URL(url);
|
|
169
|
+
const result = {};
|
|
170
|
+
for (const [key, value] of searchParams.entries()) {
|
|
171
|
+
if (converters && key in converters) {
|
|
172
|
+
try {
|
|
173
|
+
result[key] = converters[key](value);
|
|
174
|
+
} catch {
|
|
175
|
+
}
|
|
176
|
+
} else {
|
|
177
|
+
result[key] = value;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
} catch {
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
__name(parseTypedQueryParams, "parseTypedQueryParams");
|
|
186
|
+
|
|
187
|
+
export { appendQueryParams, createUrlBuilder, extractQueryParams, getDomain, getExtension, isValidUrl, joinPaths, normalizeUrl, parseQueryString, parseTypedQueryParams, removeQueryParams };
|