@catbee/utils 2.0.0-next.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/README.md +52 -16
- package/array/index.cjs +180 -71
- package/array/index.d.ts +293 -1
- package/array/index.mjs +171 -72
- package/async/index.cjs +92 -36
- package/async/index.d.ts +275 -1
- package/async/index.mjs +92 -36
- package/cache/index.cjs +1 -1
- package/cache/index.d.ts +155 -1
- package/cache/index.mjs +2 -2
- package/config/index.cjs +78 -64
- package/config/index.d.ts +64 -2
- package/config/index.mjs +76 -64
- package/context-store/index.d.ts +192 -1
- package/crypto/index.d.ts +163 -1
- package/date/index.cjs +46 -1
- package/date/index.d.ts +190 -1
- package/date/index.mjs +45 -2
- package/decorators/index.cjs +1156 -18
- package/decorators/index.d.ts +684 -1
- package/decorators/index.mjs +1156 -18
- package/dir/index.cjs +4 -3
- package/dir/index.d.ts +195 -1
- package/dir/index.mjs +4 -3
- package/env/index.cjs +10 -26
- package/env/index.d.ts +379 -1
- package/env/index.mjs +10 -26
- package/exception/index.d.ts +232 -1
- package/fs/index.cjs +70 -36
- package/fs/index.d.ts +205 -1
- package/fs/index.mjs +64 -34
- package/http-status-codes/index.d.ts +267 -1
- package/id/index.d.ts +37 -1
- package/index.cjs +3 -3
- package/index.d.ts +1 -1
- package/index.mjs +1 -1
- package/logger/index.cjs +11 -11
- package/logger/index.d.ts +189 -1
- package/logger/index.mjs +12 -12
- package/middleware/index.d.ts +103 -1
- package/obj/index.cjs +150 -162
- package/obj/index.d.ts +136 -1
- package/obj/index.mjs +150 -162
- package/package.json +11 -11
- package/performance/index.cjs +2 -2
- package/performance/index.d.ts +138 -1
- package/performance/index.mjs +2 -2
- package/request/index.cjs +1 -1
- package/request/index.d.ts +241 -2
- package/request/index.mjs +1 -1
- package/response/index.d.ts +318 -2
- package/server/index.cjs +27 -23
- package/server/index.d.ts +785 -4
- package/server/index.mjs +28 -23
- package/stream/index.d.ts +90 -1
- package/string/index.d.ts +102 -1
- package/type/index.cjs +1 -1
- package/type/index.d.ts +107 -1
- package/type/index.mjs +1 -1
- package/types/index.d.ts +774 -4
- package/url/index.cjs +2 -4
- package/url/index.d.ts +142 -1
- package/url/index.mjs +2 -4
- package/{validate → validation}/index.cjs +89 -42
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
- package/{validate → validation}/index.mjs +85 -42
- 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/decorators.utils.d.ts +0 -705
- package/dir/dir.utils.d.ts +0 -216
- 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/obj.utils.d.ts +0 -156
- 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/obj.utils.d.ts
DELETED
|
@@ -1,156 +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
|
-
* Checks whether the object has no own enumerable properties.
|
|
27
|
-
*
|
|
28
|
-
* @param {Record<any, any>} obj - The object to check.
|
|
29
|
-
* @returns {boolean} True if the object is empty, false otherwise.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isObjEmpty(obj: Record<any, any>): boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Returns a new object with only the specified keys picked.
|
|
34
|
-
*
|
|
35
|
-
* @template T
|
|
36
|
-
* @template K
|
|
37
|
-
* @param {T} obj - The source object.
|
|
38
|
-
* @param {K[]} keys - Keys to pick from the object.
|
|
39
|
-
* @returns {Pick<T, K>} New object with picked keys.
|
|
40
|
-
*/
|
|
41
|
-
export declare function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K>;
|
|
42
|
-
/**
|
|
43
|
-
* Returns a new object with the specified keys omitted.
|
|
44
|
-
*
|
|
45
|
-
* @template T
|
|
46
|
-
* @template K
|
|
47
|
-
* @param {T} obj - The source object.
|
|
48
|
-
* @param {K[]} keys - Keys to omit from the object.
|
|
49
|
-
* @returns {Omit<T, K>} New object without omitted keys.
|
|
50
|
-
*/
|
|
51
|
-
export declare function omit<T extends object, K extends keyof T>(obj: T, keys: K[]): Omit<T, K>;
|
|
52
|
-
/**
|
|
53
|
-
* Deeply merges multiple objects into the target object (mutates and returns the target object, not pure).
|
|
54
|
-
*
|
|
55
|
-
* @template T
|
|
56
|
-
* @param {T} target - The object to merge into (will be mutated).
|
|
57
|
-
* @param {...any[]} sources - The objects to merge from.
|
|
58
|
-
* @returns {T} The merged object (same as target).
|
|
59
|
-
*/
|
|
60
|
-
export declare function deepObjMerge<T extends object>(target: T, ...sources: any[]): T;
|
|
61
|
-
/**
|
|
62
|
-
* Checks if a value is a plain object (i.e., not an array, null, or a built-in object).
|
|
63
|
-
*
|
|
64
|
-
* @param value - The value to check.
|
|
65
|
-
* @returns True if the value is a plain object, false otherwise.
|
|
66
|
-
*/
|
|
67
|
-
export declare function isPlainObject(value: any): value is Record<string, any>;
|
|
68
|
-
/**
|
|
69
|
-
* Flattens a nested object using dot notation for keys (e.g., `{a: {b: 1}}` → `{ "a.b": 1 }`).
|
|
70
|
-
*
|
|
71
|
-
* @template T
|
|
72
|
-
* @param {T} obj - The object to flatten.
|
|
73
|
-
* @param {string} [prefix=""] - Optional prefix for nested keys (used internally).
|
|
74
|
-
* @returns {Record<string, any>} A new object with flattened keys.
|
|
75
|
-
*/
|
|
76
|
-
export declare function flattenObject<T extends Record<string, any>>(obj: T, prefix?: string): Record<string, any>;
|
|
77
|
-
/**
|
|
78
|
-
* Safely gets the value of a deeply nested key in an object using dot/bracket notation path.
|
|
79
|
-
*
|
|
80
|
-
* @template T
|
|
81
|
-
* @param {T} obj - The object to extract from.
|
|
82
|
-
* @param {string} path - String path using dot and/or bracket notation (e.g., 'user.friends[0].name').
|
|
83
|
-
* @returns {any} The value at the given path, or undefined if not found.
|
|
84
|
-
*/
|
|
85
|
-
export declare function getValueByPath<T extends object>(obj: T, path: string): any;
|
|
86
|
-
/**
|
|
87
|
-
* Sets a value at a deeply nested key in an object using dot/bracket notation path.
|
|
88
|
-
* Secured against prototype pollution.
|
|
89
|
-
*
|
|
90
|
-
* @template T
|
|
91
|
-
* @param {T} obj - The object to modify.
|
|
92
|
-
* @param {string} path - Dot/bracket notation path (e.g., "user.friends[0].name").
|
|
93
|
-
* @param {any} value - Value to assign.
|
|
94
|
-
* @returns {T} The modified object.
|
|
95
|
-
*/
|
|
96
|
-
export declare function setValueByPath<T extends object>(obj: T, path: string, value: any): T;
|
|
97
|
-
/**
|
|
98
|
-
* Performs a deep equality check between two objects.
|
|
99
|
-
*
|
|
100
|
-
* @param {any} a - First value to compare.
|
|
101
|
-
* @param {any} b - Second value to compare.
|
|
102
|
-
* @returns {boolean} True if values are deeply equal.
|
|
103
|
-
*/
|
|
104
|
-
export declare function isEqual(a: any, b: any): boolean;
|
|
105
|
-
/**
|
|
106
|
-
* Filters object properties based on a predicate function.
|
|
107
|
-
*
|
|
108
|
-
* @template T
|
|
109
|
-
* @param {T} obj - The source object.
|
|
110
|
-
* @param {(value: any, key: string, obj: T) => boolean} predicate - Filter function.
|
|
111
|
-
* @returns {Partial<T>} New object with filtered properties.
|
|
112
|
-
*/
|
|
113
|
-
export declare function filterObject<T extends object>(obj: T, predicate: (value: any, key: string, obj: T) => boolean): Partial<T>;
|
|
114
|
-
/**
|
|
115
|
-
* Maps object values to new values using a mapping function.
|
|
116
|
-
*
|
|
117
|
-
* @template T
|
|
118
|
-
* @template U
|
|
119
|
-
* @param {T} obj - The source object.
|
|
120
|
-
* @param {(value: any, key: string, obj: T) => U} mapFn - Mapping function.
|
|
121
|
-
* @returns {Record<keyof T, U>} New object with mapped values.
|
|
122
|
-
*/
|
|
123
|
-
export declare function mapObject<T extends object, U>(obj: T, mapFn: (value: any, key: string, obj: T) => U): Record<keyof T, U>;
|
|
124
|
-
/**
|
|
125
|
-
* Create a deep clone of a value. Uses structuredClone when available, falls back to JSON
|
|
126
|
-
* serialization as a best-effort fallback (may lose functions and special objects).
|
|
127
|
-
*
|
|
128
|
-
* @template T
|
|
129
|
-
* @param {T} value
|
|
130
|
-
* @returns {T}
|
|
131
|
-
*/
|
|
132
|
-
export declare function deepClone<T>(value: T): T;
|
|
133
|
-
/**
|
|
134
|
-
* Recursively freezes an object and all its properties.
|
|
135
|
-
* Makes an object immutable.
|
|
136
|
-
*
|
|
137
|
-
* @template T
|
|
138
|
-
* @param {T} obj - The object to freeze.
|
|
139
|
-
* @returns {Readonly<T>} The frozen object.
|
|
140
|
-
*/
|
|
141
|
-
export declare function deepFreeze<T extends object>(obj: T): Readonly<T>;
|
|
142
|
-
/**
|
|
143
|
-
* Safely checks if a value is an object (not null, not array).
|
|
144
|
-
*
|
|
145
|
-
* @param {unknown} value - Value to check.
|
|
146
|
-
* @returns {boolean} True if value is a non-null, non-array object.
|
|
147
|
-
*/
|
|
148
|
-
export declare function isObject(value: unknown): value is Record<string, any>;
|
|
149
|
-
/**
|
|
150
|
-
* Gets all paths in an object using dot notation.
|
|
151
|
-
*
|
|
152
|
-
* @param {Record<string, any>} obj - Object to analyze.
|
|
153
|
-
* @param {string} [parentPath=""] - Internal param for recursion.
|
|
154
|
-
* @returns {string[]} Array of all paths in dot notation.
|
|
155
|
-
*/
|
|
156
|
-
export declare function getAllPaths(obj: Record<string, any>, parentPath?: string): string[];
|
|
@@ -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[]>;
|
|
@@ -1,186 +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 type { Request } from 'express';
|
|
26
|
-
import { ApiErrorResponse, ApiResponse } from '@catbee/utils/response';
|
|
27
|
-
/**
|
|
28
|
-
* Standard HTTP response wrapper for successful responses.
|
|
29
|
-
* Implements the `ApiResponse<T>` interface and sets default values.
|
|
30
|
-
*
|
|
31
|
-
* @typeParam T - The shape of the data returned in the response.
|
|
32
|
-
*/
|
|
33
|
-
export declare class SuccessResponse<T> implements ApiResponse<T> {
|
|
34
|
-
/** Message describing the result of the operation. */
|
|
35
|
-
message: string;
|
|
36
|
-
/** Whether the response is an error. Always false in success responses. */
|
|
37
|
-
error: boolean;
|
|
38
|
-
/** The payload returned from the API. */
|
|
39
|
-
data: T | null;
|
|
40
|
-
/** Timestamp when the response was generated, in ISO format. */
|
|
41
|
-
timestamp: string;
|
|
42
|
-
/** Unique identifier for this response, useful for request tracing. */
|
|
43
|
-
requestId: string;
|
|
44
|
-
/**
|
|
45
|
-
* Constructs a new success response.
|
|
46
|
-
*
|
|
47
|
-
* @param {string} message - Optional message to override the default.
|
|
48
|
-
* @param {T} [data] - Optional data payload.
|
|
49
|
-
*/
|
|
50
|
-
constructor(message: string, data?: T);
|
|
51
|
-
}
|
|
52
|
-
/**
|
|
53
|
-
* Wrapper for error responses that extends the native `Error` object.
|
|
54
|
-
* Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
|
|
55
|
-
*/
|
|
56
|
-
export declare class ErrorResponse extends Error implements Omit<ApiResponse<never>, 'data'> {
|
|
57
|
-
/** HTTP status code associated with the error (e.g., 404, 500). */
|
|
58
|
-
status: number;
|
|
59
|
-
/** Indicates that this is an error. Always true. */
|
|
60
|
-
error: boolean;
|
|
61
|
-
/** Timestamp when the error occurred, in ISO format. */
|
|
62
|
-
timestamp: string;
|
|
63
|
-
/** Unique identifier for this error instance. */
|
|
64
|
-
requestId: string;
|
|
65
|
-
/**
|
|
66
|
-
* Constructs a new error response.
|
|
67
|
-
*
|
|
68
|
-
* @param {string} message - The error message to display or log.
|
|
69
|
-
* @param {number} [status=500] - Optional HTTP status code (defaults to 500).
|
|
70
|
-
*/
|
|
71
|
-
constructor(message: string, status?: number);
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Response with paginated data extending the standard success response.
|
|
75
|
-
* Useful for APIs that return large collections of data.
|
|
76
|
-
*
|
|
77
|
-
* @typeParam T - The shape of each item in the paginated collection.
|
|
78
|
-
*/
|
|
79
|
-
export declare class PaginatedResponse<T> extends SuccessResponse<T[]> {
|
|
80
|
-
/** Total number of items across all pages */
|
|
81
|
-
total: number;
|
|
82
|
-
/** Current page number (1-based) */
|
|
83
|
-
page: number;
|
|
84
|
-
/** Number of items per page */
|
|
85
|
-
pageSize: number;
|
|
86
|
-
/** Total number of pages */
|
|
87
|
-
totalPages: number;
|
|
88
|
-
/** Whether there's a next page available */
|
|
89
|
-
hasNext: boolean;
|
|
90
|
-
/** Whether there's a previous page available */
|
|
91
|
-
hasPrevious: boolean;
|
|
92
|
-
/**
|
|
93
|
-
* Constructs a new paginated response.
|
|
94
|
-
*
|
|
95
|
-
* @param {T[]} items - The current page of items.
|
|
96
|
-
* @param {Object} pagination - Pagination information.
|
|
97
|
-
* @param {number} pagination.total - Total number of items across all pages.
|
|
98
|
-
* @param {number} pagination.page - Current page number (1-based).
|
|
99
|
-
* @param {number} pagination.pageSize - Number of items per page.
|
|
100
|
-
* @param {string} [message="Success"] - Optional custom message.
|
|
101
|
-
*/
|
|
102
|
-
constructor(items: T[], pagination: {
|
|
103
|
-
total: number;
|
|
104
|
-
page: number;
|
|
105
|
-
pageSize: number;
|
|
106
|
-
}, message?: string);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Specialized response for operations that don't return data (HTTP 204).
|
|
110
|
-
*/
|
|
111
|
-
export declare class NoContentResponse extends SuccessResponse<null> {
|
|
112
|
-
/**
|
|
113
|
-
* Constructs a new no-content response.
|
|
114
|
-
*
|
|
115
|
-
* @param {string} [message="Operation completed successfully"] - Optional custom message.
|
|
116
|
-
*/
|
|
117
|
-
constructor(message?: string);
|
|
118
|
-
}
|
|
119
|
-
/**
|
|
120
|
-
* Specialized response for redirects.
|
|
121
|
-
*/
|
|
122
|
-
export declare class RedirectResponse {
|
|
123
|
-
/** The URL to redirect to */
|
|
124
|
-
redirectUrl: string;
|
|
125
|
-
/** HTTP status code for the redirect (301, 302, 303, 307, 308) */
|
|
126
|
-
statusCode: number;
|
|
127
|
-
/** Whether the response is a redirect */
|
|
128
|
-
isRedirect: boolean;
|
|
129
|
-
/** Unique identifier for this response */
|
|
130
|
-
requestId: string;
|
|
131
|
-
/**
|
|
132
|
-
* Constructs a new redirect response.
|
|
133
|
-
*
|
|
134
|
-
* @param {string} url - The URL to redirect to.
|
|
135
|
-
* @param {number} [statusCode=302] - HTTP status code for the redirect.
|
|
136
|
-
*/
|
|
137
|
-
constructor(url: string, statusCode?: number);
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Creates a standard success response.
|
|
141
|
-
*
|
|
142
|
-
* @typeParam T - The shape of the data returned in the response.
|
|
143
|
-
* @param {T} data - The data to include in the response.
|
|
144
|
-
* @param {string} [message="Success"] - Optional custom message.
|
|
145
|
-
* @returns {SuccessResponse<T>} A properly formatted success response.
|
|
146
|
-
*/
|
|
147
|
-
export declare function createSuccessResponse<T>(data: T, message?: string): SuccessResponse<T>;
|
|
148
|
-
/**
|
|
149
|
-
* Creates a standard error response.
|
|
150
|
-
*
|
|
151
|
-
* @param {string} message - The error message.
|
|
152
|
-
* @param {number} [statusCode=500] - HTTP status code for the error.
|
|
153
|
-
* @returns {ErrorResponse} A properly formatted error response.
|
|
154
|
-
*/
|
|
155
|
-
export declare function createErrorResponse(message: string, statusCode?: number): ErrorResponse;
|
|
156
|
-
/**
|
|
157
|
-
* Creates a final error response with request information.
|
|
158
|
-
*
|
|
159
|
-
* @param req - The original request object.
|
|
160
|
-
* @param status - The HTTP status code.
|
|
161
|
-
* @param message - The error message.
|
|
162
|
-
* @param error - The original error object (optional).
|
|
163
|
-
* @param options - Additional options for the response (optional).
|
|
164
|
-
* @returns A properly formatted final error response.
|
|
165
|
-
*/
|
|
166
|
-
export declare function createFinalErrorResponse(req: Request, status: number, message: string, error?: any, options?: {
|
|
167
|
-
includeDetails?: boolean;
|
|
168
|
-
}): ApiErrorResponse;
|
|
169
|
-
/**
|
|
170
|
-
* Creates a paginated response from array data.
|
|
171
|
-
*
|
|
172
|
-
* @typeParam T - The shape of each item in the collection.
|
|
173
|
-
* @param {T[]} allItems - The complete array of items to paginate.
|
|
174
|
-
* @param {number} page - The requested page (1-based).
|
|
175
|
-
* @param {number} pageSize - The number of items per page.
|
|
176
|
-
* @param {string} [message="Success"] - Optional custom message.
|
|
177
|
-
* @returns {PaginatedResponse<T>} A properly formatted paginated response.
|
|
178
|
-
*/
|
|
179
|
-
export declare function createPaginatedResponse<T>(allItems: T[], page: number, pageSize: number, message?: string): PaginatedResponse<T>;
|
|
180
|
-
/**
|
|
181
|
-
* Adapter to convert API responses to Express.js response format.
|
|
182
|
-
*
|
|
183
|
-
* @param {any} res - Express response object.
|
|
184
|
-
* @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
|
|
185
|
-
*/
|
|
186
|
-
export declare function sendResponse(res: any, apiResponse: SuccessResponse<any> | ErrorResponse | RedirectResponse): void;
|