@alextheman/utility 5.14.0 → 5.15.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.
@@ -25,7 +25,7 @@ type IsTypeArgumentString<Argument extends string> = Argument;
25
25
  type NullableOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condition extends true ? ResolvedTypeIfTrue : ResolvedTypeIfTrue | null;
26
26
  //#endregion
27
27
  //#region src/v6/CodeError.d.ts
28
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
28
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
29
29
  expectedCode?: ErrorCode;
30
30
  }
31
31
  /**
@@ -50,8 +50,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
50
50
  *
51
51
  * @returns `true` if the input is a CodeError, and `false` otherwise. The type of the input will also be narrowed down to CodeError if `true`.
52
52
  */
53
- static check(input: unknown): input is CodeError<string>;
54
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
54
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
55
+ /**
56
+ * Check a `CodeError` against its error code
57
+ *
58
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
59
+ *
60
+ * @template ErrorCode The type of the error code
61
+ *
62
+ * @param input - The input to check.
63
+ * @param code - The expected code of the resulting error.
64
+ *
65
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
66
+ */
67
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
55
68
  /**
56
69
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
57
70
  *
@@ -63,7 +76,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
63
76
  *
64
77
  * @returns The `CodeError` that was thrown by the `errorFunction`
65
78
  */
66
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
79
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
67
80
  /**
68
81
  * Gets the thrown `CodeError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
69
82
  *
@@ -75,18 +88,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
75
88
  *
76
89
  * @returns The `CodeError` that was thrown by the `errorFunction`
77
90
  */
78
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
91
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
79
92
  }
80
93
  //#endregion
81
94
  //#region src/v6/DataError.d.ts
82
- type DefaultDataErrorCode = "INVALID_DATA";
83
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
84
- expectedCode?: ErrorCode | DefaultDataErrorCode;
85
- }
86
- declare const DataErrorCode: {
87
- readonly INVALID_DATA: "INVALID_DATA";
88
- };
89
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
90
95
  /**
91
96
  * Represents errors you may get that may've been caused by a specific piece of data.
92
97
  *
@@ -94,7 +99,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
94
99
  *
95
100
  * @template DataType - The type of the data that caused the error.
96
101
  */
97
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
102
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
98
103
  data: DataType;
99
104
  /**
100
105
  * @param data - The data that caused the error.
@@ -102,7 +107,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
102
107
  * @param message - A human-readable error message (e.g. The data provided is invalid).
103
108
  * @param options - Extra options to pass to super Error constructor.
104
109
  */
105
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
110
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
106
111
  /**
107
112
  * Checks whether the given input may have been caused by a DataError.
108
113
  *
@@ -110,7 +115,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
110
115
  *
111
116
  * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
112
117
  */
113
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
118
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
119
+ /**
120
+ * Check a `DataError` against its error code
121
+ *
122
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
123
+ *
124
+ * @template ErrorCode The type of the error code
125
+ *
126
+ * @param input - The input to check.
127
+ * @param code - The expected code of the resulting error.
128
+ *
129
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
130
+ */
131
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
114
132
  /**
115
133
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
116
134
  *
@@ -122,7 +140,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
122
140
  *
123
141
  * @returns The `DataError` that was thrown by the `errorFunction`
124
142
  */
125
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
143
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
126
144
  /**
127
145
  * Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
128
146
  *
@@ -134,7 +152,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
152
  *
135
153
  * @returns The `DataError` that was thrown by the `errorFunction`
136
154
  */
137
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
155
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
138
156
  }
139
157
  //#endregion
140
158
  //#region src/internal/DependencyGroup.d.ts
@@ -25,7 +25,7 @@ type IsTypeArgumentString<Argument extends string> = Argument;
25
25
  type NullableOnCondition<Condition extends boolean, ResolvedTypeIfTrue> = Condition extends true ? ResolvedTypeIfTrue : ResolvedTypeIfTrue | null;
26
26
  //#endregion
27
27
  //#region src/v6/CodeError.d.ts
28
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
28
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
29
29
  expectedCode?: ErrorCode;
30
30
  }
31
31
  /**
@@ -50,8 +50,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
50
50
  *
51
51
  * @returns `true` if the input is a CodeError, and `false` otherwise. The type of the input will also be narrowed down to CodeError if `true`.
52
52
  */
