@zthun/webigail-http 1.2.0 → 2.0.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.js ADDED
@@ -0,0 +1,1011 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
+ return value;
6
+ };
7
+ import axios from "axios";
8
+ var ZHttpMethod = /* @__PURE__ */ ((ZHttpMethod2) => {
9
+ ZHttpMethod2["Get"] = "get";
10
+ ZHttpMethod2["Put"] = "put";
11
+ ZHttpMethod2["Post"] = "post";
12
+ ZHttpMethod2["Delete"] = "delete";
13
+ ZHttpMethod2["Patch"] = "patch";
14
+ ZHttpMethod2["Options"] = "options";
15
+ ZHttpMethod2["Head"] = "head";
16
+ return ZHttpMethod2;
17
+ })(ZHttpMethod || {});
18
+ class ZHttpRequestBuilder {
19
+ /**
20
+ * Initializes a new instance of this object.
21
+ */
22
+ constructor() {
23
+ __publicField(this, "_request");
24
+ /**
25
+ * Constructs a get request.
26
+ *
27
+ * @returns
28
+ * This object.
29
+ */
30
+ __publicField(this, "get", this._method.bind(
31
+ this,
32
+ "get"
33
+ /* Get */
34
+ ));
35
+ /**
36
+ * Constructs a post request.
37
+ *
38
+ * @returns
39
+ * This object.
40
+ */
41
+ __publicField(this, "post", this._method.bind(
42
+ this,
43
+ "post"
44
+ /* Post */
45
+ ));
46
+ /**
47
+ * Constructs a put request.
48
+ *
49
+ * @returns
50
+ * This object.
51
+ */
52
+ __publicField(this, "put", this._method.bind(
53
+ this,
54
+ "put"
55
+ /* Put */
56
+ ));
57
+ /**
58
+ * Constructs a delete request.
59
+ *
60
+ * @returns
61
+ * This object.
62
+ */
63
+ __publicField(this, "delete", this._method.bind(
64
+ this,
65
+ "delete"
66
+ /* Delete */
67
+ ));
68
+ /**
69
+ * Constructs a patch request.
70
+ *
71
+ * @returns
72
+ * This object.
73
+ */
74
+ __publicField(this, "patch", this._method.bind(
75
+ this,
76
+ "patch"
77
+ /* Patch */
78
+ ));
79
+ /**
80
+ * Constructs a options request.
81
+ *
82
+ * @returns
83
+ * This object.
84
+ */
85
+ __publicField(this, "options", this._method.bind(
86
+ this,
87
+ "options"
88
+ /* Options */
89
+ ));
90
+ /**
91
+ * Constructs a head request.
92
+ *
93
+ * @returns
94
+ * This object.
95
+ */
96
+ __publicField(this, "head", this._method.bind(
97
+ this,
98
+ "head"
99
+ /* Head */
100
+ ));
101
+ this._request = {
102
+ method: "get",
103
+ url: ""
104
+ };
105
+ }
106
+ /**
107
+ * Sets the method.
108
+ *
109
+ * @param method -
110
+ * The method to set.
111
+ * @param body -
112
+ * The post, put, or patch body.
113
+ *
114
+ * @returns
115
+ * This object.
116
+ */
117
+ _method(method, body) {
118
+ this._request.method = method;
119
+ this._request.body = body;
120
+ if (this._request.body === void 0) {
121
+ delete this._request.body;
122
+ }
123
+ return this;
124
+ }
125
+ /**
126
+ * Sets the url to make the request from.
127
+ *
128
+ * @param url -
129
+ * The url to make the request to.
130
+ *
131
+ * @returns
132
+ * This object.
133
+ */
134
+ url(url) {
135
+ this._request.url = url;
136
+ return this;
137
+ }
138
+ /**
139
+ * Sets the timeout for the url.
140
+ *
141
+ * @param ms -
142
+ * The total number of milliseconds to wait.
143
+ *
144
+ * @returns
145
+ * The object.
146
+ */
147
+ timeout(ms) {
148
+ this._request.timeout = ms;
149
+ return this;
150
+ }
151
+ /**
152
+ * Sets the headers.
153
+ *
154
+ * @param headers -
155
+ * The headers to set.
156
+ *
157
+ * @returns
158
+ * This object.
159
+ */
160
+ headers(headers) {
161
+ this._request.headers = headers;
162
+ return this;
163
+ }
164
+ /**
165
+ * Sets an individual header.
166
+ *
167
+ * @param key -
168
+ * The header key to set.
169
+ * @param value -
170
+ * The value to set.
171
+ *
172
+ * @returns
173
+ * This object.
174
+ */
175
+ header(key, value) {
176
+ this._request.headers = this._request.headers || {};
177
+ if (value == null) {
178
+ delete this._request.headers[key];
179
+ } else {
180
+ this._request.headers[key] = `${value}`;
181
+ }
182
+ return this;
183
+ }
184
+ /**
185
+ * Copies other to this object.
186
+ *
187
+ * @param other -
188
+ * The request to copy.
189
+ *
190
+ * @returns
191
+ * This object.
192
+ */
193
+ copy(other) {
194
+ this._request = JSON.parse(JSON.stringify(other));
195
+ return this;
196
+ }
197
+ /**
198
+ * Returns the constructed request.
199
+ *
200
+ * @returns
201
+ * The constructed request.
202
+ */
203
+ build() {
204
+ return JSON.parse(JSON.stringify(this._request));
205
+ }
206
+ }
207
+ var ZHttpCodeClient = /* @__PURE__ */ ((ZHttpCodeClient2) => {
208
+ ZHttpCodeClient2[ZHttpCodeClient2["BadRequest"] = 400] = "BadRequest";
209
+ ZHttpCodeClient2[ZHttpCodeClient2["Unauthorized"] = 401] = "Unauthorized";
210
+ ZHttpCodeClient2[ZHttpCodeClient2["PaymentRequired"] = 402] = "PaymentRequired";
211
+ ZHttpCodeClient2[ZHttpCodeClient2["Forbidden"] = 403] = "Forbidden";
212
+ ZHttpCodeClient2[ZHttpCodeClient2["NotFound"] = 404] = "NotFound";
213
+ ZHttpCodeClient2[ZHttpCodeClient2["MethodNotAllowed"] = 405] = "MethodNotAllowed";
214
+ ZHttpCodeClient2[ZHttpCodeClient2["NotAcceptable"] = 406] = "NotAcceptable";
215
+ ZHttpCodeClient2[ZHttpCodeClient2["ProxyAuthenticationRequired"] = 407] = "ProxyAuthenticationRequired";
216
+ ZHttpCodeClient2[ZHttpCodeClient2["RequestTimeout"] = 408] = "RequestTimeout";
217
+ ZHttpCodeClient2[ZHttpCodeClient2["Conflict"] = 409] = "Conflict";
218
+ ZHttpCodeClient2[ZHttpCodeClient2["Gone"] = 410] = "Gone";
219
+ ZHttpCodeClient2[ZHttpCodeClient2["LengthRequired"] = 411] = "LengthRequired";
220
+ ZHttpCodeClient2[ZHttpCodeClient2["PreconditionFailed"] = 412] = "PreconditionFailed";
221
+ ZHttpCodeClient2[ZHttpCodeClient2["PayloadTooLarge"] = 413] = "PayloadTooLarge";
222
+ ZHttpCodeClient2[ZHttpCodeClient2["URITooLong"] = 414] = "URITooLong";
223
+ ZHttpCodeClient2[ZHttpCodeClient2["UnsupportedMediaType"] = 415] = "UnsupportedMediaType";
224
+ ZHttpCodeClient2[ZHttpCodeClient2["RangeNotSatisfiable"] = 416] = "RangeNotSatisfiable";
225
+ ZHttpCodeClient2[ZHttpCodeClient2["ExpectationFailed"] = 417] = "ExpectationFailed";
226
+ ZHttpCodeClient2[ZHttpCodeClient2["ImATeapot"] = 418] = "ImATeapot";
227
+ ZHttpCodeClient2[ZHttpCodeClient2["MisdirectedRequest"] = 421] = "MisdirectedRequest";
228
+ ZHttpCodeClient2[ZHttpCodeClient2["UnProcessableEntity"] = 422] = "UnProcessableEntity";
229
+ ZHttpCodeClient2[ZHttpCodeClient2["Locked"] = 423] = "Locked";
230
+ ZHttpCodeClient2[ZHttpCodeClient2["FailedDependency"] = 424] = "FailedDependency";
231
+ ZHttpCodeClient2[ZHttpCodeClient2["UpgradeRequired"] = 426] = "UpgradeRequired";
232
+ ZHttpCodeClient2[ZHttpCodeClient2["PreconditionRequired"] = 428] = "PreconditionRequired";
233
+ ZHttpCodeClient2[ZHttpCodeClient2["TooManyRequests"] = 429] = "TooManyRequests";
234
+ ZHttpCodeClient2[ZHttpCodeClient2["RequestHeaderFieldsTooLarge"] = 431] = "RequestHeaderFieldsTooLarge";
235
+ ZHttpCodeClient2[ZHttpCodeClient2["UnavailableForLegalReasons"] = 451] = "UnavailableForLegalReasons";
236
+ return ZHttpCodeClient2;
237
+ })(ZHttpCodeClient || {});
238
+ const ZHttpCodeClientNames = {
239
+ [
240
+ 400
241
+ /* BadRequest */
242
+ ]: "Bad Request",
243
+ [
244
+ 401
245
+ /* Unauthorized */
246
+ ]: "Unauthorized",
247
+ [
248
+ 402
249
+ /* PaymentRequired */
250
+ ]: "Payment Required",
251
+ [
252
+ 403
253
+ /* Forbidden */
254
+ ]: "Forbidden",
255
+ [
256
+ 404
257
+ /* NotFound */
258
+ ]: "Not Found",
259
+ [
260
+ 405
261
+ /* MethodNotAllowed */
262
+ ]: "Method not Allowed",
263
+ [
264
+ 406
265
+ /* NotAcceptable */
266
+ ]: "Not Acceptable",
267
+ [
268
+ 407
269
+ /* ProxyAuthenticationRequired */
270
+ ]: "Proxy Authentication Required",
271
+ [
272
+ 408
273
+ /* RequestTimeout */
274
+ ]: "Request Timeout",
275
+ [
276
+ 409
277
+ /* Conflict */
278
+ ]: "Conflict",
279
+ [
280
+ 410
281
+ /* Gone */
282
+ ]: "Gone",
283
+ [
284
+ 411
285
+ /* LengthRequired */
286
+ ]: "Length Required",
287
+ [
288
+ 412
289
+ /* PreconditionFailed */
290
+ ]: "Precondition Failed",
291
+ [
292
+ 413
293
+ /* PayloadTooLarge */
294
+ ]: "Payload Too Large",
295
+ [
296
+ 414
297
+ /* URITooLong */
298
+ ]: "URI Too Long",
299
+ [
300
+ 415
301
+ /* UnsupportedMediaType */
302
+ ]: "Unsupported Media Type",
303
+ [
304
+ 416
305
+ /* RangeNotSatisfiable */
306
+ ]: "Range Not Satisfiable",
307
+ [
308
+ 417
309
+ /* ExpectationFailed */
310
+ ]: "Expectation Failed",
311
+ [
312
+ 418
313
+ /* ImATeapot */
314
+ ]: "I am a Teapot",
315
+ [
316
+ 421
317
+ /* MisdirectedRequest */
318
+ ]: "Misdirected Requested",
319
+ [
320
+ 422
321
+ /* UnProcessableEntity */
322
+ ]: "Entity Not Processable",
323
+ [
324
+ 423
325
+ /* Locked */
326
+ ]: "Locked",
327
+ [
328
+ 424
329
+ /* FailedDependency */
330
+ ]: "Failed Dependency",
331
+ [
332
+ 426
333
+ /* UpgradeRequired */
334
+ ]: "Upgrade Required",
335
+ [
336
+ 428
337
+ /* PreconditionRequired */
338
+ ]: "Precondition Required",
339
+ [
340
+ 429
341
+ /* TooManyRequests */
342
+ ]: "Too Many Requests",
343
+ [
344
+ 431
345
+ /* RequestHeaderFieldsTooLarge */
346
+ ]: "Request Header Fields Too Large",
347
+ [
348
+ 451
349
+ /* UnavailableForLegalReasons */
350
+ ]: "Unavailable for Legal Reasons"
351
+ };
352
+ const ZHttpCodeClientDescriptions = {
353
+ [
354
+ 400
355
+ /* BadRequest */
356
+ ]: "A bad request was sent.",
357
+ [
358
+ 401
359
+ /* Unauthorized */
360
+ ]: "You are not authenticated and cannot view this content.",
361
+ [
362
+ 402
363
+ /* PaymentRequired */
364
+ ]: "Payment is required",
365
+ [
366
+ 403
367
+ /* Forbidden */
368
+ ]: "You are not authorized to view this content.",
369
+ [
370
+ 404
371
+ /* NotFound */
372
+ ]: "The resource you are looking for could not be found.",
373
+ [
374
+ 405
375
+ /* MethodNotAllowed */
376
+ ]: "The requested operation was not allowed.",
377
+ [
378
+ 406
379
+ /* NotAcceptable */
380
+ ]: "The requested resource is not capable of generating the content for you.",
381
+ [
382
+ 407
383
+ /* ProxyAuthenticationRequired */
384
+ ]: "You must first authenticate your self with the proxy.",
385
+ [
386
+ 408
387
+ /* RequestTimeout */
388
+ ]: "The server timed out waiting for a request. Please try again.",
389
+ [
390
+ 409
391
+ /* Conflict */
392
+ ]: "There was a conflict with request. Try something else.",
393
+ [
394
+ 410
395
+ /* Gone */
396
+ ]: "The resource you requested is no longer available.",
397
+ [
398
+ 411
399
+ /* LengthRequired */
400
+ ]: "Your request did not specify the length of its content, which is required by the requested resource.",
401
+ [
402
+ 412
403
+ /* PreconditionFailed */
404
+ ]: "The server did not meet the requirements that was required to meet the request.",
405
+ [
406
+ 413
407
+ /* PayloadTooLarge */
408
+ ]: "The request is too large and the server cannot handle it.",
409
+ [
410
+ 414
411
+ /* URITooLong */
412
+ ]: "The URI provided was too long for the server to process.",
413
+ [
414
+ 415
415
+ /* UnsupportedMediaType */
416
+ ]: "The media type requested is not supported by the server.",
417
+ [
418
+ 416
419
+ /* RangeNotSatisfiable */
420
+ ]: "A portion of the file was requested by the server cannot supply said portion.",
421
+ [
422
+ 417
423
+ /* ExpectationFailed */
424
+ ]: "The server cannot meet the requirements of the expectation made of it.",
425
+ [
426
+ 418
427
+ /* ImATeapot */
428
+ ]: "Short and stout. Here is my handle, here is my spout. When I get all steamed up, hear me shout. Tip me over and pour me out!",
429
+ [
430
+ 421
431
+ /* MisdirectedRequest */
432
+ ]: "The request was directed at the server, but the server cannot produce a response.",
433
+ [
434
+ 422
435
+ /* UnProcessableEntity */
436
+ ]: "The request was well-formed but was unable to be followed due to semantic errors.",
437
+ [
438
+ 423
439
+ /* Locked */
440
+ ]: "The resource that is being accessed is locked.",
441
+ [
442
+ 424
443
+ /* FailedDependency */
444
+ ]: "The request failed because it depended on another request and that request failed.",
445
+ [
446
+ 426
447
+ /* UpgradeRequired */
448
+ ]: "The client needs to switch to a different protocol.",
449
+ [
450
+ 428
451
+ /* PreconditionRequired */
452
+ ]: "The origin server requires the request to be conditional.",
453
+ [
454
+ 429
455
+ /* TooManyRequests */
456
+ ]: "The user has sent too many requests in a given amount of time.",
457
+ [
458
+ 431
459
+ /* RequestHeaderFieldsTooLarge */
460
+ ]: "The request cannot be processed because the collective header fields are too large.",
461
+ [
462
+ 451
463
+ /* UnavailableForLegalReasons */
464
+ ]: "Call your lawyer!"
465
+ };
466
+ var ZHttpCodeInformationalResponse = /* @__PURE__ */ ((ZHttpCodeInformationalResponse2) => {
467
+ ZHttpCodeInformationalResponse2[ZHttpCodeInformationalResponse2["Continue"] = 100] = "Continue";
468
+ ZHttpCodeInformationalResponse2[ZHttpCodeInformationalResponse2["SwitchingProtocols"] = 101] = "SwitchingProtocols";
469
+ ZHttpCodeInformationalResponse2[ZHttpCodeInformationalResponse2["Processing"] = 102] = "Processing";
470
+ ZHttpCodeInformationalResponse2[ZHttpCodeInformationalResponse2["EarlyHints"] = 103] = "EarlyHints";
471
+ return ZHttpCodeInformationalResponse2;
472
+ })(ZHttpCodeInformationalResponse || {});
473
+ const ZHttpCodeInformationalResponseNames = {
474
+ [
475
+ 100
476
+ /* Continue */
477
+ ]: "Continue",
478
+ [
479
+ 101
480
+ /* SwitchingProtocols */
481
+ ]: "Switching Protocols",
482
+ [
483
+ 102
484
+ /* Processing */
485
+ ]: "Processing",
486
+ [
487
+ 103
488
+ /* EarlyHints */
489
+ ]: "Early Hints"
490
+ };
491
+ const ZHttpCodeInformationalResponseDescriptions = {
492
+ [
493
+ 100
494
+ /* Continue */
495
+ ]: "The client should continue to send the request body.",
496
+ [
497
+ 101
498
+ /* SwitchingProtocols */
499
+ ]: "The requestor has asked the server to switch protocols and the server has agreed to do so.",
500
+ [
501
+ 102
502
+ /* Processing */
503
+ ]: "The server has received and is processing the request, but a response is not available yet.",
504
+ [
505
+ 103
506
+ /* EarlyHints */
507
+ ]: "There are some early response headers available for you before the final message."
508
+ };
509
+ var ZHttpCodeRedirection = /* @__PURE__ */ ((ZHttpCodeRedirection2) => {
510
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["MultipleChoices"] = 300] = "MultipleChoices";
511
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["MovedPermanently"] = 301] = "MovedPermanently";
512
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["Found"] = 302] = "Found";
513
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["SeeOther"] = 303] = "SeeOther";
514
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["NotModified"] = 304] = "NotModified";
515
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["UseProxy"] = 305] = "UseProxy";
516
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["SwitchProxy"] = 306] = "SwitchProxy";
517
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["TemporaryRedirect"] = 307] = "TemporaryRedirect";
518
+ ZHttpCodeRedirection2[ZHttpCodeRedirection2["PermanentRedirect"] = 308] = "PermanentRedirect";
519
+ return ZHttpCodeRedirection2;
520
+ })(ZHttpCodeRedirection || {});
521
+ const ZHttpCodeRedirectionNames = {
522
+ [
523
+ 300
524
+ /* MultipleChoices */
525
+ ]: "Multiple Choices",
526
+ [
527
+ 301
528
+ /* MovedPermanently */
529
+ ]: "Moved Permanently",
530
+ [
531
+ 302
532
+ /* Found */
533
+ ]: "Found",
534
+ [
535
+ 303
536
+ /* SeeOther */
537
+ ]: "See Other",
538
+ [
539
+ 304
540
+ /* NotModified */
541
+ ]: "Not Modified",
542
+ [
543
+ 305
544
+ /* UseProxy */
545
+ ]: "Use Proxy",
546
+ [
547
+ 306
548
+ /* SwitchProxy */
549
+ ]: "Switch Proxy",
550
+ [
551
+ 307
552
+ /* TemporaryRedirect */
553
+ ]: "Temporary Redirect",
554
+ [
555
+ 308
556
+ /* PermanentRedirect */
557
+ ]: "Permanent Redirect"
558
+ };
559
+ const ZHttpCodeRedirectionDescriptions = {
560
+ [
561
+ 300
562
+ /* MultipleChoices */
563
+ ]: "Indicates multiple options for the resource from which the client may choose.",
564
+ [
565
+ 301
566
+ /* MovedPermanently */
567
+ ]: "This and all future requests should be directed to the given URI.",
568
+ [
569
+ 302
570
+ /* Found */
571
+ ]: "Tells the client to look at another url",
572
+ [
573
+ 303
574
+ /* SeeOther */
575
+ ]: "The response to the request can be found under another URI using the GET method.",
576
+ [
577
+ 304
578
+ /* NotModified */
579
+ ]: "Indicates that the resource has not been modified since the version specified by the request headers If-Modified-Since or If-None-Match.",
580
+ [
581
+ 305
582
+ /* UseProxy */
583
+ ]: "The requested resource is available only through a proxy, the address for which is provided in the response.",
584
+ [
585
+ 306
586
+ /* SwitchProxy */
587
+ ]: 'No longer used. Originally meant "Subsequent requests should use the specified proxy.',
588
+ [
589
+ 307
590
+ /* TemporaryRedirect */
591
+ ]: "In this case, the request should be repeated with another URI; however, future requests should still use the original URI.",
592
+ [
593
+ 308
594
+ /* PermanentRedirect */
595
+ ]: "The request and all future requests should be repeated using another URI."
596
+ };
597
+ var ZHttpCodeServer = /* @__PURE__ */ ((ZHttpCodeServer2) => {
598
+ ZHttpCodeServer2[ZHttpCodeServer2["InternalServerError"] = 500] = "InternalServerError";
599
+ ZHttpCodeServer2[ZHttpCodeServer2["NotImplemented"] = 501] = "NotImplemented";
600
+ ZHttpCodeServer2[ZHttpCodeServer2["BadGateway"] = 502] = "BadGateway";
601
+ ZHttpCodeServer2[ZHttpCodeServer2["ServiceUnavailable"] = 503] = "ServiceUnavailable";
602
+ ZHttpCodeServer2[ZHttpCodeServer2["GatewayTimeout"] = 504] = "GatewayTimeout";
603
+ ZHttpCodeServer2[ZHttpCodeServer2["HttpVersionNotSupported"] = 505] = "HttpVersionNotSupported";
604
+ ZHttpCodeServer2[ZHttpCodeServer2["VariantAlsoNegotiates"] = 506] = "VariantAlsoNegotiates";
605
+ ZHttpCodeServer2[ZHttpCodeServer2["InsufficientStorage"] = 507] = "InsufficientStorage";
606
+ ZHttpCodeServer2[ZHttpCodeServer2["LoopDetected"] = 508] = "LoopDetected";
607
+ ZHttpCodeServer2[ZHttpCodeServer2["NotExtended"] = 510] = "NotExtended";
608
+ ZHttpCodeServer2[ZHttpCodeServer2["NetworkAuthenticationRequired"] = 511] = "NetworkAuthenticationRequired";
609
+ return ZHttpCodeServer2;
610
+ })(ZHttpCodeServer || {});
611
+ const ZHttpCodeServerNames = {
612
+ [
613
+ 500
614
+ /* InternalServerError */
615
+ ]: "Internal Server Error",
616
+ [
617
+ 501
618
+ /* NotImplemented */
619
+ ]: "Not Implemented",
620
+ [
621
+ 502
622
+ /* BadGateway */
623
+ ]: "Bad Gateway",
624
+ [
625
+ 503
626
+ /* ServiceUnavailable */
627
+ ]: "Service Unavailable",
628
+ [
629
+ 504
630
+ /* GatewayTimeout */
631
+ ]: "Gateway Timeout",
632
+ [
633
+ 505
634
+ /* HttpVersionNotSupported */
635
+ ]: "HTTP Version Not Supported",
636
+ [
637
+ 506
638
+ /* VariantAlsoNegotiates */
639
+ ]: "Variant Also Negotiates",
640
+ [
641
+ 507
642
+ /* InsufficientStorage */
643
+ ]: "Insufficient Storage",
644
+ [
645
+ 508
646
+ /* LoopDetected */
647
+ ]: "Loop Detected",
648
+ [
649
+ 510
650
+ /* NotExtended */
651
+ ]: "Not Extended",
652
+ [
653
+ 511
654
+ /* NetworkAuthenticationRequired */
655
+ ]: "Network Authentication Required"
656
+ };
657
+ const ZHttpCodeServerDescriptions = {
658
+ [
659
+ 500
660
+ /* InternalServerError */
661
+ ]: "An unexpected condition was encountered on the server.",
662
+ [
663
+ 501
664
+ /* NotImplemented */
665
+ ]: "The server either does not recognize the request method, or it lacks the ability to fulfil the request. Usually this implies future availability (e.g., a new feature of a web-service API).",
666
+ [
667
+ 502
668
+ /* BadGateway */
669
+ ]: " The server was acting as a gateway or proxy and received an invalid response from the upstream server.",
670
+ [
671
+ 503
672
+ /* ServiceUnavailable */
673
+ ]: "The server is currently unavailable (because it is overloaded or down for maintenance). Generally, this is a temporary state.",
674
+ [
675
+ 504
676
+ /* GatewayTimeout */
677
+ ]: "The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.",
678
+ [
679
+ 505
680
+ /* HttpVersionNotSupported */
681
+ ]: "The server does not support the HTTP protocol version used in the request.",
682
+ [
683
+ 506
684
+ /* VariantAlsoNegotiates */
685
+ ]: " Transparent content negotiation for the request results in a circular reference.",
686
+ [
687
+ 507
688
+ /* InsufficientStorage */
689
+ ]: "The server is unable to store the representation needed to complete the request.",
690
+ [
691
+ 508
692
+ /* LoopDetected */
693
+ ]: "The server detected an infinite loop while processing the request.",
694
+ [
695
+ 510
696
+ /* NotExtended */
697
+ ]: "Further extensions to the request are required for the server to fulfil it.",
698
+ [
699
+ 511
700
+ /* NetworkAuthenticationRequired */
701
+ ]: "The client needs to authenticate to gain network access."
702
+ };
703
+ var ZHttpCodeSuccess = /* @__PURE__ */ ((ZHttpCodeSuccess2) => {
704
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["OK"] = 200] = "OK";
705
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["Created"] = 201] = "Created";
706
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["Accepted"] = 202] = "Accepted";
707
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["NonAuthoritativeInformation"] = 203] = "NonAuthoritativeInformation";
708
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["NoContent"] = 204] = "NoContent";
709
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["ResetContent"] = 205] = "ResetContent";
710
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["PartialContent"] = 206] = "PartialContent";
711
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["MultiStatus"] = 207] = "MultiStatus";
712
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["AlreadyReported"] = 208] = "AlreadyReported";
713
+ ZHttpCodeSuccess2[ZHttpCodeSuccess2["IMUsed"] = 226] = "IMUsed";
714
+ return ZHttpCodeSuccess2;
715
+ })(ZHttpCodeSuccess || {});
716
+ const ZHttpCodeSuccessNames = {
717
+ [
718
+ 200
719
+ /* OK */
720
+ ]: "OK",
721
+ [
722
+ 201
723
+ /* Created */
724
+ ]: "Created",
725
+ [
726
+ 202
727
+ /* Accepted */
728
+ ]: "Accepted",
729
+ [
730
+ 203
731
+ /* NonAuthoritativeInformation */
732
+ ]: "Non-Authoritative Information",
733
+ [
734
+ 204
735
+ /* NoContent */
736
+ ]: "No Content",
737
+ [
738
+ 205
739
+ /* ResetContent */
740
+ ]: "Reset Content",
741
+ [
742
+ 206
743
+ /* PartialContent */
744
+ ]: "Partial Content",
745
+ [
746
+ 207
747
+ /* MultiStatus */
748
+ ]: "Multi Status",
749
+ [
750
+ 208
751
+ /* AlreadyReported */
752
+ ]: "Already Reported",
753
+ [
754
+ 226
755
+ /* IMUsed */
756
+ ]: "IM Used"
757
+ };
758
+ const ZHttpCodeSuccessDescriptions = {
759
+ [
760
+ 200
761
+ /* OK */
762
+ ]: "The request was successful.",
763
+ [
764
+ 201
765
+ /* Created */
766
+ ]: "The request has been fulfilled, resulting in the creation of a new resource.",
767
+ [
768
+ 202
769
+ /* Accepted */
770
+ ]: "The request has been accepted for processing, but the processing has not been completed.",
771
+ [
772
+ 203
773
+ /* NonAuthoritativeInformation */
774
+ ]: "The server is a transforming proxy that received an OK from its origin,but is returning a modified version of the response.",
775
+ [
776
+ 204
777
+ /* NoContent */
778
+ ]: "The server successfully processed the request and is not returning any content.",
779
+ [
780
+ 205
781
+ /* ResetContent */
782
+ ]: "The server successfully processed the request, but is not returning any content. The document view must be refreshed.",
783
+ [
784
+ 206
785
+ /* PartialContent */
786
+ ]: "he server is delivering only part of the resource due to a range header sent by the client.",
787
+ [
788
+ 207
789
+ /* MultiStatus */
790
+ ]: "The message body that follows is by default an XML message and can contain a number of separate response codes, depending on how many sub-requests were made.",
791
+ [
792
+ 208
793
+ /* AlreadyReported */
794
+ ]: "The members of a DAV binding have already been enumerated in a preceding part of the response, and are not being included again.",
795
+ [
796
+ 226
797
+ /* IMUsed */
798
+ ]: "The server has fulfilled a request for the resource, and the response is a representation of the result of one or more instance-manipulations applied to the current instance."
799
+ };
800
+ var ZHttpCodeCategory = /* @__PURE__ */ ((ZHttpCodeCategory2) => {
801
+ ZHttpCodeCategory2["InformationalResponse"] = "Informational Response";
802
+ ZHttpCodeCategory2["Success"] = "Success";
803
+ ZHttpCodeCategory2["Redirection"] = "Redirection";
804
+ ZHttpCodeCategory2["Client"] = "Client Error";
805
+ ZHttpCodeCategory2["Server"] = "Server Error";
806
+ return ZHttpCodeCategory2;
807
+ })(ZHttpCodeCategory || {});
808
+ var ZHttpCodeSeverity = /* @__PURE__ */ ((ZHttpCodeSeverity2) => {
809
+ ZHttpCodeSeverity2["Info"] = "info";
810
+ ZHttpCodeSeverity2["Success"] = "success";
811
+ ZHttpCodeSeverity2["Warning"] = "warning";
812
+ ZHttpCodeSeverity2["Error"] = "error";
813
+ return ZHttpCodeSeverity2;
814
+ })(ZHttpCodeSeverity || {});
815
+ function getHttpCodeName(code) {
816
+ return ZHttpCodeInformationalResponseNames[code] || ZHttpCodeSuccessNames[code] || ZHttpCodeRedirectionNames[code] || ZHttpCodeClientNames[code] || ZHttpCodeServerNames[code];
817
+ }
818
+ function getHttpCodeDescription(code) {
819
+ return ZHttpCodeInformationalResponseDescriptions[code] || ZHttpCodeSuccessDescriptions[code] || ZHttpCodeRedirectionDescriptions[code] || ZHttpCodeClientDescriptions[code] || ZHttpCodeServerDescriptions[code];
820
+ }
821
+ function getHttpCodeSeverity(code) {
822
+ if (code >= 200 && code < 300) {
823
+ return "success";
824
+ }
825
+ if (code >= 400 && code < 500) {
826
+ return "warning";
827
+ }
828
+ if (code >= 500) {
829
+ return "error";
830
+ }
831
+ return "info";
832
+ }
833
+ function getHttpCodeCategory(code) {
834
+ if (code >= 100 && code < 200) {
835
+ return "Informational Response";
836
+ }
837
+ if (code >= 200 && code < 300) {
838
+ return "Success";
839
+ }
840
+ if (code >= 300 && code < 400) {
841
+ return "Redirection";
842
+ }
843
+ if (code >= 400 && code < 500) {
844
+ return "Client Error";
845
+ }
846
+ return "Server Error";
847
+ }
848
+ class ZHttpResultBuilder {
849
+ /**
850
+ * Initializes a new instance of this object.
851
+ *
852
+ * @param data -
853
+ * The data result.
854
+ */
855
+ constructor(data) {
856
+ __publicField(this, "_result");
857
+ this._result = {
858
+ status: ZHttpCodeSuccess.OK,
859
+ headers: {},
860
+ data
861
+ };
862
+ }
863
+ /**
864
+ * Sets the data.
865
+ *
866
+ * @param data -
867
+ * The data to set.
868
+ *
869
+ * @returns
870
+ * This object.
871
+ */
872
+ data(data) {
873
+ this._result.data = data;
874
+ return this;
875
+ }
876
+ /**
877
+ * Sets the status code and the english description.
878
+ *
879
+ * @param code -
880
+ * The code to set.
881
+ *
882
+ * @returns
883
+ * This object.
884
+ */
885
+ status(code) {
886
+ this._result.status = code;
887
+ return this;
888
+ }
889
+ /**
890
+ * Sets the return headers.
891
+ *
892
+ * @param headers -
893
+ * The headers to set.
894
+ *
895
+ * @returns
896
+ * This object.
897
+ */
898
+ headers(headers) {
899
+ this._result.headers = headers;
900
+ return this;
901
+ }
902
+ /**
903
+ * Returns the built up result.
904
+ *
905
+ * @returns
906
+ * A shallow copy of the built up result.
907
+ */
908
+ build() {
909
+ return { ...this._result };
910
+ }
911
+ }
912
+ class ZHttpServiceMock {
913
+ constructor() {
914
+ __publicField(this, "_mapping", {});
915
+ }
916
+ /**
917
+ * Sets the result of a given endpoint.
918
+ *
919
+ * @param endpoint -
920
+ * The endpoint to set.
921
+ * @param verb -
922
+ * The endpoint verb to respond to.
923
+ * @param invoke -
924
+ * The result method. If this is falsy, then the endpoint is removed.
925
+ */
926
+ set(endpoint, verb, invoke) {
927
+ this._mapping[endpoint] = this._mapping[endpoint] || {};
928
+ this._mapping[endpoint][verb] = typeof invoke === "function" ? invoke : () => invoke;
929
+ }
930
+ /**
931
+ * Invokes the request given the allowed api implementations.
932
+ *
933
+ * @param req -
934
+ * The request that has been made.
935
+ *
936
+ * @returns
937
+ * A promise that resolves with the given result if the status code is less than 400.
938
+ * Any status code above 400 will result in a rejected promise.
939
+ */
940
+ async request(req) {
941
+ const endpointConfig = this._mapping[req.url];
942
+ const result = endpointConfig == null ? void 0 : endpointConfig[req.method];
943
+ if (result == null) {
944
+ const notFound = new ZHttpResultBuilder(null).status(ZHttpCodeClient.NotFound).build();
945
+ return Promise.reject(notFound);
946
+ }
947
+ const errorThreshold = 400;
948
+ const intermediate = await result(req);
949
+ return +intermediate.status < errorThreshold ? Promise.resolve(intermediate) : Promise.reject(intermediate);
950
+ }
951
+ }
952
+ class ZHttpService {
953
+ /**
954
+ * Invokes the request with a real http service.
955
+ *
956
+ * @param req -
957
+ * The request information to make.
958
+ */
959
+ async request(req) {
960
+ try {
961
+ const res = await axios({
962
+ url: req.url,
963
+ method: req.method,
964
+ data: req.body,
965
+ timeout: req.timeout,
966
+ headers: req.headers
967
+ });
968
+ return new ZHttpResultBuilder(res.data).headers(res.headers).status(res.status).build();
969
+ } catch (e) {
970
+ const error = e;
971
+ let builder = new ZHttpResultBuilder(null).headers({});
972
+ if (error.response) {
973
+ builder = builder.headers(error.response.headers).status(error.response.status).data(error.response.data);
974
+ } else if (error.request) {
975
+ builder = builder.status(ZHttpCodeServer.ServiceUnavailable).data("The target endpoint could not be reached. You may need to try again later.");
976
+ } else {
977
+ builder = builder.status(ZHttpCodeServer.InternalServerError).data(error.message || "An unexpected error occurred.");
978
+ }
979
+ return Promise.reject(builder.build());
980
+ }
981
+ }
982
+ }
983
+ export {
984
+ ZHttpCodeCategory,
985
+ ZHttpCodeClient,
986
+ ZHttpCodeClientDescriptions,
987
+ ZHttpCodeClientNames,
988
+ ZHttpCodeInformationalResponse,
989
+ ZHttpCodeInformationalResponseDescriptions,
990
+ ZHttpCodeInformationalResponseNames,
991
+ ZHttpCodeRedirection,
992
+ ZHttpCodeRedirectionDescriptions,
993
+ ZHttpCodeRedirectionNames,
994
+ ZHttpCodeServer,
995
+ ZHttpCodeServerDescriptions,
996
+ ZHttpCodeServerNames,
997
+ ZHttpCodeSeverity,
998
+ ZHttpCodeSuccess,
999
+ ZHttpCodeSuccessDescriptions,
1000
+ ZHttpCodeSuccessNames,
1001
+ ZHttpMethod,
1002
+ ZHttpRequestBuilder,
1003
+ ZHttpResultBuilder,
1004
+ ZHttpService,
1005
+ ZHttpServiceMock,
1006
+ getHttpCodeCategory,
1007
+ getHttpCodeDescription,
1008
+ getHttpCodeName,
1009
+ getHttpCodeSeverity
1010
+ };
1011
+ //# sourceMappingURL=index.js.map