@alextheman/utility 5.11.3 → 5.13.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/dist/index.cjs +605 -544
- package/dist/index.d.cts +174 -59
- package/dist/index.d.ts +174 -59
- package/dist/index.js +606 -544
- package/dist/internal/index.cjs +244 -185
- package/dist/internal/index.d.cts +101 -41
- package/dist/internal/index.d.ts +101 -41
- package/dist/internal/index.js +244 -185
- package/dist/node/index.cjs +197 -138
- package/dist/node/index.d.cts +1 -1
- package/dist/node/index.d.ts +1 -1
- package/dist/node/index.js +197 -138
- package/dist/v6/index.cjs +409 -0
- package/dist/v6/index.d.cts +137 -0
- package/dist/v6/index.d.ts +137 -0
- package/dist/v6/index.js +405 -0
- package/package.json +6 -1
package/dist/index.d.cts
CHANGED
|
@@ -461,62 +461,6 @@ declare class APIError extends Error {
|
|
|
461
461
|
static check(input: unknown): input is APIError;
|
|
462
462
|
}
|
|
463
463
|
//#endregion
|
|
464
|
-
//#region src/root/types/DataError.d.ts
|
|
465
|
-
interface ExpectErrorOptions {
|
|
466
|
-
expectedCode?: string;
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
470
|
-
*
|
|
471
|
-
* @category Types
|
|
472
|
-
*
|
|
473
|
-
* @template DataType - The type of the data that caused the error.
|
|
474
|
-
*/
|
|
475
|
-
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
476
|
-
code: string;
|
|
477
|
-
data: DataType;
|
|
478
|
-
/**
|
|
479
|
-
* @param data - The data that caused the error.
|
|
480
|
-
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
481
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
482
|
-
* @param options - Extra options to pass to super Error constructor.
|
|
483
|
-
*/
|
|
484
|
-
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
485
|
-
private static checkCaughtError;
|
|
486
|
-
/**
|
|
487
|
-
* Checks whether the given input may have been caused by a DataError.
|
|
488
|
-
*
|
|
489
|
-
* @param input - The input to check.
|
|
490
|
-
*
|
|
491
|
-
* @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`.
|
|
492
|
-
*/
|
|
493
|
-
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
494
|
-
/**
|
|
495
|
-
* 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.
|
|
496
|
-
*
|
|
497
|
-
* @param errorFunction - The function expected to throw the error.
|
|
498
|
-
* @param options - Extra options to apply.
|
|
499
|
-
*
|
|
500
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
501
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
502
|
-
*
|
|
503
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
504
|
-
*/
|
|
505
|
-
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
506
|
-
/**
|
|
507
|
-
* 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.
|
|
508
|
-
*
|
|
509
|
-
* @param errorFunction - The function expected to throw the error.
|
|
510
|
-
* @param options - Extra options to apply.
|
|
511
|
-
*
|
|
512
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
513
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
514
|
-
*
|
|
515
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
516
|
-
*/
|
|
517
|
-
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
518
|
-
}
|
|
519
|
-
//#endregion
|
|
520
464
|
//#region src/root/types/VersionNumber.d.ts
|
|
521
465
|
/**
|
|
522
466
|
* Options to apply to the stringification of the version number.
|
|
@@ -794,6 +738,119 @@ type VersionType = CreateEnumType<typeof VersionType>;
|
|
|
794
738
|
*/
|
|
795
739
|
declare function parseVersionType(input: unknown): VersionType;
|
|
796
740
|
//#endregion
|
|
741
|
+
//#region src/v6/CodeError.d.ts
|
|
742
|
+
interface ExpectErrorOptions$2<ErrorCode extends string = string> {
|
|
743
|
+
expectedCode?: ErrorCode;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Represents errors that can be described using a standardised error code, and a human-readable error message.
|
|
747
|
+
*
|
|
748
|
+
* @category Types
|
|
749
|
+
*
|
|
750
|
+
* @template ErrorCode The type of the standardised error code.
|
|
751
|
+
*/
|
|
752
|
+
declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
753
|
+
code: ErrorCode;
|
|
754
|
+
/**
|
|
755
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
756
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
757
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
758
|
+
*/
|
|
759
|
+
constructor(code: ErrorCode, message?: string, options?: ErrorOptions);
|
|
760
|
+
/**
|
|
761
|
+
* Checks whether the given input may have been caused by a CodeError.
|
|
762
|
+
*
|
|
763
|
+
* @param input - The input to check.
|
|
764
|
+
*
|
|
765
|
+
* @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`.
|
|
766
|
+
*/
|
|
767
|
+
static check(input: unknown): input is CodeError<string>;
|
|
768
|
+
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$2<ErrorCode>): CodeError;
|
|
769
|
+
/**
|
|
770
|
+
* 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.
|
|
771
|
+
*
|
|
772
|
+
* @param errorFunction - The function expected to throw the error.
|
|
773
|
+
* @param options - Extra options to apply.
|
|
774
|
+
*
|
|
775
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
|
|
776
|
+
* @throws {Error} If no `CodeError` was thrown by the `errorFunction`
|
|
777
|
+
*
|
|
778
|
+
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
779
|
+
*/
|
|
780
|
+
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$2<ErrorCode>): CodeError;
|
|
781
|
+
/**
|
|
782
|
+
* 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.
|
|
783
|
+
*
|
|
784
|
+
* @param errorFunction - The function expected to throw the error.
|
|
785
|
+
* @param options - Extra options to apply.
|
|
786
|
+
*
|
|
787
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
|
|
788
|
+
* @throws {Error} If no `CodeError` was thrown by the `errorFunction`
|
|
789
|
+
*
|
|
790
|
+
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
791
|
+
*/
|
|
792
|
+
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$2<ErrorCode>): Promise<CodeError>;
|
|
793
|
+
}
|
|
794
|
+
//#endregion
|
|
795
|
+
//#region src/v6/DataError.d.ts
|
|
796
|
+
type DefaultDataErrorCode = "INVALID_DATA";
|
|
797
|
+
interface ExpectErrorOptions$1<ErrorCode extends string = DefaultDataErrorCode> {
|
|
798
|
+
expectedCode?: ErrorCode | DefaultDataErrorCode;
|
|
799
|
+
}
|
|
800
|
+
declare const DataErrorCode: {
|
|
801
|
+
readonly INVALID_DATA: "INVALID_DATA";
|
|
802
|
+
};
|
|
803
|
+
type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
|
|
804
|
+
/**
|
|
805
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
806
|
+
*
|
|
807
|
+
* @category Types
|
|
808
|
+
*
|
|
809
|
+
* @template DataType - The type of the data that caused the error.
|
|
810
|
+
*/
|
|
811
|
+
declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
|
|
812
|
+
data: DataType;
|
|
813
|
+
/**
|
|
814
|
+
* @param data - The data that caused the error.
|
|
815
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
816
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
817
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
818
|
+
*/
|
|
819
|
+
constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
|
|
820
|
+
/**
|
|
821
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
822
|
+
*
|
|
823
|
+
* @param input - The input to check.
|
|
824
|
+
*
|
|
825
|
+
* @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`.
|
|
826
|
+
*/
|
|
827
|
+
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError$1<DataType, ErrorCode>;
|
|
828
|
+
/**
|
|
829
|
+
* 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.
|
|
830
|
+
*
|
|
831
|
+
* @param errorFunction - The function expected to throw the error.
|
|
832
|
+
* @param options - Extra options to apply.
|
|
833
|
+
*
|
|
834
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
835
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
836
|
+
*
|
|
837
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
838
|
+
*/
|
|
839
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
840
|
+
/**
|
|
841
|
+
* 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.
|
|
842
|
+
*
|
|
843
|
+
* @param errorFunction - The function expected to throw the error.
|
|
844
|
+
* @param options - Extra options to apply.
|
|
845
|
+
*
|
|
846
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
847
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
848
|
+
*
|
|
849
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
850
|
+
*/
|
|
851
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
852
|
+
}
|
|
853
|
+
//#endregion
|
|
797
854
|
//#region src/root/functions/parsers/zod/parseZodSchema.d.ts
|
|
798
855
|
/**
|
|
799
856
|
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
@@ -809,11 +866,11 @@ declare function parseVersionType(input: unknown): VersionType;
|
|
|
809
866
|
* @param input - The data to parse.
|
|
810
867
|
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
811
868
|
*
|
|
812
|
-
* @throws {
|
|
869
|
+
* @throws {DataErrorCode} If the given data cannot be parsed according to the schema.
|
|
813
870
|
*
|
|
814
871
|
* @returns The parsed data from the Zod schema.
|
|
815
872
|
*/
|
|
816
|
-
declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Error = DataError>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): z.infer<SchemaType>;
|
|
873
|
+
declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Error = DataError$1>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): z.infer<SchemaType>;
|
|
817
874
|
//#endregion
|
|
818
875
|
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.d.ts
|
|
819
876
|
/**
|
|
@@ -832,7 +889,7 @@ declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Er
|
|
|
832
889
|
*
|
|
833
890
|
* @returns The parsed data from the Zod schema.
|
|
834
891
|
*/
|
|
835
|
-
declare function parseZodSchemaAsync<SchemaType extends ZodType, ErrorType extends Error = DataError>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): Promise<z.infer<SchemaType>>;
|
|
892
|
+
declare function parseZodSchemaAsync<SchemaType extends ZodType, ErrorType extends Error = DataError$1>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): Promise<z.infer<SchemaType>>;
|
|
836
893
|
//#endregion
|
|
837
894
|
//#region src/root/functions/recursive/deepCopy.d.ts
|
|
838
895
|
/**
|
|
@@ -1139,6 +1196,64 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1139
1196
|
*/
|
|
1140
1197
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1141
1198
|
//#endregion
|
|
1199
|
+
//#region src/root/deprecated/DataError.d.ts
|
|
1200
|
+
interface ExpectErrorOptions {
|
|
1201
|
+
expectedCode?: string;
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1205
|
+
*
|
|
1206
|
+
* @category Types
|
|
1207
|
+
*
|
|
1208
|
+
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
1209
|
+
*
|
|
1210
|
+
* @template DataType - The type of the data that caused the error.
|
|
1211
|
+
*/
|
|
1212
|
+
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
1213
|
+
code: string;
|
|
1214
|
+
data: DataType;
|
|
1215
|
+
/**
|
|
1216
|
+
* @param data - The data that caused the error.
|
|
1217
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
1218
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1219
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
1220
|
+
*/
|
|
1221
|
+
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
1222
|
+
private static checkCaughtError;
|
|
1223
|
+
/**
|
|
1224
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
1225
|
+
*
|
|
1226
|
+
* @param input - The input to check.
|
|
1227
|
+
*
|
|
1228
|
+
* @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`.
|
|
1229
|
+
*/
|
|
1230
|
+
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
1231
|
+
/**
|
|
1232
|
+
* 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.
|
|
1233
|
+
*
|
|
1234
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1235
|
+
* @param options - Extra options to apply.
|
|
1236
|
+
*
|
|
1237
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1238
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1239
|
+
*
|
|
1240
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1241
|
+
*/
|
|
1242
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
1243
|
+
/**
|
|
1244
|
+
* 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.
|
|
1245
|
+
*
|
|
1246
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1247
|
+
* @param options - Extra options to apply.
|
|
1248
|
+
*
|
|
1249
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1250
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1251
|
+
*
|
|
1252
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1253
|
+
*/
|
|
1254
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
1255
|
+
}
|
|
1256
|
+
//#endregion
|
|
1142
1257
|
//#region src/root/deprecated/RecordKey.d.ts
|
|
1143
1258
|
/**
|
|
1144
1259
|
* Represents the native Record's possible key type.
|
package/dist/index.d.ts
CHANGED
|
@@ -461,62 +461,6 @@ declare class APIError extends Error {
|
|
|
461
461
|
static check(input: unknown): input is APIError;
|
|
462
462
|
}
|
|
463
463
|
//#endregion
|
|
464
|
-
//#region src/root/types/DataError.d.ts
|
|
465
|
-
interface ExpectErrorOptions {
|
|
466
|
-
expectedCode?: string;
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
470
|
-
*
|
|
471
|
-
* @category Types
|
|
472
|
-
*
|
|
473
|
-
* @template DataType - The type of the data that caused the error.
|
|
474
|
-
*/
|
|
475
|
-
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
476
|
-
code: string;
|
|
477
|
-
data: DataType;
|
|
478
|
-
/**
|
|
479
|
-
* @param data - The data that caused the error.
|
|
480
|
-
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
481
|
-
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
482
|
-
* @param options - Extra options to pass to super Error constructor.
|
|
483
|
-
*/
|
|
484
|
-
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
485
|
-
private static checkCaughtError;
|
|
486
|
-
/**
|
|
487
|
-
* Checks whether the given input may have been caused by a DataError.
|
|
488
|
-
*
|
|
489
|
-
* @param input - The input to check.
|
|
490
|
-
*
|
|
491
|
-
* @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`.
|
|
492
|
-
*/
|
|
493
|
-
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
494
|
-
/**
|
|
495
|
-
* 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.
|
|
496
|
-
*
|
|
497
|
-
* @param errorFunction - The function expected to throw the error.
|
|
498
|
-
* @param options - Extra options to apply.
|
|
499
|
-
*
|
|
500
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
501
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
502
|
-
*
|
|
503
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
504
|
-
*/
|
|
505
|
-
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
506
|
-
/**
|
|
507
|
-
* 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.
|
|
508
|
-
*
|
|
509
|
-
* @param errorFunction - The function expected to throw the error.
|
|
510
|
-
* @param options - Extra options to apply.
|
|
511
|
-
*
|
|
512
|
-
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
513
|
-
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
514
|
-
*
|
|
515
|
-
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
516
|
-
*/
|
|
517
|
-
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
518
|
-
}
|
|
519
|
-
//#endregion
|
|
520
464
|
//#region src/root/types/VersionNumber.d.ts
|
|
521
465
|
/**
|
|
522
466
|
* Options to apply to the stringification of the version number.
|
|
@@ -794,6 +738,119 @@ type VersionType = CreateEnumType<typeof VersionType>;
|
|
|
794
738
|
*/
|
|
795
739
|
declare function parseVersionType(input: unknown): VersionType;
|
|
796
740
|
//#endregion
|
|
741
|
+
//#region src/v6/CodeError.d.ts
|
|
742
|
+
interface ExpectErrorOptions$2<ErrorCode extends string = string> {
|
|
743
|
+
expectedCode?: ErrorCode;
|
|
744
|
+
}
|
|
745
|
+
/**
|
|
746
|
+
* Represents errors that can be described using a standardised error code, and a human-readable error message.
|
|
747
|
+
*
|
|
748
|
+
* @category Types
|
|
749
|
+
*
|
|
750
|
+
* @template ErrorCode The type of the standardised error code.
|
|
751
|
+
*/
|
|
752
|
+
declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
753
|
+
code: ErrorCode;
|
|
754
|
+
/**
|
|
755
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
756
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
757
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
758
|
+
*/
|
|
759
|
+
constructor(code: ErrorCode, message?: string, options?: ErrorOptions);
|
|
760
|
+
/**
|
|
761
|
+
* Checks whether the given input may have been caused by a CodeError.
|
|
762
|
+
*
|
|
763
|
+
* @param input - The input to check.
|
|
764
|
+
*
|
|
765
|
+
* @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`.
|
|
766
|
+
*/
|
|
767
|
+
static check(input: unknown): input is CodeError<string>;
|
|
768
|
+
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions$2<ErrorCode>): CodeError;
|
|
769
|
+
/**
|
|
770
|
+
* 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.
|
|
771
|
+
*
|
|
772
|
+
* @param errorFunction - The function expected to throw the error.
|
|
773
|
+
* @param options - Extra options to apply.
|
|
774
|
+
*
|
|
775
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
|
|
776
|
+
* @throws {Error} If no `CodeError` was thrown by the `errorFunction`
|
|
777
|
+
*
|
|
778
|
+
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
779
|
+
*/
|
|
780
|
+
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions$2<ErrorCode>): CodeError;
|
|
781
|
+
/**
|
|
782
|
+
* 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.
|
|
783
|
+
*
|
|
784
|
+
* @param errorFunction - The function expected to throw the error.
|
|
785
|
+
* @param options - Extra options to apply.
|
|
786
|
+
*
|
|
787
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `CodeError`.
|
|
788
|
+
* @throws {Error} If no `CodeError` was thrown by the `errorFunction`
|
|
789
|
+
*
|
|
790
|
+
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
791
|
+
*/
|
|
792
|
+
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$2<ErrorCode>): Promise<CodeError>;
|
|
793
|
+
}
|
|
794
|
+
//#endregion
|
|
795
|
+
//#region src/v6/DataError.d.ts
|
|
796
|
+
type DefaultDataErrorCode = "INVALID_DATA";
|
|
797
|
+
interface ExpectErrorOptions$1<ErrorCode extends string = DefaultDataErrorCode> {
|
|
798
|
+
expectedCode?: ErrorCode | DefaultDataErrorCode;
|
|
799
|
+
}
|
|
800
|
+
declare const DataErrorCode: {
|
|
801
|
+
readonly INVALID_DATA: "INVALID_DATA";
|
|
802
|
+
};
|
|
803
|
+
type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
|
|
804
|
+
/**
|
|
805
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
806
|
+
*
|
|
807
|
+
* @category Types
|
|
808
|
+
*
|
|
809
|
+
* @template DataType - The type of the data that caused the error.
|
|
810
|
+
*/
|
|
811
|
+
declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode> extends CodeError<ErrorCode | DataErrorCode> {
|
|
812
|
+
data: DataType;
|
|
813
|
+
/**
|
|
814
|
+
* @param data - The data that caused the error.
|
|
815
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
816
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
817
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
818
|
+
*/
|
|
819
|
+
constructor(data: DataType, code?: ErrorCode | DefaultDataErrorCode, message?: string, options?: ErrorOptions);
|
|
820
|
+
/**
|
|
821
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
822
|
+
*
|
|
823
|
+
* @param input - The input to check.
|
|
824
|
+
*
|
|
825
|
+
* @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`.
|
|
826
|
+
*/
|
|
827
|
+
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = DataErrorCode>(input: unknown): input is DataError$1<DataType, ErrorCode>;
|
|
828
|
+
/**
|
|
829
|
+
* 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.
|
|
830
|
+
*
|
|
831
|
+
* @param errorFunction - The function expected to throw the error.
|
|
832
|
+
* @param options - Extra options to apply.
|
|
833
|
+
*
|
|
834
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
835
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
836
|
+
*
|
|
837
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
838
|
+
*/
|
|
839
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => unknown, options?: ExpectErrorOptions$1<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
840
|
+
/**
|
|
841
|
+
* 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.
|
|
842
|
+
*
|
|
843
|
+
* @param errorFunction - The function expected to throw the error.
|
|
844
|
+
* @param options - Extra options to apply.
|
|
845
|
+
*
|
|
846
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
847
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
848
|
+
*
|
|
849
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
850
|
+
*/
|
|
851
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = DefaultDataErrorCode>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
852
|
+
}
|
|
853
|
+
//#endregion
|
|
797
854
|
//#region src/root/functions/parsers/zod/parseZodSchema.d.ts
|
|
798
855
|
/**
|
|
799
856
|
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
@@ -809,11 +866,11 @@ declare function parseVersionType(input: unknown): VersionType;
|
|
|
809
866
|
* @param input - The data to parse.
|
|
810
867
|
* @param onError - A custom error to throw on invalid data (defaults to `DataError`). May either be the error itself, or a function that returns the error or nothing. If nothing is returned, the default error is thrown instead.
|
|
811
868
|
*
|
|
812
|
-
* @throws {
|
|
869
|
+
* @throws {DataErrorCode} If the given data cannot be parsed according to the schema.
|
|
813
870
|
*
|
|
814
871
|
* @returns The parsed data from the Zod schema.
|
|
815
872
|
*/
|
|
816
|
-
declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Error = DataError>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): z.infer<SchemaType>;
|
|
873
|
+
declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Error = DataError$1>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): z.infer<SchemaType>;
|
|
817
874
|
//#endregion
|
|
818
875
|
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.d.ts
|
|
819
876
|
/**
|
|
@@ -832,7 +889,7 @@ declare function parseZodSchema<SchemaType extends ZodType, ErrorType extends Er
|
|
|
832
889
|
*
|
|
833
890
|
* @returns The parsed data from the Zod schema.
|
|
834
891
|
*/
|
|
835
|
-
declare function parseZodSchemaAsync<SchemaType extends ZodType, ErrorType extends Error = DataError>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): Promise<z.infer<SchemaType>>;
|
|
892
|
+
declare function parseZodSchemaAsync<SchemaType extends ZodType, ErrorType extends Error = DataError$1>(schema: SchemaType, input: unknown, onError?: ErrorType | ((zodError: ZodError) => ErrorType | void)): Promise<z.infer<SchemaType>>;
|
|
836
893
|
//#endregion
|
|
837
894
|
//#region src/root/functions/recursive/deepCopy.d.ts
|
|
838
895
|
/**
|
|
@@ -1139,6 +1196,64 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1139
1196
|
*/
|
|
1140
1197
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1141
1198
|
//#endregion
|
|
1199
|
+
//#region src/root/deprecated/DataError.d.ts
|
|
1200
|
+
interface ExpectErrorOptions {
|
|
1201
|
+
expectedCode?: string;
|
|
1202
|
+
}
|
|
1203
|
+
/**
|
|
1204
|
+
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1205
|
+
*
|
|
1206
|
+
* @category Types
|
|
1207
|
+
*
|
|
1208
|
+
* @deprecated Please use `DataError` from `@alextheman/utility/v6` instead.
|
|
1209
|
+
*
|
|
1210
|
+
* @template DataType - The type of the data that caused the error.
|
|
1211
|
+
*/
|
|
1212
|
+
declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>> extends Error {
|
|
1213
|
+
code: string;
|
|
1214
|
+
data: DataType;
|
|
1215
|
+
/**
|
|
1216
|
+
* @param data - The data that caused the error.
|
|
1217
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
1218
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1219
|
+
* @param options - Extra options to pass to super Error constructor.
|
|
1220
|
+
*/
|
|
1221
|
+
constructor(data: DataType, code?: string, message?: string, options?: ErrorOptions);
|
|
1222
|
+
private static checkCaughtError;
|
|
1223
|
+
/**
|
|
1224
|
+
* Checks whether the given input may have been caused by a DataError.
|
|
1225
|
+
*
|
|
1226
|
+
* @param input - The input to check.
|
|
1227
|
+
*
|
|
1228
|
+
* @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`.
|
|
1229
|
+
*/
|
|
1230
|
+
static check<DataType extends Record<PropertyKey, unknown> = Record<PropertyKey, unknown>>(input: unknown): input is DataError<DataType>;
|
|
1231
|
+
/**
|
|
1232
|
+
* 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.
|
|
1233
|
+
*
|
|
1234
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1235
|
+
* @param options - Extra options to apply.
|
|
1236
|
+
*
|
|
1237
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1238
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1239
|
+
*
|
|
1240
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1241
|
+
*/
|
|
1242
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions): DataError;
|
|
1243
|
+
/**
|
|
1244
|
+
* 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.
|
|
1245
|
+
*
|
|
1246
|
+
* @param errorFunction - The function expected to throw the error.
|
|
1247
|
+
* @param options - Extra options to apply.
|
|
1248
|
+
*
|
|
1249
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `DataError`.
|
|
1250
|
+
* @throws {Error} If no `DataError` was thrown by the `errorFunction`
|
|
1251
|
+
*
|
|
1252
|
+
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1253
|
+
*/
|
|
1254
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions): Promise<DataError>;
|
|
1255
|
+
}
|
|
1256
|
+
//#endregion
|
|
1142
1257
|
//#region src/root/deprecated/RecordKey.d.ts
|
|
1143
1258
|
/**
|
|
1144
1259
|
* Represents the native Record's possible key type.
|