@alextheman/utility 5.15.1 → 5.16.1
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 +71 -46
- package/dist/index.d.cts +43 -28
- package/dist/index.d.ts +43 -28
- package/dist/index.js +71 -46
- 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 +14 -11
package/dist/internal/index.d.ts
CHANGED
|
@@ -89,6 +89,12 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
89
89
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
90
90
|
*/
|
|
91
91
|
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
|
|
92
|
+
/**
|
|
93
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
94
|
+
*
|
|
95
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
96
|
+
*/
|
|
97
|
+
toJSON(): Omit<CodeError<ErrorCode>, "toJSON" | "name">;
|
|
92
98
|
}
|
|
93
99
|
//#endregion
|
|
94
100
|
//#region src/v6/DataError.d.ts
|
|
@@ -97,14 +103,15 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
97
103
|
*
|
|
98
104
|
* @category Types
|
|
99
105
|
*
|
|
100
|
-
* @template DataType
|
|
106
|
+
* @template DataType The type of the data that caused the error.
|
|
107
|
+
* @template ErrorCode The type of the standardised error code.
|
|
101
108
|
*/
|
|
102
109
|
declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
|
|
103
110
|
data: DataType;
|
|
104
111
|
/**
|
|
105
112
|
* @param data - The data that caused the error.
|
|
106
113
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
107
|
-
* @param message
|
|
114
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
108
115
|
* @param options - Extra options to pass to super Error constructor.
|
|
109
116
|
*/
|
|
110
117
|
constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
|
|
@@ -153,6 +160,12 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
153
160
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
154
161
|
*/
|
|
155
162
|
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
163
|
+
/**
|
|
164
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
165
|
+
*
|
|
166
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
167
|
+
*/
|
|
168
|
+
toJSON(): Omit<DataError<DataType, ErrorCode>, "toJSON" | "name">;
|
|
156
169
|
}
|
|
157
170
|
//#endregion
|
|
158
171
|
//#region src/internal/DependencyGroup.d.ts
|
package/dist/internal/index.js
CHANGED
|
@@ -159,6 +159,17 @@ var CodeError = class CodeError extends Error {
|
|
|
159
159
|
}
|
|
160
160
|
throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
|
|
161
161
|
}
|
|
162
|
+
/**
|
|
163
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
164
|
+
*
|
|
165
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
166
|
+
*/
|
|
167
|
+
toJSON() {
|
|
168
|
+
return {
|
|
169
|
+
code: this.code,
|
|
170
|
+
message: this.message
|
|
171
|
+
};
|
|
172
|
+
}
|
|
162
173
|
};
|
|
163
174
|
//#endregion
|
|
164
175
|
//#region src/v6/DataError.ts
|
|
@@ -167,14 +178,15 @@ var CodeError = class CodeError extends Error {
|
|
|
167
178
|
*
|
|
168
179
|
* @category Types
|
|
169
180
|
*
|
|
170
|
-
* @template DataType
|
|
181
|
+
* @template DataType The type of the data that caused the error.
|
|
182
|
+
* @template ErrorCode The type of the standardised error code.
|
|
171
183
|
*/
|
|
172
184
|
var DataError = class DataError extends CodeError {
|
|
173
185
|
data;
|
|
174
186
|
/**
|
|
175
187
|
* @param data - The data that caused the error.
|
|
176
188
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
177
|
-
* @param message
|
|
189
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
178
190
|
* @param options - Extra options to pass to super Error constructor.
|
|
179
191
|
*/
|
|
180
192
|
constructor(data, code, message = "The data provided is invalid", options) {
|
|
@@ -194,7 +206,7 @@ var DataError = class DataError extends CodeError {
|
|
|
194
206
|
*/
|
|
195
207
|
static check(input) {
|
|
196
208
|
if (input instanceof DataError) return true;
|
|
197
|
-
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
209
|
+
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;
|
|
198
210
|
}
|
|
199
211
|
/**
|
|
200
212
|
* Check a `DataError` against its error code
|
|
@@ -239,6 +251,17 @@ var DataError = class DataError extends CodeError {
|
|
|
239
251
|
static async expectErrorAsync(errorFunction, options) {
|
|
240
252
|
return await super.expectErrorAsync(errorFunction, options);
|
|
241
253
|
}
|
|
254
|
+
/**
|
|
255
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
256
|
+
*
|
|
257
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
258
|
+
*/
|
|
259
|
+
toJSON() {
|
|
260
|
+
return {
|
|
261
|
+
...super.toJSON(),
|
|
262
|
+
data: this.data
|
|
263
|
+
};
|
|
264
|
+
}
|
|
242
265
|
};
|
|
243
266
|
//#endregion
|
|
244
267
|
//#region src/root/functions/parsers/parseIntStrict.ts
|
package/dist/node/index.cjs
CHANGED
|
@@ -300,6 +300,17 @@ var CodeError = class CodeError extends Error {
|
|
|
300
300
|
}
|
|
301
301
|
throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
|
|
302
302
|
}
|
|
303
|
+
/**
|
|
304
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
305
|
+
*
|
|
306
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
307
|
+
*/
|
|
308
|
+
toJSON() {
|
|
309
|
+
return {
|
|
310
|
+
code: this.code,
|
|
311
|
+
message: this.message
|
|
312
|
+
};
|
|
313
|
+
}
|
|
303
314
|
};
|
|
304
315
|
//#endregion
|
|
305
316
|
//#region src/v6/DataError.ts
|
|
@@ -308,14 +319,15 @@ var CodeError = class CodeError extends Error {
|
|
|
308
319
|
*
|
|
309
320
|
* @category Types
|
|
310
321
|
*
|
|
311
|
-
* @template DataType
|
|
322
|
+
* @template DataType The type of the data that caused the error.
|
|
323
|
+
* @template ErrorCode The type of the standardised error code.
|
|
312
324
|
*/
|
|
313
325
|
var DataError = class DataError extends CodeError {
|
|
314
326
|
data;
|
|
315
327
|
/**
|
|
316
328
|
* @param data - The data that caused the error.
|
|
317
329
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
318
|
-
* @param message
|
|
330
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
319
331
|
* @param options - Extra options to pass to super Error constructor.
|
|
320
332
|
*/
|
|
321
333
|
constructor(data, code, message = "The data provided is invalid", options) {
|
|
@@ -335,7 +347,7 @@ var DataError = class DataError extends CodeError {
|
|
|
335
347
|
*/
|
|
336
348
|
static check(input) {
|
|
337
349
|
if (input instanceof DataError) return true;
|
|
338
|
-
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
350
|
+
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;
|
|
339
351
|
}
|
|
340
352
|
/**
|
|
341
353
|
* Check a `DataError` against its error code
|
|
@@ -380,6 +392,17 @@ var DataError = class DataError extends CodeError {
|
|
|
380
392
|
static async expectErrorAsync(errorFunction, options) {
|
|
381
393
|
return await super.expectErrorAsync(errorFunction, options);
|
|
382
394
|
}
|
|
395
|
+
/**
|
|
396
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
397
|
+
*
|
|
398
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
399
|
+
*/
|
|
400
|
+
toJSON() {
|
|
401
|
+
return {
|
|
402
|
+
...super.toJSON(),
|
|
403
|
+
data: this.data
|
|
404
|
+
};
|
|
405
|
+
}
|
|
383
406
|
};
|
|
384
407
|
//#endregion
|
|
385
408
|
//#region src/node/functions/parseFilePath.ts
|
package/dist/node/index.js
CHANGED
|
@@ -276,6 +276,17 @@ var CodeError = class CodeError extends Error {
|
|
|
276
276
|
}
|
|
277
277
|
throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
|
|
278
278
|
}
|
|
279
|
+
/**
|
|
280
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
281
|
+
*
|
|
282
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
283
|
+
*/
|
|
284
|
+
toJSON() {
|
|
285
|
+
return {
|
|
286
|
+
code: this.code,
|
|
287
|
+
message: this.message
|
|
288
|
+
};
|
|
289
|
+
}
|
|
279
290
|
};
|
|
280
291
|
//#endregion
|
|
281
292
|
//#region src/v6/DataError.ts
|
|
@@ -284,14 +295,15 @@ var CodeError = class CodeError extends Error {
|
|
|
284
295
|
*
|
|
285
296
|
* @category Types
|
|
286
297
|
*
|
|
287
|
-
* @template DataType
|
|
298
|
+
* @template DataType The type of the data that caused the error.
|
|
299
|
+
* @template ErrorCode The type of the standardised error code.
|
|
288
300
|
*/
|
|
289
301
|
var DataError = class DataError extends CodeError {
|
|
290
302
|
data;
|
|
291
303
|
/**
|
|
292
304
|
* @param data - The data that caused the error.
|
|
293
305
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
294
|
-
* @param message
|
|
306
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
295
307
|
* @param options - Extra options to pass to super Error constructor.
|
|
296
308
|
*/
|
|
297
309
|
constructor(data, code, message = "The data provided is invalid", options) {
|
|
@@ -311,7 +323,7 @@ var DataError = class DataError extends CodeError {
|
|
|
311
323
|
*/
|
|
312
324
|
static check(input) {
|
|
313
325
|
if (input instanceof DataError) return true;
|
|
314
|
-
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
326
|
+
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;
|
|
315
327
|
}
|
|
316
328
|
/**
|
|
317
329
|
* Check a `DataError` against its error code
|
|
@@ -356,6 +368,17 @@ var DataError = class DataError extends CodeError {
|
|
|
356
368
|
static async expectErrorAsync(errorFunction, options) {
|
|
357
369
|
return await super.expectErrorAsync(errorFunction, options);
|
|
358
370
|
}
|
|
371
|
+
/**
|
|
372
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
373
|
+
*
|
|
374
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
375
|
+
*/
|
|
376
|
+
toJSON() {
|
|
377
|
+
return {
|
|
378
|
+
...super.toJSON(),
|
|
379
|
+
data: this.data
|
|
380
|
+
};
|
|
381
|
+
}
|
|
359
382
|
};
|
|
360
383
|
//#endregion
|
|
361
384
|
//#region src/node/functions/parseFilePath.ts
|
package/dist/v6/index.cjs
CHANGED
|
@@ -236,6 +236,135 @@ var CodeError = class CodeError extends Error {
|
|
|
236
236
|
}
|
|
237
237
|
throw new Error(`Expected a ${this.name} to be thrown but none was thrown`);
|
|
238
238
|
}
|
|
239
|
+
/**
|
|
240
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
241
|
+
*
|
|
242
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
243
|
+
*/
|
|
244
|
+
toJSON() {
|
|
245
|
+
return {
|
|
246
|
+
code: this.code,
|
|
247
|
+
message: this.message
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
//#endregion
|
|
252
|
+
//#region src/v6/APIError.ts
|
|
253
|
+
const httpErrorCodeLookup = {
|
|
254
|
+
400: "BAD_REQUEST",
|
|
255
|
+
401: "UNAUTHORISED",
|
|
256
|
+
403: "FORBIDDEN",
|
|
257
|
+
404: "NOT_FOUND",
|
|
258
|
+
418: "I_AM_A_TEAPOT",
|
|
259
|
+
500: "INTERNAL_SERVER_ERROR"
|
|
260
|
+
};
|
|
261
|
+
/**
|
|
262
|
+
* Represents common errors you may get from a HTTP API request.
|
|
263
|
+
*
|
|
264
|
+
* @category Types
|
|
265
|
+
*
|
|
266
|
+
* @template DataType The type of the data that caused the error.
|
|
267
|
+
* @template ErrorCode The type of the standardised error code.
|
|
268
|
+
*/
|
|
269
|
+
var APIError = class APIError extends CodeError {
|
|
270
|
+
data;
|
|
271
|
+
status;
|
|
272
|
+
/**
|
|
273
|
+
* @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.
|
|
274
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
275
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
276
|
+
* @param data - The data that caused the error.
|
|
277
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
278
|
+
*/
|
|
279
|
+
constructor(status, code, message = "There was an error with your API request.", data, options) {
|
|
280
|
+
super(code, message, options);
|
|
281
|
+
if (Error.captureStackTrace) Error.captureStackTrace(this, new.target);
|
|
282
|
+
this.data = data;
|
|
283
|
+
this.name = new.target.name;
|
|
284
|
+
this.status = status;
|
|
285
|
+
Object.defineProperty(this, "message", { enumerable: true });
|
|
286
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
290
|
+
*
|
|
291
|
+
* @param input - The input to check.
|
|
292
|
+
*
|
|
293
|
+
* @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`.
|
|
294
|
+
*/
|
|
295
|
+
static check(input) {
|
|
296
|
+
if (input instanceof APIError) return true;
|
|
297
|
+
return typeof input === "object" && input !== null && "status" in input && typeof input.status === "number" && "code" in input && typeof input.code === "string" && "message" in input && typeof input.message === "string" && (!("data" in input) || input.data === void 0 || typeof input.data === "object" && input.data !== null);
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Check an `APIError` against its error code
|
|
301
|
+
*
|
|
302
|
+
* This will also automatically narrow down the type of the input to be `APIError`, with its error code properly typed if this function returns true.
|
|
303
|
+
*
|
|
304
|
+
* @template ErrorCode The type of the error code
|
|
305
|
+
*
|
|
306
|
+
* @param input - The input to check.
|
|
307
|
+
* @param code - The expected code of the resulting error.
|
|
308
|
+
*
|
|
309
|
+
* @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `APIError`, and its code will be narrowed to the expected code's type if the function returns `true`.
|
|
310
|
+
*/
|
|
311
|
+
static checkWithCode(input, code) {
|
|
312
|
+
return this.check(input) && input.code === code;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Gets the thrown `APIError` from a given function if one was thrown, and re-throws any other errors, or throws a default `APIError` if no error thrown.
|
|
316
|
+
*
|
|
317
|
+
* @param errorFunction - The function expected to throw the error.
|
|
318
|
+
* @param options - Extra options to apply.
|
|
319
|
+
*
|
|
320
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `APIError`.
|
|
321
|
+
* @throws {Error} If no `APIError` was thrown by the `errorFunction`
|
|
322
|
+
*
|
|
323
|
+
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
324
|
+
*/
|
|
325
|
+
static expectError(errorFunction, options) {
|
|
326
|
+
return super.expectError(errorFunction, options);
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Gets the thrown `APIError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `APIError` if no error thrown.
|
|
330
|
+
*
|
|
331
|
+
* @param errorFunction - The function expected to throw the error.
|
|
332
|
+
* @param options - Extra options to apply.
|
|
333
|
+
*
|
|
334
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `APIError`.
|
|
335
|
+
* @throws {Error} If no `APIError` was thrown by the `errorFunction`
|
|
336
|
+
*
|
|
337
|
+
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
338
|
+
*/
|
|
339
|
+
static async expectErrorAsync(errorFunction, options) {
|
|
340
|
+
return await super.expectErrorAsync(errorFunction, options);
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Create a new `APIError` with the error code derived from the status directly.
|
|
344
|
+
*
|
|
345
|
+
* @param status - A HTTP status code. Must be one of the supported error codes from our custom `httpErrorCodeLookup`.
|
|
346
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
347
|
+
* @param data - The data that caused the error.
|
|
348
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
349
|
+
*
|
|
350
|
+
* @returns A new `APIError` with the error code derived from the status code.
|
|
351
|
+
*/
|
|
352
|
+
static fromStatus(status, message, data, options) {
|
|
353
|
+
const code = httpErrorCodeLookup[status];
|
|
354
|
+
return new APIError(status, code, message, data, options);
|
|
355
|
+
}
|
|
356
|
+
/**
|
|
357
|
+
* Converts the `APIError` instance to a serialised JSON payload.
|
|
358
|
+
*
|
|
359
|
+
* @returns A JSON serialised version of the current `APIError` instance.
|
|
360
|
+
*/
|
|
361
|
+
toJSON() {
|
|
362
|
+
return {
|
|
363
|
+
...super.toJSON(),
|
|
364
|
+
status: this.status,
|
|
365
|
+
data: this.data
|
|
366
|
+
};
|
|
367
|
+
}
|
|
239
368
|
};
|
|
240
369
|
//#endregion
|
|
241
370
|
//#region src/v6/DataError.ts
|
|
@@ -244,14 +373,15 @@ var CodeError = class CodeError extends Error {
|
|
|
244
373
|
*
|
|
245
374
|
* @category Types
|
|
246
375
|
*
|
|
247
|
-
* @template DataType
|
|
376
|
+
* @template DataType The type of the data that caused the error.
|
|
377
|
+
* @template ErrorCode The type of the standardised error code.
|
|
248
378
|
*/
|
|
249
379
|
var DataError = class DataError extends CodeError {
|
|
250
380
|
data;
|
|
251
381
|
/**
|
|
252
382
|
* @param data - The data that caused the error.
|
|
253
383
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
254
|
-
* @param message
|
|
384
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
255
385
|
* @param options - Extra options to pass to super Error constructor.
|
|
256
386
|
*/
|
|
257
387
|
constructor(data, code, message = "The data provided is invalid", options) {
|
|
@@ -271,7 +401,7 @@ var DataError = class DataError extends CodeError {
|
|
|
271
401
|
*/
|
|
272
402
|
static check(input) {
|
|
273
403
|
if (input instanceof DataError) return true;
|
|
274
|
-
return typeof input === "object" && input !== null && "message" in input && typeof input.message === "string" && "code" in input && typeof input.code === "string" && "data" in input;
|
|
404
|
+
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;
|
|
275
405
|
}
|
|
276
406
|
/**
|
|
277
407
|
* Check a `DataError` against its error code
|
|
@@ -316,6 +446,17 @@ var DataError = class DataError extends CodeError {
|
|
|
316
446
|
static async expectErrorAsync(errorFunction, options) {
|
|
317
447
|
return await super.expectErrorAsync(errorFunction, options);
|
|
318
448
|
}
|
|
449
|
+
/**
|
|
450
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
451
|
+
*
|
|
452
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
453
|
+
*/
|
|
454
|
+
toJSON() {
|
|
455
|
+
return {
|
|
456
|
+
...super.toJSON(),
|
|
457
|
+
data: this.data
|
|
458
|
+
};
|
|
459
|
+
}
|
|
319
460
|
};
|
|
320
461
|
//#endregion
|
|
321
462
|
//#region src/v6/sayHello.ts
|
|
@@ -431,6 +572,8 @@ I'll commit to you!
|
|
|
431
572
|
`;
|
|
432
573
|
}
|
|
433
574
|
//#endregion
|
|
575
|
+
exports.APIError = APIError;
|
|
434
576
|
exports.CodeError = CodeError;
|
|
435
577
|
exports.DataError = DataError;
|
|
578
|
+
exports.httpErrorCodeLookup = httpErrorCodeLookup;
|
|
436
579
|
exports.sayHello = sayHello;
|
package/dist/v6/index.d.cts
CHANGED
|
@@ -1,3 +1,16 @@
|
|
|
1
|
+
//#region src/root/types/CreateEnumType.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Get the value types from a const object so the object can behave similarly to an enum.
|
|
4
|
+
*
|
|
5
|
+
* @category Types
|
|
6
|
+
*
|
|
7
|
+
* @template ObjectType - The type of the object to get the value types for.
|
|
8
|
+
*/
|
|
9
|
+
type CreateEnumType<ObjectType extends Record<PropertyKey, unknown>> = ObjectType[keyof ObjectType];
|
|
10
|
+
//#endregion
|
|
11
|
+
//#region src/root/types/IsTypeArgumentString.d.ts
|
|
12
|
+
type IsTypeArgumentString<Argument extends string> = Argument;
|
|
13
|
+
//#endregion
|
|
1
14
|
//#region src/v6/CodeError.d.ts
|
|
2
15
|
interface ExpectErrorOptions<ErrorCode extends string = string> {
|
|
3
16
|
expectedCode?: ErrorCode;
|
|
@@ -63,6 +76,106 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
63
76
|
* @returns The `CodeError` that was thrown by the `errorFunction`
|
|
64
77
|
*/
|
|
65
78
|
static expectErrorAsync<ErrorCode extends string = string>(this: typeof CodeError, errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<CodeError>;
|
|
79
|
+
/**
|
|
80
|
+
* Converts the `CodeError` instance to a serialised JSON payload.
|
|
81
|
+
*
|
|
82
|
+
* @returns A JSON serialised version of the current `CodeError` instance.
|
|
83
|
+
*/
|
|
84
|
+
toJSON(): Omit<CodeError<ErrorCode>, "toJSON" | "name">;
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
//#region src/v6/APIError.d.ts
|
|
88
|
+
declare const httpErrorCodeLookup: {
|
|
89
|
+
readonly 400: "BAD_REQUEST";
|
|
90
|
+
readonly 401: "UNAUTHORISED";
|
|
91
|
+
readonly 403: "FORBIDDEN";
|
|
92
|
+
readonly 404: "NOT_FOUND";
|
|
93
|
+
readonly 418: "I_AM_A_TEAPOT";
|
|
94
|
+
readonly 500: "INTERNAL_SERVER_ERROR";
|
|
95
|
+
};
|
|
96
|
+
type HTTPErrorCode = keyof typeof httpErrorCodeLookup;
|
|
97
|
+
type APIErrorCode = CreateEnumType<typeof httpErrorCodeLookup>;
|
|
98
|
+
/**
|
|
99
|
+
* Represents common errors you may get from a HTTP API request.
|
|
100
|
+
*
|
|
101
|
+
* @category Types
|
|
102
|
+
*
|
|
103
|
+
* @template DataType The type of the data that caused the error.
|
|
104
|
+
* @template ErrorCode The type of the standardised error code.
|
|
105
|
+
*/
|
|
106
|
+
declare class APIError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
|
|
107
|
+
data?: DataType;
|
|
108
|
+
status: number;
|
|
109
|
+
/**
|
|
110
|
+
* @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.
|
|
111
|
+
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
112
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
113
|
+
* @param data - The data that caused the error.
|
|
114
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
115
|
+
*/
|
|
116
|
+
constructor(status: number, code: ErrorCode, message?: string, data?: DataType, options?: ErrorOptions);
|
|
117
|
+
/**
|
|
118
|
+
* Checks whether the given input may have been caused by an APIError.
|
|
119
|
+
*
|
|
120
|
+
* @param input - The input to check.
|
|
121
|
+
*
|
|
122
|
+
* @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`.
|
|
123
|
+
*/
|
|
124
|
+
static check<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown): input is APIError<DataType, ErrorCode>;
|
|
125
|
+
/**
|
|
126
|
+
* Check an `APIError` against its error code
|
|
127
|
+
*
|
|
128
|
+
* This will also automatically narrow down the type of the input to be `APIError`, with its error code properly typed if this function returns true.
|
|
129
|
+
*
|
|
130
|
+
* @template ErrorCode The type of the error code
|
|
131
|
+
*
|
|
132
|
+
* @param input - The input to check.
|
|
133
|
+
* @param code - The expected code of the resulting error.
|
|
134
|
+
*
|
|
135
|
+
* @returns `true` if the error code matches the expected code, and `false` otherwise. The type of the input will also be narrowed down to `APIError`, and its code will be narrowed to the expected code's type if the function returns `true`.
|
|
136
|
+
*/
|
|
137
|
+
static checkWithCode<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string>(input: unknown, code: ErrorCode): input is APIError<DataType, ErrorCode>;
|
|
138
|
+
/**
|
|
139
|
+
* Gets the thrown `APIError` from a given function if one was thrown, and re-throws any other errors, or throws a default `APIError` if no error thrown.
|
|
140
|
+
*
|
|
141
|
+
* @param errorFunction - The function expected to throw the error.
|
|
142
|
+
* @param options - Extra options to apply.
|
|
143
|
+
*
|
|
144
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `APIError`.
|
|
145
|
+
* @throws {Error} If no `APIError` was thrown by the `errorFunction`
|
|
146
|
+
*
|
|
147
|
+
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
148
|
+
*/
|
|
149
|
+
static expectError<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => unknown, options?: ExpectErrorOptions<ErrorCode>): APIError<DataType, ErrorCode>;
|
|
150
|
+
/**
|
|
151
|
+
* Gets the thrown `APIError` from a given asynchronous function if one was thrown, and re-throws any other errors, or throws a default `APIError` if no error thrown.
|
|
152
|
+
*
|
|
153
|
+
* @param errorFunction - The function expected to throw the error.
|
|
154
|
+
* @param options - Extra options to apply.
|
|
155
|
+
*
|
|
156
|
+
* @throws {Error} Any other errors thrown by the `errorFunction` that are not a `APIError`.
|
|
157
|
+
* @throws {Error} If no `APIError` was thrown by the `errorFunction`
|
|
158
|
+
*
|
|
159
|
+
* @returns The `APIError` that was thrown by the `errorFunction`
|
|
160
|
+
*/
|
|
161
|
+
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<APIError<DataType, ErrorCode>>;
|
|
162
|
+
/**
|
|
163
|
+
* Create a new `APIError` with the error code derived from the status directly.
|
|
164
|
+
*
|
|
165
|
+
* @param status - A HTTP status code. Must be one of the supported error codes from our custom `httpErrorCodeLookup`.
|
|
166
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
167
|
+
* @param data - The data that caused the error.
|
|
168
|
+
* @param options - Extra options to be passed to super Error constructor.
|
|
169
|
+
*
|
|
170
|
+
* @returns A new `APIError` with the error code derived from the status code.
|
|
171
|
+
*/
|
|
172
|
+
static fromStatus<DataType extends object = Record<PropertyKey, unknown>>(status: HTTPErrorCode, message?: string, data?: DataType, options?: ErrorOptions): APIError<DataType, APIErrorCode>;
|
|
173
|
+
/**
|
|
174
|
+
* Converts the `APIError` instance to a serialised JSON payload.
|
|
175
|
+
*
|
|
176
|
+
* @returns A JSON serialised version of the current `APIError` instance.
|
|
177
|
+
*/
|
|
178
|
+
toJSON(): Omit<APIError<DataType, ErrorCode>, "toJSON" | "name">;
|
|
66
179
|
}
|
|
67
180
|
//#endregion
|
|
68
181
|
//#region src/v6/DataError.d.ts
|
|
@@ -71,14 +184,15 @@ declare class CodeError<ErrorCode extends string = string> extends Error {
|
|
|
71
184
|
*
|
|
72
185
|
* @category Types
|
|
73
186
|
*
|
|
74
|
-
* @template DataType
|
|
187
|
+
* @template DataType The type of the data that caused the error.
|
|
188
|
+
* @template ErrorCode The type of the standardised error code.
|
|
75
189
|
*/
|
|
76
190
|
declare class DataError<DataType extends object = Record<PropertyKey, unknown>, ErrorCode extends string = string> extends CodeError<ErrorCode> {
|
|
77
191
|
data: DataType;
|
|
78
192
|
/**
|
|
79
193
|
* @param data - The data that caused the error.
|
|
80
194
|
* @param code - A standardised code (e.g. UNEXPECTED_DATA).
|
|
81
|
-
* @param message
|
|
195
|
+
* @param message - A human-readable error message (e.g. The data provided is invalid).
|
|
82
196
|
* @param options - Extra options to pass to super Error constructor.
|
|
83
197
|
*/
|
|
84
198
|
constructor(data: DataType, code: ErrorCode, message?: string, options?: ErrorOptions);
|
|
@@ -127,6 +241,12 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
127
241
|
* @returns The `DataError` that was thrown by the `errorFunction`
|
|
128
242
|
*/
|
|
129
243
|
static expectErrorAsync<DataType extends Record<PropertyKey, unknown>, ErrorCode extends string = string>(errorFunction: () => Promise<unknown>, options?: ExpectErrorOptions<ErrorCode>): Promise<DataError<DataType, ErrorCode>>;
|
|
244
|
+
/**
|
|
245
|
+
* Converts the `DataError` instance to a serialised JSON payload.
|
|
246
|
+
*
|
|
247
|
+
* @returns A JSON serialised version of the current `DataError` instance.
|
|
248
|
+
*/
|
|
249
|
+
toJSON(): Omit<DataError<DataType, ErrorCode>, "toJSON" | "name">;
|
|
130
250
|
}
|
|
131
251
|
//#endregion
|
|
132
252
|
//#region src/v6/sayHello.d.ts
|
|
@@ -139,7 +259,4 @@ declare class DataError<DataType extends object = Record<PropertyKey, unknown>,
|
|
|
139
259
|
*/
|
|
140
260
|
declare function sayHello(): string;
|
|
141
261
|
//#endregion
|
|
142
|
-
|
|
143
|
-
type IsTypeArgumentString<Argument extends string> = Argument;
|
|
144
|
-
//#endregion
|
|
145
|
-
export { CodeError, DataError, type ExpectErrorOptions, type IsTypeArgumentString, sayHello };
|
|
262
|
+
export { APIError, type APIErrorCode, CodeError, DataError, type ExpectErrorOptions, type HTTPErrorCode, type IsTypeArgumentString, httpErrorCodeLookup, sayHello };
|