53
- static check(input: unknown): input is CodeError<string>;
54
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
54
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
55
+ /**
56
+ * Check a `CodeError` against its error code
57
+ *
58
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
59
+ *
60
+ * @template ErrorCode The type of the error code
61
+ *
62
+ * @param input - The input to check.
63
+ * @param code - The expected code of the resulting error.
64
+ *
65
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
66
+ */
67
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
55
68
  /**
56
69
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
57
70
  *
@@ -63,7 +76,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
63
76
  *
64
77
  * @returns The `CodeError` that was thrown by the `errorFunction`
65
78
  */
66
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
79
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
67
80
  /**
68
81
  * Gets the thrown `CodeError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
69
82
  *
@@ -75,18 +88,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
75
88
  *
76
89
  * @returns The `CodeError` that was thrown by the `errorFunction`
77
90
  */
78
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
91
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
79
92
  }
80
93
  //#endregion
81
94
  //#region src/v6/DataError.d.ts
82
- type DefaultDataErrorCode = "INVALID_DATA";
83
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
84
- expectedCode?: ErrorCode | DefaultDataErrorCode;
85
- }
86
- declare const DataErrorCode: {
87
- readonly INVALID_DATA: "INVALID_DATA";
88
- };
89
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
90
95
  /**
91
96
  * Represents errors you may get that may've been caused by a specific piece of data.
92
97
  *
@@ -94,7 +99,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
94
99
  *
95
100
  * @template DataType - The type of the data that caused the error.
96
101
  */
97
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
102
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
98
103
  data: DataType;
99
104
  /**
100
105
  * @param data - The data that caused the error.
@@ -102,7 +107,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
102
107
  * @param message - A human-readable error message (e.g. The data provided is invalid).
103
108
  * @param options - Extra options to pass to super Error constructor.
104
109
  */
105
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
110
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
106
111
  /**
107
112
  * Checks whether the given input may have been caused by a DataError.
108
113
  *
@@ -110,7 +115,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
110
115
  *
111
116
  * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
112
117
  */
113
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
118
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
119
+ /**
120
+ * Check a `DataError` against its error code
121
+ *
122
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
123
+ *
124
+ * @template ErrorCode The type of the error code
125
+ *
126
+ * @param input - The input to check.
127
+ * @param code - The expected code of the resulting error.
128
+ *
129
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
130
+ */
131
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
114
132
  /**
115
133
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
116
134
  *
@@ -122,7 +140,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
122
140
  *
123
141
  * @returns The `DataError` that was thrown by the `errorFunction`
124
142
  */
125
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
143
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
126
144
  /**
127
145
  * Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
128
146
  *
@@ -134,7 +152,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
152
  *
135
153
  * @returns The `DataError` that was thrown by the `errorFunction`
136
154
  */
137
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
155
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
138
156
  }
139
157
  //#endregion
140
158
  //#region src/internal/DependencyGroup.d.ts
@@ -107,6 +107,21 @@ var CodeError = class CodeError extends Error {
107
107
  throw error;
108
108
  }
109
109
  /**
110
+ * Check a `CodeError` against its error code
111
+ *
112
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
113
+ *
114
+ * @template ErrorCode The type of the error code
115
+ *
116
+ * @param input - The input to check.
117
+ * @param code - The expected code of the resulting error.
118
+ *
119
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
120
+ */
121
+ static checkWithCode(input, code) {
122
+ return this.check(input) && input.code === code;
123
+ }
124
+ /**
110
125
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
111
126
  *
112
127
  * @param errorFunction - The function expected to throw the error.
@@ -162,11 +177,10 @@ var DataError = class DataError extends CodeError {
162
177
  * @param message - A human-readable error message (e.g. The data provided is invalid).
163
178
  * @param options - Extra options to pass to super Error constructor.
164
179
  */
