@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.
Files changed (97) hide show
  1. package/README.md +52 -16
  2. package/array/index.cjs +180 -71
  3. package/array/index.d.ts +293 -1
  4. package/array/index.mjs +171 -72
  5. package/async/index.cjs +92 -36
  6. package/async/index.d.ts +275 -1
  7. package/async/index.mjs +92 -36
  8. package/cache/index.cjs +1 -1
  9. package/cache/index.d.ts +155 -1
  10. package/cache/index.mjs +2 -2
  11. package/config/index.cjs +78 -64
  12. package/config/index.d.ts +64 -2
  13. package/config/index.mjs +76 -64
  14. package/context-store/index.d.ts +192 -1
  15. package/crypto/index.d.ts +163 -1
  16. package/date/index.cjs +46 -1
  17. package/date/index.d.ts +190 -1
  18. package/date/index.mjs +45 -2
  19. package/decorators/index.cjs +1156 -18
  20. package/decorators/index.d.ts +684 -1
  21. package/decorators/index.mjs +1156 -18
  22. package/dir/index.cjs +4 -3
  23. package/dir/index.d.ts +195 -1
  24. package/dir/index.mjs +4 -3
  25. package/env/index.cjs +10 -26
  26. package/env/index.d.ts +379 -1
  27. package/env/index.mjs +10 -26
  28. package/exception/index.d.ts +232 -1
  29. package/fs/index.cjs +70 -36
  30. package/fs/index.d.ts +205 -1
  31. package/fs/index.mjs +64 -34
  32. package/http-status-codes/index.d.ts +267 -1
  33. package/id/index.d.ts +37 -1
  34. package/index.cjs +3 -3
  35. package/index.d.ts +1 -1
  36. package/index.mjs +1 -1
  37. package/logger/index.cjs +11 -11
  38. package/logger/index.d.ts +189 -1
  39. package/logger/index.mjs +12 -12
  40. package/middleware/index.d.ts +103 -1
  41. package/obj/index.cjs +150 -162
  42. package/obj/index.d.ts +136 -1
  43. package/obj/index.mjs +150 -162
  44. package/package.json +11 -11
  45. package/performance/index.cjs +2 -2
  46. package/performance/index.d.ts +138 -1
  47. package/performance/index.mjs +2 -2
  48. package/request/index.cjs +1 -1
  49. package/request/index.d.ts +241 -2
  50. package/request/index.mjs +1 -1
  51. package/response/index.d.ts +318 -2
  52. package/server/index.cjs +27 -23
  53. package/server/index.d.ts +785 -4
  54. package/server/index.mjs +28 -23
  55. package/stream/index.d.ts +90 -1
  56. package/string/index.d.ts +102 -1
  57. package/type/index.cjs +1 -1
  58. package/type/index.d.ts +107 -1
  59. package/type/index.mjs +1 -1
  60. package/types/index.d.ts +774 -4
  61. package/url/index.cjs +2 -4
  62. package/url/index.d.ts +142 -1
  63. package/url/index.mjs +2 -4
  64. package/{validate → validation}/index.cjs +89 -42
  65. package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
  66. package/{validate → validation}/index.mjs +85 -42
  67. package/array/array.utils.d.ts +0 -191
  68. package/async/async.utils.d.ts +0 -296
  69. package/cache/cache.utils.d.ts +0 -176
  70. package/config/config.d.ts +0 -57
  71. package/context-store/context-store.utils.d.ts +0 -212
  72. package/crypto/crypto.utils.d.ts +0 -183
  73. package/date/date.utils.d.ts +0 -190
  74. package/decorators/decorators.utils.d.ts +0 -705
  75. package/dir/dir.utils.d.ts +0 -216
  76. package/env/env.utils.d.ts +0 -400
  77. package/exception/exception.utils.d.ts +0 -253
  78. package/fs/fs.utils.d.ts +0 -196
  79. package/http-status-codes/http-status-codes.d.ts +0 -289
  80. package/id/id.utils.d.ts +0 -59
  81. package/logger/logger.utils.d.ts +0 -210
  82. package/middleware/middleware.utils.d.ts +0 -123
  83. package/obj/obj.utils.d.ts +0 -156
  84. package/performance/performance.utils.d.ts +0 -159
  85. package/request/request.utils.d.ts +0 -109
  86. package/response/response.utils.d.ts +0 -186
  87. package/server/server.builder.d.ts +0 -531
  88. package/server/server.d.ts +0 -303
  89. package/stream/stream.utils.d.ts +0 -111
  90. package/string/string.utils.d.ts +0 -124
  91. package/type/type.utils.d.ts +0 -129
  92. package/types/api-response.d.ts +0 -175
  93. package/types/common.d.ts +0 -148
  94. package/types/config.d.ts +0 -88
  95. package/types/server.d.ts +0 -291
  96. package/url/url.utils.d.ts +0 -164
  97. package/validate/index.d.ts +0 -25
