@geekmidas/errors 10.0.0-alpha.12 → 10.0.0-alpha.121

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.d.mts CHANGED
@@ -12,7 +12,7 @@
12
12
  * });
13
13
  * ```
14
14
  */
15
- declare class HttpError extends Error {
15
+ export declare class HttpError extends Error {
16
16
  /** The HTTP status code (e.g., 400, 404, 500) */
17
17
  readonly statusCode: number;
18
18
  /** The standard HTTP status message (e.g., 'Bad Request', 'Not Found') */
@@ -23,6 +23,13 @@ declare class HttpError extends Error {
23
23
  readonly details?: any;
24
24
  /** Application-specific error code for client-side handling */
25
25
  readonly code?: string;
26
+ /**
27
+ * Headers the response that reports this error carries — a 429's
28
+ * `Retry-After`, a 401's `WWW-Authenticate`. Every endpoint adaptor sets
29
+ * them on the error response, so the error says everything the client is
30
+ * told.
31
+ */
32
+ readonly headers?: Readonly<Record<string, string>>;
26
33
  /**
27
34
  * Creates a new HttpError instance.
28
35
  *
@@ -32,12 +39,14 @@ declare class HttpError extends Error {
32
39
  * @param options.statusMessage - Override the default status message
33
40
  * @param options.details - Additional error details or context
34
41
  * @param options.code - Application-specific error code
42
+ * @param options.headers - Headers the error response carries
35
43
  * @param options.cause - The underlying error that caused this error (ES2022)
36
44
  */
37
45
  constructor(statusCode: number, message?: string, options?: {
38
46
  statusMessage?: string;
39
47
  details?: any;
40
48
  code?: string;
49
+ headers?: Readonly<Record<string, string>>;
41
50
  cause?: Error;
42
51
  });
43
52
  /**
@@ -82,7 +91,7 @@ declare class HttpError extends Error {
82
91
  * throw new BadRequestError('Invalid JSON', { line: 5, column: 12 });
83
92
  * ```
84
93
  */
