@alextheman/utility 5.13.1 → 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 +302 -237
- package/dist/index.d.cts +207 -160
- package/dist/index.d.ts +206 -159
- package/dist/index.js +302 -238
- package/dist/internal/index.cjs +342 -43
- package/dist/internal/index.d.cts +36 -18
- package/dist/internal/index.d.ts +36 -18
- package/dist/internal/index.js +342 -43
- 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.
|
|
@@ -893,7 +922,217 @@ function parseBoolean(inputString) {
|
|
|
893
922
|
return normalisedString === "true";
|
|
894
923
|
}
|
|
895
924
|
//#endregion
|
|
896
|
-
//#region src/root/
|
|
925
|
+
//#region src/root/types/APIError.ts
|
|
926
|
+
const httpErrorCodeLookup = {
|
|
927
|
+
400: "BAD_REQUEST",
|
|
928
|
+
401: "UNAUTHORISED",
|
|
929
|
+
403: "FORBIDDEN",
|
|
930
|
+
404: "NOT_FOUND",
|
|
931
|
+
418: "I_AM_A_TEAPOT",
|
|
932
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
933
|
+
};
|
|
934
|
+
/**
|
|
935
|
+
* Represents common errors you may get from a HTTP API request.
|
|
936
|
+
*
|
|
937
|
+
* @category Types
|
|
938
|
+
*/
|
|
939
|
+
var APIError = class APIError extends Error {
|
|
940
|
+
status;
|
|
941
|
+
/**
|
|
942
|
+
* @param status - A HTTP status code. Can be any number, but numbers between 400 and 600 are encouraged to fit with HTTP status code conventions.
|
|
943
|
+
* @param message - An error message to display alongside the status code.
|
|
944
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
945
|
+
*/
|
|
946
|
+
constructor(status = 500, message, options) {
|
|
947
|
+
super(message, options);
|
|
948
|
+
this.status = status;
|
|
949
|
+
if (message) this.message = message;
|
|
950
|
+
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
951
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
952
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
953
|
+
}
|
|
954
|
+
/**
|
|
955
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
956
|
+
*
|
|
957
|
+
* @param input - The input to check.
|
|
958
|
+
*
|
|
959
|
+
* @returns `true` if the input is an APIError, and `false` otherwise. The type of the input will also be narrowed down to APIError if `true`.
|
|
960
|
+
*/
|
|
961
|
+
static check(input) {
|
|
962
|
+
if (input instanceof APIError) return true;
|
|
963
|
+
const data = input;
|
|
964
|
+
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
965
|
+
}
|
|
966
|
+
};
|
|
967
|
+
//#endregion
|
|
968
|
+
//#region src/root/types/VersionNumber.ts
|
|
969
|
+
/**
|
|
970
|
+
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
971
|
+
*
|
|
972
|
+
* @category Types
|
|
973
|
+
*/
|
|
974
|
+
var VersionNumber = class VersionNumber {
|
|
975
|
+
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
976
|
+
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
977
|
+
major = 0;
|
|
978
|
+
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
979
|
+
minor = 0;
|
|
980
|
+
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
981
|
+
patch = 0;
|
|
982
|
+
/**
|
|
983
|
+
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
984
|
+
*/
|
|
985
|
+
constructor(input) {
|
|
986
|
+
if (input instanceof VersionNumber) {
|
|
987
|
+
this.major = input.major;
|
|
988
|
+
this.minor = input.minor;
|
|
989
|
+
this.patch = input.patch;
|
|
990
|
+
} else if (typeof input === "string") {
|
|
991
|
+
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
992
|
+
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
993
|
+
return parseIntStrict(number);
|
|
994
|
+
});
|
|
995
|
+
this.major = major;
|
|
996
|
+
this.minor = minor;
|
|
997
|
+
this.patch = patch;
|
|
998
|
+
} else if (Array.isArray(input)) {
|
|
999
|
+
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1000
|
+
const [major, minor, patch] = input.map((number) => {
|
|
1001
|
+
const parsedInteger = parseIntStrict(number?.toString());
|
|
1002
|
+
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1003
|
+
return parsedInteger;
|
|
1004
|
+
});
|
|
1005
|
+
this.major = major;
|
|
1006
|
+
this.minor = minor;
|
|
1007
|
+
this.patch = patch;
|
|
1008
|
+
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1009
|
+
The provided input can not be parsed into a valid version number.
|
|
1010
|
+
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
1011
|
+
`);
|
|
1012
|
+
}
|
|
1013
|
+
/**
|
|
1014
|
+
* Gets the current version type of the current instance of `VersionNumber`.
|
|
1015
|
+
*
|
|
1016
|
+
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
1017
|
+
*/
|
|
1018
|
+
get type() {
|
|
1019
|
+
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
1020
|
+
if (this.patch === 0) return VersionType.MINOR;
|
|
1021
|
+
return VersionType.PATCH;
|
|
1022
|
+
}
|
|
1023
|
+
static formatString(input, options) {
|
|
1024
|
+
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
1025
|
+
return input.startsWith("v") ? input : `v${input}`;
|
|
1026
|
+
}
|
|
1027
|
+
/**
|
|
1028
|
+
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
1029
|
+
*
|
|
1030
|
+
* @param firstVersion - The first version number to compare.
|
|
1031
|
+
* @param secondVersion - The second version number to compare.
|
|
1032
|
+
*
|
|
1033
|
+
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
1034
|
+
*/
|
|
1035
|
+
static isEqual(firstVersion, secondVersion) {
|
|
1036
|
+
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
1037
|
+
}
|
|
1038
|
+
/**
|
|
1039
|
+
* Get a formatted string representation of the current version number
|
|
1040
|
+
*
|
|
1041
|
+
* @param options - Options to apply to the string formatting.
|
|
1042
|
+
*
|
|
1043
|
+
* @returns A formatted string representation of the current version number with the options applied.
|
|
1044
|
+
*/
|
|
1045
|
+
format(options) {
|
|
1046
|
+
let baseOutput = `${this.major}`;
|
|
1047
|
+
if (!options?.omitMinor) {
|
|
1048
|
+
baseOutput += `.${this.minor}`;
|
|
1049
|
+
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
1050
|
+
}
|
|
1051
|
+
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
1052
|
+
}
|
|
1053
|
+
/**
|
|
1054
|
+
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
1055
|
+
*
|
|
1056
|
+
* @param incrementType - The type of increment. Can be one of the following:
|
|
1057
|
+
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
1058
|
+
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
1059
|
+
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
1060
|
+
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
1061
|
+
*
|
|
1062
|
+
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
1063
|
+
*/
|
|
1064
|
+
increment(incrementType, incrementAmount = 1) {
|
|
1065
|
+
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
1066
|
+
const calculatedRawVersion = {
|
|
1067
|
+
major: [
|
|
1068
|
+
this.major + incrementBy,
|
|
1069
|
+
0,
|
|
1070
|
+
0
|
|
1071
|
+
],
|
|
1072
|
+
minor: [
|
|
1073
|
+
this.major,
|
|
1074
|
+
this.minor + incrementBy,
|
|
1075
|
+
0
|
|
1076
|
+
],
|
|
1077
|
+
patch: [
|
|
1078
|
+
this.major,
|
|
1079
|
+
this.minor,
|
|
1080
|
+
this.patch + incrementBy
|
|
1081
|
+
]
|
|
1082
|
+
}[incrementType];
|
|
1083
|
+
try {
|
|
1084
|
+
return new VersionNumber(calculatedRawVersion);
|
|
1085
|
+
} catch (error) {
|
|
1086
|
+
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1087
|
+
currentVersion: this.toString(),
|
|
1088
|
+
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1089
|
+
incrementAmount
|
|
1090
|
+
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
1091
|
+
else throw error;
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
1096
|
+
*
|
|
1097
|
+
* @param hint - Not used as of now, but generally used to help with numeric coercion, I think (which we most likely do not need for version numbers).
|
|
1098
|
+
*
|
|
1099
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1100
|
+
*/
|
|
1101
|
+
[Symbol.toPrimitive](hint) {
|
|
1102
|
+
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1103
|
+
return this.toString();
|
|
1104
|
+
}
|
|
1105
|
+
/**
|
|
1106
|
+
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
1107
|
+
*
|
|
1108
|
+
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1109
|
+
*/
|
|
1110
|
+
toJSON() {
|
|
1111
|
+
return this.toString();
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* Get a string representation of the current version number.
|
|
1115
|
+
*
|
|
1116
|
+
* @returns A stringified representation of the current version number with the prefix.
|
|
1117
|
+
*/
|
|
1118
|
+
toString() {
|
|
1119
|
+
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
1120
|
+
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
1121
|
+
}
|
|
1122
|
+
};
|
|
1123
|
+
const zodVersionNumber = zod.default.union([
|
|
1124
|
+
zod.default.string(),
|
|
1125
|
+
zod.default.tuple([
|
|
1126
|
+
zod.default.number(),
|
|
1127
|
+
zod.default.number(),
|
|
1128
|
+
zod.default.number()
|
|
1129
|
+
]),
|
|
1130
|
+
zod.default.instanceof(VersionNumber)
|
|
1131
|
+
]).transform((rawVersionNumber) => {
|
|
1132
|
+
return new VersionNumber(rawVersionNumber);
|
|
1133
|
+
});
|
|
1134
|
+
//#endregion
|
|
1135
|
+
//#region src/root/zod/_parseZodSchema.ts
|
|
897
1136
|
function _parseZodSchema(parsedResult, input, onError) {
|
|
898
1137
|
if (!parsedResult.success) {
|
|
899
1138
|
if (onError) {
|
|
@@ -917,7 +1156,7 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
917
1156
|
return parsedResult.data;
|
|
918
1157
|
}
|
|
919
1158
|
//#endregion
|
|
920
|
-
//#region src/root/
|
|
1159
|
+
//#region src/root/zod/parseZodSchema.ts
|
|
921
1160
|
/**
|
|
922
1161
|
* An alternative function to zodSchema.parse() that can be used to strictly parse Zod schemas.
|
|
923
1162
|
*
|
|
@@ -925,6 +1164,8 @@ function _parseZodSchema(parsedResult, input, onError) {
|
|
|
925
1164
|
*
|
|
926
1165
|
* @category Parsers
|
|
927
1166
|
*
|
|
1167
|
+
* @deprecated Please use `az.with(schema).parse(input)` instead.
|
|
1168
|
+
*
|
|
928
1169
|
* @template SchemaType - The Zod schema type.
|
|
929
1170
|
* @template ErrorType - The type of error to throw on invalid data.
|
|
930
1171
|
*
|
|
@@ -940,6 +1181,60 @@ function parseZodSchema(schema, input, onError) {
|
|
|
940
1181
|
return _parseZodSchema(schema.safeParse(input), input, onError);
|
|
941
1182
|
}
|
|
942
1183
|
//#endregion
|
|
1184
|
+
//#region src/root/zod/parseZodSchemaAsync.ts
|
|
1185
|
+
/**
|
|
1186
|
+
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
1187
|
+
*
|
|
1188
|
+
* @category Parsers
|
|
1189
|
+
*
|
|
1190
|
+
* @deprecated Please use `az.with(schema).parseAsync(input)` instead.
|
|
1191
|
+
*
|
|
1192
|
+
* @template SchemaType - The Zod schema type.
|
|
1193
|
+
* @template ErrorType - The type of error to throw on invalid data.
|
|
1194
|
+
*
|
|
1195
|
+
* @param schema - The Zod schema to use in parsing.
|
|
1196
|
+
* @param input - The data to parse.
|
|
1197
|
+
* @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.
|
|
1198
|
+
*
|
|
1199
|
+
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
1200
|
+
*
|
|
1201
|
+
* @returns The parsed data from the Zod schema.
|
|
1202
|
+
*/
|
|
1203
|
+
async function parseZodSchemaAsync(schema, input, onError) {
|
|
1204
|
+
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
1205
|
+
}
|
|
1206
|
+
//#endregion
|
|
1207
|
+
//#region src/root/zod/zodFieldWrapper.ts
|
|
1208
|
+
function zodFieldWrapper(schema) {
|
|
1209
|
+
return zod.default.string().trim().transform((value) => {
|
|
1210
|
+
return value === "" ? null : value;
|
|
1211
|
+
}).pipe(schema);
|
|
1212
|
+
}
|
|
1213
|
+
//#endregion
|
|
1214
|
+
//#region src/root/zod/az.ts
|
|
1215
|
+
const az = {
|
|
1216
|
+
field: zodFieldWrapper,
|
|
1217
|
+
fieldNumber: () => {
|
|
1218
|
+
return zod.default.coerce.number();
|
|
1219
|
+
},
|
|
1220
|
+
versionNumber: () => {
|
|
1221
|
+
return zodVersionNumber;
|
|
1222
|
+
},
|
|
1223
|
+
fieldDate: () => {
|
|
1224
|
+
return zod.default.coerce.date();
|
|
1225
|
+
},
|
|
1226
|
+
with: (schema) => {
|
|
1227
|
+
return {
|
|
1228
|
+
parse: (input, error) => {
|
|
1229
|
+
return parseZodSchema(schema, input, error);
|
|
1230
|
+
},
|
|
1231
|
+
parseAsync: async (input, error) => {
|
|
1232
|
+
return await parseZodSchemaAsync(schema, input, error);
|
|
1233
|
+
}
|
|
1234
|
+
};
|
|
1235
|
+
}
|
|
1236
|
+
};
|
|
1237
|
+
//#endregion
|
|
943
1238
|
//#region src/root/functions/parsers/parseEnv.ts
|
|
944
1239
|
/**
|
|
945
1240
|
* Represents the three common development environments.
|
|
@@ -963,7 +1258,7 @@ const Env = {
|
|
|
963
1258
|
* @returns The specified environment if allowed.
|
|
964
1259
|
*/
|
|
965
1260
|
function parseEnv(input) {
|
|
966
|
-
return
|
|
1261
|
+
return az.with(zod.z.enum(Env)).parse(input, new DataError$1({ input }, "INVALID_ENV", "The provided environment type must be one of `test | development | production`"));
|
|
967
1262
|
}
|
|
968
1263
|
//#endregion
|
|
969
1264
|
//#region src/root/functions/parsers/parseFormData.ts
|
|
@@ -1029,28 +1324,7 @@ const VersionType = {
|
|
|
1029
1324
|
* @returns The given version type if allowed.
|
|
1030
1325
|
*/
|
|
1031
1326
|
function parseVersionType(input) {
|
|
1032
|
-
return
|
|
1033
|
-
}
|
|
1034
|
-
//#endregion
|
|
1035
|
-
//#region src/root/functions/parsers/zod/parseZodSchemaAsync.ts
|
|
1036
|
-
/**
|
|
1037
|
-
* An alternative function to zodSchema.parseAsync() that can be used to strictly parse asynchronous Zod schemas.
|
|
1038
|
-
*
|
|
1039
|
-
* @category Parsers
|
|
1040
|
-
*
|
|
1041
|
-
* @template SchemaType - The Zod schema type.
|
|
1042
|
-
* @template ErrorType - The type of error to throw on invalid data.
|
|
1043
|
-
*
|
|
1044
|
-
* @param schema - The Zod schema to use in parsing.
|
|
1045
|
-
* @param input - The data to parse.
|
|
1046
|
-
* @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.
|
|
1047
|
-
*
|
|
1048
|
-
* @throws {DataError} If the given data cannot be parsed according to the schema.
|
|
1049
|
-
*
|
|
1050
|
-
* @returns The parsed data from the Zod schema.
|
|
1051
|
-
*/
|
|
1052
|
-
async function parseZodSchemaAsync(schema, input, onError) {
|
|
1053
|
-
return _parseZodSchema(await schema.safeParseAsync(input), input, onError);
|
|
1327
|
+
return az.with(zod.default.enum(VersionType)).parse(input, new DataError$1({ input }, "INVALID_VERSION_TYPE", "The provided version type must be one of `major | minor | patch`"));
|
|
1054
1328
|
}
|
|
1055
1329
|
//#endregion
|
|
1056
1330
|
//#region src/root/functions/recursive/deepCopy.ts
|
|
@@ -1556,216 +1830,6 @@ var DataError = class DataError extends Error {
|
|
|
1556
1830
|
}
|
|
1557
1831
|
};
|
|
1558
1832
|
//#endregion
|
|
1559
|
-
//#region src/root/types/APIError.ts
|
|
1560
|
-
const httpErrorCodeLookup = {
|
|
1561
|
-
400: "BAD_REQUEST",
|
|
1562
|
-
401: "UNAUTHORISED",
|
|
1563
|
-
403: "FORBIDDEN",
|
|
1564
|
-
404: "NOT_FOUND",
|
|
1565
|
-
418: "I_AM_A_TEAPOT",
|
|
1566
|
-
500: "INTERNAL_SERVER_ERROR"
|
|
1567
|
-
};
|
|
1568
|
-
/**
|
|
1569
|
-
* Represents common errors you may get from a HTTP API request.
|
|
1570
|
-
*
|
|
1571
|
-
* @category Types
|
|
1572
|
-
*/
|
|
1573
|
-
var APIError = class APIError extends Error {
|
|
1574
|
-
status;
|
|
1575
|
-
/**
|
|
1576
|
-
* @param status - A HTTP status code. Can be any number, but numbers between 400 and 600 are encouraged to fit with HTTP status code conventions.
|
|
1577
|
-
* @param message - An error message to display alongside the status code.
|
|
1578
|
-
* @param options - Extra options to be passed to super Error constructor.
|
|
1579
|
-
*/
|
|
1580
|
-
constructor(status = 500, message, options) {
|
|
1581
|
-
super(message, options);
|
|
1582
|
-
this.status = status;
|
|
1583
|
-
if (message) this.message = message;
|
|
1584
|
-
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
1585
|
-
Object.defineProperty(this, "message", { enumerable: true });
|
|
1586
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
1587
|
-
}
|
|
1588
|
-
/**
|
|
1589
|
-
* Checks whether the given input may have been caused by an APIError.
|
|
1590
|
-
*
|
|
1591
|
-
* @param input - The input to check.
|
|
1592
|
-
*
|
|
1593
|
-
* @returns `true` if the input is an APIError, and `false` otherwise. The type of the input will also be narrowed down to APIError if `true`.
|
|
1594
|
-
*/
|
|
1595
|
-
static check(input) {
|
|
1596
|
-
if (input instanceof APIError) return true;
|
|
1597
|
-
const data = input;
|
|
1598
|
-
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
1599
|
-
}
|
|
1600
|
-
};
|
|
1601
|
-
//#endregion
|
|
1602
|
-
//#region src/root/types/VersionNumber.ts
|
|
1603
|
-
/**
|
|
1604
|
-
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
1605
|
-
*
|
|
1606
|
-
* @category Types
|
|
1607
|
-
*/
|
|
1608
|
-
var VersionNumber = class VersionNumber {
|
|
1609
|
-
static NON_NEGATIVE_TUPLE_ERROR = "Input array must be a tuple of three non-negative integers.";
|
|
1610
|
-
/** The major number. Increments when a feature is removed or changed in a way that is not backwards-compatible with the previous release. */
|
|
1611
|
-
major = 0;
|
|
1612
|
-
/** The minor number. Increments when a new feature is added/deprecated and is expected to be backwards-compatible with the previous release. */
|
|
1613
|
-
minor = 0;
|
|
1614
|
-
/** The patch number. Increments when the next release is fixing a bug or doing a small refactor that should not be noticeable in practice. */
|
|
1615
|
-
patch = 0;
|
|
1616
|
-
/**
|
|
1617
|
-
* @param input - The input to create a new instance of `VersionNumber` from.
|
|
1618
|
-
*/
|
|
1619
|
-
constructor(input) {
|
|
1620
|
-
if (input instanceof VersionNumber) {
|
|
1621
|
-
this.major = input.major;
|
|
1622
|
-
this.minor = input.minor;
|
|
1623
|
-
this.patch = input.patch;
|
|
1624
|
-
} else if (typeof input === "string") {
|
|
1625
|
-
if (!VERSION_NUMBER_REGEX.test(input)) throw new DataError$1({ input }, "INVALID_VERSION", `"${input}" is not a valid version number. Version numbers must be of the format "X.Y.Z" or "vX.Y.Z", where X, Y, and Z are non-negative integers.`);
|
|
1626
|
-
const [major, minor, patch] = VersionNumber.formatString(input, { omitPrefix: true }).split(".").map((number) => {
|
|
1627
|
-
return parseIntStrict(number);
|
|
1628
|
-
});
|
|
1629
|
-
this.major = major;
|
|
1630
|
-
this.minor = minor;
|
|
1631
|
-
this.patch = patch;
|
|
1632
|
-
} else if (Array.isArray(input)) {
|
|
1633
|
-
if (input.length !== 3) throw new DataError$1({ input }, "INVALID_LENGTH", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1634
|
-
const [major, minor, patch] = input.map((number) => {
|
|
1635
|
-
const parsedInteger = parseIntStrict(number?.toString());
|
|
1636
|
-
if (parsedInteger < 0) throw new DataError$1({ input }, "NEGATIVE_INPUTS", VersionNumber.NON_NEGATIVE_TUPLE_ERROR);
|
|
1637
|
-
return parsedInteger;
|
|
1638
|
-
});
|
|
1639
|
-
this.major = major;
|
|
1640
|
-
this.minor = minor;
|
|
1641
|
-
this.patch = patch;
|
|
1642
|
-
} else throw new DataError$1({ input }, "INVALID_INPUT", normaliseIndents`
|
|
1643
|
-
The provided input can not be parsed into a valid version number.
|
|
1644
|
-
Expected either a string of format X.Y.Z or vX.Y.Z, a tuple of three numbers, or another \`VersionNumber\` instance.
|
|
1645
|
-
`);
|
|
1646
|
-
}
|
|
1647
|
-
/**
|
|
1648
|
-
* Gets the current version type of the current instance of `VersionNumber`.
|
|
1649
|
-
*
|
|
1650
|
-
* @returns Either `"major"`, `"minor"`, or `"patch"`, depending on the version type.
|
|
1651
|
-
*/
|
|
1652
|
-
get type() {
|
|
1653
|
-
if (this.minor === 0 && this.patch === 0) return VersionType.MAJOR;
|
|
1654
|
-
if (this.patch === 0) return VersionType.MINOR;
|
|
1655
|
-
return VersionType.PATCH;
|
|
1656
|
-
}
|
|
1657
|
-
static formatString(input, options) {
|
|
1658
|
-
if (options?.omitPrefix) return input.startsWith("v") ? input.slice(1) : input;
|
|
1659
|
-
return input.startsWith("v") ? input : `v${input}`;
|
|
1660
|
-
}
|
|
1661
|
-
/**
|
|
1662
|
-
* Checks if the provided version numbers have the exact same major, minor, and patch numbers.
|
|
1663
|
-
*
|
|
1664
|
-
* @param firstVersion - The first version number to compare.
|
|
1665
|
-
* @param secondVersion - The second version number to compare.
|
|
1666
|
-
*
|
|
1667
|
-
* @returns `true` if the provided version numbers have exactly the same major, minor, and patch numbers, and returns `false` otherwise.
|
|
1668
|
-
*/
|
|
1669
|
-
static isEqual(firstVersion, secondVersion) {
|
|
1670
|
-
return firstVersion.major === secondVersion.major && firstVersion.minor === secondVersion.minor && firstVersion.patch === secondVersion.patch;
|
|
1671
|
-
}
|
|
1672
|
-
/**
|
|
1673
|
-
* Get a formatted string representation of the current version number
|
|
1674
|
-
*
|
|
1675
|
-
* @param options - Options to apply to the string formatting.
|
|
1676
|
-
*
|
|
1677
|
-
* @returns A formatted string representation of the current version number with the options applied.
|
|
1678
|
-
*/
|
|
1679
|
-
format(options) {
|
|
1680
|
-
let baseOutput = `${this.major}`;
|
|
1681
|
-
if (!options?.omitMinor) {
|
|
1682
|
-
baseOutput += `.${this.minor}`;
|
|
1683
|
-
if (!options?.omitPatch) baseOutput += `.${this.patch}`;
|
|
1684
|
-
}
|
|
1685
|
-
return VersionNumber.formatString(baseOutput, { omitPrefix: options?.omitPrefix });
|
|
1686
|
-
}
|
|
1687
|
-
/**
|
|
1688
|
-
* Increments the current version number by the given increment type, returning the result as a new reference in memory.
|
|
1689
|
-
*
|
|
1690
|
-
* @param incrementType - The type of increment. Can be one of the following:
|
|
1691
|
-
* - `"major"`: Change the major version `v1.2.3` → `v2.0.0`
|
|
1692
|
-
* - `"minor"`: Change the minor version `v1.2.3` → `v1.3.0`
|
|
1693
|
-
* - `"patch"`: Change the patch version `v1.2.3` → `v1.2.4`
|
|
1694
|
-
* @param incrementAmount - The amount to increment by (defaults to 1).
|
|
1695
|
-
*
|
|
1696
|
-
* @returns A new instance of `VersionNumber` with the increment applied.
|
|
1697
|
-
*/
|
|
1698
|
-
increment(incrementType, incrementAmount = 1) {
|
|
1699
|
-
const incrementBy = parseIntStrict(String(incrementAmount));
|
|
1700
|
-
const calculatedRawVersion = {
|
|
1701
|
-
major: [
|
|
1702
|
-
this.major + incrementBy,
|
|
1703
|
-
0,
|
|
1704
|
-
0
|
|
1705
|
-
],
|
|
1706
|
-
minor: [
|
|
1707
|
-
this.major,
|
|
1708
|
-
this.minor + incrementBy,
|
|
1709
|
-
0
|
|
1710
|
-
],
|
|
1711
|
-
patch: [
|
|
1712
|
-
this.major,
|
|
1713
|
-
this.minor,
|
|
1714
|
-
this.patch + incrementBy
|
|
1715
|
-
]
|
|
1716
|
-
}[incrementType];
|
|
1717
|
-
try {
|
|
1718
|
-
return new VersionNumber(calculatedRawVersion);
|
|
1719
|
-
} catch (error) {
|
|
1720
|
-
if (DataError$1.check(error) && error.code === "NEGATIVE_INPUTS") throw new DataError$1({
|
|
1721
|
-
currentVersion: this.toString(),
|
|
1722
|
-
calculatedRawVersion: `v${calculatedRawVersion.join(".")}`,
|
|
1723
|
-
incrementAmount
|
|
1724
|
-
}, "NEGATIVE_VERSION", "Cannot apply this increment amount as it would lead to a negative version number.");
|
|
1725
|
-
else throw error;
|
|
1726
|
-
}
|
|
1727
|
-
}
|
|
1728
|
-
/**
|
|
1729
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be coerced to a string.
|
|
1730
|
-
*
|
|
1731
|
-
* @param hint - Not used as of now, but generally used to help with numeric coercion, I think (which we most likely do not need for version numbers).
|
|
1732
|
-
*
|
|
1733
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1734
|
-
*/
|
|
1735
|
-
[Symbol.toPrimitive](hint) {
|
|
1736
|
-
if (hint === "number") throw new DataError$1({ thisVersion: this.toString() }, "INVALID_COERCION", "VersionNumber cannot be coerced to a number type.");
|
|
1737
|
-
return this.toString();
|
|
1738
|
-
}
|
|
1739
|
-
/**
|
|
1740
|
-
* Ensures that the VersionNumber behaves correctly when attempted to be converted to JSON.
|
|
1741
|
-
*
|
|
1742
|
-
* @returns A stringified representation of the current version number, prefixed with `v`.
|
|
1743
|
-
*/
|
|
1744
|
-
toJSON() {
|
|
1745
|
-
return this.toString();
|
|
1746
|
-
}
|
|
1747
|
-
/**
|
|
1748
|
-
* Get a string representation of the current version number.
|
|
1749
|
-
*
|
|
1750
|
-
* @returns A stringified representation of the current version number with the prefix.
|
|
1751
|
-
*/
|
|
1752
|
-
toString() {
|
|
1753
|
-
const rawString = `${this.major}.${this.minor}.${this.patch}`;
|
|
1754
|
-
return VersionNumber.formatString(rawString, { omitPrefix: false });
|
|
1755
|
-
}
|
|
1756
|
-
};
|
|
1757
|
-
const zodVersionNumber = zod.default.union([
|
|
1758
|
-
zod.default.string(),
|
|
1759
|
-
zod.default.tuple([
|
|
1760
|
-
zod.default.number(),
|
|
1761
|
-
zod.default.number(),
|
|
1762
|
-
zod.default.number()
|
|
1763
|
-
]),
|
|
1764
|
-
zod.default.instanceof(VersionNumber)
|
|
1765
|
-
]).transform((rawVersionNumber) => {
|
|
1766
|
-
return new VersionNumber(rawVersionNumber);
|
|
1767
|
-
});
|
|
1768
|
-
//#endregion
|
|
1769
1833
|
exports.APIError = APIError;
|
|
1770
1834
|
exports.DataError = DataError;
|
|
1771
1835
|
exports.Env = Env;
|
|
@@ -1780,6 +1844,7 @@ exports.VersionNumber = VersionNumber;
|
|
|
1780
1844
|
exports.VersionType = VersionType;
|
|
1781
1845
|
exports.addDaysToDate = addDaysToDate;
|
|
1782
1846
|
exports.appendSemicolon = appendSemicolon;
|
|
1847
|
+
exports.az = az;
|
|
1783
1848
|
exports.calculateMonthlyDifference = calculateMonthlyDifference;
|
|
1784
1849
|
exports.camelToKebab = camelToKebab;
|
|
1785
1850
|
exports.convertFileToBase64 = convertFileToBase64;
|