@@ -1,253 +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 { ErrorResponse } from '@catbee/utils/response';
26
- /**
27
- * Generic HTTP error class used for custom exceptions with any status code.
28
- * Inherit this when you need to throw errors dynamically at runtime.
29
- */
30
- export declare class HttpError extends ErrorResponse {
31
- /**
32
- * Creates a new HTTP error instance.
33
- * @param status - A valid HTTP status code (e.g., 400, 500).
34
- * @param message - The error message to return to the client.
35
- */
36
- constructor(status: number, message: string);
37
- }
38
- /**
39
- * Represents a 500 Internal Server Error.
40
- * Use this for unexpected backend errors that are not caused by the client.
41
- */
42
- export declare class InternalServerErrorException extends ErrorResponse {
43
- /**
44
- * @param message - Optional custom message. Defaults to "Internal server error".
45
- */
46
- constructor(message?: string);
47
- }
48
- /**
49
- * Represents a 401 Unauthorized Error.
50
- * Indicates that authentication is required and has failed or not been provided.
51
- */
52
- export declare class UnauthorizedException extends ErrorResponse {
53
- /**
54
- * @param message - Optional custom message. Defaults to "Unauthorized".
55
- */
56
- constructor(message?: string);
57
- }
58
- /**
59
- * Represents a 400 Bad Request Error.
60
- * Indicates that the client has sent invalid data (e.g., malformed request body).
61
- */
62
- export declare class BadRequestException extends ErrorResponse {
63
- /**
64
- * @param message - Optional custom message. Defaults to "Bad request".
65
- */
66
- constructor(message?: string);
67
- }
68
- /**
69
- * Represents a 404 Not Found Error.
70
- * Used when a requested resource (e.g., user, file) could not be located.
71
- */
72
- export declare class NotFoundException extends ErrorResponse {
73
- /**
74
- * @param message - Optional custom message. Defaults to "Resource not found".
75
- */
76
- constructor(message?: string);
77
- }
78
- /**
79
- * Represents a 403 Forbidden Error.
80
- * Indicates that the client is authenticated but not allowed to access the resource.
81
- */
82
- export declare class ForbiddenException extends ErrorResponse {
83
- /**
84
- * @param message - Optional custom message. Defaults to "Forbidden".
85
- */
86
- constructor(message?: string);
87
- }
88
- /**
89
- * Represents a 409 Conflict Error.
90
- * Commonly used when a resource already exists or a versioning conflict occurs.
91
- */
92
- export declare class ConflictException extends ErrorResponse {
93
- /**
94
- * @param message - Optional custom message. Defaults to "Conflict".
95
- */
96
- constructor(message?: string);
97
- }
98
- /**
99
- * Represents a 502 Bad Gateway Error.
100
- * Used when the server receives an invalid response from an upstream server.
101
- */
102
- export declare class BadGatewayException extends ErrorResponse {
103
- /**
104
- * @param message - Optional custom message. Defaults to "Bad Gateway".
105
- */
106
- constructor(message?: string);
107
- }
108
- /**
109
- * Represents a 429 Too Many Requests Error.
110
- * Returned when the client has hit a rate limit.
111
- */
112
- export declare class TooManyRequestsException extends ErrorResponse {
113
- /**
114
- * @param message - Optional custom message. Defaults to "Too many requests".
115
- */
116
- constructor(message?: string);
117
- }
118
- /**
119
- * Represents a 503 Service Unavailable Error.
120
- * Indicates that the server is temporarily unavailable (e.g., for maintenance).
121
- */
122
- export declare class ServiceUnavailableException extends ErrorResponse {
123
- /**
124
- * @param message - Optional custom message. Defaults to "Service Unavailable".
125
- */
126
- constructor(message?: string);
127
- }
128
- /**
129
- * Represents a 504 Gateway Timeout Error.
130
- * Returned when the server acting as a gateway times out waiting for a response.
131
- */
132
- export declare class GatewayTimeoutException extends ErrorResponse {
133
- /**
134
- * @param message - Optional custom message. Defaults to "Gateway Timeout".
135
- */
136
- constructor(message?: string);
137
- }
138
- /**
139
- * Represents a 422 Unprocessable Entity Error.
140
- * Used when the server understands the content type but cannot process the contained instructions.
141
- */
142
- export declare class UnprocessableEntityException extends ErrorResponse {
143
- details?: Record<string, any> | undefined;
144
- /**
145
- * @param message - Optional custom message. Defaults to "Unprocessable Entity".
146
- * @param details - Optional validation details or additional error context.
147
- */
148
- constructor(message?: string, details?: Record<string, any> | undefined);
149
- }
150
- /**
151
- * Represents a 405 Method Not Allowed Error.
152
- * Used when the request method is not supported for the requested resource.
153
- */
154
- export declare class MethodNotAllowedException extends ErrorResponse {
155
- allowedMethods?: string[] | undefined;
156
- /**
157
- * @param message - Optional custom message. Defaults to "Method Not Allowed".
158
- * @param allowedMethods - Optional array of allowed HTTP methods.
159
- */
160
- constructor(message?: string, allowedMethods?: string[] | undefined);
161
- }
162
- /**
163
- * Represents a 406 Not Acceptable Error.
164
- * Used when the server cannot produce a response matching the client's accepted content types.
165
- */
166
- export declare class NotAcceptableException extends ErrorResponse {
167
- /**
168
- * @param message - Optional custom message. Defaults to "Not Acceptable".
169
- */
170
- constructor(message?: string);
171
- }
172
- /**
173
- * Represents a 408 Request Timeout Error.
174
- * Used when the server times out waiting for the client to send a request.
175
- */
176
- export declare class RequestTimeoutException extends ErrorResponse {
177
- /**
178
- * @param message - Optional custom message. Defaults to "Request Timeout".
179
- */
180
- constructor(message?: string);
181
- }
182
- /**
183
- * Represents a 415 Unsupported Media Type Error.
184
- * Used when the request's Content-Type is not supported.
185
- */
186
- export declare class UnsupportedMediaTypeException extends ErrorResponse {
187
- /**
188
- * @param message - Optional custom message. Defaults to "Unsupported Media Type".
189
- */
190
- constructor(message?: string);
191
- }
192
- /**
193
- * Represents a 413 Payload Too Large Error.
194
- * Used when the request body exceeds server limits.
195
- */
196
- export declare class PayloadTooLargeException extends ErrorResponse {
197
- /**
198
- * @param message - Optional custom message. Defaults to "Payload Too Large".
199
- */
200
- constructor(message?: string);
201
- }
202
- /**
203
- * Represents a 507 Insufficient Storage Error.
204
- * Used when the server has insufficient storage to complete the request.
205
- */
206
- export declare class InsufficientStorageException extends ErrorResponse {
207
- /**
208
- * @param message - Optional custom message. Defaults to "Insufficient Storage".
209
- */
210
- constructor(message?: string);
211
- }
212
- /**
213
- * Checks if an error is an instance of HttpError or its subclasses.
214
- *
215
- * @param error - The error to check.
216
- * @returns True if the error is an HttpError, false otherwise.
217
- */
218
- export declare function isHttpError(error: unknown): error is ErrorResponse;
219
- /**
220
- * Creates an HTTP error with the specified status code and message.
221
- * Factory function for creating appropriate HTTP error instances.
222
- *
223
- * @param status - HTTP status code.
224
- * @param message - Optional custom error message.
225
- * @returns An instance of the appropriate HTTP error class.
226
- */
227
- export declare function createHttpError(status: number, message?: string): ErrorResponse;
228
- /**
229
- * Type guard to check if an object has specific error properties.
230
- *
231
- * @param error - The object to check.
232
- * @returns True if object has error-like structure.
233
- */
234
- export declare function hasErrorShape(error: unknown): error is {
235
- message: string;
236
- status?: number;
237
- code?: string;
238
- };
239
- /**
240
- * Safely extracts message from any error type.
241
- *
242
- * @param error - Any error object or value.
243
- * @returns A string error message.
244
- */
245
- export declare function getErrorMessage(error: unknown): string;
246
- /**
247
- * Wraps a function and transforms any errors into HTTP errors.
248
- * Useful for API handlers to ensure consistent error responses.
249
- *
250
- * @param handler - The async function to wrap.
251
- * @returns A function that always returns or throws an HTTP error.
252
- */
253
- export declare function withErrorHandling<T extends (...args: any[]) => Promise<any>>(handler: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
package/fs/fs.utils.d.ts DELETED
@@ -1,196 +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 { BufferEncoding } from '@catbee/utils/crypto';
26
- import { Stats } from 'fs';
27
- /**
28
- * Checks whether a file or directory exists at the given path.
29
- *
30
- * @param {string} path - The file or directory path to check.
31
- * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
32
- */
33
- export declare function fileExists(path: string): Promise<boolean>;
34
- /**
35
- * Reads and parses a JSON file from the specified path.
36
- *
37
- * @typeParam T - The expected type of the parsed JSON object.
38
- * @param {string} path - The path to the JSON file.
39
- * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
40
- */
41
- export declare function readJsonFile<T = any>(path: string): Promise<T | null>;
42
- /**
43
- * Read and parse JSON file or return a provided default value on error.
44
- *
45
- * @typeParam T
46
- * @param {string} path - Path to JSON file
47
- * @param {T} defaultValue - Default value to return when read/parse fails
48
- * @returns {Promise<T>} Parsed JSON or defaultValue
49
- */
50
- export declare function readJsonOrDefault<T = any>(path: string, defaultValue: T): Promise<T>;
51
- /**
52
- * Writes a JavaScript object to a file as formatted JSON.
53
- *
54
- * @param {string} path - The destination file path.
55
- * @param {any} data - The object to serialize and write.
56
- * @param {number} [space=2] - Number of spaces for JSON formatting.
57
- * @returns {Promise<void>} Resolves when writing is complete.
58
- */
59
- export declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
60
- /**
61
- * Deletes a file if it exists.
62
- *
63
- * @param {string} path - The file path to delete.
64
- * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
65
- */
66
- export declare function deleteFileIfExists(path: string): Promise<boolean>;
67
- /**
68
- * Reads a text file from the specified path.
69
- *
70
- * @param {string} path - The path to the text file.
71
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
72
- * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
73
- */
74
- export declare function readTextFile(path: string, encoding?: BufferEncoding): Promise<string | null>;
75
- /**
76
- * Writes text content to a file.
77
- *
78
- * @param {string} path - The destination file path.
79
- * @param {string} content - The text content to write.
80
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
81
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
82
- */
83
- export declare function writeTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
84
- /**
85
- * Appends text content to a file.
86
- *
87
- * @param {string} path - The file path to append to.
88
- * @param {string} content - The text content to append.
89
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
90
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
91
- */
92
- export declare function appendTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
93
- /**
94
- * Copies a file from source to destination.
95
- *
96
- * @param {string} source - Source file path.
97
- * @param {string} destination - Destination file path.
98
- * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
99
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
100
- */
101
- export declare function copyFile(source: string, destination: string, overwrite?: boolean): Promise<boolean>;
102
- /**
103
- * Renames/moves a file.
104
- *
105
- * @param {string} oldPath - Current file path.
106
- * @param {string} newPath - New file path.
107
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
108
- */
109
- export declare function moveFile(oldPath: string, newPath: string): Promise<boolean>;
110
- /**
111
- * Gets file stats if the file exists.
112
- *
113
- * @param {string} path - Path to the file.
114
- * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
115
- */
116
- export declare function getFileStats(path: string): Promise<Stats | null>;
117
- /**
118
- * Creates a temporary file with optional content.
119
- *
120
- * @param {object} [options] - Options for creating the temp file.
121
- * @param {string} [options.prefix="tmp-"] - Filename prefix.
122
- * @param {string} [options.extension=""] - File extension.
123
- * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
124
- * @param {string | Buffer} [options.content] - Optional content to write to the file.
125
- * @returns {Promise<string>} Path to the created temporary file.
126
- */
127
- export declare function createTempFile({ prefix, extension, dir, content }?: {
128
- prefix?: string;
129
- extension?: string;
130
- dir?: string;
131
- content?: string | Buffer;
132
- }): Promise<string>;
133
- /**
134
- * Streams a file from source to destination.
135
- * Useful for large files to avoid loading the entire file into memory.
136
- *
137
- * @param {string} source - Source file path.
138
- * @param {string} destination - Destination file path.
139
- * @returns {Promise<void>} Resolves when streaming completes.
140
- * @throws {Error} If streaming fails.
141
- */
142
- export declare function streamFile(source: string, destination: string): Promise<void>;
143
- /**
144
- * Reads a directory and returns file names.
145
- *
146
- * @param {string} dirPath - Path to the directory.
147
- * @param {object} [options] - Options for reading the directory.
148
- * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
149
- * @param {RegExp} [options.filter] - Optional regex to filter files.
150
- * @returns {Promise<string[]>} Array of file names or paths.
151
- * @throws {Error} If directory cannot be read.
152
- */
153
- export declare function readDirectory(dirPath: string, options?: {
154
- fullPaths?: boolean;
155
- filter?: RegExp;
156
- }): Promise<string[]>;
157
- /**
158
- * Creates a directory if it doesn't exist.
159
- *
160
- * @param {string} dirPath - Path to the directory.
161
- * @param {boolean} [recursive=true] - Whether to create parent directories.
162
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
163
- */
164
- export declare function createDirectory(dirPath: string, recursive?: boolean): Promise<boolean>;
165
- /**
166
- * Safely reads and parses a JSON file with error details.
167
- *
168
- * @typeParam T - The expected type of the parsed JSON object.
169
- * @param {string} path - The path to the JSON file.
170
- * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
171
- */
172
- export declare function safeReadJsonFile<T = any>(path: string): Promise<{
173
- data: T | null;
174
- error: Error | null;
175
- }>;
176
- /**
177
- * Checks if a path points to a file (not a directory).
178
- *
179
- * @param {string} path - Path to check.
180
- * @returns {Promise<boolean>} True if the path is a file, false otherwise.
181
- */
182
- export declare function isFile(path: string): Promise<boolean>;
183
- /**
184
- * Gets the size of a file in bytes.
185
- *
186
- * @param {string} path - Path to the file.
187
- * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
188
- */
189
- export declare function getFileSize(path: string): Promise<number>;
190
- /**
191
- * Reads a file as a Buffer.
192
- *
193
- * @param {string} path - Path to the file.
194
- * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
195
- */
196
- export declare function readFileBuffer(path: string): Promise<Buffer | null>;