@xeno-js/shared 2.0.1 → 3.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.
@@ -0,0 +1,381 @@
1
+ import { O as Optional, M as Maybe } from './common.types-DT8JtZ0E.js';
2
+
3
+ /**
4
+ * A class representing an application error, which extends the built-in Error class.
5
+ * It includes additional properties such as an error code and an HTTP status code.
6
+
7
+ *
8
+ * @author Xeno
9
+ * @version 1.0.0
10
+ * @since 2025-09-30
11
+ * @link https://github.com/xeno-js/xeno-js
12
+ */
13
+ interface ErrorPayload {
14
+ /** The error message describing the error.
15
+ *
16
+ * @author Xeno
17
+ * @version 1.0.0
18
+ * @since 2025-09-30
19
+ * @link https://github.com/xeno-js/xeno-js
20
+ */
21
+ message: string;
22
+ /** The error code representing the type of error.
23
+ *
24
+ * @author Xeno
25
+ * @version 1.0.0
26
+ * @since 2025-09-30
27
+ * @link https://github.com/xeno-js/xeno-js
28
+ */
29
+ code: string;
30
+ /** The HTTP status code associated with the error.
31
+ *
32
+ * @author Xeno
33
+ * @version 1.0.0
34
+ * @since 2025-09-30
35
+ * @link https://github.com/xeno-js/xeno-js
36
+ */
37
+ status: number;
38
+ /** The name of the error, typically the class name.
39
+ *
40
+ * @author Xeno
41
+ * @version 1.0.0
42
+ * @since 2025-09-30
43
+ * @link https://github.com/xeno-js/xeno-js
44
+ */
45
+ name: string;
46
+ /** An optional property to hold the original error or any additional context.
47
+ *
48
+ * @author Xeno
49
+ * @version 1.0.0
50
+ * @since 2025-09-30
51
+ * @link https://github.com/xeno-js/xeno-js
52
+ */
53
+ cause: Optional<unknown>;
54
+ /** A Dictionary to hold any additional context or information related to the error.
55
+ *
56
+ * @author Xeno
57
+ * @version 1.0.0
58
+ * @since 2025-09-30
59
+ * @link https://github.com/xeno-js/xeno-js
60
+ */
61
+ [key: string]: unknown;
62
+ }
63
+ /**
64
+ * A class representing an application error, which extends the built-in Error class.
65
+ * It includes additional properties such as an error code and an HTTP status code.
66
+
67
+ *
68
+ * @author Xeno
69
+ * @version 1.0.0
70
+ * @since 2025-09-30
71
+ * @link https://github.com/xeno-js/xeno-js
72
+ */
73
+ declare class AppError extends Error {
74
+ /**
75
+ * The error code representing the type of error.
76
+
77
+ *
78
+ * @author Xeno
79
+ * @version 1.0.0
80
+ * @since 2025-09-30
81
+ * @link https://github.com/xeno-js/xeno-js
82
+ */
83
+ readonly code: string;
84
+ /**
85
+ * The HTTP status code associated with the error.
86
+
87
+ *
88
+ * @author Xeno
89
+ * @version 1.0.0
90
+ * @since 2025-09-30
91
+ * @link https://github.com/xeno-js/xeno-js
92
+ */
93
+ readonly status: number;
94
+ /**
95
+ * A Dictionary to hold any additional context or information related to the error.
96
+ *
97
+ * @author Xeno
98
+ * @version 1.0.0
99
+ * @since 2025-09-30
100
+ * @link https://github.com/xeno-js/xeno-js
101
+ */
102
+ readonly [key: string]: unknown;
103
+ /**
104
+ * Private constructor to prevent direct instantiation. Use the static methods `create` and `throw` to create instances.
105
+ *
106
+ * @param payload - The payload containing error details.
107
+
108
+ *
109
+ * @author Xeno
110
+ * @version 1.0.0
111
+ * @since 2025-09-30
112
+ * @link https://github.com/xeno-js/xeno-js
113
+ */
114
+ private constructor();
115
+ /**
116
+ * Creates an AppError instance with the given error payload.
117
+ *
118
+ * @param payload - The payload containing error details.
119
+ * @returns An AppError instance representing the error.
120
+
121
+ *
122
+ * @author Xeno
123
+ * @version 1.0.0
124
+ * @since 2025-09-30
125
+ * @link https://github.com/xeno-js/xeno-js
126
+ */
127
+ static create(payload: ErrorPayload): AppError;
128
+ /**
129
+ * Creates an AppError instance and throws it immediately.
130
+ * @param payload - The payload containing error details.
131
+ * @throws An AppError instance representing the error.
132
+
133
+ *
134
+ * @author Xeno
135
+ * @version 1.0.0
136
+ * @since 2025-09-30
137
+ * @link https://github.com/xeno-js/xeno-js
138
+ */
139
+ static throw(payload: ErrorPayload): never;
140
+ /**
141
+ * Creates an AppError instance representing an aborted request.
142
+ * @param name - The name of the error, typically the class name or context where the error occurred.
143
+ * @returns An AppError instance representing the aborted request error.
144
+
145
+ *
146
+ * @author Xeno
147
+ * @version 1.0.0
148
+ * @since 2025-09-30
149
+ * @link https://github.com/xeno-js/xeno-js
150
+ */
151
+ static aborted(name: string): AppError;
152
+ /**
153
+ * Utility method to check if an AbortSignal has been triggered and throw an AppError if it has.
154
+ * @param signal - The AbortSignal to check for abortion.
155
+ * @param name - The name of the error, typically the class name or context where the error occurred.
156
+
157
+ *
158
+ * @author Xeno
159
+ * @version 1.0.0
160
+ * @since 2025-09-30
161
+ * @link https://github.com/xeno-js/xeno-js
162
+ */
163
+ static throwIfAborted(signal: Maybe<AbortSignal>, name: string): void;
164
+ /** @description Creates an AppError instance representing an unauthorized access error. This method is used to generate a standardized error response when a user attempts to access a resource or perform an action without the necessary authentication or authorization.
165
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
166
+ * @param message A custom message describing the reason for the unauthorized access. This message is included in the AppError's cause for detailed error reporting.
167
+ * @returns An AppError instance representing the unauthorized access error.
168
+ *
169
+ * @author Xeno
170
+ * @version 1.0.0
171
+ * @since 2025-09-30
172
+ * @link https://github.com/xeno-js/xeno-js
173
+ */
174
+ static unauthorized(name: string, message: string): AppError;
175
+ static notSupported(name: string, message: string): AppError;
176
+ /** @description Creates an AppError instance representing a forbidden access error. This method is used to generate a standardized error response when a user attempts to access a resource or perform an action that they are not authorized to access, even if they are authenticated.
177
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
178
+ * @param message A custom message describing the reason for the forbidden access. This message is included in the AppError's cause for detailed error reporting.
179
+ * @returns An AppError instance representing the forbidden access error.
180
+ *
181
+ * @author Xeno
182
+ * @version 1.0.0
183
+ * @since 2025-09-30
184
+ * @link https://github.com/xeno-js/xeno-js
185
+ */
186
+ static forbidden(name: string, message: string): AppError;
187
+ /** @description Creates an AppError instance representing a bad request error. This method is used to generate a standardized error response when a request made by the client is invalid or cannot be processed due to client-side issues, such as validation errors or malformed requests.
188
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
189
+ * @param message A custom message describing the reason for the bad request. This message is included in the AppError's cause for detailed error reporting.
190
+ * @returns An AppError instance representing the bad request error.
191
+ *
192
+ * @author Xeno
193
+ * @version 1.0.0
194
+ * @since 2025-09-30
195
+ * @link https://github.com/xeno-js/xeno-js
196
+ */
197
+ static badRequest(name: string, message: string): AppError;
198
+ /** @description Creates an AppError instance representing a validation error. This method is used to generate a standardized error response when one or more input fields fail invariant or schema validation, indicating that the request cannot be processed due to invalid data.
199
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
200
+ * @param message A custom message describing the reason for the validation failure. This message is included in the AppError's cause for detailed error reporting.
201
+ * @returns An AppError instance representing the validation error.
202
+ *
203
+ * @author Xeno
204
+ * @version 1.0.0
205
+ * @since 2025-09-30
206
+ * @link https://github.com/xeno-js/xeno-js
207
+ */
208
+ static validationError(name: string, message: string): AppError;
209
+ /** @description Creates an AppError instance representing a conflict error. This method is used to generate a standardized error response when a request conflicts with the current state of the resource, such as when attempting to create a resource that already exists or update a resource that has been modified by another process.
210
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
211
+ * @param message A custom message describing the reason for the conflict. This message is included in the AppError's cause for detailed error reporting.
212
+ * @returns An AppError instance representing the conflict error.
213
+ *
214
+ * @author Xeno
215
+ * @version 1.0.0
216
+ * @since 2025-09-30
217
+ * @link https://github.com/xeno-js/xeno-js
218
+ */
219
+ static conflict(name: string, message: string): AppError;
220
+ /** @description Creates an AppError instance representing a not found error. This method is used to generate a standardized error response when a requested resource does not exist, indicating that the client attempted to access a resource that could not be found on the server.
221
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
222
+ * @param message A custom message describing the reason for the not found error. This message is included in the AppError's cause for detailed error reporting.
223
+ * @returns An AppError instance representing the not found error.
224
+ *
225
+ * @author Xeno
226
+ * @version 1.0.0
227
+ * @since 2025-09-30
228
+ * @link https://github.com/xeno-js/xeno-js
229
+ */
230
+ static notFound(name: string, message: string): AppError;
231
+ /** @description Creates an AppError instance representing a Authentication failed. This method is used to generate a standardized error response when an auth requested produce an error..
232
+ * @param name The name of the error, typically the class name or context where the error occurred. This helps in identifying the source of the error in logs and error reports.
233
+ * @param message A custom message describing the reason for the Authentication failed. This message is included in the AppError's cause for detailed error reporting.
234
+ * @returns An AppError instance representing the Authentication failed.
235
+ *
236
+ * @author Xeno
237
+ * @version 1.0.0
238
+ * @since 2025-09-30
239
+ * @link https://github.com/xeno-js/xeno-js
240
+ */
241
+ static authFailed(name: string, message: string): AppError;
242
+ }
243
+
244
+ /**
245
+ * A class representing the result of an operation, which can either be a success or a failure.
246
+ * It encapsulates the value of a successful operation or the error of a failed operation.
247
+ *
248
+ * @template TValue - The type of the value in case of a successful operation.
249
+ * @template TError - The type of the error in case of a failed operation (default is never).
250
+
251
+ *
252
+ * @author Xeno
253
+ * @version 1.0.0
254
+ * @since 2025-09-30
255
+ * @link https://github.com/xeno-js/xeno-js
256
+ */
257
+ declare class Result<TValue, TError = never> {
258
+ /**
259
+ * Indicates whether the operation was successful or not.
260
+
261
+ *
262
+ * @author Xeno
263
+ * @version 1.0.0
264
+ * @since 2025-09-30
265
+ * @link https://github.com/xeno-js/xeno-js
266
+ */
267
+ private readonly _isSuccess;
268
+ /**
269
+ * The error of the operation in case it failed.
270
+
271
+ *
272
+ * @author Xeno
273
+ * @version 1.0.0
274
+ * @since 2025-09-30
275
+ * @link https://github.com/xeno-js/xeno-js
276
+ */
277
+ private readonly _error;
278
+ /**
279
+ * The value of the operation in case it succeeded.
280
+
281
+ *
282
+ * @author Xeno
283
+ * @version 1.0.0
284
+ * @since 2025-09-30
285
+ * @link https://github.com/xeno-js/xeno-js
286
+ */
287
+ private readonly _value;
288
+ /**
289
+ * Private constructor to prevent direct instantiation. Use the static methods `ok` and `fail` to create instances.
290
+ *
291
+ * @param isSuccess - A boolean indicating whether the operation was successful.
292
+ * @param error - The error of the operation in case it failed (optional).
293
+ * @param value - The value of the operation in case it succeeded (optional).
294
+
295
+ *
296
+ * @author Xeno
297
+ * @version 1.0.0
298
+ * @since 2025-09-30
299
+ * @link https://github.com/xeno-js/xeno-js
300
+ */
301
+ private constructor();
302
+ /**
303
+ * Creates a successful result with the given value.
304
+ *
305
+ * @param value - The value of the successful operation.
306
+ * @returns A Result instance representing a successful operation.
307
+
308
+ *
309
+ * @author Xeno
310
+ * @version 1.0.0
311
+ * @since 2025-09-30
312
+ * @link https://github.com/xeno-js/xeno-js
313
+ */
314
+ static ok<U>(value?: U): Result<U>;
315
+ /**
316
+ * Creates a failed result with the given error.
317
+ *
318
+ * @param error - The error of the failed operation.
319
+ * @returns A Result instance representing a failed operation.
320
+
321
+ *
322
+ * @author Xeno
323
+ * @version 1.0.0
324
+ * @since 2025-09-30
325
+ * @link https://github.com/xeno-js/xeno-js
326
+ */
327
+ static fail<U, V = never>(error: V): Result<U, V>;
328
+ /**
329
+ * Checks if the result is a success.
330
+ *
331
+ * @returns True if the result is a success, false otherwise.
332
+
333
+ *
334
+ * @author Xeno
335
+ * @version 1.0.0
336
+ * @since 2025-09-30
337
+ * @link https://github.com/xeno-js/xeno-js
338
+ */
339
+ isOk(): boolean;
340
+ /**
341
+ * Gets the value of the result or throws an error if the result is a failure.
342
+ *
343
+ * @returns The value of the result.
344
+ * @throws An error if the result is a failure.
345
+
346
+ *
347
+ * @author Xeno
348
+ * @version 1.0.0
349
+ * @since 2025-09-30
350
+ * @link https://github.com/xeno-js/xeno-js
351
+ */
352
+ getValueOrThrow(): Optional<TValue>;
353
+ /**
354
+ * Gets the error of the result or throws an error if the result is a success.
355
+ *
356
+ * @returns The error of the result.
357
+ * @throws An error if the result is a success.
358
+
359
+ *
360
+ * @author Xeno
361
+ * @version 1.0.0
362
+ * @since 2025-09-30
363
+ * @link https://github.com/xeno-js/xeno-js
364
+ */
365
+ getErrorOrThrow(): TError;
366
+ }
367
+
368
+ /**
369
+ * A utility type to extract the value type from a Result instance.
370
+ *
371
+ * @template T - The type of the Result instance.
372
+
373
+ *
374
+ * @author Xeno
375
+ * @version 1.0.0
376
+ * @since 2025-09-30
377
+ * @link https://github.com/xeno-js/xeno-js
378
+ */
379
+ type ResultType<T, E = AppError> = Result<T, E>;
380
+
381
+ export { AppError as A, type ResultType as R, Result as a };