@catbee/utils 2.0.0-next.0 → 2.0.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/LICENSE +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/obj/index.mjs
DELETED
|
@@ -1,301 +0,0 @@
|
|
|
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
|
-
var __defProp = Object.defineProperty;
|
|
26
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
27
|
-
|
|
28
|
-
// src/obj/obj.utils.ts
|
|
29
|
-
function isObjEmpty(obj) {
|
|
30
|
-
return !!obj && typeof obj === "object" && Object.keys(obj).length === 0;
|
|
31
|
-
}
|
|
32
|
-
__name(isObjEmpty, "isObjEmpty");
|
|
33
|
-
function pick(obj, keys) {
|
|
34
|
-
return Object.fromEntries(keys.map((key) => [
|
|
35
|
-
key,
|
|
36
|
-
obj[key]
|
|
37
|
-
]));
|
|
38
|
-
}
|
|
39
|
-
__name(pick, "pick");
|
|
40
|
-
function omit(obj, keys) {
|
|
41
|
-
return Object.fromEntries(Object.entries(obj).filter(([key]) => !keys.includes(key)));
|
|
42
|
-
}
|
|
43
|
-
__name(omit, "omit");
|
|
44
|
-
function deepObjMerge(target, ...sources) {
|
|
45
|
-
if (!sources.length) return target;
|
|
46
|
-
const seen = /* @__PURE__ */ new WeakMap();
|
|
47
|
-
seen.set(target, target);
|
|
48
|
-
for (const s of sources) {
|
|
49
|
-
if (s && typeof s === "object") {
|
|
50
|
-
seen.set(s, target);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function deepClone2(value) {
|
|
54
|
-
if (value === null || typeof value !== "object") return value;
|
|
55
|
-
if (seen.has(value)) return seen.get(value);
|
|
56
|
-
if (value instanceof Date) {
|
|
57
|
-
return new Date(value.getTime());
|
|
58
|
-
}
|
|
59
|
-
if (Array.isArray(value)) {
|
|
60
|
-
const clone2 = [];
|
|
61
|
-
seen.set(value, clone2);
|
|
62
|
-
value.forEach((item, i) => clone2[i] = deepClone2(item));
|
|
63
|
-
return clone2;
|
|
64
|
-
}
|
|
65
|
-
if (value instanceof Set) {
|
|
66
|
-
const clone2 = /* @__PURE__ */ new Set();
|
|
67
|
-
seen.set(value, clone2);
|
|
68
|
-
value.forEach((item) => clone2.add(deepClone2(item)));
|
|
69
|
-
return clone2;
|
|
70
|
-
}
|
|
71
|
-
if (value instanceof Map) {
|
|
72
|
-
const clone2 = /* @__PURE__ */ new Map();
|
|
73
|
-
seen.set(value, clone2);
|
|
74
|
-
value.forEach((val, key) => clone2.set(key, deepClone2(val)));
|
|
75
|
-
return clone2;
|
|
76
|
-
}
|
|
77
|
-
if (value instanceof RegExp) {
|
|
78
|
-
return new RegExp(value.source, value.flags);
|
|
79
|
-
}
|
|
80
|
-
if (value instanceof ArrayBuffer) {
|
|
81
|
-
return value.slice(0);
|
|
82
|
-
}
|
|
83
|
-
if (ArrayBuffer.isView(value) && !(value instanceof DataView)) {
|
|
84
|
-
const typed = value;
|
|
85
|
-
const ctor = typed.constructor;
|
|
86
|
-
const typedArrayCtors = [
|
|
87
|
-
Int8Array,
|
|
88
|
-
Uint8Array,
|
|
89
|
-
Uint8ClampedArray,
|
|
90
|
-
Int16Array,
|
|
91
|
-
Uint16Array,
|
|
92
|
-
Int32Array,
|
|
93
|
-
Uint32Array,
|
|
94
|
-
Float32Array,
|
|
95
|
-
Float64Array,
|
|
96
|
-
BigInt64Array,
|
|
97
|
-
BigUint64Array
|
|
98
|
-
].filter(Boolean);
|
|
99
|
-
const typedArrayCtorSet = new Set(typedArrayCtors);
|
|
100
|
-
if (typeof ctor === "function" && typedArrayCtorSet.has(ctor)) {
|
|
101
|
-
const buffer = typed.buffer && typeof typed.buffer.slice === "function" ? typed.buffer.slice(0) : typed.buffer;
|
|
102
|
-
const clone2 = new ctor(buffer, typed.byteOffset, typed.length);
|
|
103
|
-
seen.set(value, clone2);
|
|
104
|
-
return clone2;
|
|
105
|
-
} else if (typeof typed.slice === "function") {
|
|
106
|
-
const clone2 = typed.slice();
|
|
107
|
-
seen.set(value, clone2);
|
|
108
|
-
return clone2;
|
|
109
|
-
} else {
|
|
110
|
-
throw new Error("Cannot deep clone unknown TypedArray type: " + (typed && typed.constructor && typed.constructor.name));
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
const clone = Object.create(Object.getPrototypeOf(value));
|
|
114
|
-
seen.set(value, clone);
|
|
115
|
-
Reflect.ownKeys(value).forEach((key) => {
|
|
116
|
-
const descriptor = Object.getOwnPropertyDescriptor(value, key);
|
|
117
|
-
if (descriptor.get || descriptor.set) {
|
|
118
|
-
Object.defineProperty(clone, key, descriptor);
|
|
119
|
-
} else {
|
|
120
|
-
clone[key] = deepClone2(value[key]);
|
|
121
|
-
}
|
|
122
|
-
});
|
|
123
|
-
return clone;
|
|
124
|
-
}
|
|
125
|
-
__name(deepClone2, "deepClone");
|
|
126
|
-
function mergeProp(target2, source, key) {
|
|
127
|
-
const targetValue = target2[key];
|
|
128
|
-
const sourceValue = source[key];
|
|
129
|
-
if (sourceValue === void 0) return;
|
|
130
|
-
if (sourceValue === null || typeof sourceValue !== "object") {
|
|
131
|
-
target2[key] = sourceValue;
|
|
132
|
-
return;
|
|
133
|
-
}
|
|
134
|
-
if (Array.isArray(sourceValue)) {
|
|
135
|
-
target2[key] = deepClone2(sourceValue);
|
|
136
|
-
return;
|
|
137
|
-
}
|
|
138
|
-
if (sourceValue instanceof Date || sourceValue instanceof RegExp || sourceValue instanceof Set || sourceValue instanceof Map || sourceValue instanceof ArrayBuffer || ArrayBuffer.isView(sourceValue) && !(sourceValue instanceof DataView)) {
|
|
139
|
-
target2[key] = deepClone2(sourceValue);
|
|
140
|
-
return;
|
|
141
|
-
}
|
|
142
|
-
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
143
|
-
for (const k of Reflect.ownKeys(sourceValue)) {
|
|
144
|
-
mergeProp(targetValue, sourceValue, k);
|
|
145
|
-
}
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
target2[key] = deepClone2(sourceValue);
|
|
149
|
-
}
|
|
150
|
-
__name(mergeProp, "mergeProp");
|
|
151
|
-
for (const source of sources) {
|
|
152
|
-
if (source == null) continue;
|
|
153
|
-
for (const key of Reflect.ownKeys(source)) {
|
|
154
|
-
mergeProp(target, source, key);
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
return target;
|
|
158
|
-
}
|
|
159
|
-
__name(deepObjMerge, "deepObjMerge");
|
|
160
|
-
function isPlainObject(value) {
|
|
161
|
-
if (typeof value !== "object" || value === null) return false;
|
|
162
|
-
if (Array.isArray(value)) return false;
|
|
163
|
-
const proto = Object.getPrototypeOf(value);
|
|
164
|
-
return proto === Object.prototype || proto === null;
|
|
165
|
-
}
|
|
166
|
-
__name(isPlainObject, "isPlainObject");
|
|
167
|
-
function flattenObject(obj, prefix = "") {
|
|
168
|
-
return Object.entries(obj).reduce((acc, [key, value]) => {
|
|
169
|
-
const newKey = prefix ? `${prefix}.${key}` : key;
|
|
170
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
171
|
-
Object.assign(acc, flattenObject(value, newKey));
|
|
172
|
-
} else {
|
|
173
|
-
acc[newKey] = value;
|
|
174
|
-
}
|
|
175
|
-
return acc;
|
|
176
|
-
}, {});
|
|
177
|
-
}
|
|
178
|
-
__name(flattenObject, "flattenObject");
|
|
179
|
-
function getValueByPath(obj, path) {
|
|
180
|
-
if (!obj || typeof obj !== "object") return void 0;
|
|
181
|
-
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
182
|
-
return parts.reduce((acc, key) => acc?.[key], obj);
|
|
183
|
-
}
|
|
184
|
-
__name(getValueByPath, "getValueByPath");
|
|
185
|
-
function setValueByPath(obj, path, value) {
|
|
186
|
-
if (!obj || typeof obj !== "object") return obj;
|
|
187
|
-
const parts = path.replace(/\[(\d+)\]/g, ".$1").split(".").filter(Boolean);
|
|
188
|
-
if (parts.length === 0) return obj;
|
|
189
|
-
const forbiddenKeys = /* @__PURE__ */ new Set([
|
|
190
|
-
"__proto__",
|
|
191
|
-
"prototype",
|
|
192
|
-
"constructor"
|
|
193
|
-
]);
|
|
194
|
-
let current = obj;
|
|
195
|
-
for (let i = 0; i < parts.length - 1; i++) {
|
|
196
|
-
const key = parts[i];
|
|
197
|
-
if (forbiddenKeys.has(key)) {
|
|
198
|
-
return obj;
|
|
199
|
-
}
|
|
200
|
-
const nextKey = parts[i + 1];
|
|
201
|
-
const isArrayIndex = !isNaN(Number(nextKey));
|
|
202
|
-
if (current[key] === void 0) {
|
|
203
|
-
current[key] = isArrayIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
204
|
-
} else if (typeof current[key] !== "object" || current[key] === null) {
|
|
205
|
-
current[key] = isArrayIndex ? [] : /* @__PURE__ */ Object.create(null);
|
|
206
|
-
}
|
|
207
|
-
current = current[key];
|
|
208
|
-
}
|
|
209
|
-
const finalKey = parts[parts.length - 1];
|
|
210
|
-
if (forbiddenKeys.has(finalKey)) {
|
|
211
|
-
return obj;
|
|
212
|
-
}
|
|
213
|
-
current[finalKey] = value;
|
|
214
|
-
return obj;
|
|
215
|
-
}
|
|
216
|
-
__name(setValueByPath, "setValueByPath");
|
|
217
|
-
function isEqual(a, b) {
|
|
218
|
-
if (a === b) return true;
|
|
219
|
-
if (a == null || b == null || typeof a !== "object" || typeof b !== "object") {
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
if (a instanceof Date && b instanceof Date) {
|
|
223
|
-
return a.getTime() === b.getTime();
|
|
224
|
-
}
|
|
225
|
-
if (a instanceof Date || b instanceof Date) {
|
|
226
|
-
return false;
|
|
227
|
-
}
|
|
228
|
-
if (Array.isArray(a) && Array.isArray(b)) {
|
|
229
|
-
if (a.length !== b.length) return false;
|
|
230
|
-
for (let i = 0; i < a.length; i++) {
|
|
231
|
-
if (!isEqual(a[i], b[i])) return false;
|
|
232
|
-
}
|
|
233
|
-
return true;
|
|
234
|
-
}
|
|
235
|
-
if (Array.isArray(a) || Array.isArray(b)) {
|
|
236
|
-
return false;
|
|
237
|
-
}
|
|
238
|
-
const keysA = Object.keys(a);
|
|
239
|
-
const keysB = Object.keys(b);
|
|
240
|
-
if (keysA.length !== keysB.length) return false;
|
|
241
|
-
return keysA.every((key) => Object.prototype.hasOwnProperty.call(b, key) && isEqual(a[key], b[key]));
|
|
242
|
-
}
|
|
243
|
-
__name(isEqual, "isEqual");
|
|
244
|
-
function filterObject(obj, predicate) {
|
|
245
|
-
return Object.fromEntries(Object.entries(obj).filter(([key, value]) => predicate(value, key, obj)));
|
|
246
|
-
}
|
|
247
|
-
__name(filterObject, "filterObject");
|
|
248
|
-
function mapObject(obj, mapFn) {
|
|
249
|
-
return Object.fromEntries(Object.entries(obj).map(([key, value]) => [
|
|
250
|
-
key,
|
|
251
|
-
mapFn(value, key, obj)
|
|
252
|
-
]));
|
|
253
|
-
}
|
|
254
|
-
__name(mapObject, "mapObject");
|
|
255
|
-
function deepClone(value) {
|
|
256
|
-
try {
|
|
257
|
-
if (typeof globalThis.structuredClone === "function") {
|
|
258
|
-
return globalThis.structuredClone(value);
|
|
259
|
-
}
|
|
260
|
-
} catch (_) {
|
|
261
|
-
}
|
|
262
|
-
try {
|
|
263
|
-
return JSON.parse(JSON.stringify(value));
|
|
264
|
-
} catch (_) {
|
|
265
|
-
return value;
|
|
266
|
-
}
|
|
267
|
-
}
|
|
268
|
-
__name(deepClone, "deepClone");
|
|
269
|
-
function deepFreeze(obj) {
|
|
270
|
-
Object.freeze(obj);
|
|
271
|
-
for (const prop of Object.getOwnPropertyNames(obj)) {
|
|
272
|
-
const value = obj[prop];
|
|
273
|
-
if (value !== null && typeof value === "object" && !Object.isFrozen(value)) {
|
|
274
|
-
deepFreeze(value);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
return obj;
|
|
278
|
-
}
|
|
279
|
-
__name(deepFreeze, "deepFreeze");
|
|
280
|
-
function isObject(value) {
|
|
281
|
-
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
282
|
-
}
|
|
283
|
-
__name(isObject, "isObject");
|
|
284
|
-
function getAllPaths(obj, parentPath = "") {
|
|
285
|
-
if (!isObject(obj)) return [];
|
|
286
|
-
return Object.entries(obj).flatMap(([key, value]) => {
|
|
287
|
-
const currentPath = parentPath ? `${parentPath}.${key}` : key;
|
|
288
|
-
if (isObject(value)) {
|
|
289
|
-
return [
|
|
290
|
-
currentPath,
|
|
291
|
-
...getAllPaths(value, currentPath)
|
|
292
|
-
];
|
|
293
|
-
}
|
|
294
|
-
return [
|
|
295
|
-
currentPath
|
|
296
|
-
];
|
|
297
|
-
});
|
|
298
|
-
}
|
|
299
|
-
__name(getAllPaths, "getAllPaths");
|
|
300
|
-
|
|
301
|
-
export { deepClone, deepFreeze, deepObjMerge, filterObject, flattenObject, getAllPaths, getValueByPath, isEqual, isObjEmpty, isObject, isPlainObject, mapObject, omit, pick, setValueByPath };
|
|
@@ -1,159 +0,0 @@
|
|
|
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
|
-
* Options for timing function execution.
|
|
27
|
-
*/
|
|
28
|
-
export interface TimingOptions {
|
|
29
|
-
/** Optional label for the timing (defaults to function name) */
|
|
30
|
-
label?: string;
|
|
31
|
-
/** Whether to log the timing (default: false) */
|
|
32
|
-
log?: boolean;
|
|
33
|
-
/** Log level to use if logging is enabled (default: 'debug') */
|
|
34
|
-
logLevel?: 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Result of a timing operation.
|
|
38
|
-
*/
|
|
39
|
-
export interface TimingResult {
|
|
40
|
-
/** Duration in milliseconds */
|
|
41
|
-
durationMs: number;
|
|
42
|
-
/** Duration in seconds */
|
|
43
|
-
durationSec: number;
|
|
44
|
-
/** Start timestamp */
|
|
45
|
-
startTime: number;
|
|
46
|
-
/** End timestamp */
|
|
47
|
-
endTime: number;
|
|
48
|
-
/** Label used for the timing */
|
|
49
|
-
label: string;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Measure the execution time of a synchronous function.
|
|
53
|
-
*
|
|
54
|
-
* @param fn - Function to measure
|
|
55
|
-
* @param options - Timing options
|
|
56
|
-
* @returns Result containing the return value and timing information
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* ```typescript
|
|
60
|
-
* const { result, timing } = timeSync(() => {
|
|
61
|
-
* // Some expensive operation
|
|
62
|
-
* return computeResult();
|
|
63
|
-
* }, { label: 'Computation', log: true });
|
|
64
|
-
*
|
|
65
|
-
* console.log(`Result: ${result}, took ${timing.durationMs}ms`);
|
|
66
|
-
* ```
|
|
67
|
-
*/
|
|
68
|
-
export declare function timeSync<T>(fn: () => T, options?: TimingOptions): {
|
|
69
|
-
result: T;
|
|
70
|
-
timing: TimingResult;
|
|
71
|
-
};
|
|
72
|
-
/**
|
|
73
|
-
* Measure the execution time of an asynchronous function.
|
|
74
|
-
*
|
|
75
|
-
* @param fn - Async function to measure
|
|
76
|
-
* @param options - Timing options
|
|
77
|
-
* @returns Promise resolving to result and timing information
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```typescript
|
|
81
|
-
* const { result, timing } = await timeAsync(async () => {
|
|
82
|
-
* // Some expensive async operation
|
|
83
|
-
* const data = await fetchData();
|
|
84
|
-
* return processData(data);
|
|
85
|
-
* }, { label: 'API Request', log: true });
|
|
86
|
-
*
|
|
87
|
-
* console.log(`Fetched ${result.length} items in ${timing.durationSec.toFixed(2)}s`);
|
|
88
|
-
* ```
|
|
89
|
-
*/
|
|
90
|
-
export declare function timeAsync<T>(fn: () => Promise<T>, options?: TimingOptions): Promise<{
|
|
91
|
-
result: T;
|
|
92
|
-
timing: TimingResult;
|
|
93
|
-
}>;
|
|
94
|
-
/**
|
|
95
|
-
* Create a timing decorator for class methods.
|
|
96
|
-
*
|
|
97
|
-
* @param options - Timing options
|
|
98
|
-
* @returns Method decorator
|
|
99
|
-
*
|
|
100
|
-
* @example
|
|
101
|
-
* ```typescript
|
|
102
|
-
* class DataService {
|
|
103
|
-
* @timed({ log: true, logLevel: 'info' })
|
|
104
|
-
* async fetchData() {
|
|
105
|
-
* // ...implementation
|
|
106
|
-
* }
|
|
107
|
-
* }
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
export declare function timed(options?: TimingOptions): (target: any, propertyKeyOrContext: string | symbol | any, descriptor?: PropertyDescriptor) => void;
|
|
111
|
-
/**
|
|
112
|
-
* Memoize function results with optional TTL and max cache size.
|
|
113
|
-
*
|
|
114
|
-
* @param fn - Function to memoize
|
|
115
|
-
* @param options - Memoization options
|
|
116
|
-
* @returns Memoized function
|
|
117
|
-
*
|
|
118
|
-
* @example
|
|
119
|
-
* ```typescript
|
|
120
|
-
* // Cache results for 30 seconds, with a maximum of 100 entries
|
|
121
|
-
* const cachedFetch = memoize(
|
|
122
|
-
* async (url) => {
|
|
123
|
-
* const response = await fetch(url);
|
|
124
|
-
* return response.json();
|
|
125
|
-
* },
|
|
126
|
-
* { ttl: 30000, maxSize: 100, cacheKey: (url) => url }
|
|
127
|
-
* );
|
|
128
|
-
* ```
|
|
129
|
-
*/
|
|
130
|
-
export declare function memoize<T, Args extends any[]>(fn: (...args: Args) => T, options?: {
|
|
131
|
-
/** Time-to-live in milliseconds (default: indefinite) */
|
|
132
|
-
ttl?: number;
|
|
133
|
-
/** Maximum cache size (default: unlimited) */
|
|
134
|
-
maxSize?: number;
|
|
135
|
-
/** Function to generate a cache key from arguments */
|
|
136
|
-
cacheKey?: (...args: Args) => string;
|
|
137
|
-
/** Auto-cleanup interval in milliseconds (default: disabled) */
|
|
138
|
-
autoCleanupMs?: number;
|
|
139
|
-
}): (...args: Args) => T;
|
|
140
|
-
/**
|
|
141
|
-
* Track memory usage for a function execution.
|
|
142
|
-
*
|
|
143
|
-
* @param fn - Function to track
|
|
144
|
-
* @param options - Memory tracking options
|
|
145
|
-
* @returns Result and memory usage information
|
|
146
|
-
*/
|
|
147
|
-
export declare function trackMemoryUsage<T>(fn: () => T, options?: {
|
|
148
|
-
/** Whether to log the memory usage (default: false) */
|
|
149
|
-
log?: boolean;
|
|
150
|
-
/** Label for the memory tracking (default: function name) */
|
|
151
|
-
label?: string;
|
|
152
|
-
}): {
|
|
153
|
-
result: T;
|
|
154
|
-
memoryUsage: {
|
|
155
|
-
before: NodeJS.MemoryUsage;
|
|
156
|
-
after: NodeJS.MemoryUsage;
|
|
157
|
-
diff: Record<string, number>;
|
|
158
|
-
};
|
|
159
|
-
};
|
|
@@ -1,109 +0,0 @@
|
|
|
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 { Request } from 'express';
|
|
26
|
-
import type { WithPagination } from '@catbee/utils/response';
|
|
27
|
-
/**
|
|
28
|
-
* Options for parsing and validating request parameters.
|
|
29
|
-
*/
|
|
30
|
-
export interface ValidationOptions {
|
|
31
|
-
/** Whether to throw an error on validation failure */
|
|
32
|
-
throwOnError?: boolean;
|
|
33
|
-
/** Custom error message for validation failures */
|
|
34
|
-
errorMessage?: string;
|
|
35
|
-
/** Default value to use if parameter is missing */
|
|
36
|
-
defaultValue?: any;
|
|
37
|
-
/** Whether the parameter is required */
|
|
38
|
-
required?: boolean;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* Result of a parameter validation operation.
|
|
42
|
-
*/
|
|
43
|
-
export interface ValidationResult<T> {
|
|
44
|
-
/** Whether validation was successful */
|
|
45
|
-
isValid: boolean;
|
|
46
|
-
/** The validated and potentially transformed value */
|
|
47
|
-
value: T | null;
|
|
48
|
-
/** Error message if validation failed */
|
|
49
|
-
error?: string;
|
|
50
|
-
}
|
|
51
|
-
/**
|
|
52
|
-
* Extracts pagination, sorting, and optional search params from a request.
|
|
53
|
-
*
|
|
54
|
-
* @param req - Express request
|
|
55
|
-
* @returns Object containing validated pagination, sorting, and additional query params
|
|
56
|
-
*/
|
|
57
|
-
export declare const getPaginationParams: <T = {}>(req: Request) => WithPagination<T>;
|
|
58
|
-
/**
|
|
59
|
-
* Safely parses a string parameter to a number.
|
|
60
|
-
*
|
|
61
|
-
* @param value - String value to parse
|
|
62
|
-
* @param options - Validation options
|
|
63
|
-
* @returns Validation result containing the parsed number or error
|
|
64
|
-
*/
|
|
65
|
-
export declare function parseNumberParam(value: string | undefined, options?: ValidationOptions): ValidationResult<number>;
|
|
66
|
-
/**
|
|
67
|
-
* Safely parses a string parameter to a boolean.
|
|
68
|
-
*
|
|
69
|
-
* @param value - String value to parse
|
|
70
|
-
* @param options - Validation options
|
|
71
|
-
* @returns Validation result containing the parsed boolean or error
|
|
72
|
-
*/
|
|
73
|
-
export declare function parseBooleanParam(value: string | undefined, options?: ValidationOptions): ValidationResult<boolean>;
|
|
74
|
-
/**
|
|
75
|
-
* Extracts pagination parameters from request query parameters.
|
|
76
|
-
*
|
|
77
|
-
* @param query - Query parameters object
|
|
78
|
-
* @param defaultPage - Default page number if not specified (defaults to 1)
|
|
79
|
-
* @param defaultLimit - Default per page size if not specified (defaults to 20)
|
|
80
|
-
* @param maxLimitSize - Maximum allowed per page size (defaults to 100)
|
|
81
|
-
* @returns Object containing validated page and limit
|
|
82
|
-
*/
|
|
83
|
-
export declare function extractPaginationParams(query: Record<string, string | string[]>, defaultPage?: number, defaultLimit?: number, maxLimitSize?: number): {
|
|
84
|
-
page: number;
|
|
85
|
-
limit: number;
|
|
86
|
-
};
|
|
87
|
-
/**
|
|
88
|
-
* Extracts sorting parameters from request query parameters.
|
|
89
|
-
*
|
|
90
|
-
* @param query - Query parameters object
|
|
91
|
-
* @param allowedFields - Array of field names that are allowed to be sorted
|
|
92
|
-
* @param defaultSort - Default sort configuration if not specified
|
|
93
|
-
* @returns Object containing sort field and direction
|
|
94
|
-
*/
|
|
95
|
-
export declare function extractSortParams(query: Record<string, string | string[]>, allowedFields: string[], defaultSort?: {
|
|
96
|
-
sortBy: string;
|
|
97
|
-
sortOrder: 'asc' | 'desc';
|
|
98
|
-
}): {
|
|
99
|
-
sortBy: string;
|
|
100
|
-
sortOrder: 'asc' | 'desc';
|
|
101
|
-
};
|
|
102
|
-
/**
|
|
103
|
-
* Extracts filter parameters from query parameters based on allowed filter fields.
|
|
104
|
-
*
|
|
105
|
-
* @param query - Query parameters object
|
|
106
|
-
* @param allowedFilters - Array of field names that are allowed to be used as filters
|
|
107
|
-
* @returns Object containing the filters as key-value pairs
|
|
108
|
-
*/
|
|
109
|
-
export declare function extractFilterParams(query: Record<string, string | string[]>, allowedFilters: string[]): Record<string, string | string[]>;
|