85
- declare class BadRequestError extends HttpError {
94
+ export declare class BadRequestError extends HttpError {
86
95
  constructor(message?: string, details?: any);
87
96
  }
88
97
  /**
@@ -96,7 +105,7 @@ declare class BadRequestError extends HttpError {
96
105
  * throw new UnauthorizedError('Invalid token');
97
106
  * ```
98
107
  */
99
- declare class UnauthorizedError extends HttpError {
108
+ export declare class UnauthorizedError extends HttpError {
100
109
  constructor(message?: string, details?: any);
101
110
  }
102
111
  /**
@@ -110,7 +119,7 @@ declare class UnauthorizedError extends HttpError {
110
119
  * throw new ForbiddenError('Insufficient permissions', { required: 'admin' });
111
120
  * ```
112
121
  */
113
- declare class ForbiddenError extends HttpError {
122
+ export declare class ForbiddenError extends HttpError {
114
123
  constructor(message?: string, details?: any);
115
124
  }
116
125
  /**
@@ -124,7 +133,7 @@ declare class ForbiddenError extends HttpError {
124
133
  * throw new NotFoundError('User not found', { userId: '123' });
125
134
  * ```
126
135
  */
127
- declare class NotFoundError extends HttpError {
136
+ export declare class NotFoundError extends HttpError {
128
137
  constructor(message?: string, details?: any);
129
138
  }
130
139
  /**
@@ -138,7 +147,7 @@ declare class NotFoundError extends HttpError {
138
147
  * throw new MethodNotAllowedError('DELETE not supported', ['GET', 'POST', 'PUT']);
139
148
  * ```
140
149
  */
141
- declare class MethodNotAllowedError extends HttpError {
150
+ export declare class MethodNotAllowedError extends HttpError {
142
151
  /**
143
152
  * @param message - Optional error message
144
153
  * @param allowedMethods - Array of allowed HTTP methods for this resource
@@ -156,7 +165,7 @@ declare class MethodNotAllowedError extends HttpError {
156
165
  * throw new ConflictError('Email already exists', { email: 'user@example.com' });
157
166
  * ```
158
167
  */
159
- declare class ConflictError extends HttpError {
168
+ export declare class ConflictError extends HttpError {
160
169
  constructor(message?: string, details?: any);
161
170
  }
162
171
  /**
@@ -173,7 +182,7 @@ declare class ConflictError extends HttpError {
173
182
  * });
174
183
  * ```
175
184
  */
176
- declare class UnprocessableEntityError extends HttpError {
185
+ export declare class UnprocessableEntityError extends HttpError {
177
186
  /**
178
187
  * @param message - Optional error message
179
188
  * @param validationErrors - Object containing field-specific validation errors
@@ -191,12 +200,19 @@ declare class UnprocessableEntityError extends HttpError {
191
200
  * throw new TooManyRequestsError('Rate limit exceeded', 60); // retry after 60 seconds
192
201
  * ```
193
202
  */
194
- declare class TooManyRequestsError extends HttpError {
203
+ export declare class TooManyRequestsError extends HttpError {
204
+ /** Seconds the client should wait before retrying, where known. */
205
+ readonly retryAfter?: number;
195
206
  /**
196
207
  * @param message - Optional error message
197
- * @param retryAfter - Number of seconds the client should wait before retrying
208
+ * @param retryAfter - Number of seconds the client should wait before
209
+ * retrying, sent as the `Retry-After` header
210
+ * @param options.headers - Further headers the response carries, such as
211
+ * a rate limiter's `X-RateLimit-*`
198
212
  */
199
- constructor(message?: string, retryAfter?: number);
213
+ constructor(message?: string, retryAfter?: number, options?: {
214
+ headers?: Readonly<Record<string, string>>;
215
+ });
200
216
  }
201
217
  /**
202
218
  * Represents a 500 Internal Server Error.
@@ -209,7 +225,7 @@ declare class TooManyRequestsError extends HttpError {
209
225
  * throw new InternalServerError('Database connection failed');
210
226
  * ```
211
227
  */
212
- declare class InternalServerError extends HttpError {
228
+ export declare class InternalServerError extends HttpError {
213
229
  constructor(message?: string, details?: any);
214
230
  }
215
231
  /**
@@ -223,7 +239,7 @@ declare class InternalServerError extends HttpError {
223
239
  * throw new NotImplementedError('WebSocket support not implemented');
224
240
  * ```
225
241
  */
226
- declare class NotImplementedError extends HttpError {
242
+ export declare class NotImplementedError extends HttpError {
227
243
  constructor(message?: string, details?: any);
228
244
  }
229
245
  /**
@@ -237,7 +253,7 @@ declare class NotImplementedError extends HttpError {
237
253
  * throw new BadGatewayError('Upstream server error');
238
254
  * ```
239
255
  */
240
- declare class BadGatewayError extends HttpError {
256
+ export declare class BadGatewayError extends HttpError {
241
257
  constructor(message?: string, details?: any);
242
258
  }
243
259
  /**
@@ -251,7 +267,7 @@ declare class BadGatewayError extends HttpError {
251
267
  * throw new ServiceUnavailableError('Maintenance in progress', 300); // retry after 5 minutes
252
268
  * ```
253
269
  */
254
- declare class ServiceUnavailableError extends HttpError {
270
+ export declare class ServiceUnavailableError extends HttpError {
255
271
  /**
256
272
  * @param message - Optional error message
257
273
  * @param retryAfter - Number of seconds the client should wait before retrying
@@ -269,61 +285,61 @@ declare class ServiceUnavailableError extends HttpError {
269
285
  * throw new GatewayTimeoutError('Upstream server timeout');
270
286
  * ```
271
287
  */
272
- declare class GatewayTimeoutError extends HttpError {
288
+ export declare class GatewayTimeoutError extends HttpError {
273
289
  constructor(message?: string, details?: any);
274
290
  }
275
291
  /** Type-safe error registry mapping status codes to their factory functions */
276
292
  declare const errorRegistry: {
277
293
  readonly 400: {
278
- readonly type: "standard";
294
+ readonly type: 'standard';
279
295
  readonly factory: (m: string, d: any) => BadRequestError;
280
296
  };
281
297
  readonly 401: {
282
- readonly type: "standard";
298
+ readonly type: 'standard';
283
299
  readonly factory: (m: string, d: any) => UnauthorizedError;
284
300
  };
285
301
  readonly 403: {
286
- readonly type: "standard";
302
+ readonly type: 'standard';
287
303
  readonly factory: (m: string, d: any) => ForbiddenError;
288
304
  };
289
305
  readonly 404: {
290
- readonly type: "standard";
306
+ readonly type: 'standard';
291
307
  readonly factory: (m: string, d: any) => NotFoundError;
292
308
  };
293
309
  readonly 405: {
294
- readonly type: "methodNotAllowed";
310
+ readonly type: 'methodNotAllowed';
295
311
  readonly factory: (m: string, am: string[]) => MethodNotAllowedError;
296
312
  };
297
313
  readonly 409: {
298
- readonly type: "standard";
314
+ readonly type: 'standard';
299
315
  readonly factory: (m: string, d: any) => ConflictError;
300
316
  };
301
317
  readonly 422: {
302
- readonly type: "validation";
318
+ readonly type: 'validation';
303
319
  readonly factory: (m: string, ve: any) => UnprocessableEntityError;
304
320
  };
305
321
  readonly 429: {
306
- readonly type: "retryAfter";
322
+ readonly type: 'retryAfter';
307
323
  readonly factory: (m: string, ra: number) => TooManyRequestsError;
308
324
  };
309
325
  readonly 500: {
310
- readonly type: "standard";
326
+ readonly type: 'standard';
311
327
  readonly factory: (m: string, d: any) => InternalServerError;
312
328
  };
313
329
  readonly 501: {
314
- readonly type: "standard";
330
+ readonly type: 'standard';
315
331
  readonly factory: (m: string, d: any) => NotImplementedError;
316
332
  };
317
333
  readonly 502: {
318
- readonly type: "standard";
334
+ readonly type: 'standard';
319
335
  readonly factory: (m: string, d: any) => BadGatewayError;
320
336
  };
321
337
  readonly 503: {
322
- readonly type: "retryAfter";
338
+ readonly type: 'retryAfter';
323
339
  readonly factory: (m: string, ra: number) => ServiceUnavailableError;
324
340
  };
325
341
  readonly 504: {
326
- readonly type: "standard";
342
+ readonly type: 'standard';
327
343
  readonly factory: (m: string, d: any) => GatewayTimeoutError;
328
344
  };
329
345
  };
@@ -366,8 +382,8 @@ type ErrorOptions<T extends number> = T extends 405 ? {
366
382
  * createHttpError(429, 'Rate limited', { retryAfter: 60 });
367
383
  * ```
368
384
  */
369
- declare function createHttpError<T extends ValidStatusCode>(statusCode: T, message?: string, options?: ErrorOptions<T>): HttpError;
370
- declare function createHttpError(statusCode: number, message?: string, options?: HttpErrorOptions): HttpError;
385
+ export declare function createHttpError<T extends ValidStatusCode>(statusCode: T, message?: string, options?: ErrorOptions<T>): HttpError;
386
+ export declare function createHttpError(statusCode: number, message?: string, options?: HttpErrorOptions): HttpError;
371
387
  /**
372
388
  * Type-safe error creation utilities with descriptive method names.
373
389
  * Provides a fluent API for creating specific HTTP errors.
@@ -379,7 +395,7 @@ declare function createHttpError(statusCode: number, message?: string, options?:
379
395
  * createError.methodNotAllowed('DELETE not supported', ['GET', 'POST']);
380
396
  * ```
381
397
  */
382
- declare const createError: {
398
+ export declare const createError: {
383
399
  readonly badRequest: (message?: string, details?: any) => BadRequestError;
384
400
  readonly unauthorized: (message?: string, details?: any) => UnauthorizedError;
385
401
  readonly forbidden: (message?: string, details?: any) => ForbiddenError;
@@ -412,7 +428,7 @@ declare const createError: {
412
428
  * }
413
429
  * ```
414
430
  */
415
- declare function isHttpError(error: unknown): error is HttpError;
431
+ export declare function isHttpError(error: unknown): error is HttpError;
416
432
  /**
417
433
  * Type guard to check if an error is a client error (4xx status code).
418
434
  *
@@ -426,7 +442,7 @@ declare function isHttpError(error: unknown): error is HttpError;
426
442
  * }
427
443
  * ```
428
444
  */
429
- declare function isClientError(error: unknown): error is HttpError;
445
+ export declare function isClientError(error: unknown): error is HttpError;
430
446
  /**
431
447
  * Type guard to check if an error is a server error (5xx status code).
432
448
  *
@@ -440,10 +456,16 @@ declare function isClientError(error: unknown): error is HttpError;
440
456
  * }
441
457
  * ```
442
458
  */
443
- declare function isServerError(error: unknown): error is HttpError;
459
+ export declare function isServerError(error: unknown): error is HttpError;
444
460
  /**
445
461
  * Wraps an unknown error into an HttpError.
446
- * If the error is already an HttpError, returns it unchanged.
462
+ *
463
+ * One rule: an error that is an HttpError ({@link isHttpError}) passes
464
+ * through unchanged, its status and headers with it; anything else — however
465
+ * it is shaped, a `statusCode` field included — becomes `statusCode` (500 by
466
+ * default). An error that should answer with its own status extends
467
+ * HttpError, so the decision is made where the error is defined rather than
468
+ * guessed from its fields here.
447
469
  *
448
470
  * @param error - The error to wrap
449
471
  * @param statusCode - The HTTP status code to use (defaults to 500)
@@ -459,26 +481,27 @@ declare function isServerError(error: unknown): error is HttpError;
459
481
  * }
460
482
  * ```
461
483
  */
462
- declare function wrapError(error: unknown, statusCode?: number, message?: string): HttpError;
484
+ export declare function wrapError(error: unknown, statusCode?: number, message?: string): HttpError;
463
485
  /**
464
486
  * Options for creating an HttpError.
465
487
  */
466
- interface HttpErrorOptions {
488
+ export interface HttpErrorOptions {
467
489
  statusMessage?: string;
468
490
  details?: any;
469
491
  code?: string;
492
+ headers?: Readonly<Record<string, string>>;
470
493
  cause?: Error;
471
494
  }
472
495
  /**
473
496
  * Constructor type for HttpError classes.
474
497
  * Useful for factory patterns and dependency injection.
475
498
  */
476
- type HttpErrorConstructor = new (message?: string, options?: HttpErrorOptions) => HttpError;
499
+ export type HttpErrorConstructor = new (message?: string, options?: HttpErrorOptions) => HttpError;
477
500
  /**
478
501
  * HTTP status code enum for type-safe status code usage.
479
502
  * Includes common 2xx, 3xx, 4xx, and 5xx status codes.
480
503
  */
481
- declare enum HttpStatusCode {
504
+ export declare enum HttpStatusCode {
482
505
  OK = 200,
483
506
  CREATED = 201,
484
507
  ACCEPTED = 202,
@@ -501,7 +524,7 @@ declare enum HttpStatusCode {
501
524
  NOT_IMPLEMENTED = 501,
502
525
  BAD_GATEWAY = 502,
503
526
  SERVICE_UNAVAILABLE = 503,
504
- GATEWAY_TIMEOUT = 504,
527
+ GATEWAY_TIMEOUT = 504
505
528
  }
506
529
  /**
507
530
  * Namespace containing all HTTP error classes.
@@ -513,7 +536,7 @@ declare enum HttpStatusCode {
513
536
  * throw new HttpErrors.NotFoundError('Resource not found');
514
537
  * ```
515
538
  */
516
- declare const HttpErrors: {
539
+ export declare const HttpErrors: {
517
540
  HttpError: typeof HttpError;
518
541
  BadRequestError: typeof BadRequestError;
519
542
  UnauthorizedError: typeof UnauthorizedError;
@@ -530,5 +553,4 @@ declare const HttpErrors: {
530
553
  GatewayTimeoutError: typeof GatewayTimeoutError;
531
554
  };
532
555
  //#endregion
533
- export { BadGatewayError, BadRequestError, ConflictError, ForbiddenError, GatewayTimeoutError, HttpError, HttpErrorConstructor, HttpErrorOptions, HttpErrors, HttpStatusCode, InternalServerError, MethodNotAllowedError, NotFoundError, NotImplementedError, ServiceUnavailableError, TooManyRequestsError, UnauthorizedError, UnprocessableEntityError, createError, createHttpError, isClientError, isHttpError, isServerError, wrapError };
534
556
  //# sourceMappingURL=index.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"sourcesContent":[],"mappings":";;AAeA;;;;AAAoC;AA4HpC;AAiBA;AAiBA;AAiBA;AAiBA;AAuBA;AAoBA;AAuBa,cAlQA,SAAA,SAAkB,KAAA,CAkQW;EAyB7B;EAiBA,SAAA,UAAA,EAAA,MAAoB;EAiBpB;EAiBA,SAAA,aAAA,EAAA,MAAwB;EAuBxB;EA+BP,SAAA,WAqDI,GAAA,IAAA;EAAA;EAAA,SAlDmB,OAAA,CAAA,EAAA,GAAA;EAAA;EAIA,SAIA,IAAA,CAAA,EAAA,MAAA;EAAA;;;;;;;;;;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jC;EAA2B;EAAe,QAAQ;IACpD;EACG;EAAwB;EAAe,QAAQ;IACjD;EACG;EAAqB;EAAe,QAAQ;;EAC5C;EAAe;EAAe,QAAQ;;;;;;;;;;;;;;;;;;;;;wBAoC7B,gBAAgB,UAAU,iBACzC,YAAY,GACZ,kBACA,UAAU,aAAa,KACrB;wBACa,gBACf,oBACA,kBACA,UAAU,mBACR;;;;;;;;;;;;qBA4BU;WACZ,aAAU,kBAAmB,kBAAe;WAG5C,eAAY,kBAAmB,kBAAe;WAG9C,YAAS,kBAAmB,kBAAe;WAG3C,WAAQ,kBAAmB,kBAAe;WAG1C,mBAAgB,kBAAmB,8BAA2B;WAG9D,WAAQ,kBAAmB,kBAAe;WAG1C,sBAAmB,kBAAmB,2BAAwB;WAG9D,kBAAe,kBAAmB,wBAAqB;WAGvD,sBAAmB,kBAAmB,kBAAe;WAGrD,iBAAc,kBAAmB,kBAAe;WAGhD,aAAU,kBAAmB,kBAAe;WAG5C,qBAAkB,kBAAmB,wBAAqB;WAG1D,iBAAc,kBAAmB,kBAAe;;;;;;;;;;;;;;;;;;;;wBAwBjC,YAAY,iBAAiB,SAAS;;;;;;;;;;;;;;wBAuBtC,cAAc,iBAAiB,SAAS;;;;;;;;;;;;;;wBAmBxC,cAAc,iBAAiB,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;wBAgCxC,UACf,gBACA,qBACA,mBACE;;;;iBAec;EAChB;EACA;EACA;EACA,UAAU,SAAS;EACnB,QAAQ;;;;;;YAOG,4BACX,kBACA,UAAU,qBACN;;;;;oBAMO;EAEX;EACA;EACA;EACA;EAGA;EACA;EACA;EAGA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EAGA;EACA;EACA;EACA;EACA;;;;;;;;;;;;qBAaY"}
package/dist/index.mjs CHANGED
@@ -24,6 +24,13 @@ var HttpError = class extends Error {
24
24
  /** Application-specific error code for client-side handling */
25
25
  code;
26
26
  /**
27
+ * Headers the response that reports this error carries — a 429's
28
+ * `Retry-After`, a 401's `WWW-Authenticate`. Every endpoint adaptor sets
29
+ * them on the error response, so the error says everything the client is
30
+ * told.
31
+ */
32
+ headers;
33
+ /**
27
34
  * Creates a new HttpError instance.
28
35
  *
29
36
  * @param statusCode - The HTTP status code
@@ -32,6 +39,7 @@ var HttpError = class extends Error {
32
39
  * @param options.statusMessage - Override the default status message
33
40
  * @param options.details - Additional error details or context
34
41
  * @param options.code - Application-specific error code
42
+ * @param options.headers - Headers the error response carries
35
43
  * @param options.cause - The underlying error that caused this error (ES2022)
36
44
  */
37
45
  constructor(statusCode, message, options) {
@@ -41,6 +49,7 @@ var HttpError = class extends Error {
41
49
  this.statusMessage = options?.statusMessage || this.getDefaultStatusMessage(statusCode);
42
50
  this.details = options?.details;
43
51
  this.code = options?.code;
52
+ if (options?.headers) this.headers = options.headers;
44
53
  if (options?.cause) this.cause = options.cause;
45
54
  Error.captureStackTrace(this, this.constructor);
46
55
  }
@@ -65,7 +74,7 @@ var HttpError = class extends Error {
65
74
  * @private
66
75
  */
67
76
  getDefaultStatusMessage(statusCode) {
68
- const statusMessages = {
77
+ return {
69
78
  400: "Bad Request",
70
79
  401: "Unauthorized",
71
80
  403: "Forbidden",
@@ -82,8 +91,7 @@ var HttpError = class extends Error {
82
91
  502: "Bad Gateway",
83
92
  503: "Service Unavailable",
84
93
  504: "Gateway Timeout"
85
- };
86
- return statusMessages[statusCode] || "Unknown Error";
94
+ }[statusCode] || "Unknown Error";
87
95
  }
88
96
  /**
89
97
  * Serializes the error to a JSON-compatible object.
@@ -238,12 +246,25 @@ var UnprocessableEntityError = class extends HttpError {
238
246
  * ```
239
247
  */
240
248
  var TooManyRequestsError = class extends HttpError {
249
+ /** Seconds the client should wait before retrying, where known. */
250
+ retryAfter;
241
251
  /**
242
252
  * @param message - Optional error message
243
- * @param retryAfter - Number of seconds the client should wait before retrying
253
+ * @param retryAfter - Number of seconds the client should wait before
254
+ * retrying, sent as the `Retry-After` header
255
+ * @param options.headers - Further headers the response carries, such as
256
+ * a rate limiter's `X-RateLimit-*`
244
257
  */
245
- constructor(message, retryAfter) {
246
- super(429, message, { details: retryAfter ? { retryAfter } : void 0 });
258
+ constructor(message, retryAfter, options) {
259
+ const headers = {
260
+ ...options?.headers,
261
+ ...retryAfter !== void 0 ? { "Retry-After": String(retryAfter) } : {}
262
+ };
263
+ super(429, message, {
264
+ details: retryAfter !== void 0 ? { retryAfter } : void 0,
265
+ ...Object.keys(headers).length > 0 ? { headers } : {}
266
+ });
267
+ if (retryAfter !== void 0) this.retryAfter = retryAfter;
247
268
  }
248
269
  };
249
270
  /**
@@ -481,7 +502,13 @@ function isServerError(error) {
481
502
  }
482
503
  /**
483
504
  * Wraps an unknown error into an HttpError.
484
- * If the error is already an HttpError, returns it unchanged.
505
+ *
506
+ * One rule: an error that is an HttpError ({@link isHttpError}) passes
507
+ * through unchanged, its status and headers with it; anything else — however
508
+ * it is shaped, a `statusCode` field included — becomes `statusCode` (500 by
509
+ * default). An error that should answer with its own status extends
510
+ * HttpError, so the decision is made where the error is defined rather than
511
+ * guessed from its fields here.
485
512
  *
486
513
  * @param error - The error to wrap
487
514
  * @param statusCode - The HTTP status code to use (defaults to 500)
@@ -499,38 +526,37 @@ function isServerError(error) {
499
526
  */
500
527
  function wrapError(error, statusCode = 500, message) {
501
528
  if (isHttpError(error)) return error;
502
- if (error instanceof HttpError) return error;
503
529
  return new HttpError(statusCode, message || "An unknown error occurred", { details: { originalError: error } });
504
530
  }
505
531
  /**
506
532
  * HTTP status code enum for type-safe status code usage.
507
533
  * Includes common 2xx, 3xx, 4xx, and 5xx status codes.
508
534
  */
509
- let HttpStatusCode = /* @__PURE__ */ function(HttpStatusCode$1) {
510
- HttpStatusCode$1[HttpStatusCode$1["OK"] = 200] = "OK";
511
- HttpStatusCode$1[HttpStatusCode$1["CREATED"] = 201] = "CREATED";
512
- HttpStatusCode$1[HttpStatusCode$1["ACCEPTED"] = 202] = "ACCEPTED";
513
- HttpStatusCode$1[HttpStatusCode$1["NO_CONTENT"] = 204] = "NO_CONTENT";
514
- HttpStatusCode$1[HttpStatusCode$1["MOVED_PERMANENTLY"] = 301] = "MOVED_PERMANENTLY";
515
- HttpStatusCode$1[HttpStatusCode$1["FOUND"] = 302] = "FOUND";
516
- HttpStatusCode$1[HttpStatusCode$1["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
517
- HttpStatusCode$1[HttpStatusCode$1["BAD_REQUEST"] = 400] = "BAD_REQUEST";
518
- HttpStatusCode$1[HttpStatusCode$1["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
519
- HttpStatusCode$1[HttpStatusCode$1["FORBIDDEN"] = 403] = "FORBIDDEN";
520
- HttpStatusCode$1[HttpStatusCode$1["NOT_FOUND"] = 404] = "NOT_FOUND";
521
- HttpStatusCode$1[HttpStatusCode$1["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
522
- HttpStatusCode$1[HttpStatusCode$1["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
523
- HttpStatusCode$1[HttpStatusCode$1["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
524
- HttpStatusCode$1[HttpStatusCode$1["CONFLICT"] = 409] = "CONFLICT";
525
- HttpStatusCode$1[HttpStatusCode$1["GONE"] = 410] = "GONE";
526
- HttpStatusCode$1[HttpStatusCode$1["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
527
- HttpStatusCode$1[HttpStatusCode$1["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
528
- HttpStatusCode$1[HttpStatusCode$1["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
529
- HttpStatusCode$1[HttpStatusCode$1["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
530
- HttpStatusCode$1[HttpStatusCode$1["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
531
- HttpStatusCode$1[HttpStatusCode$1["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
532
- HttpStatusCode$1[HttpStatusCode$1["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
533
- return HttpStatusCode$1;
535
+ let HttpStatusCode = /* @__PURE__ */ function(HttpStatusCode) {
536
+ HttpStatusCode[HttpStatusCode["OK"] = 200] = "OK";
537
+ HttpStatusCode[HttpStatusCode["CREATED"] = 201] = "CREATED";
538
+ HttpStatusCode[HttpStatusCode["ACCEPTED"] = 202] = "ACCEPTED";
539
+ HttpStatusCode[HttpStatusCode["NO_CONTENT"] = 204] = "NO_CONTENT";
540
+ HttpStatusCode[HttpStatusCode["MOVED_PERMANENTLY"] = 301] = "MOVED_PERMANENTLY";
541
+ HttpStatusCode[HttpStatusCode["FOUND"] = 302] = "FOUND";
542
+ HttpStatusCode[HttpStatusCode["NOT_MODIFIED"] = 304] = "NOT_MODIFIED";
543
+ HttpStatusCode[HttpStatusCode["BAD_REQUEST"] = 400] = "BAD_REQUEST";
544
+ HttpStatusCode[HttpStatusCode["UNAUTHORIZED"] = 401] = "UNAUTHORIZED";
545
+ HttpStatusCode[HttpStatusCode["FORBIDDEN"] = 403] = "FORBIDDEN";
546
+ HttpStatusCode[HttpStatusCode["NOT_FOUND"] = 404] = "NOT_FOUND";
547
+ HttpStatusCode[HttpStatusCode["METHOD_NOT_ALLOWED"] = 405] = "METHOD_NOT_ALLOWED";
548
+ HttpStatusCode[HttpStatusCode["NOT_ACCEPTABLE"] = 406] = "NOT_ACCEPTABLE";
549
+ HttpStatusCode[HttpStatusCode["REQUEST_TIMEOUT"] = 408] = "REQUEST_TIMEOUT";
550
+ HttpStatusCode[HttpStatusCode["CONFLICT"] = 409] = "CONFLICT";
551
+ HttpStatusCode[HttpStatusCode["GONE"] = 410] = "GONE";
552
+ HttpStatusCode[HttpStatusCode["UNPROCESSABLE_ENTITY"] = 422] = "UNPROCESSABLE_ENTITY";
553
+ HttpStatusCode[HttpStatusCode["TOO_MANY_REQUESTS"] = 429] = "TOO_MANY_REQUESTS";
554
+ HttpStatusCode[HttpStatusCode["INTERNAL_SERVER_ERROR"] = 500] = "INTERNAL_SERVER_ERROR";
555
+ HttpStatusCode[HttpStatusCode["NOT_IMPLEMENTED"] = 501] = "NOT_IMPLEMENTED";
556
+ HttpStatusCode[HttpStatusCode["BAD_GATEWAY"] = 502] = "BAD_GATEWAY";
557
+ HttpStatusCode[HttpStatusCode["SERVICE_UNAVAILABLE"] = 503] = "SERVICE_UNAVAILABLE";
558
+ HttpStatusCode[HttpStatusCode["GATEWAY_TIMEOUT"] = 504] = "GATEWAY_TIMEOUT";
559
+ return HttpStatusCode;
534
560
  }({});
535
561
  /**
536
562
  * Namespace containing all HTTP error classes.
@@ -558,7 +584,7 @@ const HttpErrors = {
558
584
  ServiceUnavailableError,
559
585
  GatewayTimeoutError
560
586
  };
561
-
562
587
  //#endregion
563
588
  export { BadGatewayError, BadRequestError, ConflictError, ForbiddenError, GatewayTimeoutError, HttpError, HttpErrors, HttpStatusCode, InternalServerError, MethodNotAllowedError, NotFoundError, NotImplementedError, ServiceUnavailableError, TooManyRequestsError, UnauthorizedError, UnprocessableEntityError, createError, createHttpError, isClientError, isHttpError, isServerError, wrapError };
589
+
564
590
  //# sourceMappingURL=index.mjs.map