@alextheman/utility 5.15.0 → 5.16.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 +88 -46
- package/dist/index.d.cts +246 -217
- package/dist/index.d.ts +245 -216
- package/dist/index.js +88 -47
- package/dist/internal/index.cjs +26 -3
- package/dist/internal/index.d.cts +15 -2
- package/dist/internal/index.d.ts +15 -2
- package/dist/internal/index.js +26 -3
- package/dist/node/index.cjs +26 -3
- package/dist/node/index.js +26 -3
- package/dist/v6/index.cjs +146 -3
- package/dist/v6/index.d.cts +123 -6
- package/dist/v6/index.d.ts +123 -6
- package/dist/v6/index.js +145 -4
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -38,6 +38,51 @@ const UUID_REGEX = new RegExp(`^${UUID_PATTERN}$`);
|
|
|
38
38
|
const VERSION_NUMBER_PATTERN = String.raw`^(?:v)?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)$`;
|
|
39
39
|
const VERSION_NUMBER_REGEX = RegExp(`^${VERSION_NUMBER_PATTERN}$`);
|
|
40
40
|
//#endregion
|
|
41
|
+
//#region src/root/deprecated/APIError.ts
|
|
42
|
+
const httpErrorCodeLookup = {
|
|
43
|
+
400: "BAD_REQUEST",
|
|
44
|
+
401: "UNAUTHORISED",
|
|
45
|
+
403: "FORBIDDEN",
|
|
46
|
+
404: "NOT_FOUND",
|
|
47
|
+
418: "I_AM_A_TEAPOT",
|
|
48
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Represents common errors you may get from a HTTP API request.
|
|
52
|
+
*
|
|
53
|
+
* @category Types
|
|
54
|
+
*
|
|
55
|
+
* @deprecated Please use `APIError` from `@alextheman/utility/v6` instead.
|
|
56
|
+
*/
|
|
57
|
+
var APIError = class APIError extends Error {
|
|
58
|
+
status;
|
|
59
|
+
/**
|
|
60
|
+
* @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.
|
|
61
|
+
* @param message - An error message to display alongside the status code.
|
|
62
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
63
|
+
*/
|
|
64
|
+
constructor(status = 500, message, options) {
|
|
65
|
+
super(message, options);
|
|
66
|
+
this.status = status;
|
|
67
|
+
if (message) this.message = message;
|
|
68
|
+
else this.message = httpErrorCodeLookup[this.status] ?? "API_ERROR";
|
|
69
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
70
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
74
|
+
*
|
|
75
|
+
* @param input - The input to check.
|
|
76
|
+
*
|
|
77
|
+
* @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`.
|
|
78
|
+
*/
|
|
79
|
+
static check(input) {
|
|
80
|
+
if (input instanceof APIError) return true;
|
|
81
|
+
const data = input;
|
|
82
|
+
return typeof data === "object" && data !== null && typeof data?.status === "number" && typeof data?.message === "string";
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
//#endregion
|
|
41
86
|
//#region src/root/functions/arrayHelpers/fillArray.ts
|
|
42
87
|
/**
|
|
43
88
|
* Creates a new array where each element is the result of the provided callback.
|
|
@@ -185,6 +230,17 @@ var CodeError = class CodeError extends Error {
|
|
|
185
230
|
}
|
|
186
231
|
throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
|
|
187
232
|
}
|
|
233
|
+
/**
|
|
234
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
235
|
+
*
|
|
236
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
237
|
+
*/
|
|
238
|
+
toJSON() {
|
|
239
|
+
return {
|
|
240
|
+
code: this.code,
|
|
241
|
+
message: this.message
|
|
242
|
+
};
|
|
243
|
+
}
|
|
188
244
|
};
|
|
189
245
|
//#endregion
|
|
190
246
|
//#region src/v6/DataError.ts
|
|
@@ -193,14 +249,15 @@ var CodeError = class CodeError extends Error {
|
|
|
193
249
|
*
|
|
194
250
|
* @category Types
|
|
195
251
|
*
|
|
196
|
-
* @template DataType
|
|
252
|
+
* @template DataType The type of the data that caused the error.
|
|
253
|
+
* @template ErrorCode The type of the standardised error code.
|
|
197
254
|
*/
|
|
198
255
|
var DataError$1 = class DataError$1 extends CodeError {
|
|
199
256
|
data;
|
|
200
257
|
/**
|
|
201
258
|
* @param data - The data that caused the error.
|
|
202
259
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
203
|
-
* @param message
|
|
260
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
204
261
|
* @param options - Extra options to pass to super Error constructor.
|
|
205
262
|
*/
|
|
206
263
|
constructor(data, code, message = "The data provided is invalid", options) {
|
|
@@ -220,7 +277,7 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
220
277
|
*/
|
|
221
278
|
static check(input) {
|
|
222
279
|
if (input instanceof DataError$1) return true;
|
|
223
|
-
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
280
|
+
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input && typeof input.data === "object" && input.data !== null;
|
|
224
281
|
}
|
|
225
282
|
/**
|
|
226
283
|
* Check a `DataError` against its error code
|
|
@@ -265,6 +322,17 @@ var DataError$1 = class DataError$1 extends CodeError {
|
|
|
265
322
|
static async expectErrorAsync(errorFunction, options) {
|
|
266
323
|
return await super.expectErrorAsync(errorFunction, options);
|
|
267
324
|
}
|
|
325
|
+
/**
|
|
326
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
327
|
+
*
|
|
328
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
329
|
+
*/
|
|
330
|
+
toJSON() {
|
|
331
|
+
return {
|
|
332
|
+
...super.toJSON(),
|
|
333
|
+
data: this.data
|
|
334
|
+
};
|
|
335
|
+
}
|
|
268
336
|
};
|
|
269
337
|
//#endregion
|
|
270
338
|
//#region src/root/functions/parsers/parseIntStrict.ts
|
|
@@ -922,49 +990,6 @@ function parseBoolean(inputString) {
|
|
|
922
990
|
return normalisedString === "true";
|
|
923
991
|
}
|
|
924
992
|
//#endregion
|
|
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
993
|
//#region src/root/types/VersionNumber.ts
|
|
969
994
|
/**
|
|
970
995
|
* Represents a software version number, considered to be made up of a major, minor, and patch part.
|
|
@@ -1830,6 +1855,22 @@ var DataError = class DataError extends Error {
|
|
|
1830
1855
|
}
|
|
1831
1856
|
};
|
|
1832
1857
|
//#endregion
|
|
1858
|
+
//#region src/root/errors/assertNotNull.ts
|
|
1859
|
+
/**
|
|
1860
|
+
* Asserts that a given input is not `null`, and throws a DataError if it does.
|
|
1861
|
+
*
|
|
1862
|
+
* If no error is thrown from this, the input type gets narrowed down to not include `null`.
|
|
1863
|
+
*
|
|
1864
|
+
* @template InputType The type of the input.
|
|
1865
|
+
*
|
|
1866
|
+
* @param input - The input to assert against
|
|
1867
|
+
*
|
|
1868
|
+
* @throws {DataError} If the input is `null`.
|
|
1869
|
+
*/
|
|
1870
|
+
function assertNotNull(input) {
|
|
1871
|
+
if (input === null) throw new DataError$1({ input }, "NULL_INPUT", "Expected the input not to be null");
|
|
1872
|
+
}
|
|
1873
|
+
//#endregion
|
|
1833
1874
|
exports.APIError = APIError;
|
|
1834
1875
|
exports.DataError = DataError;
|
|
1835
1876
|
exports.Env = Env;
|
|
@@ -1844,6 +1885,7 @@ exports.VersionNumber = VersionNumber;
|
|
|
1844
1885
|
exports.VersionType = VersionType;
|
|
1845
1886
|
exports.addDaysToDate = addDaysToDate;
|
|
1846
1887
|
exports.appendSemicolon = appendSemicolon;
|
|
1888
|
+
exports.assertNotNull = assertNotNull;
|
|
1847
1889
|
exports.az = az;
|
|
1848
1890
|
exports.calculateMonthlyDifference = calculateMonthlyDifference;
|
|
1849
1891
|
exports.camelToKebab = camelToKebab;
|