165
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
180
+ constructor(data, code, message = "The data provided is invalid", options) {
166
181
  super(code, message, options);
167
182
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
168
183
  this.name = new.target.name;
169
- this.code = code;
170
184
  this.data = data;
171
185
  Object.defineProperty(this, "message", { enumerable: true });
172
186
  Object.setPrototypeOf(this, new.target.prototype);
@@ -183,6 +197,21 @@ var DataError = class DataError extends CodeError {
183
197
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
184
198
  }
185
199
  /**
200
+ * Check a `DataError` against its error code
201
+ *
202
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
203
+ *
204
+ * @template ErrorCode The type of the error code
205
+ *
206
+ * @param input - The input to check.
207
+ * @param code - The expected code of the resulting error.
208
+ *
209
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
210
+ */
211
+ static checkWithCode(input, code) {
212
+ return this.check(input) && input.code === code;
213
+ }
214
+ /**
186
215
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
187
216
  *
188
217
  * @param errorFunction - The function expected to throw the error.
@@ -248,6 +248,21 @@ var CodeError = class CodeError extends Error {
248
248
  throw error;
249
249
  }
250
250
  /**
251
+ * Check a `CodeError` against its error code
252
+ *
253
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
254
+ *
255
+ * @template ErrorCode The type of the error code
256
+ *
257
+ * @param input - The input to check.
258
+ * @param code - The expected code of the resulting error.
259
+ *
260
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
261
+ */
262
+ static checkWithCode(input, code) {
263
+ return this.check(input) && input.code === code;
264
+ }
265
+ /**
251
266
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
252
267
  *
253
268
  * @param errorFunction - The function expected to throw the error.
@@ -303,11 +318,10 @@ var DataError = class DataError extends CodeError {
303
318
  * @param message - A human-readable error message (e.g. The data provided is invalid).
304
319
  * @param options - Extra options to pass to super Error constructor.
305
320
  */
306
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
321
+ constructor(data, code, message = "The data provided is invalid", options) {
307
322
  super(code, message, options);
308
323
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
309
324
  this.name = new.target.name;
310
- this.code = code;
311
325
  this.data = data;
312
326
  Object.defineProperty(this, "message", { enumerable: true });
313
327
  Object.setPrototypeOf(this, new.target.prototype);
@@ -324,6 +338,21 @@ var DataError = class DataError extends CodeError {
324
338
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
325
339
  }
326
340
  /**
341
+ * Check a `DataError` against its error code
342
+ *
343
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
344
+ *
345
+ * @template ErrorCode The type of the error code
346
+ *
347
+ * @param input - The input to check.
348
+ * @param code - The expected code of the resulting error.
349
+ *
350
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
351
+ */
352
+ static checkWithCode(input, code) {
353
+ return this.check(input) && input.code === code;
354
+ }
355
+ /**
327
356
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
328
357
  *
329
358
  * @param errorFunction - The function expected to throw the error.
@@ -224,6 +224,21 @@ var CodeError = class CodeError extends Error {
224
224
  throw error;
225
225
  }
226
226
  /**
227
+ * Check a `CodeError` against its error code
228
+ *
229
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
230
+ *
231
+ * @template ErrorCode The type of the error code
232
+ *
233
+ * @param input - The input to check.
234
+ * @param code - The expected code of the resulting error.
235
+ *
236
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
237
+ */
238
+ static checkWithCode(input, code) {
239
+ return this.check(input) && input.code === code;
240
+ }
241
+ /**
227
242
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
228
243
  *
229
244
  * @param errorFunction - The function expected to throw the error.
@@ -279,11 +294,10 @@ var DataError = class DataError extends CodeError {
279
294
  * @param message - A human-readable error message (e.g. The data provided is invalid).
280
295
  * @param options - Extra options to pass to super Error constructor.
281
296
  */
282
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
297
+ constructor(data, code, message = "The data provided is invalid", options) {
283
298
  super(code, message, options);
284
299
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
285
300
  this.name = new.target.name;
286
- this.code = code;
287
301
  this.data = data;
288
302
  Object.defineProperty(this, "message", { enumerable: true });
289
303
  Object.setPrototypeOf(this, new.target.prototype);
@@ -300,6 +314,21 @@ var DataError = class DataError extends CodeError {
300
314
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
301
315
  }
302
316
  /**
317
+ * Check a `DataError` against its error code
318
+ *
319
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
320
+ *
321
+ * @template ErrorCode The type of the error code
322
+ *
323
+ * @param input - The input to check.
324
+ * @param code - The expected code of the resulting error.
325
+ *
326
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
327
+ */
328
+ static checkWithCode(input, code) {
329
+ return this.check(input) && input.code === code;
330
+ }
331
+ /**
303
332
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
304
333
  *
305
334
  * @param errorFunction - The function expected to throw the error.
package/dist/v6/index.cjs CHANGED
@@ -184,6 +184,21 @@ var CodeError = class CodeError extends Error {
184
184
  throw error;
185
185
  }
186
186
  /**
187
+ * Check a `CodeError` against its error code
188
+ *
189
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
190
+ *
191
+ * @template ErrorCode The type of the error code
192
+ *
193
+ * @param input - The input to check.
194
+ * @param code - The expected code of the resulting error.
195
+ *
196
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
197
+ */
198
+ static checkWithCode(input, code) {
199
+ return this.check(input) && input.code === code;
200
+ }
201
+ /**
187
202
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
188
203
  *
189
204
  * @param errorFunction - The function expected to throw the error.
@@ -224,7 +239,6 @@ var CodeError = class CodeError extends Error {
224
239
  };
225
240
  //#endregion
226
241
  //#region src/v6/DataError.ts
227
- const DataErrorCode = { INVALID_DATA: "INVALID_DATA" };
228
242
  /**
229
243
  * Represents errors you may get that may've been caused by a specific piece of data.
230
244
  *
@@ -240,11 +254,10 @@ var DataError = class DataError extends CodeError {
240
254
  * @param message - A human-readable error message (e.g. The data provided is invalid).
241
255
  * @param options - Extra options to pass to super Error constructor.
242
256
  */
243
- constructor(data, code = "INVALID_DATA", message = "The data provided is invalid", options) {
257
+ constructor(data, code, message = "The data provided is invalid", options) {
244
258
  super(code, message, options);
245
259
  if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
246
260
  this.name = new.target.name;
247
- this.code = code;
248
261
  this.data = data;
249
262
  Object.defineProperty(this, "message", { enumerable: true });
250
263
  Object.setPrototypeOf(this, new.target.prototype);
@@ -261,6 +274,21 @@ var DataError = class DataError extends CodeError {
261
274
  return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
262
275
  }
263
276
  /**
277
+ * Check a `DataError` against its error code
278
+ *
279
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
280
+ *
281
+ * @template ErrorCode The type of the error code
282
+ *
283
+ * @param input - The input to check.
284
+ * @param code - The expected code of the resulting error.
285
+ *
286
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
287
+ */
288
+ static checkWithCode(input, code) {
289
+ return this.check(input) && input.code === code;
290
+ }
291
+ /**
264
292
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
265
293
  *
266
294
  * @param errorFunction - The function expected to throw the error.
@@ -405,5 +433,4 @@ I'll commit to you!
405
433
  //#endregion
406
434
  exports.CodeError = CodeError;
407
435
  exports.DataError = DataError;
408
- exports.DataErrorCode = DataErrorCode;
409
436
  exports.sayHello = sayHello;
@@ -1,5 +1,5 @@
1
1
  //#region src/v6/CodeError.d.ts
2
- interface ExpectErrorOptions$1<ErrorCode extends string = string> {
2
+ interface ExpectErrorOptions<ErrorCode extends string = string> {
3
3
  expectedCode?: ErrorCode;
4
4
  }
5
5
  /**
@@ -24,8 +24,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
24
24
  *
25
25
  * @returns `true` if the input is a CodeError, and `false` otherwise. The type of the input will also be narrowed down to CodeError if `true`.
26
26
  */
27
- static check(input: unknown): input is CodeError<string>;
28
- protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
27
+ static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
28
+ protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
29
+ /**
30
+ * Check a `CodeError` against its error code
31
+ *
32
+ * This will also automatically narrow down the type of the input to be `CodeError`, with its error code properly typed if this function returns true.
33
+ *
34
+ * @template ErrorCode The type of the error code
35
+ *
36
+ * @param input - The input to check.
37
+ * @param code - The expected code of the resulting error.
38
+ *
39
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to CodeError, and its code will be narrowed to the expected code's type if the function returns `true`.
40
+ */
41
+ static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
29
42
  /**
30
43
  * Gets the thrown `CodeError` from a given function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
31
44
  *
@@ -37,7 +50,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
37
50
  *
38
51
  * @returns The `CodeError` that was thrown by the `errorFunction`
39
52
  */
40
- static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): CodeError;
53
+ static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
41
54
  /**
42
55
  * Gets the thrown `CodeError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `CodeError` if no error thrown.
43
56
  *
@@ -49,31 +62,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
49
62
  *
50
63
  * @returns The `CodeError` that was thrown by the `errorFunction`
51
64
  */
52
- static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<CodeError>;
65
+ static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
53
66
  }
54
67
  //#endregion
55
- //#region src/root/types/CreateEnumType.d.ts
56
- /**
57
- * Get the value types from a const object so the object can behave similarly to an enum.
58
- *
59
- * @category Types
60
- *
61
- * @template ObjectType - The type of the object to get the value types for.
62
- */
63
- type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
64
- //#endregion
65
- //#region src/root/types/IsTypeArgumentString.d.ts
66
- type IsTypeArgumentString<Argument extends string> = Argument;
67
- //#endregion
68
68
  //#region src/v6/DataError.d.ts
69
- type DefaultDataErrorCode = "INVALID_DATA";
70
- interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
71
- expectedCode?: ErrorCode | DefaultDataErrorCode;
72
- }
73
- declare const DataErrorCode: {
74
- readonly INVALID_DATA: "INVALID_DATA";
75
- };
76
- type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
77
69
  /**
78
70
  * Represents errors you may get that may've been caused by a specific piece of data.
79
71
  *
@@ -81,7 +73,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
81
73
  *
82
74
  * @template DataType - The type of the data that caused the error.
83
75
  */
84
- declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
76
+ declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
85
77
  data: DataType;
86
78
  /**
87
79
  * @param data - The data that caused the error.
@@ -89,7 +81,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
89
81
  * @param message - A human-readable error message (e.g. The data provided is invalid).
90
82
  * @param options - Extra options to pass to super Error constructor.
91
83
  */
92
- constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
84
+ constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
93
85
  /**
94
86
  * Checks whether the given input may have been caused by a DataError.
95
87
  *
@@ -97,7 +89,20 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
97
89
  *
98
90
  * @returns `true` if the input is a DataError, and `false` otherwise. The type of the input will also be narrowed down to DataError if `true`.
99
91
  */
100
- static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError<DataType, ErrorCode>;
92
+ static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError<DataType, ErrorCode>;
93
+ /**
94
+ * Check a `DataError` against its error code
95
+ *
96
+ * This will also automatically narrow down the type of the input to be `DataError`, with its error code properly typed if this function returns true.
97
+ *
98
+ * @template ErrorCode The type of the error code
99
+ *
100
+ * @param input - The input to check.
101
+ * @param code - The expected code of the resulting error.
102
+ *
103
+ * @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `DataError`, and its code will be narrowed to the expected code's type if the function returns `true`.
104
+ */
105
+ static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError<DataType, ErrorCode>;
101
106
  /**
102
107
  * Gets the thrown `DataError` from a given function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
103
108
  *
@@ -109,7 +114,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
109
114
  *
110
115
  * @returns The `DataError` that was thrown by the `errorFunction`
111
116
  */
112
- static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
117
+ static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError<DataType, ErrorCode>;
113
118
  /**
114
119
  * Gets the thrown `DataError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `DataError` if no error thrown.
115
120
  *
@@ -121,7 +126,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
121
126
  *
122
127
  * @returns The `DataError` that was thrown by the `errorFunction`
123
128
  */
124
- static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
129
+ static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
125
130
  }
126
131
  //#endregion
127
132
  //#region src/v6/sayHello.d.ts
@@ -134,4 +139,7 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
134
139
  */
135
140
  declare function sayHello(): string;
136
141
  //#endregion
137
- export { CodeError, DataError, DataErrorCode, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
142
+ //#region src/root/types/IsTypeArgumentString.d.ts
143
+ type IsTypeArgumentString<Argument extends string> = Argument;
144
+ //#endregion
145
+ export { CodeError, DataError, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };