@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.
- package/dist/index.cjs +31 -2
- package/dist/index.d.cts +53 -25
- package/dist/index.d.ts +53 -25
- package/dist/index.js +31 -2
- package/dist/internal/index.cjs +31 -2
- package/dist/internal/index.d.cts +36 -18
- package/dist/internal/index.d.ts +36 -18
- package/dist/internal/index.js +31 -2
- package/dist/node/index.cjs +31 -2
- package/dist/node/index.js +31 -2
- package/dist/v6/index.cjs +31 -4
- package/dist/v6/index.d.cts +40 -32
- package/dist/v6/index.d.ts +40 -32
- package/dist/v6/index.js +32 -4
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -133,6 +133,21 @@ var CodeError = class CodeError extends Error {
|
|
|
133
133
|
throw error;
|
|
134
134
|
}
|
|
135
135
|
/**
|
|
136
|
+
* Check a `CodeError` against its error code
|
|
137
|
+
*
|
|
138
|
+
* 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.
|
|
139
|
+
*
|
|
140
|
+
* @template ErrorCode The type of the error code
|
|
141
|
+
*
|
|
142
|
+
* @param input - The input to check.
|
|
143
|
+
* @param code - The expected code of the resulting error.
|
|
144
|
+
*
|
|
145
|
+
* @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`.
|
|
146
|
+
*/
|
|
147
|
+
static checkWithCode(input, code) {
|
|
148
|
+
return this.check(input) && input.code === code;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
136
151
|
* 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.
|
|
137
152
|
*
|
|
138
153
|
* @param errorFunction - The function expected to throw the error.
|
|
@@ -188,11 +203,10 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
188
203
|
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
189
204
|
* @param options - Extra options to pass to super Error constructor.
|
|
190
205
|
*/
|
|
191
|
-
constructor(data, code
|
|
206
|
+
constructor(data, code, message = "The data provided is invalid", options) {
|
|
192
207
|
super(code, message, options);
|
|
193
208
|
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
194
209
|
this.name = new.target.name;
|
|
195
|
-
this.code = code;
|
|
196
210
|
this.data = data;
|
|
197
211
|
Object.defineProperty(this, "message", { enumerable: true });
|
|
198
212
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -209,6 +223,21 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
209
223
|
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
210
224
|
}
|
|
211
225
|
/**
|
|
226
|
+
* Check a `DataError` against its error code
|
|
227
|
+
*
|
|
228
|
+
* 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.
|
|
229
|
+
*
|
|
230
|
+
* @template ErrorCode The type of the error code
|
|
231
|
+
*
|
|
232
|
+
* @param input - The input to check.
|
|
233
|
+
* @param code - The expected code of the resulting error.
|
|
234
|
+
*
|
|
235
|
+
* @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`.
|
|
236
|
+
*/
|
|
237
|
+
static checkWithCode(input, code) {
|
|
238
|
+
return this.check(input) && input.code === code;
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
212
241
|
* 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.
|
|
213
242
|
*
|
|
214
243
|
* @param errorFunction - The function expected to throw the error.
|
package/dist/index.d.cts
CHANGED
|
@@ -17,7 +17,7 @@ declare const VERSION_NUMBER_PATTERN: string;
|
|
|
17
17
|
declare const VERSION_NUMBER_REGEX: RegExp;
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/root/deprecated/DataError.d.ts
|
|
20
|
-
interface ExpectErrorOptions$
|
|
20
|
+
interface ExpectErrorOptions$1 {
|
|
21
21
|
expectedCode?: string;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
@@ -59,7 +59,7 @@ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<P
|
|
|
59
59
|
*
|
|
60
60
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
61
61
|
*/
|
|
62
|
-
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions$
|
|
62
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions$1): DataError;
|
|
63
63
|
/**
|
|
64
64
|
* 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.
|
|
65
65
|
*
|
|
@@ -71,7 +71,7 @@ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<P
|
|
|
71
71
|
*
|
|
72
72
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
73
73
|
*/
|
|
74
|
-
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$
|
|
74
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1): Promise<DataError>;
|
|
75
75
|
}
|
|
76
76
|
//#endregion
|
|
77
77
|
//#region src/root/deprecated/RecordKey.d.ts
|
|
@@ -674,9 +674,19 @@ type IgnoreCase<StringType extends string> = string extends StringType ? string
|
|
|
674
674
|
//#region src/root/types/IsTypeArgumentString.d.ts
|
|
675
675
|
type IsTypeArgumentString<Argument extends string> = Argument;
|
|
676
676
|
//#endregion
|
|
677
|
+
//#region src/root/types/NonNull.d.ts
|
|
678
|
+
/**
|
|
679
|
+
* Removes `null` from a type.
|
|
680
|
+
*
|
|
681
|
+
* @category Types
|
|
682
|
+
*
|
|
683
|
+
* @template InputType - The type to check.
|
|
684
|
+
*/
|
|
685
|
+
type NonNull<InputType> = InputType extends null ? never : InputType;
|
|
686
|
+
//#endregion
|
|
677
687
|
//#region src/root/types/NonUndefined.d.ts
|
|
678
688
|
/**
|
|
679
|
-
*
|
|
689
|
+
* Removes `undefined` from a type.
|
|
680
690
|
*
|
|
681
691
|
* @category Types
|
|
682
692
|
*
|
|
@@ -1112,7 +1122,7 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1112
1122
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1113
1123
|
//#endregion
|
|
1114
1124
|
//#region src/v6/CodeError.d.ts
|
|
1115
|
-
interface ExpectErrorOptions
|
|
1125
|
+
interface ExpectErrorOptions<ErrorCode extends string = string> {
|
|
1116
1126
|
expectedCode?: ErrorCode;
|
|
1117
1127
|
}
|
|
1118
1128
|
/**
|
|
@@ -1137,8 +1147,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
1137
1147
|
*
|
|
1138
1148
|
* @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`.
|
|
1139
1149
|
*/
|
|
1140
|
-
static check(input: unknown): input is CodeError<
|
|
1141
|
-
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions
|
|
1150
|
+
static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
|
|
1151
|
+
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
|
|
1152
|
+
/**
|
|
1153
|
+
* Check a `CodeError` against its error code
|
|
1154
|
+
*
|
|
1155
|
+
* 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.
|
|
1156
|
+
*
|
|
1157
|
+
* @template ErrorCode The type of the error code
|
|
1158
|
+
*
|
|
1159
|
+
* @param input - The input to check.
|
|
1160
|
+
* @param code - The expected code of the resulting error.
|
|
1161
|
+
*
|
|
1162
|
+
* @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`.
|
|
1163
|
+
*/
|
|
1164
|
+
static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
|
|
1142
1165
|
/**
|
|
1143
1166
|
* 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.
|
|
1144
1167
|
*
|
|
@@ -1150,7 +1173,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
1150
1173
|
*
|
|
1151
1174
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
1152
1175
|
*/
|
|
1153
|
-
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions
|
|
1176
|
+
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
|
|
1154
1177
|
/**
|
|
1155
1178
|
* 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.
|
|
1156
1179
|
*
|
|
@@ -1162,18 +1185,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
1162
1185
|
*
|
|
1163
1186
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
1164
1187
|
*/
|
|
1165
|
-
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions
|
|
1188
|
+
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
|
|
1166
1189
|
}
|
|
1167
1190
|
//#endregion
|
|
1168
1191
|
//#region src/v6/DataError.d.ts
|
|
1169
|
-
type DefaultDataErrorCode = "INVALID_DATA";
|
|
1170
|
-
interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
|
|
1171
|
-
expectedCode?: ErrorCode | DefaultDataErrorCode;
|
|
1172
|
-
}
|
|
1173
|
-
declare const DataErrorCode: {
|
|
1174
|
-
readonly INVALID_DATA: "INVALID_DATA";
|
|
1175
|
-
};
|
|
1176
|
-
type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
|
|
1177
1192
|
/**
|
|
1178
1193
|
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1179
1194
|
*
|
|
@@ -1181,7 +1196,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
|
|
|
1181
1196
|
*
|
|
1182
1197
|
* @template DataType - The type of the data that caused the error.
|
|
1183
1198
|
*/
|
|
1184
|
-
declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1199
|
+
declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
|
|
1185
1200
|
data: DataType;
|
|
1186
1201
|
/**
|
|
1187
1202
|
* @param data - The data that caused the error.
|
|
@@ -1189,7 +1204,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1189
1204
|
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1190
1205
|
* @param options - Extra options to pass to super Error constructor.
|
|
1191
1206
|
*/
|
|
1192
|
-
constructor(data: DataType, code
|
|
1207
|
+
constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
|
|
1193
1208
|
/**
|
|
1194
1209
|
* Checks whether the given input may have been caused by a DataError.
|
|
1195
1210
|
*
|
|
@@ -1197,7 +1212,20 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1197
1212
|
*
|
|
1198
1213
|
* @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`.
|
|
1199
1214
|
*/
|
|
1200
|
-
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1215
|
+
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError$1<DataType, ErrorCode>;
|
|
1216
|
+
/**
|
|
1217
|
+
* Check a `DataError` against its error code
|
|
1218
|
+
*
|
|
1219
|
+
* 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.
|
|
1220
|
+
*
|
|
1221
|
+
* @template ErrorCode The type of the error code
|
|
1222
|
+
*
|
|
1223
|
+
* @param input - The input to check.
|
|
1224
|
+
* @param code - The expected code of the resulting error.
|
|
1225
|
+
*
|
|
1226
|
+
* @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`.
|
|
1227
|
+
*/
|
|
1228
|
+
static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError$1<DataType, ErrorCode>;
|
|
1201
1229
|
/**
|
|
1202
1230
|
* 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.
|
|
1203
1231
|
*
|
|
@@ -1209,7 +1237,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1209
1237
|
*
|
|
1210
1238
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1211
1239
|
*/
|
|
1212
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1240
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
1213
1241
|
/**
|
|
1214
1242
|
* 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.
|
|
1215
1243
|
*
|
|
@@ -1221,7 +1249,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1221
1249
|
*
|
|
1222
1250
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1223
1251
|
*/
|
|
1224
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1252
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
1225
1253
|
}
|
|
1226
1254
|
//#endregion
|
|
1227
1255
|
//#region src/root/zod/parseZodSchema.d.ts
|
|
@@ -1278,9 +1306,9 @@ declare const az: {
|
|
|
1278
1306
|
versionNumber: () => ZodType<VersionNumber, unknown>;
|
|
1279
1307
|
fieldDate: () => ZodCoercedDate<string | null>;
|
|
1280
1308
|
with: <SchemaType extends ZodType>(schema: SchemaType) => {
|
|
1281
|
-
parse: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>,
|
|
1282
|
-
parseAsync: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>,
|
|
1309
|
+
parse: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>, string>>(input: unknown, error?: ErrorType | ((zodError: z.ZodError<unknown>) => void | ErrorType)) => ReturnType<typeof parseZodSchema<SchemaType, ErrorType>>;
|
|
1310
|
+
parseAsync: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>, string>>(input: unknown, error?: ErrorType | ((zodError: z.ZodError<unknown>) => void | ErrorType)) => ReturnType<typeof parseZodSchemaAsync<SchemaType, ErrorType>>;
|
|
1283
1311
|
};
|
|
1284
1312
|
};
|
|
1285
1313
|
//#endregion
|
|
1286
|
-
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, type StringListToArrayOptions, ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
|
1314
|
+
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, type NonNull, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, type StringListToArrayOptions, ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
package/dist/index.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ declare const VERSION_NUMBER_PATTERN: string;
|
|
|
17
17
|
declare const VERSION_NUMBER_REGEX: RegExp;
|
|
18
18
|
//#endregion
|
|
19
19
|
//#region src/root/deprecated/DataError.d.ts
|
|
20
|
-
interface ExpectErrorOptions$
|
|
20
|
+
interface ExpectErrorOptions$1 {
|
|
21
21
|
expectedCode?: string;
|
|
22
22
|
}
|
|
23
23
|
/**
|
|
@@ -59,7 +59,7 @@ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<P
|
|
|
59
59
|
*
|
|
60
60
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
61
61
|
*/
|
|
62
|
-
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions$
|
|
62
|
+
static expectError(errorFunction: () => unknown, options?: ExpectErrorOptions$1): DataError;
|
|
63
63
|
/**
|
|
64
64
|
* 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.
|
|
65
65
|
*
|
|
@@ -71,7 +71,7 @@ declare class DataError<DataType extends Record<PropertyKey, unknown> = Record<P
|
|
|
71
71
|
*
|
|
72
72
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
73
73
|
*/
|
|
74
|
-
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$
|
|
74
|
+
static expectErrorAsync(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions$1): Promise<DataError>;
|
|
75
75
|
}
|
|
76
76
|
//#endregion
|
|
77
77
|
//#region src/root/deprecated/RecordKey.d.ts
|
|
@@ -674,9 +674,19 @@ type IgnoreCase<StringType extends string> = string extends StringType ? string
|
|
|
674
674
|
//#region src/root/types/IsTypeArgumentString.d.ts
|
|
675
675
|
type IsTypeArgumentString<Argument extends string> = Argument;
|
|
676
676
|
//#endregion
|
|
677
|
+
//#region src/root/types/NonNull.d.ts
|
|
678
|
+
/**
|
|
679
|
+
* Removes `null` from a type.
|
|
680
|
+
*
|
|
681
|
+
* @category Types
|
|
682
|
+
*
|
|
683
|
+
* @template InputType - The type to check.
|
|
684
|
+
*/
|
|
685
|
+
type NonNull<InputType> = InputType extends null ? never : InputType;
|
|
686
|
+
//#endregion
|
|
677
687
|
//#region src/root/types/NonUndefined.d.ts
|
|
678
688
|
/**
|
|
679
|
-
*
|
|
689
|
+
* Removes `undefined` from a type.
|
|
680
690
|
*
|
|
681
691
|
* @category Types
|
|
682
692
|
*
|
|
@@ -1112,7 +1122,7 @@ declare function normaliseIndents(strings: TemplateStringsArray, ...interpolatio
|
|
|
1112
1122
|
declare const normalizeIndents: typeof normaliseIndents;
|
|
1113
1123
|
//#endregion
|
|
1114
1124
|
//#region src/v6/CodeError.d.ts
|
|
1115
|
-
interface ExpectErrorOptions
|
|
1125
|
+
interface ExpectErrorOptions<ErrorCode extends string = string> {
|
|
1116
1126
|
expectedCode?: ErrorCode;
|
|
1117
1127
|
}
|
|
1118
1128
|
/**
|
|
@@ -1137,8 +1147,21 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
1137
1147
|
*
|
|
1138
1148
|
* @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`.
|
|
1139
1149
|
*/
|
|
1140
|
-
static check(input: unknown): input is CodeError<
|
|
1141
|
-
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions
|
|
1150
|
+
static check<ErrorCode extends string = string>(input: unknown): input is CodeError<ErrorCode>;
|
|
1151
|
+
protected static checkCaughtError<ErrorCode extends string = string>(this: typeof CodeError, error: unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
|
|
1152
|
+
/**
|
|
1153
|
+
* Check a `CodeError` against its error code
|
|
1154
|
+
*
|
|
1155
|
+
* 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.
|
|
1156
|
+
*
|
|
1157
|
+
* @template ErrorCode The type of the error code
|
|
1158
|
+
*
|
|
1159
|
+
* @param input - The input to check.
|
|
1160
|
+
* @param code - The expected code of the resulting error.
|
|
1161
|
+
*
|
|
1162
|
+
* @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`.
|
|
1163
|
+
*/
|
|
1164
|
+
static checkWithCode<ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is CodeError<ErrorCode>;
|
|
1142
1165
|
/**
|
|
1143
1166
|
* 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.
|
|
1144
1167
|
*
|
|
@@ -1150,7 +1173,7 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
1150
1173
|
*
|
|
1151
1174
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
1152
1175
|
*/
|
|
1153
|
-
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions
|
|
1176
|
+
static expectError<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): CodeError<ErrorCode>;
|
|
1154
1177
|
/**
|
|
1155
1178
|
* 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.
|
|
1156
1179
|
*
|
|
@@ -1162,18 +1185,10 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
1162
1185
|
*
|
|
1163
1186
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
1164
1187
|
*/
|
|
1165
|
-
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions
|
|
1188
|
+
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
|
|
1166
1189
|
}
|
|
1167
1190
|
//#endregion
|
|
1168
1191
|
//#region src/v6/DataError.d.ts
|
|
1169
|
-
type DefaultDataErrorCode = "INVALID_DATA";
|
|
1170
|
-
interface ExpectErrorOptions<ErrorCode extends string = DefaultDataErrorCode> {
|
|
1171
|
-
expectedCode?: ErrorCode | DefaultDataErrorCode;
|
|
1172
|
-
}
|
|
1173
|
-
declare const DataErrorCode: {
|
|
1174
|
-
readonly INVALID_DATA: "INVALID_DATA";
|
|
1175
|
-
};
|
|
1176
|
-
type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
|
|
1177
1192
|
/**
|
|
1178
1193
|
* Represents errors you may get that may've been caused by a specific piece of data.
|
|
1179
1194
|
*
|
|
@@ -1181,7 +1196,7 @@ type DataErrorCode = CreateEnumType<typeof DataErrorCode>;
|
|
|
1181
1196
|
*
|
|
1182
1197
|
* @template DataType - The type of the data that caused the error.
|
|
1183
1198
|
*/
|
|
1184
|
-
declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1199
|
+
declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
|
|
1185
1200
|
data: DataType;
|
|
1186
1201
|
/**
|
|
1187
1202
|
* @param data - The data that caused the error.
|
|
@@ -1189,7 +1204,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1189
1204
|
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
1190
1205
|
* @param options - Extra options to pass to super Error constructor.
|
|
1191
1206
|
*/
|
|
1192
|
-
constructor(data: DataType, code
|
|
1207
|
+
constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
|
|
1193
1208
|
/**
|
|
1194
1209
|
* Checks whether the given input may have been caused by a DataError.
|
|
1195
1210
|
*
|
|
@@ -1197,7 +1212,20 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1197
1212
|
*
|
|
1198
1213
|
* @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`.
|
|
1199
1214
|
*/
|
|
1200
|
-
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1215
|
+
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is DataError$1<DataType, ErrorCode>;
|
|
1216
|
+
/**
|
|
1217
|
+
* Check a `DataError` against its error code
|
|
1218
|
+
*
|
|
1219
|
+
* 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.
|
|
1220
|
+
*
|
|
1221
|
+
* @template ErrorCode The type of the error code
|
|
1222
|
+
*
|
|
1223
|
+
* @param input - The input to check.
|
|
1224
|
+
* @param code - The expected code of the resulting error.
|
|
1225
|
+
*
|
|
1226
|
+
* @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`.
|
|
1227
|
+
*/
|
|
1228
|
+
static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is DataError$1<DataType, ErrorCode>;
|
|
1201
1229
|
/**
|
|
1202
1230
|
* 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.
|
|
1203
1231
|
*
|
|
@@ -1209,7 +1237,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1209
1237
|
*
|
|
1210
1238
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1211
1239
|
*/
|
|
1212
|
-
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1240
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): DataError$1<DataType, ErrorCode>;
|
|
1213
1241
|
/**
|
|
1214
1242
|
* 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.
|
|
1215
1243
|
*
|
|
@@ -1221,7 +1249,7 @@ declare class DataError$1<DataType extends object = Record<PropertyKey, unknown>
|
|
|
1221
1249
|
*
|
|
1222
1250
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
1223
1251
|
*/
|
|
1224
|
-
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string =
|
|
1252
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError$1<DataType, ErrorCode>>;
|
|
1225
1253
|
}
|
|
1226
1254
|
//#endregion
|
|
1227
1255
|
//#region src/root/zod/parseZodSchema.d.ts
|
|
@@ -1278,9 +1306,9 @@ declare const az: {
|
|
|
1278
1306
|
versionNumber: () => ZodType<VersionNumber, unknown>;
|
|
1279
1307
|
fieldDate: () => ZodCoercedDate<string | null>;
|
|
1280
1308
|
with: <SchemaType extends ZodType>(schema: SchemaType) => {
|
|
1281
|
-
parse: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>,
|
|
1282
|
-
parseAsync: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>,
|
|
1309
|
+
parse: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>, string>>(input: unknown, error?: ErrorType | ((zodError: z$1.ZodError<unknown>) => void | ErrorType)) => ReturnType<typeof parseZodSchema<SchemaType, ErrorType>>;
|
|
1310
|
+
parseAsync: <ErrorType extends Error = DataError$1<Record<PropertyKey, unknown>, string>>(input: unknown, error?: ErrorType | ((zodError: z$1.ZodError<unknown>) => void | ErrorType)) => ReturnType<typeof parseZodSchemaAsync<SchemaType, ErrorType>>;
|
|
1283
1311
|
};
|
|
1284
1312
|
};
|
|
1285
1313
|
//#endregion
|
|
1286
|
-
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, type StringListToArrayOptions, ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
|
1314
|
+
export { APIError, type ArrayElement, type CallReturnType, CamelToKebabOptions, type CreateEnumType, type CreateFormDataOptions, type CreateFormDataOptionsNullableResolution, type CreateFormDataOptionsUndefinedOrNullResolution, DataError, type DisallowUndefined, Env, FILE_PATH_PATTERN, FILE_PATH_REGEX, type FormDataArrayResolutionStrategy, type FormDataNullableResolutionStrategy, type HTTPErrorCode, type IgnoreCase, type IsTypeArgumentString, KebabToCamelOptions, type NonNull, type NonUndefined, NormaliseIndentsFunction, NormaliseIndentsOptions, NormalizeIndentsFunction, NormalizeIndentsOptions, type NullableOnCondition, ONE_DAY_IN_MILLISECONDS, type OptionalOnCondition, ParallelTuple, RecordKey, RemoveUndefined, type StringListToArrayOptions, ToTitleCaseOptions, UUID_PATTERN, UUID_REGEX, VERSION_NUMBER_PATTERN, VERSION_NUMBER_REGEX, VersionNumber, type FormatOptionsBase as VersionNumberToStringOptions, VersionType, addDaysToDate, appendSemicolon, az, calculateMonthlyDifference, camelToKebab, convertFileToBase64, createFormData, createTemplateStringsArray, deepCopy, deepFreeze, escapeRegexPattern, fillArray, formatDateAndTime, getRandomNumber, getRecordKeys, getStringsAndInterpolations, httpErrorCodeLookup, interpolate, interpolateObjects, isAnniversary, isLeapYear, isMonthlyMultiple, isOrdered, isSameDate, isTemplateStringsArray, kebabToCamel, normaliseIndents, normalizeIndents, omitProperties, paralleliseArrays, parseBoolean, parseEnv, parseFormData, parseIntStrict, parseUUID, parseVersionType, parseZodSchema, parseZodSchemaAsync, randomiseArray, range, removeDuplicates, removeUndefinedFromObject, sayHello, stringListToArray, stringifyDotenv, toTitleCase, truncate, wait, zodVersionNumber };
|
package/dist/index.js
CHANGED
|
@@ -109,6 +109,21 @@ var CodeError = class CodeError extends Error {
|
|
|
109
109
|
throw error;
|
|
110
110
|
}
|
|
111
111
|
/**
|
|
112
|
+
* Check a `CodeError` against its error code
|
|
113
|
+
*
|
|
114
|
+
* 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.
|
|
115
|
+
*
|
|
116
|
+
* @template ErrorCode The type of the error code
|
|
117
|
+
*
|
|
118
|
+
* @param input - The input to check.
|
|
119
|
+
* @param code - The expected code of the resulting error.
|
|
120
|
+
*
|
|
121
|
+
* @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`.
|
|
122
|
+
*/
|
|
123
|
+
static checkWithCode(input, code) {
|
|
124
|
+
return this.check(input) && input.code === code;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
112
127
|
* 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.
|
|
113
128
|
*
|
|
114
129
|
* @param errorFunction - The function expected to throw the error.
|
|
@@ -164,11 +179,10 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
164
179
|
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
165
180
|
* @param options - Extra options to pass to super Error constructor.
|
|
166
181
|
*/
|
|
167
|
-
constructor(data, code
|
|
182
|
+
constructor(data, code, message = "The data provided is invalid", options) {
|
|
168
183
|
super(code, message, options);
|
|
169
184
|
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
170
185
|
this.name = new.target.name;
|
|
171
|
-
this.code = code;
|
|
172
186
|
this.data = data;
|
|
173
187
|
Object.defineProperty(this, "message", { enumerable: true });
|
|
174
188
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -185,6 +199,21 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
185
199
|
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
186
200
|
}
|
|
187
201
|
/**
|
|
202
|
+
* Check a `DataError` against its error code
|
|
203
|
+
*
|
|
204
|
+
* 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.
|
|
205
|
+
*
|
|
206
|
+
* @template ErrorCode The type of the error code
|
|
207
|
+
*
|
|
208
|
+
* @param input - The input to check.
|
|
209
|
+
* @param code - The expected code of the resulting error.
|
|
210
|
+
*
|
|
211
|
+
* @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`.
|
|
212
|
+
*/
|
|
213
|
+
static checkWithCode(input, code) {
|
|
214
|
+
return this.check(input) && input.code === code;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
188
217
|
* 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.
|
|
189
218
|
*
|
|
190
219
|
* @param errorFunction - The function expected to throw the error.
|
package/dist/internal/index.cjs
CHANGED
|
@@ -132,6 +132,21 @@ var CodeError = class CodeError extends Error {
|
|
|
132
132
|
throw error;
|
|
133
133
|
}
|
|
134
134
|
/**
|
|
135
|
+
* Check a `CodeError` against its error code
|
|
136
|
+
*
|
|
137
|
+
* 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.
|
|
138
|
+
*
|
|
139
|
+
* @template ErrorCode The type of the error code
|
|
140
|
+
*
|
|
141
|
+
* @param input - The input to check.
|
|
142
|
+
* @param code - The expected code of the resulting error.
|
|
143
|
+
*
|
|
144
|
+
* @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`.
|
|
145
|
+
*/
|
|
146
|
+
static checkWithCode(input, code) {
|
|
147
|
+
return this.check(input) && input.code === code;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
135
150
|
* 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.
|
|
136
151
|
*
|
|
137
152
|
* @param errorFunction - The function expected to throw the error.
|
|
@@ -187,11 +202,10 @@ var DataError = class DataError extends CodeError {
|
|
|
187
202
|
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
188
203
|
* @param options - Extra options to pass to super Error constructor.
|
|
189
204
|
*/
|
|
190
|
-
constructor(data, code
|
|
205
|
+
constructor(data, code, message = "The data provided is invalid", options) {
|
|
191
206
|
super(code, message, options);
|
|
192
207
|
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
193
208
|
this.name = new.target.name;
|
|
194
|
-
this.code = code;
|
|
195
209
|
this.data = data;
|
|
196
210
|
Object.defineProperty(this, "message", { enumerable: true });
|
|
197
211
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
@@ -208,6 +222,21 @@ var DataError = class DataError extends CodeError {
|
|
|
208
222
|
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
209
223
|
}
|
|
210
224
|
/**
|
|
225
|
+
* Check a `DataError` against its error code
|
|
226
|
+
*
|
|
227
|
+
* 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.
|
|
228
|
+
*
|
|
229
|
+
* @template ErrorCode The type of the error code
|
|
230
|
+
*
|
|
231
|
+
* @param input - The input to check.
|
|
232
|
+
* @param code - The expected code of the resulting error.
|
|
233
|
+
*
|
|
234
|
+
* @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`.
|
|
235
|
+
*/
|
|
236
|
+
static checkWithCode(input, code) {
|
|
237
|
+
return this.check(input) && input.code === code;
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
211
240
|
* 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.
|
|
212
241
|
*
|
|
213
242
|
* @param errorFunction - The function expected to throw the error.
|