@catbee/utils 1.0.5 → 2.0.0-next.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 +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7579
- package/build/index.d.ts +0 -5774
- package/build/index.mjs +0 -7274
|
@@ -0,0 +1,338 @@
|
|
|
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 { HttpStatusCodes } from '@catbee/utils/http-status-codes';
|
|
26
|
+
import { getLogger } from '@catbee/utils/logger';
|
|
27
|
+
import { ErrorResponse } from '@catbee/utils/response';
|
|
28
|
+
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
30
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
var HttpError = class extends ErrorResponse {
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "HttpError");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a new HTTP error instance.
|
|
37
|
+
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
38
|
+
* @param message - The error message to return to the client.
|
|
39
|
+
*/
|
|
40
|
+
constructor(status, message) {
|
|
41
|
+
super(message);
|
|
42
|
+
this.status = status;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var InternalServerErrorException = class extends ErrorResponse {
|
|
46
|
+
static {
|
|
47
|
+
__name(this, "InternalServerErrorException");
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
51
|
+
*/
|
|
52
|
+
constructor(message = "Internal server error") {
|
|
53
|
+
super(message);
|
|
54
|
+
this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
var UnauthorizedException = class extends ErrorResponse {
|
|
58
|
+
static {
|
|
59
|
+
__name(this, "UnauthorizedException");
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
63
|
+
*/
|
|
64
|
+
constructor(message = "Unauthorized") {
|
|
65
|
+
super(message);
|
|
66
|
+
this.status = HttpStatusCodes.UNAUTHORIZED;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
var BadRequestException = class extends ErrorResponse {
|
|
70
|
+
static {
|
|
71
|
+
__name(this, "BadRequestException");
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* @param message - Optional custom message. Defaults to "Bad request".
|
|
75
|
+
*/
|
|
76
|
+
constructor(message = "Bad request") {
|
|
77
|
+
super(message);
|
|
78
|
+
this.status = HttpStatusCodes.BAD_REQUEST;
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var NotFoundException = class extends ErrorResponse {
|
|
82
|
+
static {
|
|
83
|
+
__name(this, "NotFoundException");
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
87
|
+
*/
|
|
88
|
+
constructor(message = "Resource not found") {
|
|
89
|
+
super(message);
|
|
90
|
+
this.status = HttpStatusCodes.NOT_FOUND;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var ForbiddenException = class extends ErrorResponse {
|
|
94
|
+
static {
|
|
95
|
+
__name(this, "ForbiddenException");
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
99
|
+
*/
|
|
100
|
+
constructor(message = "Forbidden") {
|
|
101
|
+
super(message);
|
|
102
|
+
this.status = HttpStatusCodes.FORBIDDEN;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
var ConflictException = class extends ErrorResponse {
|
|
106
|
+
static {
|
|
107
|
+
__name(this, "ConflictException");
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @param message - Optional custom message. Defaults to "Conflict".
|
|
111
|
+
*/
|
|
112
|
+
constructor(message = "Conflict") {
|
|
113
|
+
super(message);
|
|
114
|
+
this.status = HttpStatusCodes.CONFLICT;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var BadGatewayException = class extends ErrorResponse {
|
|
118
|
+
static {
|
|
119
|
+
__name(this, "BadGatewayException");
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
123
|
+
*/
|
|
124
|
+
constructor(message = "Bad Gateway") {
|
|
125
|
+
super(message);
|
|
126
|
+
this.status = HttpStatusCodes.BAD_GATEWAY;
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
var TooManyRequestsException = class extends ErrorResponse {
|
|
130
|
+
static {
|
|
131
|
+
__name(this, "TooManyRequestsException");
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
135
|
+
*/
|
|
136
|
+
constructor(message = "Too many requests") {
|
|
137
|
+
super(message);
|
|
138
|
+
this.status = HttpStatusCodes.TOO_MANY_REQUESTS;
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
var ServiceUnavailableException = class extends ErrorResponse {
|
|
142
|
+
static {
|
|
143
|
+
__name(this, "ServiceUnavailableException");
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
147
|
+
*/
|
|
148
|
+
constructor(message = "Service Unavailable") {
|
|
149
|
+
super(message);
|
|
150
|
+
this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
var GatewayTimeoutException = class extends ErrorResponse {
|
|
154
|
+
static {
|
|
155
|
+
__name(this, "GatewayTimeoutException");
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
159
|
+
*/
|
|
160
|
+
constructor(message = "Gateway Timeout") {
|
|
161
|
+
super(message);
|
|
162
|
+
this.status = HttpStatusCodes.GATEWAY_TIMEOUT;
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
var UnprocessableEntityException = class extends ErrorResponse {
|
|
166
|
+
static {
|
|
167
|
+
__name(this, "UnprocessableEntityException");
|
|
168
|
+
}
|
|
169
|
+
details;
|
|
170
|
+
/**
|
|
171
|
+
* @param message - Optional custom message. Defaults to "Unprocessable Entity".
|
|
172
|
+
* @param details - Optional validation details or additional error context.
|
|
173
|
+
*/
|
|
174
|
+
constructor(message = "Unprocessable Entity", details) {
|
|
175
|
+
super(message), this.details = details;
|
|
176
|
+
this.status = HttpStatusCodes.UNPROCESSABLE_ENTITY;
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
var MethodNotAllowedException = class extends ErrorResponse {
|
|
180
|
+
static {
|
|
181
|
+
__name(this, "MethodNotAllowedException");
|
|
182
|
+
}
|
|
183
|
+
allowedMethods;
|
|
184
|
+
/**
|
|
185
|
+
* @param message - Optional custom message. Defaults to "Method Not Allowed".
|
|
186
|
+
* @param allowedMethods - Optional array of allowed HTTP methods.
|
|
187
|
+
*/
|
|
188
|
+
constructor(message = "Method Not Allowed", allowedMethods) {
|
|
189
|
+
super(message), this.allowedMethods = allowedMethods;
|
|
190
|
+
this.status = HttpStatusCodes.METHOD_NOT_ALLOWED;
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
var NotAcceptableException = class extends ErrorResponse {
|
|
194
|
+
static {
|
|
195
|
+
__name(this, "NotAcceptableException");
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* @param message - Optional custom message. Defaults to "Not Acceptable".
|
|
199
|
+
*/
|
|
200
|
+
constructor(message = "Not Acceptable") {
|
|
201
|
+
super(message);
|
|
202
|
+
this.status = HttpStatusCodes.NOT_ACCEPTABLE;
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
var RequestTimeoutException = class extends ErrorResponse {
|
|
206
|
+
static {
|
|
207
|
+
__name(this, "RequestTimeoutException");
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* @param message - Optional custom message. Defaults to "Request Timeout".
|
|
211
|
+
*/
|
|
212
|
+
constructor(message = "Request Timeout") {
|
|
213
|
+
super(message);
|
|
214
|
+
this.status = HttpStatusCodes.REQUEST_TIMEOUT;
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
var UnsupportedMediaTypeException = class extends ErrorResponse {
|
|
218
|
+
static {
|
|
219
|
+
__name(this, "UnsupportedMediaTypeException");
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* @param message - Optional custom message. Defaults to "Unsupported Media Type".
|
|
223
|
+
*/
|
|
224
|
+
constructor(message = "Unsupported Media Type") {
|
|
225
|
+
super(message);
|
|
226
|
+
this.status = HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
var PayloadTooLargeException = class extends ErrorResponse {
|
|
230
|
+
static {
|
|
231
|
+
__name(this, "PayloadTooLargeException");
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* @param message - Optional custom message. Defaults to "Payload Too Large".
|
|
235
|
+
*/
|
|
236
|
+
constructor(message = "Payload Too Large") {
|
|
237
|
+
super(message);
|
|
238
|
+
this.status = HttpStatusCodes.PAYLOAD_TOO_LARGE;
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
var InsufficientStorageException = class extends ErrorResponse {
|
|
242
|
+
static {
|
|
243
|
+
__name(this, "InsufficientStorageException");
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* @param message - Optional custom message. Defaults to "Insufficient Storage".
|
|
247
|
+
*/
|
|
248
|
+
constructor(message = "Insufficient Storage") {
|
|
249
|
+
super(message);
|
|
250
|
+
this.status = HttpStatusCodes.INSUFFICIENT_STORAGE;
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
function isHttpError(error) {
|
|
254
|
+
return error instanceof ErrorResponse;
|
|
255
|
+
}
|
|
256
|
+
__name(isHttpError, "isHttpError");
|
|
257
|
+
function createHttpError(status, message) {
|
|
258
|
+
switch (status) {
|
|
259
|
+
case HttpStatusCodes.BAD_REQUEST:
|
|
260
|
+
return new BadRequestException(message);
|
|
261
|
+
case HttpStatusCodes.UNAUTHORIZED:
|
|
262
|
+
return new UnauthorizedException(message);
|
|
263
|
+
case HttpStatusCodes.FORBIDDEN:
|
|
264
|
+
return new ForbiddenException(message);
|
|
265
|
+
case HttpStatusCodes.NOT_FOUND:
|
|
266
|
+
return new NotFoundException(message);
|
|
267
|
+
case HttpStatusCodes.METHOD_NOT_ALLOWED:
|
|
268
|
+
return new MethodNotAllowedException(message);
|
|
269
|
+
case HttpStatusCodes.NOT_ACCEPTABLE:
|
|
270
|
+
return new NotAcceptableException(message);
|
|
271
|
+
case HttpStatusCodes.REQUEST_TIMEOUT:
|
|
272
|
+
return new RequestTimeoutException(message);
|
|
273
|
+
case HttpStatusCodes.CONFLICT:
|
|
274
|
+
return new ConflictException(message);
|
|
275
|
+
case HttpStatusCodes.GONE:
|
|
276
|
+
return new HttpError(HttpStatusCodes.GONE, message || "Gone");
|
|
277
|
+
case HttpStatusCodes.PAYLOAD_TOO_LARGE:
|
|
278
|
+
return new PayloadTooLargeException(message);
|
|
279
|
+
case HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:
|
|
280
|
+
return new UnsupportedMediaTypeException(message);
|
|
281
|
+
case HttpStatusCodes.UNPROCESSABLE_ENTITY:
|
|
282
|
+
return new UnprocessableEntityException(message);
|
|
283
|
+
case HttpStatusCodes.TOO_MANY_REQUESTS:
|
|
284
|
+
return new TooManyRequestsException(message);
|
|
285
|
+
case HttpStatusCodes.INTERNAL_SERVER_ERROR:
|
|
286
|
+
return new InternalServerErrorException(message);
|
|
287
|
+
case HttpStatusCodes.BAD_GATEWAY:
|
|
288
|
+
return new BadGatewayException(message);
|
|
289
|
+
case HttpStatusCodes.SERVICE_UNAVAILABLE:
|
|
290
|
+
return new ServiceUnavailableException(message);
|
|
291
|
+
case HttpStatusCodes.GATEWAY_TIMEOUT:
|
|
292
|
+
return new GatewayTimeoutException(message);
|
|
293
|
+
case HttpStatusCodes.INSUFFICIENT_STORAGE:
|
|
294
|
+
return new InsufficientStorageException(message);
|
|
295
|
+
default:
|
|
296
|
+
return new HttpError(status, message || `HTTP Error ${status}`);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
__name(createHttpError, "createHttpError");
|
|
300
|
+
function hasErrorShape(error) {
|
|
301
|
+
return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
|
|
302
|
+
}
|
|
303
|
+
__name(hasErrorShape, "hasErrorShape");
|
|
304
|
+
function getErrorMessage(error) {
|
|
305
|
+
if (typeof error === "string") {
|
|
306
|
+
return error;
|
|
307
|
+
}
|
|
308
|
+
if (error instanceof Error) {
|
|
309
|
+
return error.message;
|
|
310
|
+
}
|
|
311
|
+
if (hasErrorShape(error)) {
|
|
312
|
+
return error.message;
|
|
313
|
+
}
|
|
314
|
+
return "Unknown error occurred";
|
|
315
|
+
}
|
|
316
|
+
__name(getErrorMessage, "getErrorMessage");
|
|
317
|
+
function withErrorHandling(handler) {
|
|
318
|
+
return async (...args) => {
|
|
319
|
+
try {
|
|
320
|
+
return await handler(...args);
|
|
321
|
+
} catch (err) {
|
|
322
|
+
if (isHttpError(err)) {
|
|
323
|
+
throw err;
|
|
324
|
+
}
|
|
325
|
+
getLogger().error({
|
|
326
|
+
err
|
|
327
|
+
}, "Caught error in handler");
|
|
328
|
+
let status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
329
|
+
if (hasErrorShape(err) && typeof err.status === "number") {
|
|
330
|
+
status = err.status;
|
|
331
|
+
}
|
|
332
|
+
throw createHttpError(status, getErrorMessage(err));
|
|
333
|
+
}
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
__name(withErrorHandling, "withErrorHandling");
|
|
337
|
+
|
|
338
|
+
export { BadGatewayException, BadRequestException, ConflictException, ForbiddenException, GatewayTimeoutException, HttpError, InsufficientStorageException, InternalServerErrorException, MethodNotAllowedException, NotAcceptableException, NotFoundException, PayloadTooLargeException, RequestTimeoutException, ServiceUnavailableException, TooManyRequestsException, UnauthorizedException, UnprocessableEntityException, UnsupportedMediaTypeException, createHttpError, getErrorMessage, hasErrorShape, isHttpError, withErrorHandling };
|
package/fs/fs.utils.d.ts
ADDED
|
@@ -0,0 +1,196 @@
|
|
|
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>;
|