@catbee/utils 0.0.8-next.0 → 0.0.8-rc.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.
- package/README.md +1 -26
- package/build/esm/config.d.ts +122 -0
- package/build/esm/config.js +133 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +27 -0
- package/build/esm/index.js +50 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/servers/server.builder.d.ts +508 -0
- package/build/esm/servers/server.builder.js +702 -0
- package/build/esm/servers/server.builder.js.map +1 -0
- package/build/esm/servers/server.d.ts +256 -0
- package/build/esm/servers/server.js +1248 -0
- package/build/esm/servers/server.js.map +1 -0
- package/build/esm/types/api-response.d.ts +152 -0
- package/build/esm/types/api-response.js +34 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/types/index.d.ts +125 -0
- package/build/esm/types/index.js +25 -0
- package/build/esm/types/index.js.map +1 -0
- package/build/esm/types/server.d.ts +268 -0
- package/build/esm/types/server.js +25 -0
- package/build/esm/types/server.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +168 -0
- package/build/esm/utils/array.utils.js +382 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +265 -0
- package/build/esm/utils/async.utils.js +896 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +153 -0
- package/build/esm/utils/cache.utils.js +499 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +189 -0
- package/build/esm/utils/context-store.utils.js +307 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +160 -0
- package/build/esm/utils/crypto.utils.js +375 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/date.utils.d.ts +159 -0
- package/build/esm/utils/date.utils.js +389 -0
- package/build/esm/utils/date.utils.js.map +1 -0
- package/build/esm/utils/decorators.utils.d.ts +315 -0
- package/build/esm/utils/decorators.utils.js +666 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +196 -0
- package/build/esm/utils/dir.utils.js +998 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +377 -0
- package/build/esm/utils/env.utils.js +873 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +230 -0
- package/build/esm/utils/exception.utils.js +559 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +164 -0
- package/build/esm/utils/fs.utils.js +580 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +266 -0
- package/build/esm/utils/http-status-codes.js +295 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +36 -0
- package/build/esm/utils/id.utils.js +87 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +160 -0
- package/build/esm/utils/logger.utils.js +341 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/middleware.utils.d.ts +100 -0
- package/build/esm/utils/middleware.utils.js +325 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +124 -0
- package/build/esm/utils/obj.utils.js +519 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/performance.utils.d.ts +136 -0
- package/build/esm/utils/performance.utils.js +376 -0
- package/build/esm/utils/performance.utils.js.map +1 -0
- package/build/esm/utils/request.utils.d.ts +86 -0
- package/build/esm/utils/request.utils.js +223 -0
- package/build/esm/utils/request.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +163 -0
- package/build/esm/utils/response.utils.js +284 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/stream.utils.d.ts +88 -0
- package/build/esm/utils/stream.utils.js +284 -0
- package/build/esm/utils/stream.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +93 -0
- package/build/esm/utils/string.utils.js +169 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/type.utils.d.ts +90 -0
- package/build/esm/utils/type.utils.js +190 -0
- package/build/esm/utils/type.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +141 -0
- package/build/esm/utils/url.utils.js +381 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +177 -0
- package/build/esm/utils/validate.utils.js +336 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +122 -0
- package/build/esnext/config.js +133 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +27 -0
- package/build/esnext/index.js +50 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/servers/server.builder.d.ts +508 -0
- package/build/esnext/servers/server.builder.js +657 -0
- package/build/esnext/servers/server.builder.js.map +1 -0
- package/build/esnext/servers/server.d.ts +256 -0
- package/build/esnext/servers/server.js +955 -0
- package/build/esnext/servers/server.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +152 -0
- package/build/esnext/types/api-response.js +34 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/types/index.d.ts +125 -0
- package/build/esnext/types/index.js +25 -0
- package/build/esnext/types/index.js.map +1 -0
- package/build/esnext/types/server.d.ts +268 -0
- package/build/esnext/types/server.js +25 -0
- package/build/esnext/types/server.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +168 -0
- package/build/esnext/utils/array.utils.js +345 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +265 -0
- package/build/esnext/utils/async.utils.js +620 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +153 -0
- package/build/esnext/utils/cache.utils.js +293 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +189 -0
- package/build/esnext/utils/context-store.utils.js +289 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +160 -0
- package/build/esnext/utils/crypto.utils.js +276 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/date.utils.d.ts +159 -0
- package/build/esnext/utils/date.utils.js +384 -0
- package/build/esnext/utils/date.utils.js.map +1 -0
- package/build/esnext/utils/decorators.utils.d.ts +315 -0
- package/build/esnext/utils/decorators.utils.js +504 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +196 -0
- package/build/esnext/utils/dir.utils.js +477 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +377 -0
- package/build/esnext/utils/env.utils.js +785 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +230 -0
- package/build/esnext/utils/exception.utils.js +381 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +164 -0
- package/build/esnext/utils/fs.utils.js +349 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +266 -0
- package/build/esnext/utils/http-status-codes.js +295 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +36 -0
- package/build/esnext/utils/id.utils.js +84 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +160 -0
- package/build/esnext/utils/logger.utils.js +300 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/middleware.utils.d.ts +100 -0
- package/build/esnext/utils/middleware.utils.js +236 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +124 -0
- package/build/esnext/utils/obj.utils.js +413 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/performance.utils.d.ts +136 -0
- package/build/esnext/utils/performance.utils.js +271 -0
- package/build/esnext/utils/performance.utils.js.map +1 -0
- package/build/esnext/utils/request.utils.d.ts +86 -0
- package/build/esnext/utils/request.utils.js +186 -0
- package/build/esnext/utils/request.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +163 -0
- package/build/esnext/utils/response.utils.js +247 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/stream.utils.d.ts +88 -0
- package/build/esnext/utils/stream.utils.js +210 -0
- package/build/esnext/utils/stream.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +93 -0
- package/build/esnext/utils/string.utils.js +165 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/type.utils.d.ts +90 -0
- package/build/esnext/utils/type.utils.js +187 -0
- package/build/esnext/utils/type.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +141 -0
- package/build/esnext/utils/url.utils.js +304 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +177 -0
- package/build/esnext/utils/validate.utils.js +320 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +122 -0
- package/build/src/config.js +138 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +27 -0
- package/build/src/index.js +69 -0
- package/build/src/index.js.map +1 -0
- package/build/src/servers/server.builder.d.ts +508 -0
- package/build/src/servers/server.builder.js +661 -0
- package/build/src/servers/server.builder.js.map +1 -0
- package/build/src/servers/server.d.ts +256 -0
- package/build/src/servers/server.js +962 -0
- package/build/src/servers/server.js.map +1 -0
- package/build/src/types/api-response.d.ts +152 -0
- package/build/src/types/api-response.js +37 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/types/index.d.ts +125 -0
- package/build/src/types/index.js +26 -0
- package/build/src/types/index.js.map +1 -0
- package/build/src/types/server.d.ts +268 -0
- package/build/src/types/server.js +26 -0
- package/build/src/types/server.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +168 -0
- package/build/src/utils/array.utils.js +364 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +265 -0
- package/build/src/utils/async.utils.js +641 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +153 -0
- package/build/src/utils/cache.utils.js +297 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +189 -0
- package/build/src/utils/context-store.utils.js +296 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +160 -0
- package/build/src/utils/crypto.utils.js +293 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/date.utils.d.ts +159 -0
- package/build/src/utils/date.utils.js +396 -0
- package/build/src/utils/date.utils.js.map +1 -0
- package/build/src/utils/decorators.utils.d.ts +315 -0
- package/build/src/utils/decorators.utils.js +516 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +196 -0
- package/build/src/utils/dir.utils.js +501 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +377 -0
- package/build/src/utils/env.utils.js +789 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +230 -0
- package/build/src/utils/exception.utils.js +407 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +164 -0
- package/build/src/utils/fs.utils.js +372 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +266 -0
- package/build/src/utils/http-status-codes.js +298 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +36 -0
- package/build/src/utils/id.utils.js +91 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +160 -0
- package/build/src/utils/logger.utils.js +346 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/middleware.utils.d.ts +100 -0
- package/build/src/utils/middleware.utils.js +244 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +124 -0
- package/build/src/utils/obj.utils.js +429 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/performance.utils.d.ts +136 -0
- package/build/src/utils/performance.utils.js +278 -0
- package/build/src/utils/performance.utils.js.map +1 -0
- package/build/src/utils/request.utils.d.ts +86 -0
- package/build/src/utils/request.utils.js +195 -0
- package/build/src/utils/request.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +163 -0
- package/build/src/utils/response.utils.js +260 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/stream.utils.d.ts +88 -0
- package/build/src/utils/stream.utils.js +218 -0
- package/build/src/utils/stream.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +93 -0
- package/build/src/utils/string.utils.js +179 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/type.utils.d.ts +90 -0
- package/build/src/utils/type.utils.js +196 -0
- package/build/src/utils/type.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +141 -0
- package/build/src/utils/url.utils.js +317 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +177 -0
- package/build/src/utils/validate.utils.js +345 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +66 -12
- package/build/index.cjs +0 -6888
- package/build/index.d.ts +0 -5242
- package/build/index.mjs +0 -6615
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/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
|
+
* Collection of common validation helpers for strings, numbers, email, UUID, etc.
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Checks if a string is a valid port number.
|
|
29
|
+
*
|
|
30
|
+
* @param str - The input string or number.
|
|
31
|
+
* @returns True if valid port number, else false.
|
|
32
|
+
*/
|
|
33
|
+
export function isPort(value) {
|
|
34
|
+
const port = typeof value === 'string' ? Number(value.trim()) : value;
|
|
35
|
+
return Number.isInteger(port) && port > 0 && port <= 65535;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Checks if a string is a valid email address.
|
|
39
|
+
*
|
|
40
|
+
* @param {string} str - The input string.
|
|
41
|
+
* @returns {boolean} True if valid email, else false.
|
|
42
|
+
*/
|
|
43
|
+
export function isEmail(str) {
|
|
44
|
+
if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(str))
|
|
45
|
+
return false;
|
|
46
|
+
// Additional checks
|
|
47
|
+
const [local, domain] = str.split('@');
|
|
48
|
+
if (!local || !domain)
|
|
49
|
+
return false;
|
|
50
|
+
// Disallow consecutive dots in local or domain part
|
|
51
|
+
if (local.includes('..') || domain.includes('..'))
|
|
52
|
+
return false;
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Checks if a string is a valid UUID (versions 1-5).
|
|
57
|
+
*
|
|
58
|
+
* @param {string} str - The input string.
|
|
59
|
+
* @returns {boolean} True if valid UUID, else false.
|
|
60
|
+
*/
|
|
61
|
+
export function isUUID(str) {
|
|
62
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(str);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Checks if a string is a valid URL.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} str - The input string.
|
|
68
|
+
* @returns {boolean} True if valid URL, else false.
|
|
69
|
+
*/
|
|
70
|
+
export function isURL(str) {
|
|
71
|
+
try {
|
|
72
|
+
new URL(str);
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
catch (_a) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Checks if a string is a valid international phone number (E.164 or common patterns).
|
|
81
|
+
*
|
|
82
|
+
* @param {string} str - The input string.
|
|
83
|
+
* @returns {boolean} True if looks like a phone number.
|
|
84
|
+
*/
|
|
85
|
+
export function isPhone(str) {
|
|
86
|
+
if (typeof str !== 'string')
|
|
87
|
+
return false;
|
|
88
|
+
// Strip non-digit characters to count total digits
|
|
89
|
+
const digitsOnly = str.replace(/\D/g, '');
|
|
90
|
+
if (digitsOnly.length < 6 || digitsOnly.length > 15)
|
|
91
|
+
return false;
|
|
92
|
+
// Accept typical phone characters: +, digits, space, -, (, )
|
|
93
|
+
return /^[+]?[\d\s().-]+$/.test(str);
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Checks if a string is strictly alphanumeric (letters/numbers only).
|
|
97
|
+
*
|
|
98
|
+
* @param {string} str - The input string.
|
|
99
|
+
* @returns {boolean} True if alphanumeric.
|
|
100
|
+
*/
|
|
101
|
+
export function isAlphanumeric(str) {
|
|
102
|
+
return /^[a-z0-9]+$/i.test(str);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Checks if a string or number can be safely parsed to a number.
|
|
106
|
+
*
|
|
107
|
+
* @param {string | number} value - The value to check.
|
|
108
|
+
* @returns {boolean} True if the value is numeric.
|
|
109
|
+
*/
|
|
110
|
+
export function isNumeric(value) {
|
|
111
|
+
if (typeof value === 'string' && value.trim() === '')
|
|
112
|
+
return false;
|
|
113
|
+
const num = typeof value === 'number' ? value : Number(value);
|
|
114
|
+
return typeof num === 'number' && isFinite(num);
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Checks if a string is a valid hex color code (e.g. #FFF or #FFFFFF).
|
|
118
|
+
*
|
|
119
|
+
* @param {string} str - Input string.
|
|
120
|
+
* @returns {boolean}
|
|
121
|
+
*/
|
|
122
|
+
export function isHexColor(str) {
|
|
123
|
+
return /^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(str);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Checks if a string is a valid date string.
|
|
127
|
+
*
|
|
128
|
+
* @param {string} str - Input string.
|
|
129
|
+
* @returns {boolean}
|
|
130
|
+
*/
|
|
131
|
+
export function isISODate(str) {
|
|
132
|
+
const isoRegex = /^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})?)?$/;
|
|
133
|
+
if (!isoRegex.test(str))
|
|
134
|
+
return false;
|
|
135
|
+
const date = new Date(str);
|
|
136
|
+
return !isNaN(date.getTime()) && date.toISOString().startsWith(str.slice(0, 10));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Checks if a string matches a specified length range.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} str - The input string.
|
|
142
|
+
* @param {number} min - Minimum length (inclusive).
|
|
143
|
+
* @param {number} max - Maximum length (inclusive).
|
|
144
|
+
* @returns {boolean} True if string length is within range.
|
|
145
|
+
*/
|
|
146
|
+
export function isLengthBetween(str, min, max) {
|
|
147
|
+
return str.length >= min && str.length <= max;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Validates that a number is between specified min and max values (inclusive).
|
|
151
|
+
*
|
|
152
|
+
* @param {number} value - The number to validate.
|
|
153
|
+
* @param {number} min - Minimum value.
|
|
154
|
+
* @param {number} max - Maximum value.
|
|
155
|
+
* @returns {boolean} True if number is within range.
|
|
156
|
+
*/
|
|
157
|
+
export function isNumberBetween(value, min, max) {
|
|
158
|
+
return value >= min && value <= max;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Checks if a string contains only alphabetic characters.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} str - The input string.
|
|
164
|
+
* @returns {boolean} True if alphabetic only.
|
|
165
|
+
*/
|
|
166
|
+
export function isAlpha(str) {
|
|
167
|
+
return /^[a-zA-Z]+$/.test(str);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Validates that a string contains at least one uppercase letter,
|
|
171
|
+
* one lowercase letter, one number, and one special character.
|
|
172
|
+
*
|
|
173
|
+
* @param {string} str - The input string.
|
|
174
|
+
* @returns {boolean} True if string meets password complexity requirements.
|
|
175
|
+
*/
|
|
176
|
+
export function isStrongPassword(str) {
|
|
177
|
+
if (str.length < 8)
|
|
178
|
+
return false;
|
|
179
|
+
const hasUpperCase = /[A-Z]/.test(str);
|
|
180
|
+
const hasLowerCase = /[a-z]/.test(str);
|
|
181
|
+
const hasNumbers = /[0-9]/.test(str);
|
|
182
|
+
const hasSpecialChar = /[!@#$%^&*()_+\-=[\]{};':"\\|,.<>?]/.test(str);
|
|
183
|
+
return hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChar;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Checks if a string is a valid IPv4 address.
|
|
187
|
+
*
|
|
188
|
+
* @param {string} str - The input string.
|
|
189
|
+
* @returns {boolean} True if valid IPv4 address.
|
|
190
|
+
*/
|
|
191
|
+
export function isIPv4(str) {
|
|
192
|
+
const parts = str.split('.');
|
|
193
|
+
if (parts.length !== 4)
|
|
194
|
+
return false;
|
|
195
|
+
return parts.every(part => {
|
|
196
|
+
const num = parseInt(part, 10);
|
|
197
|
+
return part === num.toString() && num >= 0 && num <= 255;
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Checks if a string is a valid IPv6 address.
|
|
202
|
+
*
|
|
203
|
+
* @param {string} str - The input string.
|
|
204
|
+
* @returns {boolean} True if valid IPv6 address.
|
|
205
|
+
*/
|
|
206
|
+
export function isIPv6(str) {
|
|
207
|
+
// Simple but effective check for common IPv6 formats
|
|
208
|
+
return /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$/.test(str);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Validates a credit card number using the Luhn algorithm.
|
|
212
|
+
*
|
|
213
|
+
* @param {string} str - The credit card number string.
|
|
214
|
+
* @returns {boolean} True if valid credit card number.
|
|
215
|
+
*/
|
|
216
|
+
export function isCreditCard(str) {
|
|
217
|
+
// Remove non-digit characters
|
|
218
|
+
const cardNumber = str.replace(/\D/g, '');
|
|
219
|
+
if (cardNumber.length < 13 || cardNumber.length > 19)
|
|
220
|
+
return false;
|
|
221
|
+
// Luhn algorithm implementation
|
|
222
|
+
let sum = 0;
|
|
223
|
+
let shouldDouble = false;
|
|
224
|
+
for (let i = cardNumber.length - 1; i >= 0; i--) {
|
|
225
|
+
let digit = parseInt(cardNumber.charAt(i), 10);
|
|
226
|
+
if (shouldDouble) {
|
|
227
|
+
digit *= 2;
|
|
228
|
+
if (digit > 9)
|
|
229
|
+
digit -= 9;
|
|
230
|
+
}
|
|
231
|
+
sum += digit;
|
|
232
|
+
shouldDouble = !shouldDouble;
|
|
233
|
+
}
|
|
234
|
+
return sum % 10 === 0;
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* Checks if a string is a valid JSON.
|
|
238
|
+
*
|
|
239
|
+
* @param {string} str - The input string.
|
|
240
|
+
* @returns {boolean} True if valid JSON.
|
|
241
|
+
*/
|
|
242
|
+
export function isValidJSON(str) {
|
|
243
|
+
try {
|
|
244
|
+
JSON.parse(str);
|
|
245
|
+
return true;
|
|
246
|
+
}
|
|
247
|
+
catch (_a) {
|
|
248
|
+
return false;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Type guard that checks if a value is an array.
|
|
253
|
+
*
|
|
254
|
+
* @template T Optional expected element type
|
|
255
|
+
* @param {unknown} value - The value to check.
|
|
256
|
+
* @param {(item: unknown) => boolean} [itemGuard] - Optional function to validate each item.
|
|
257
|
+
* @returns {boolean} True if value is an array (with optional item validation).
|
|
258
|
+
*/
|
|
259
|
+
export function isArray(value, itemGuard) {
|
|
260
|
+
if (!Array.isArray(value))
|
|
261
|
+
return false;
|
|
262
|
+
return !itemGuard || value.every(item => itemGuard(item));
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Checks if a string is a valid base64 encoded string.
|
|
266
|
+
*
|
|
267
|
+
* @param {string} str - The input string.
|
|
268
|
+
* @returns {boolean} True if valid base64.
|
|
269
|
+
*/
|
|
270
|
+
export function isBase64(str) {
|
|
271
|
+
return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(str);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Validates that an object has all required properties.
|
|
275
|
+
*
|
|
276
|
+
* @param {object} obj - The object to validate.
|
|
277
|
+
* @param {string[]} requiredProps - Array of required property names.
|
|
278
|
+
* @returns {boolean} True if all required properties exist.
|
|
279
|
+
*/
|
|
280
|
+
export function hasRequiredProps(obj, requiredProps) {
|
|
281
|
+
return requiredProps.every(prop => Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] !== undefined && obj[prop] !== null);
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Validates a date is within a specified range.
|
|
285
|
+
*
|
|
286
|
+
* @param {Date} date - The date to validate.
|
|
287
|
+
* @param {Date} [minDate] - Optional minimum date (inclusive).
|
|
288
|
+
* @param {Date} [maxDate] - Optional maximum date (inclusive).
|
|
289
|
+
* @returns {boolean} True if date is within range.
|
|
290
|
+
*/
|
|
291
|
+
export function isDateInRange(date, minDate, maxDate) {
|
|
292
|
+
if (!(date instanceof Date) || isNaN(date.getTime()))
|
|
293
|
+
return false;
|
|
294
|
+
if (minDate && date < minDate)
|
|
295
|
+
return false;
|
|
296
|
+
if (maxDate && date > maxDate)
|
|
297
|
+
return false;
|
|
298
|
+
return true;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Validates a string matches a specific regular expression pattern.
|
|
302
|
+
*
|
|
303
|
+
* @param {string} str - The input string.
|
|
304
|
+
* @param {RegExp} pattern - Regular expression to test against.
|
|
305
|
+
* @returns {boolean} True if string matches pattern.
|
|
306
|
+
*/
|
|
307
|
+
export function matchesPattern(str, pattern) {
|
|
308
|
+
return pattern.test(str);
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Validates data against multiple constraints.
|
|
312
|
+
*
|
|
313
|
+
* @param {unknown} value - The value to validate.
|
|
314
|
+
* @param {Array<(value: unknown) => boolean>} validators - Array of validation functions.
|
|
315
|
+
* @returns {boolean} True if value passes all validations.
|
|
316
|
+
*/
|
|
317
|
+
export function validateAll(value, validators) {
|
|
318
|
+
return validators.every(validator => validator(value));
|
|
319
|
+
}
|
|
320
|
+
//# sourceMappingURL=validate.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"validate.utils.js","sourceRoot":"","sources":["../../../src/utils/validate.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH;;GAEG;AAEH;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,KAAsB;IAC3C,MAAM,IAAI,GAAG,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;IACtE,OAAO,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,GAAG,CAAC,IAAI,IAAI,IAAI,KAAK,CAAC;AAC7D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,OAAO,CAAC,G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CAAC;AAC5D,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,GAAW;IAClC,OAAO,kEAAkE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACtF,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB,CAAC,GAA4B,EAAE,aAAuB;IACpF,OAAO,aAAa,CAAC,KAAK,CACxB,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,SAAS,IAAI,GAAG,CAAC,IAAI,CAAC,KAAK,IAAI,CACzG,CAAC;AACJ,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,aAAa,CAAC,IAAU,EAAE,OAAc,EAAE,OAAc;IACtE,IAAI,CAAC,CAAC,IAAI,YAAY,IAAI,CAAC,IAAI,KAAK,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;QAAE,OAAO,KAAK,CAAC;IAEnE,IAAI,OAAO,IAAI,IAAI,GAAG,OAAO;QAAE,OAAO,KAAK,CAAC;IAC5C,IAAI,OAAO,IAAI,IAAI,GAAG,OAAO;QAAE,OAAO,KAAK,CAAC;IAE5C,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,cAAc,CAAC,GAAW,EAAE,OAAe;IACzD,OAAO,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAC3B,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc,EAAE,UAA8C;IACxF,OAAO,UAAU,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC,CAAC;AACzD,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\n/**\n * Collection of common validation helpers for strings, numbers, email, UUID, etc.\n */\n\n/**\n * Checks if a string is a valid port number.\n *\n * @param str - The input string or number.\n * @returns True if valid port number, else false.\n */\nexport function isPort(value: string | number): boolean {\n const port = typeof value === 'string' ? Number(value.trim()) : value;\n return Number.isInteger(port) && port > 0 && port <= 65535;\n}\n\n/**\n * Checks if a string is a valid email address.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid email, else false.\n */\nexport function isEmail(str: string): boolean {\n if (!/^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(str)) return false;\n\n // Additional checks\n const [local, domain] = str.split('@');\n if (!local || !domain) return false;\n\n // Disallow consecutive dots in local or domain part\n if (local.includes('..') || domain.includes('..')) return false;\n\n return true;\n}\n\n/**\n * Checks if a string is a valid UUID (versions 1-5).\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid UUID, else false.\n */\nexport function isUUID(str: string): boolean {\n return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(str);\n}\n\n/**\n * Checks if a string is a valid URL.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid URL, else false.\n */\nexport function isURL(str: string): boolean {\n try {\n new URL(str);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Checks if a string is a valid international phone number (E.164 or common patterns).\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if looks like a phone number.\n */\nexport function isPhone(str: string): boolean {\n if (typeof str !== 'string') return false;\n\n // Strip non-digit characters to count total digits\n const digitsOnly = str.replace(/\\D/g, '');\n if (digitsOnly.length < 6 || digitsOnly.length > 15) return false;\n\n // Accept typical phone characters: +, digits, space, -, (, )\n return /^[+]?[\\d\\s().-]+$/.test(str);\n}\n\n/**\n * Checks if a string is strictly alphanumeric (letters/numbers only).\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if alphanumeric.\n */\nexport function isAlphanumeric(str: string): boolean {\n return /^[a-z0-9]+$/i.test(str);\n}\n\n/**\n * Checks if a string or number can be safely parsed to a number.\n *\n * @param {string | number} value - The value to check.\n * @returns {boolean} True if the value is numeric.\n */\nexport function isNumeric(value: string | number): boolean {\n if (typeof value === 'string' && value.trim() === '') return false;\n const num = typeof value === 'number' ? value : Number(value);\n return typeof num === 'number' && isFinite(num);\n}\n\n/**\n * Checks if a string is a valid hex color code (e.g. #FFF or #FFFFFF).\n *\n * @param {string} str - Input string.\n * @returns {boolean}\n */\nexport function isHexColor(str: string): boolean {\n return /^#([a-f0-9]{6}|[a-f0-9]{3})$/i.test(str);\n}\n\n/**\n * Checks if a string is a valid date string.\n *\n * @param {string} str - Input string.\n * @returns {boolean}\n */\nexport function isISODate(str: string): boolean {\n const isoRegex = /^\\d{4}-\\d{2}-\\d{2}(T\\d{2}:\\d{2}:\\d{2}(\\.\\d+)?(Z|[+-]\\d{2}:\\d{2})?)?$/;\n\n if (!isoRegex.test(str)) return false;\n\n const date = new Date(str);\n return !isNaN(date.getTime()) && date.toISOString().startsWith(str.slice(0, 10));\n}\n\n/**\n * Checks if a string matches a specified length range.\n *\n * @param {string} str - The input string.\n * @param {number} min - Minimum length (inclusive).\n * @param {number} max - Maximum length (inclusive).\n * @returns {boolean} True if string length is within range.\n */\nexport function isLengthBetween(str: string, min: number, max: number): boolean {\n return str.length >= min && str.length <= max;\n}\n\n/**\n * Validates that a number is between specified min and max values (inclusive).\n *\n * @param {number} value - The number to validate.\n * @param {number} min - Minimum value.\n * @param {number} max - Maximum value.\n * @returns {boolean} True if number is within range.\n */\nexport function isNumberBetween(value: number, min: number, max: number): boolean {\n return value >= min && value <= max;\n}\n\n/**\n * Checks if a string contains only alphabetic characters.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if alphabetic only.\n */\nexport function isAlpha(str: string): boolean {\n return /^[a-zA-Z]+$/.test(str);\n}\n\n/**\n * Validates that a string contains at least one uppercase letter,\n * one lowercase letter, one number, and one special character.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if string meets password complexity requirements.\n */\nexport function isStrongPassword(str: string): boolean {\n if (str.length < 8) return false;\n const hasUpperCase = /[A-Z]/.test(str);\n const hasLowerCase = /[a-z]/.test(str);\n const hasNumbers = /[0-9]/.test(str);\n const hasSpecialChar = /[!@#$%^&*()_+\\-=[\\]{};':\"\\\\|,.<>?]/.test(str);\n return hasUpperCase && hasLowerCase && hasNumbers && hasSpecialChar;\n}\n\n/**\n * Checks if a string is a valid IPv4 address.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid IPv4 address.\n */\nexport function isIPv4(str: string): boolean {\n const parts = str.split('.');\n if (parts.length !== 4) return false;\n\n return parts.every(part => {\n const num = parseInt(part, 10);\n return part === num.toString() && num >= 0 && num <= 255;\n });\n}\n\n/**\n * Checks if a string is a valid IPv6 address.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid IPv6 address.\n */\nexport function isIPv6(str: string): boolean {\n // Simple but effective check for common IPv6 formats\n return /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]+|::(ffff(:0{1,4})?:)?((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1?[0-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1?[0-9])?[0-9]))$/.test(\n str\n );\n}\n\n/**\n * Validates a credit card number using the Luhn algorithm.\n *\n * @param {string} str - The credit card number string.\n * @returns {boolean} True if valid credit card number.\n */\nexport function isCreditCard(str: string): boolean {\n // Remove non-digit characters\n const cardNumber = str.replace(/\\D/g, '');\n if (cardNumber.length < 13 || cardNumber.length > 19) return false;\n\n // Luhn algorithm implementation\n let sum = 0;\n let shouldDouble = false;\n\n for (let i = cardNumber.length - 1; i >= 0; i--) {\n let digit = parseInt(cardNumber.charAt(i), 10);\n\n if (shouldDouble) {\n digit *= 2;\n if (digit > 9) digit -= 9;\n }\n\n sum += digit;\n shouldDouble = !shouldDouble;\n }\n\n return sum % 10 === 0;\n}\n\n/**\n * Checks if a string is a valid JSON.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid JSON.\n */\nexport function isValidJSON(str: string): boolean {\n try {\n JSON.parse(str);\n return true;\n } catch {\n return false;\n }\n}\n\n/**\n * Type guard that checks if a value is an array.\n *\n * @template T Optional expected element type\n * @param {unknown} value - The value to check.\n * @param {(item: unknown) => boolean} [itemGuard] - Optional function to validate each item.\n * @returns {boolean} True if value is an array (with optional item validation).\n */\nexport function isArray<T = unknown>(value: unknown, itemGuard?: (item: unknown) => item is T): value is T[] {\n if (!Array.isArray(value)) return false;\n return !itemGuard || value.every(item => itemGuard(item));\n}\n\n/**\n * Checks if a string is a valid base64 encoded string.\n *\n * @param {string} str - The input string.\n * @returns {boolean} True if valid base64.\n */\nexport function isBase64(str: string): boolean {\n return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(str);\n}\n\n/**\n * Validates that an object has all required properties.\n *\n * @param {object} obj - The object to validate.\n * @param {string[]} requiredProps - Array of required property names.\n * @returns {boolean} True if all required properties exist.\n */\nexport function hasRequiredProps(obj: Record<string, unknown>, requiredProps: string[]): boolean {\n return requiredProps.every(\n prop => Object.prototype.hasOwnProperty.call(obj, prop) && obj[prop] !== undefined && obj[prop] !== null\n );\n}\n\n/**\n * Validates a date is within a specified range.\n *\n * @param {Date} date - The date to validate.\n * @param {Date} [minDate] - Optional minimum date (inclusive).\n * @param {Date} [maxDate] - Optional maximum date (inclusive).\n * @returns {boolean} True if date is within range.\n */\nexport function isDateInRange(date: Date, minDate?: Date, maxDate?: Date): boolean {\n if (!(date instanceof Date) || isNaN(date.getTime())) return false;\n\n if (minDate && date < minDate) return false;\n if (maxDate && date > maxDate) return false;\n\n return true;\n}\n\n/**\n * Validates a string matches a specific regular expression pattern.\n *\n * @param {string} str - The input string.\n * @param {RegExp} pattern - Regular expression to test against.\n * @returns {boolean} True if string matches pattern.\n */\nexport function matchesPattern(str: string, pattern: RegExp): boolean {\n return pattern.test(str);\n}\n\n/**\n * Validates data against multiple constraints.\n *\n * @param {unknown} value - The value to validate.\n * @param {Array<(value: unknown) => boolean>} validators - Array of validation functions.\n * @returns {boolean} True if value passes all validations.\n */\nexport function validateAll(value: unknown, validators: Array<(value: unknown) => boolean>): boolean {\n return validators.every(validator => validator(value));\n}\n"]}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Logger, LoggerLevels } from './utils/logger.utils';
|
|
2
|
+
/**
|
|
3
|
+
* Extends Express Request interface to add request ID tracking.
|
|
4
|
+
* Added by requestId middleware during request processing.
|
|
5
|
+
*/
|
|
6
|
+
declare global {
|
|
7
|
+
namespace Express {
|
|
8
|
+
interface Request {
|
|
9
|
+
/** Unique request identifier (set by middleware) */
|
|
10
|
+
id?: string;
|
|
11
|
+
/** Logger instance for the request (set by middleware) */
|
|
12
|
+
logger?: Logger;
|
|
13
|
+
/** User information (set by authentication middleware) */
|
|
14
|
+
user?: any;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Application runtime configuration loaded from environment variables.
|
|
20
|
+
*/
|
|
21
|
+
export declare let config: {
|
|
22
|
+
logger: {
|
|
23
|
+
/**
|
|
24
|
+
* Logging level (e.g., 'info', 'debug', 'warn', 'error').
|
|
25
|
+
*/
|
|
26
|
+
level: LoggerLevels;
|
|
27
|
+
/**
|
|
28
|
+
* Name of the logger instance (defaults to npm package name).
|
|
29
|
+
*/
|
|
30
|
+
name: string;
|
|
31
|
+
/**
|
|
32
|
+
* Enables pretty-print logging in development.
|
|
33
|
+
* Has no effect in production.
|
|
34
|
+
*/
|
|
35
|
+
pretty: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Single line output for pretty-print (default: false)
|
|
38
|
+
*/
|
|
39
|
+
singleLine: boolean;
|
|
40
|
+
};
|
|
41
|
+
cache: {
|
|
42
|
+
/**
|
|
43
|
+
* Default TTL (time to live) for cache entries in seconds
|
|
44
|
+
*/
|
|
45
|
+
defaultTtl: number;
|
|
46
|
+
};
|
|
47
|
+
};
|
|
48
|
+
export declare const defaultServerConfig: {
|
|
49
|
+
port: number;
|
|
50
|
+
host: string;
|
|
51
|
+
cors: false;
|
|
52
|
+
helmet: false;
|
|
53
|
+
compression: false;
|
|
54
|
+
bodyParser: {
|
|
55
|
+
json: {
|
|
56
|
+
limit: string;
|
|
57
|
+
};
|
|
58
|
+
urlencoded: {
|
|
59
|
+
extended: true;
|
|
60
|
+
limit: string;
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
cookieParser: false;
|
|
64
|
+
isMicroservice: false;
|
|
65
|
+
appName: string;
|
|
66
|
+
globalHeaders: {};
|
|
67
|
+
rateLimit: {
|
|
68
|
+
enable: false;
|
|
69
|
+
windowMs: number;
|
|
70
|
+
max: number;
|
|
71
|
+
message: string;
|
|
72
|
+
standardHeaders: true;
|
|
73
|
+
legacyHeaders: false;
|
|
74
|
+
};
|
|
75
|
+
requestLogging: {
|
|
76
|
+
enable: boolean;
|
|
77
|
+
ignorePaths: (req: import("express").Request<import("express-serve-static-core").ParamsDictionary, any, any, import("qs").ParsedQs, Record<string, any>>, _res: import("express").Response<any, Record<string, any>>) => boolean;
|
|
78
|
+
skipNotFoundRoutes: false;
|
|
79
|
+
};
|
|
80
|
+
openApi: {
|
|
81
|
+
enable: false;
|
|
82
|
+
mountPath: string;
|
|
83
|
+
verbose: false;
|
|
84
|
+
withGlobalPrefix: false;
|
|
85
|
+
};
|
|
86
|
+
healthCheck: {
|
|
87
|
+
path: string;
|
|
88
|
+
detailed: true;
|
|
89
|
+
};
|
|
90
|
+
requestTimeout: number;
|
|
91
|
+
responseTime: {
|
|
92
|
+
enable: false;
|
|
93
|
+
addHeader: true;
|
|
94
|
+
logOnComplete: false;
|
|
95
|
+
};
|
|
96
|
+
requestId: {
|
|
97
|
+
headerName: string;
|
|
98
|
+
exposeHeader: true;
|
|
99
|
+
generator: () => string;
|
|
100
|
+
};
|
|
101
|
+
metrics: {
|
|
102
|
+
enable: false;
|
|
103
|
+
path: string;
|
|
104
|
+
withGlobalPrefix: false;
|
|
105
|
+
};
|
|
106
|
+
serviceVersion: {
|
|
107
|
+
enable: false;
|
|
108
|
+
headerName: string;
|
|
109
|
+
version: string;
|
|
110
|
+
};
|
|
111
|
+
};
|
|
112
|
+
/**
|
|
113
|
+
* Update the @catbee/utils configuration.
|
|
114
|
+
* @param value Partial configuration object.
|
|
115
|
+
*/
|
|
116
|
+
export declare function setConfig(value: Partial<typeof config>): void;
|
|
117
|
+
/**
|
|
118
|
+
* Get the current @catbee/utils configuration.
|
|
119
|
+
* @returns The current configuration object.
|
|
120
|
+
*/
|
|
121
|
+
export declare function getConfig(): typeof config;
|
|
122
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1,138 @@
|
|
|
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.defaultServerConfig = exports.config = void 0;
|
|
27
|
+
exports.setConfig = setConfig;
|
|
28
|
+
exports.getConfig = getConfig;
|
|
29
|
+
const env_utils_1 = require("./utils/env.utils");
|
|
30
|
+
const id_utils_1 = require("./utils/id.utils");
|
|
31
|
+
const obj_utils_1 = require("./utils/obj.utils");
|
|
32
|
+
/**
|
|
33
|
+
* Application runtime configuration loaded from environment variables.
|
|
34
|
+
*/
|
|
35
|
+
exports.config = {
|
|
36
|
+
logger: {
|
|
37
|
+
/**
|
|
38
|
+
* Logging level (e.g., 'info', 'debug', 'warn', 'error').
|
|
39
|
+
*/
|
|
40
|
+
level: env_utils_1.Env.get('LOGGER_LEVEL', 'info'),
|
|
41
|
+
/**
|
|
42
|
+
* Name of the logger instance (defaults to npm package name).
|
|
43
|
+
*/
|
|
44
|
+
name: env_utils_1.Env.get('LOGGER_NAME', env_utils_1.Env.get('npm_package_name', '@catbee/utils')),
|
|
45
|
+
/**
|
|
46
|
+
* Enables pretty-print logging in development.
|
|
47
|
+
* Has no effect in production.
|
|
48
|
+
*/
|
|
49
|
+
pretty: env_utils_1.Env.getBoolean('LOGGER_PRETTY', true),
|
|
50
|
+
/**
|
|
51
|
+
* Single line output for pretty-print (default: false)
|
|
52
|
+
*/
|
|
53
|
+
singleLine: env_utils_1.Env.getBoolean('LOGGER_PRETTY_SINGLE_LINE', false)
|
|
54
|
+
},
|
|
55
|
+
cache: {
|
|
56
|
+
/**
|
|
57
|
+
* Default TTL (time to live) for cache entries in seconds
|
|
58
|
+
*/
|
|
59
|
+
defaultTtl: env_utils_1.Env.getNumber('CACHE_DEFAULT_TTL_SECONDS', 3600) * 1000
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.defaultServerConfig = {
|
|
63
|
+
port: 3000,
|
|
64
|
+
host: '0.0.0.0',
|
|
65
|
+
cors: false,
|
|
66
|
+
helmet: false,
|
|
67
|
+
compression: false,
|
|
68
|
+
bodyParser: {
|
|
69
|
+
json: { limit: '1mb' },
|
|
70
|
+
urlencoded: { extended: true, limit: '1mb' }
|
|
71
|
+
},
|
|
72
|
+
cookieParser: false,
|
|
73
|
+
isMicroservice: false,
|
|
74
|
+
appName: 'express_app',
|
|
75
|
+
globalHeaders: {},
|
|
76
|
+
rateLimit: {
|
|
77
|
+
enable: false,
|
|
78
|
+
windowMs: 15 * 60 * 1000,
|
|
79
|
+
max: 100,
|
|
80
|
+
message: 'Too many requests',
|
|
81
|
+
standardHeaders: true,
|
|
82
|
+
legacyHeaders: false
|
|
83
|
+
},
|
|
84
|
+
requestLogging: {
|
|
85
|
+
enable: env_utils_1.Env.isDev(),
|
|
86
|
+
ignorePaths: (req, _res) => {
|
|
87
|
+
const skipPaths = ['/healthz', '/favicon.ico', '/metrics', '/docs', '/.well-known'];
|
|
88
|
+
return skipPaths.some(path => req.path.startsWith(path));
|
|
89
|
+
},
|
|
90
|
+
skipNotFoundRoutes: false
|
|
91
|
+
},
|
|
92
|
+
openApi: {
|
|
93
|
+
enable: false,
|
|
94
|
+
mountPath: '/docs',
|
|
95
|
+
verbose: false,
|
|
96
|
+
withGlobalPrefix: false
|
|
97
|
+
},
|
|
98
|
+
healthCheck: {
|
|
99
|
+
path: '/healthz',
|
|
100
|
+
detailed: true
|
|
101
|
+
},
|
|
102
|
+
requestTimeout: 30000,
|
|
103
|
+
responseTime: {
|
|
104
|
+
enable: false,
|
|
105
|
+
addHeader: true,
|
|
106
|
+
logOnComplete: false
|
|
107
|
+
},
|
|
108
|
+
requestId: {
|
|
109
|
+
headerName: 'x-request-id',
|
|
110
|
+
exposeHeader: true,
|
|
111
|
+
generator: () => (0, id_utils_1.uuid)()
|
|
112
|
+
},
|
|
113
|
+
metrics: {
|
|
114
|
+
enable: false,
|
|
115
|
+
path: '/metrics',
|
|
116
|
+
withGlobalPrefix: false
|
|
117
|
+
},
|
|
118
|
+
serviceVersion: {
|
|
119
|
+
enable: false,
|
|
120
|
+
headerName: 'x-service-version',
|
|
121
|
+
version: '0.0.0'
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* Update the @catbee/utils configuration.
|
|
126
|
+
* @param value Partial configuration object.
|
|
127
|
+
*/
|
|
128
|
+
function setConfig(value) {
|
|
129
|
+
(0, obj_utils_1.deepObjMerge)(exports.config, value);
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get the current @catbee/utils configuration.
|
|
133
|
+
* @returns The current configuration object.
|
|
134
|
+
*/
|
|
135
|
+
function getConfig() {
|
|
136
|
+
return exports.config;
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/config.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AA8HH,8BAEC;AAMD,8BAEC;AArID,iDAAwC;AACxC,+CAAwC;AAExC,iDAAiD;AAmBjD;;GAEG;AACQ,QAAA,MAAM,GAAG;IAClB,MAAM,EAAE;QACN;;WAEG;QACH,KAAK,EAAE,eAAG,CAAC,GAAG,CAAC,cAAc,EAAE,MAAM,CAAiB;QAEtD;;WAEG;QACH,IAAI,EAAE,eAAG,CAAC,GAAG,CAAC,aAAa,EAAE,eAAG,CAAC,GAAG,CAAC,kBAAkB,EAAE,eAAe,CAAC,CAAC;QAE1E;;;WAGG;QACH,MAAM,EAAE,eAAG,CAAC,UAAU,CAAC,eAAe,EAAE,IAAI,CAAC;QAC7C;;WAEG;QACH,UAAU,EAAE,eAAG,CAAC,UAAU,CAAC,2BAA2B,EAAE,KAAK,CAAC;KAC/D;IAED,KAAK,EAAE;QACL;;WAEG;QACH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ACD,cAAc,EAAE,KAAK;IACrB,YAAY,EAAE;QACZ,MAAM,EAAE,KAAK;QACb,SAAS,EAAE,IAAI;QACf,aAAa,EAAE,KAAK;KACrB;IACD,SAAS,EAAE;QACT,UAAU,EAAE,cAAc;QAC1B,YAAY,EAAE,IAAI;QAClB,SAAS,EAAE,GAAG,EAAE,CAAC,IAAA,eAAI,GAAE;KACxB;IACD,OAAO,EAAE;QACP,MAAM,EAAE,KAAK;QACb,IAAI,EAAE,UAAU;QAChB,gBAAgB,EAAE,KAAK;KACxB;IACD,cAAc,EAAE;QACd,MAAM,EAAE,KAAK;QACb,UAAU,EAAE,mBAAmB;QAC/B,OAAO,EAAE,OAAO;KACjB;CACqB,CAAC;AAEzB;;;GAGG;AACH,SAAgB,SAAS,CAAC,KAA6B;IACrD,IAAA,wBAAY,EAAC,cAAM,EAAE,KAAK,CAAC,CAAC;AAC9B,CAAC;AAED;;;GAGG;AACH,SAAgB,SAAS;IACvB,OAAO,cAAM,CAAC;AAChB,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { ServerConfig } from './types/server';\nimport { Env } from './utils/env.utils';\nimport { uuid } from './utils/id.utils';\nimport { Logger, LoggerLevels } from './utils/logger.utils';\nimport { deepObjMerge } from './utils/obj.utils';\n\n/**\n * Extends Express Request interface to add request ID tracking.\n * Added by requestId middleware during request processing.\n */\ndeclare global {\n namespace Express {\n interface Request {\n /** Unique request identifier (set by middleware) */\n id?: string;\n /** Logger instance for the request (set by middleware) */\n logger?: Logger;\n /** User information (set by authentication middleware) */\n user?: any;\n }\n }\n}\n\n/**\n * Application runtime configuration loaded from environment variables.\n */\nexport let config = {\n logger: {\n /**\n * Logging level (e.g., 'info', 'debug', 'warn', 'error').\n */\n level: Env.get('LOGGER_LEVEL', 'info') as LoggerLevels,\n\n /**\n * Name of the logger instance (defaults to npm package name).\n */\n name: Env.get('LOGGER_NAME', Env.get('npm_package_name', '@catbee/utils')),\n\n /**\n * Enables pretty-print logging in development.\n * Has no effect in production.\n */\n pretty: Env.getBoolean('LOGGER_PRETTY', true),\n /**\n * Single line output for pretty-print (default: false)\n */\n singleLine: Env.getBoolean('LOGGER_PRETTY_SINGLE_LINE', false)\n },\n\n cache: {\n /**\n * Default TTL (time to live) for cache entries in seconds\n */\n defaultTtl: Env.getNumber('CACHE_DEFAULT_TTL_SECONDS', 3600) * 1000\n }\n};\n\nexport const defaultServerConfig = {\n port: 3000,\n host: '0.0.0.0',\n cors: false,\n helmet: false,\n compression: false,\n bodyParser: {\n json: { limit: '1mb' },\n urlencoded: { extended: true, limit: '1mb' }\n },\n cookieParser: false,\n isMicroservice: false,\n appName: 'express_app',\n globalHeaders: {},\n rateLimit: {\n enable: false,\n windowMs: 15 * 60 * 1000,\n max: 100,\n message: 'Too many requests',\n standardHeaders: true,\n legacyHeaders: false\n },\n requestLogging: {\n enable: Env.isDev(),\n ignorePaths: (req, _res) => {\n const skipPaths = ['/healthz', '/favicon.ico', '/metrics', '/docs', '/.well-known'];\n return skipPaths.some(path => req.path.startsWith(path));\n },\n skipNotFoundRoutes: false\n },\n openApi: {\n enable: false,\n mountPath: '/docs',\n verbose: false,\n withGlobalPrefix: false\n },\n healthCheck: {\n path: '/healthz',\n detailed: true\n },\n requestTimeout: 30000,\n responseTime: {\n enable: false,\n addHeader: true,\n logOnComplete: false\n },\n requestId: {\n headerName: 'x-request-id',\n exposeHeader: true,\n generator: () => uuid()\n },\n metrics: {\n enable: false,\n path: '/metrics',\n withGlobalPrefix: false\n },\n serviceVersion: {\n enable: false,\n headerName: 'x-service-version',\n version: '0.0.0'\n }\n} satisfies ServerConfig;\n\n/**\n * Update the @catbee/utils configuration.\n * @param value Partial configuration object.\n */\nexport function setConfig(value: Partial<typeof config>): void {\n deepObjMerge(config, value);\n}\n\n/**\n * Get the current @catbee/utils configuration.\n * @returns The current configuration object.\n */\nexport function getConfig(): typeof config {\n return config;\n}\n"]}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { setConfig, getConfig } from './config';
|
|
2
|
+
export * from './utils/array.utils';
|
|
3
|
+
export * from './utils/async.utils';
|
|
4
|
+
export * from './utils/cache.utils';
|
|
5
|
+
export * from './utils/context-store.utils';
|
|
6
|
+
export * from './utils/crypto.utils';
|
|
7
|
+
export * from './utils/decorators.utils';
|
|
8
|
+
export * from './utils/dir.utils';
|
|
9
|
+
export * from './utils/env.utils';
|
|
10
|
+
export * from './utils/exception.utils';
|
|
11
|
+
export * from './utils/fs.utils';
|
|
12
|
+
export * from './utils/http-status-codes';
|
|
13
|
+
export * from './utils/id.utils';
|
|
14
|
+
export * from './utils/logger.utils';
|
|
15
|
+
export * from './utils/middleware.utils';
|
|
16
|
+
export * from './utils/obj.utils';
|
|
17
|
+
export * from './utils/request.utils';
|
|
18
|
+
export * from './utils/response.utils';
|
|
19
|
+
export * from './utils/string.utils';
|
|
20
|
+
export * from './utils/url.utils';
|
|
21
|
+
export * from './utils/validate.utils';
|
|
22
|
+
export * from './types/index';
|
|
23
|
+
export * from './types/server';
|
|
24
|
+
export * from './types/api-response';
|
|
25
|
+
export * from './servers/server';
|
|
26
|
+
export * from './servers/server.builder';
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|