keq 5.0.0-alpha.23 → 5.0.0-alpha.25

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/dist/context/types/keq-context-options/keq-resolve-with-mode.d.ts +1 -1
  3. package/dist/context/types/keq-context-options/keq-resolve-with-mode.d.ts.map +1 -1
  4. package/dist/exception/http-exceptions/bad-gateway.exception.d.ts +2 -2
  5. package/dist/exception/http-exceptions/bad-gateway.exception.d.ts.map +1 -1
  6. package/dist/exception/http-exceptions/bad-request.exception.d.ts +2 -2
  7. package/dist/exception/http-exceptions/bad-request.exception.d.ts.map +1 -1
  8. package/dist/exception/http-exceptions/conflict.exception.d.ts +2 -2
  9. package/dist/exception/http-exceptions/conflict.exception.d.ts.map +1 -1
  10. package/dist/exception/http-exceptions/content-too-large.exception.d.ts +2 -2
  11. package/dist/exception/http-exceptions/content-too-large.exception.d.ts.map +1 -1
  12. package/dist/exception/http-exceptions/forbidden.exception.d.ts +2 -2
  13. package/dist/exception/http-exceptions/forbidden.exception.d.ts.map +1 -1
  14. package/dist/exception/http-exceptions/gateway-timeout.exception.d.ts +2 -2
  15. package/dist/exception/http-exceptions/gateway-timeout.exception.d.ts.map +1 -1
  16. package/dist/exception/http-exceptions/http-version-not-supported.exception.d.ts +2 -2
  17. package/dist/exception/http-exceptions/http-version-not-supported.exception.d.ts.map +1 -1
  18. package/dist/exception/http-exceptions/im-a-teapot.exception.d.ts +2 -2
  19. package/dist/exception/http-exceptions/im-a-teapot.exception.d.ts.map +1 -1
  20. package/dist/exception/http-exceptions/internal-server-error.exception.d.ts +2 -2
  21. package/dist/exception/http-exceptions/internal-server-error.exception.d.ts.map +1 -1
  22. package/dist/exception/http-exceptions/method-not-allowed.exception.d.ts +2 -2
  23. package/dist/exception/http-exceptions/method-not-allowed.exception.d.ts.map +1 -1
  24. package/dist/exception/http-exceptions/not-acceptable.exception.d.ts +2 -2
  25. package/dist/exception/http-exceptions/not-acceptable.exception.d.ts.map +1 -1
  26. package/dist/exception/http-exceptions/not-founded.exception.d.ts +2 -2
  27. package/dist/exception/http-exceptions/not-founded.exception.d.ts.map +1 -1
  28. package/dist/exception/http-exceptions/not-implemented.exception.d.ts +2 -2
  29. package/dist/exception/http-exceptions/not-implemented.exception.d.ts.map +1 -1
  30. package/dist/exception/http-exceptions/precondition-failed.exception.d.ts +2 -2
  31. package/dist/exception/http-exceptions/precondition-failed.exception.d.ts.map +1 -1
  32. package/dist/exception/http-exceptions/proxy-authentication-required.exception.d.ts +2 -2
  33. package/dist/exception/http-exceptions/proxy-authentication-required.exception.d.ts.map +1 -1
  34. package/dist/exception/http-exceptions/request-timeout.exception.d.ts +2 -2
  35. package/dist/exception/http-exceptions/request-timeout.exception.d.ts.map +1 -1
  36. package/dist/exception/http-exceptions/request.exception.d.ts +7 -2
  37. package/dist/exception/http-exceptions/request.exception.d.ts.map +1 -1
  38. package/dist/exception/http-exceptions/service-unavailable.exception.d.ts +2 -2
  39. package/dist/exception/http-exceptions/service-unavailable.exception.d.ts.map +1 -1
  40. package/dist/exception/http-exceptions/too-many-requests.exception.d.ts +2 -2
  41. package/dist/exception/http-exceptions/too-many-requests.exception.d.ts.map +1 -1
  42. package/dist/exception/http-exceptions/unauthorized.exception.d.ts +2 -2
  43. package/dist/exception/http-exceptions/unauthorized.exception.d.ts.map +1 -1
  44. package/dist/exception/http-exceptions/unsupported-media-type.exception.d.ts +2 -2
  45. package/dist/exception/http-exceptions/unsupported-media-type.exception.d.ts.map +1 -1
  46. package/dist/exception/http-exceptions/uri-too-long.exception.d.ts +2 -2
  47. package/dist/exception/http-exceptions/uri-too-long.exception.d.ts.map +1 -1
  48. package/dist/index.d.ts +2 -2
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +449 -402
  51. package/dist/index.js.map +1 -1
  52. package/dist/index.mjs +449 -402
  53. package/dist/index.mjs.map +1 -1
  54. package/dist/request/core.d.ts.map +1 -1
  55. package/dist/request/keq.d.ts +4 -1
  56. package/dist/request/keq.d.ts.map +1 -1
  57. package/dist/request/request.d.ts +2 -1
  58. package/dist/request/request.d.ts.map +1 -1
  59. package/dist/request/types/index.d.ts +1 -0
  60. package/dist/request/types/index.d.ts.map +1 -1
  61. package/dist/request/types/server-sent-event.d.ts +3 -0
  62. package/dist/request/types/server-sent-event.d.ts.map +1 -0
  63. package/dist/request/utils/index.d.ts +1 -0
  64. package/dist/request/utils/index.d.ts.map +1 -1
  65. package/dist/request/utils/intelligent-parse-response.d.ts.map +1 -1
  66. package/dist/request/utils/resolve-with.d.ts +3 -0
  67. package/dist/request/utils/resolve-with.d.ts.map +1 -0
  68. package/dist/router/index.d.ts +0 -5
  69. package/dist/router/index.d.ts.map +1 -1
  70. package/dist/router/keq-router.d.ts +25 -7
  71. package/dist/router/keq-router.d.ts.map +1 -1
  72. package/package.json +2 -1
package/dist/index.mjs CHANGED
@@ -2,102 +2,228 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
 
5
- // src/context/orchestrator-context.ts
6
- var OrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.orchestrator");
7
- var ExecutorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.executor");
8
- var _a, _b;
9
- _b = OrchestratorProperty, _a = ExecutorProperty;
10
- var KeqOrchestratorContext = class {
11
- constructor(orchestrator, executor) {
12
- __publicField(this, _b);
13
- __publicField(this, _a);
14
- this[OrchestratorProperty] = orchestrator;
15
- this[ExecutorProperty] = executor;
5
+ // src/exception/exception.ts
6
+ import { CustomError } from "ts-custom-error";
7
+ var Exception = class extends CustomError {
8
+ constructor(message) {
9
+ super(message);
10
+ Object.defineProperty(this, "name", { value: "KeqError" });
16
11
  }
17
- get middlewares() {
18
- return this[OrchestratorProperty].executors.map((exe) => exe.context);
12
+ };
13
+
14
+ // src/exception/type.exception.ts
15
+ var TypeException = class extends Exception {
16
+ constructor(msg) {
17
+ super(msg || "Invalid Type");
19
18
  }
20
- // NOTE: For Future
21
- // get executing(): KeqMiddlewareContext {
22
- // const current = this[OrchestratorProperty].current
23
- // const executors = this[OrchestratorProperty].executors
24
- // const executor = executors[current]
25
- // return executor.context
26
- // }
27
- /**
28
- * The middleware context of the current middleware
29
- */
30
- get middleware() {
31
- return this[ExecutorProperty].context;
19
+ };
20
+
21
+ // src/exception/abort.exception.ts
22
+ var AbortException = class _AbortException extends DOMException {
23
+ constructor(msg) {
24
+ super(msg, "AbortError");
25
+ Object.setPrototypeOf(this, _AbortException.prototype);
32
26
  }
33
- fork() {
34
- return this[OrchestratorProperty].fork();
27
+ };
28
+
29
+ // src/exception/timeout.exception.ts
30
+ var TimeoutException = class extends AbortException {
31
+ constructor(msg) {
32
+ super(msg);
35
33
  }
36
- merge(source) {
37
- this[OrchestratorProperty].merge(source);
34
+ };
35
+
36
+ // src/exception/http-exceptions/request.exception.ts
37
+ var RequestException = class extends Exception {
38
+ constructor(statusCode, message, options) {
39
+ super(message);
40
+ __publicField(this, "statusCode");
41
+ __publicField(this, "retry");
42
+ __publicField(this, "response");
43
+ this.statusCode = statusCode;
44
+ this.retry = !(options == null ? void 0 : options.fatal);
45
+ this.response = options == null ? void 0 : options.response;
46
+ Object.defineProperty(this, "name", { value: "RequestException" });
38
47
  }
39
48
  };
40
49
 
41
- // src/context/execution-context.ts
42
- var ContextOrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestrator");
43
- var ContextOrchestrationProperty = /* @__PURE__ */ Symbol("protected context.orchestration");
44
- var _a2, _b2;
45
- _b2 = ContextOrchestratorProperty, _a2 = ContextOrchestrationProperty;
46
- var KeqExecutionContext = class {
47
- constructor(orchestrator, executor) {
48
- __publicField(this, _b2);
49
- __publicField(this, _a2);
50
- this[ContextOrchestratorProperty] = orchestrator;
51
- this[ContextOrchestrationProperty] = new KeqOrchestratorContext(orchestrator, executor);
50
+ // src/exception/http-exceptions/bad-request.exception.ts
51
+ var BadRequestException = class extends RequestException {
52
+ constructor(message = "Bad Request", options) {
53
+ super(400, message, { fatal: true, ...options });
54
+ Object.defineProperty(this, "name", { value: "BadRequestException" });
52
55
  }
53
- get orchestration() {
54
- return this[ContextOrchestrationProperty];
56
+ };
57
+
58
+ // src/exception/http-exceptions/unauthorized.exception.ts
59
+ var UnauthorizedException = class extends RequestException {
60
+ constructor(message = "Unauthorized", options) {
61
+ super(401, message, { fatal: true, ...options });
62
+ Object.defineProperty(this, "name", { value: "UnauthorizedException" });
55
63
  }
56
- /**
57
- * The unique identifier of the request's location in the code
58
- */
59
- get locationId() {
60
- return this[ContextOrchestratorProperty].context.locationId;
64
+ };
65
+
66
+ // src/exception/http-exceptions/forbidden.exception.ts
67
+ var ForbiddenException = class extends RequestException {
68
+ constructor(message = "Forbidden", options) {
69
+ super(403, message, { fatal: true, ...options });
70
+ Object.defineProperty(this, "name", { value: "ForbiddenException" });
61
71
  }
62
- get request() {
63
- return this[ContextOrchestratorProperty].context.request;
72
+ };
73
+
74
+ // src/exception/http-exceptions/not-founded.exception.ts
75
+ var NotFoundedException = class extends RequestException {
76
+ constructor(message = "Not Founded", options) {
77
+ super(404, message, { fatal: true, ...options });
78
+ Object.defineProperty(this, "name", { value: "NotFoundedException" });
64
79
  }
65
- get global() {
66
- return this[ContextOrchestratorProperty].context.global;
80
+ };
81
+
82
+ // src/exception/http-exceptions/method-not-allowed.exception.ts
83
+ var MethodNotAllowedException = class extends RequestException {
84
+ constructor(message = "Method Not Allowed", options) {
85
+ super(405, message, { fatal: true, ...options });
86
+ Object.defineProperty(this, "name", {
87
+ value: "MethodNotAllowedException"
88
+ });
67
89
  }
68
- get emitter() {
69
- return this[ContextOrchestratorProperty].context.emitter;
90
+ };
91
+
92
+ // src/exception/http-exceptions/not-acceptable.exception.ts
93
+ var NotAcceptableException = class extends RequestException {
94
+ constructor(message = "Not Acceptable", options) {
95
+ super(406, message, { fatal: true, ...options });
96
+ Object.defineProperty(this, "name", { value: "NotAcceptableException" });
70
97
  }
71
- get options() {
72
- return this[ContextOrchestratorProperty].context.options;
98
+ };
99
+
100
+ // src/exception/http-exceptions/proxy-authentication-required.exception.ts
101
+ var ProxyAuthenticationRequiredException = class extends RequestException {
102
+ constructor(message = "Proxy Authentication Required", options) {
103
+ super(407, message, { fatal: true, ...options });
104
+ Object.defineProperty(this, "name", {
105
+ value: "ProxyAuthenticationRequiredException"
106
+ });
73
107
  }
74
- // The result get by user if resolveWith is set to 'intelligent' or not set
75
- set output(value) {
76
- if (this.options.resolveWith && this.options.resolveWith !== "intelligent") {
77
- console.warn("The request is configured to resolve with ".concat(this.options.resolveWith, ", so setting context.output maybe no effect."));
78
- }
79
- this[ContextOrchestratorProperty].context.output = value;
108
+ };
109
+
110
+ // src/exception/http-exceptions/request-timeout.exception.ts
111
+ var RequestTimeoutException = class extends RequestException {
112
+ constructor(message = "Request Timeout", options) {
113
+ super(408, message, { fatal: false, ...options });
114
+ Object.defineProperty(this, "name", {
115
+ value: "RequestTimeoutException"
116
+ });
80
117
  }
81
- // The original response
82
- get res() {
83
- return this[ContextOrchestratorProperty].context.res;
118
+ };
119
+
120
+ // src/exception/http-exceptions/conflict.exception.ts
121
+ var ConflictException = class extends RequestException {
122
+ constructor(message = "Conflict", options) {
123
+ super(409, message, { fatal: true, ...options });
124
+ Object.defineProperty(this, "name", { value: "ConflictException" });
84
125
  }
85
- // The original response
86
- set res(value) {
87
- this[ContextOrchestratorProperty].context.res = value;
126
+ };
127
+
128
+ // src/exception/http-exceptions/precondition-failed.exception.ts
129
+ var PreconditionFailedException = class extends RequestException {
130
+ constructor(message = "Precondition Failed", options) {
131
+ super(412, message, { fatal: true, ...options });
132
+ Object.defineProperty(this, "name", {
133
+ value: "PreconditionFailedException"
134
+ });
88
135
  }
89
- // The request response
90
- get response() {
91
- return this[ContextOrchestratorProperty].context.response;
136
+ };
137
+
138
+ // src/exception/http-exceptions/content-too-large.exception.ts
139
+ var ContentTooLargeException = class extends RequestException {
140
+ constructor(message = "Content Too Large", options) {
141
+ super(413, message, { fatal: true, ...options });
142
+ Object.defineProperty(this, "name", {
143
+ value: "ContentTooLargeException"
144
+ });
92
145
  }
93
- // The properties extends by middleware
94
- get data() {
95
- return this[ContextOrchestratorProperty].context.data;
146
+ };
147
+
148
+ // src/exception/http-exceptions/uri-too-long.exception.ts
149
+ var UriTooLongException = class extends RequestException {
150
+ constructor(message = "URI Too Long", options) {
151
+ super(414, message, { fatal: true, ...options });
152
+ Object.defineProperty(this, "name", { value: "UriTooLongException" });
96
153
  }
97
154
  };
98
155
 
99
- // src/context/shared-context.ts
100
- import mitt from "mitt";
156
+ // src/exception/http-exceptions/unsupported-media-type.exception.ts
157
+ var UnsupportedMediaTypeException = class extends RequestException {
158
+ constructor(message = "Unsupported Media Type", options) {
159
+ super(415, message, { fatal: true, ...options });
160
+ Object.defineProperty(this, "name", { value: "UnsupportedMediaTypeException" });
161
+ }
162
+ };
163
+
164
+ // src/exception/http-exceptions/im-a-teapot.exception.ts
165
+ var ImATeapotException = class extends RequestException {
166
+ constructor(message = "I'm a teapot", options) {
167
+ super(418, message, { fatal: true, ...options });
168
+ Object.defineProperty(this, "name", { value: "ImATeapotException" });
169
+ }
170
+ };
171
+
172
+ // src/exception/http-exceptions/too-many-requests.exception.ts
173
+ var TooManyRequestsException = class extends RequestException {
174
+ constructor(message = "Too Many Requests", options) {
175
+ super(429, message, { fatal: false, ...options });
176
+ Object.defineProperty(this, "name", { value: "TooManyRequestsException" });
177
+ }
178
+ };
179
+
180
+ // src/exception/http-exceptions/internal-server-error.exception.ts
181
+ var InternalServerErrorException = class extends RequestException {
182
+ constructor(message = "Internal Server Error", options) {
183
+ super(500, message, { fatal: false, ...options });
184
+ Object.defineProperty(this, "name", {
185
+ value: "InternalServerErrorException"
186
+ });
187
+ }
188
+ };
189
+
190
+ // src/exception/http-exceptions/not-implemented.exception.ts
191
+ var NotImplementedException = class extends RequestException {
192
+ constructor(message = "Not Implemented", options) {
193
+ super(501, message, { fatal: true, ...options });
194
+ Object.defineProperty(this, "name", { value: "NotImplementedException" });
195
+ }
196
+ };
197
+
198
+ // src/exception/http-exceptions/bad-gateway.exception.ts
199
+ var BadGatewayException = class extends RequestException {
200
+ constructor(message = "Bad Gateway", options) {
201
+ super(502, message, { fatal: false, ...options });
202
+ Object.defineProperty(this, "name", { value: "BadGatewayException" });
203
+ }
204
+ };
205
+
206
+ // src/exception/http-exceptions/service-unavailable.exception.ts
207
+ var ServiceUnavailableException = class extends RequestException {
208
+ constructor(message = "Service Unavailable", options) {
209
+ super(503, message, { fatal: false, ...options });
210
+ Object.defineProperty(this, "name", {
211
+ value: "ServiceUnavailableException"
212
+ });
213
+ }
214
+ };
215
+
216
+ // src/exception/http-exceptions/gateway-timeout.exception.ts
217
+ var GatewayTimeoutException = class extends RequestException {
218
+ constructor(message = "Gateway Timeout", options) {
219
+ super(504, message, { fatal: false, ...options });
220
+ Object.defineProperty(this, "name", { value: "GatewayTimeoutException" });
221
+ }
222
+ };
223
+
224
+ // src/utils/base64.ts
225
+ var base64Encode = globalThis.btoa || ((str) => Buffer.from(str).toString("base64"));
226
+ var base64Decode = globalThis.atob || ((str) => Buffer.from(str, "base64").toString("utf8"));
101
227
 
102
228
  // src/validator/validator.ts
103
229
  var Validator = class _Validator {
@@ -212,14 +338,10 @@ function shallowClone(obj) {
212
338
  return obj;
213
339
  }
214
340
 
215
- // src/exception/exception.ts
216
- import { CustomError } from "ts-custom-error";
217
- var Exception = class extends CustomError {
218
- constructor(message) {
219
- super(message);
220
- Object.defineProperty(this, "name", { value: "KeqError" });
221
- }
222
- };
341
+ // src/utils/sleep.ts
342
+ function sleep(ms) {
343
+ return new Promise((resolve) => setTimeout(resolve, ms));
344
+ }
223
345
 
224
346
  // src/request-init/utils/clone-body.ts
225
347
  function cloneBody(obj) {
@@ -335,8 +457,8 @@ function cloneRequestInit(init) {
335
457
 
336
458
  // src/request-init/request-init.ts
337
459
  var AbortControllerProperty = /* @__PURE__ */ Symbol("context.request.abortController");
338
- var _a3;
339
- _a3 = AbortControllerProperty;
460
+ var _a;
461
+ _a = AbortControllerProperty;
340
462
  var KeqRequestInit = class {
341
463
  constructor(options) {
342
464
  __publicField(this, "url");
@@ -352,7 +474,7 @@ var KeqRequestInit = class {
352
474
  __publicField(this, "redirect");
353
475
  __publicField(this, "referrer");
354
476
  __publicField(this, "referrerPolicy");
355
- __publicField(this, _a3, new AbortController());
477
+ __publicField(this, _a, new AbortController());
356
478
  this.url = new URL(options.url.href);
357
479
  this.pathParameters = shallowClone(options.pathParameters);
358
480
  this.method = options.method;
@@ -389,54 +511,151 @@ var KeqRequestInit = class {
389
511
  if (typeof this.body === "object") return "application/json";
390
512
  return void 0;
391
513
  }
392
- toFetchBody(contentType) {
393
- const body = this.body;
394
- if (body === void 0) return;
395
- if (body === null) return null;
396
- if (Validator.isBodyInit(body)) return body;
397
- if (!contentType || contentType === "application/json") {
398
- return JSON.stringify(body);
399
- }
400
- if (contentType === "application/x-www-form-urlencoded") {
401
- if (Array.isArray(body)) {
402
- throw new Exception("request.body is an array, that cannot be serialized as application/x-www-form-urlencoded format");
403
- }
404
- return toUrlSearchParams(body);
405
- }
406
- if (contentType === "multipart/form-data") {
407
- if (Array.isArray(body)) {
408
- throw new Exception("FormData cannot send array");
409
- }
410
- return toFormData(body);
514
+ toFetchBody(contentType) {
515
+ const body = this.body;
516
+ if (body === void 0) return;
517
+ if (body === null) return null;
518
+ if (Validator.isBodyInit(body)) return body;
519
+ if (!contentType || contentType === "application/json") {
520
+ return JSON.stringify(body);
521
+ }
522
+ if (contentType === "application/x-www-form-urlencoded") {
523
+ if (Array.isArray(body)) {
524
+ throw new Exception("request.body is an array, that cannot be serialized as application/x-www-form-urlencoded format");
525
+ }
526
+ return toUrlSearchParams(body);
527
+ }
528
+ if (contentType === "multipart/form-data") {
529
+ if (Array.isArray(body)) {
530
+ throw new Exception("FormData cannot send array");
531
+ }
532
+ return toFormData(body);
533
+ }
534
+ throw new Exception("Cannot auto serialize request.body with Content-Type: ".concat(contentType));
535
+ }
536
+ toFetchArguments() {
537
+ const contentType = this.getContentType();
538
+ const headers = cloneHeaders(this.headers);
539
+ if (contentType) headers.set("Content-Type", contentType);
540
+ const body = this.toFetchBody(contentType);
541
+ if (contentType === "multipart/form-data") {
542
+ headers.delete("Content-Type");
543
+ }
544
+ const requestInit = {
545
+ method: this.method.toUpperCase(),
546
+ headers,
547
+ body,
548
+ cache: this.cache,
549
+ credentials: this.credentials,
550
+ integrity: this.integrity,
551
+ keepalive: this.keepalive,
552
+ mode: this.mode,
553
+ redirect: this.redirect,
554
+ referrer: this.referrer,
555
+ referrerPolicy: this.referrerPolicy,
556
+ signal: this.signal
557
+ };
558
+ return [this.__url__.href, requestInit];
559
+ }
560
+ };
561
+
562
+ // src/context/orchestrator-context.ts
563
+ var OrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.orchestrator");
564
+ var ExecutorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.executor");
565
+ var _a2, _b;
566
+ _b = OrchestratorProperty, _a2 = ExecutorProperty;
567
+ var KeqOrchestratorContext = class {
568
+ constructor(orchestrator, executor) {
569
+ __publicField(this, _b);
570
+ __publicField(this, _a2);
571
+ this[OrchestratorProperty] = orchestrator;
572
+ this[ExecutorProperty] = executor;
573
+ }
574
+ get middlewares() {
575
+ return this[OrchestratorProperty].executors.map((exe) => exe.context);
576
+ }
577
+ // NOTE: For Future
578
+ // get executing(): KeqMiddlewareContext {
579
+ // const current = this[OrchestratorProperty].current
580
+ // const executors = this[OrchestratorProperty].executors
581
+ // const executor = executors[current]
582
+ // return executor.context
583
+ // }
584
+ /**
585
+ * The middleware context of the current middleware
586
+ */
587
+ get middleware() {
588
+ return this[ExecutorProperty].context;
589
+ }
590
+ fork() {
591
+ return this[OrchestratorProperty].fork();
592
+ }
593
+ merge(source) {
594
+ this[OrchestratorProperty].merge(source);
595
+ }
596
+ };
597
+
598
+ // src/context/execution-context.ts
599
+ var ContextOrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestrator");
600
+ var ContextOrchestrationProperty = /* @__PURE__ */ Symbol("protected context.orchestration");
601
+ var _a3, _b2;
602
+ _b2 = ContextOrchestratorProperty, _a3 = ContextOrchestrationProperty;
603
+ var KeqExecutionContext = class {
604
+ constructor(orchestrator, executor) {
605
+ __publicField(this, _b2);
606
+ __publicField(this, _a3);
607
+ this[ContextOrchestratorProperty] = orchestrator;
608
+ this[ContextOrchestrationProperty] = new KeqOrchestratorContext(orchestrator, executor);
609
+ }
610
+ get orchestration() {
611
+ return this[ContextOrchestrationProperty];
612
+ }
613
+ /**
614
+ * The unique identifier of the request's location in the code
615
+ */
616
+ get locationId() {
617
+ return this[ContextOrchestratorProperty].context.locationId;
618
+ }
619
+ get request() {
620
+ return this[ContextOrchestratorProperty].context.request;
621
+ }
622
+ get global() {
623
+ return this[ContextOrchestratorProperty].context.global;
624
+ }
625
+ get emitter() {
626
+ return this[ContextOrchestratorProperty].context.emitter;
627
+ }
628
+ get options() {
629
+ return this[ContextOrchestratorProperty].context.options;
630
+ }
631
+ // The result get by user if resolveWith is set to 'intelligent' or not set
632
+ set output(value) {
633
+ if (this.options.resolveWith && this.options.resolveWith !== "intelligent") {
634
+ console.warn("The request is configured to resolve with ".concat(this.options.resolveWith, ", so setting context.output maybe no effect."));
411
635
  }
412
- throw new Exception("Cannot auto serialize request.body with Content-Type: ".concat(contentType));
636
+ this[ContextOrchestratorProperty].context.output = value;
413
637
  }
414
- toFetchArguments() {
415
- const contentType = this.getContentType();
416
- const headers = cloneHeaders(this.headers);
417
- if (contentType) headers.set("Content-Type", contentType);
418
- const body = this.toFetchBody(contentType);
419
- if (contentType === "multipart/form-data") {
420
- headers.delete("Content-Type");
421
- }
422
- const requestInit = {
423
- method: this.method.toUpperCase(),
424
- headers,
425
- body,
426
- cache: this.cache,
427
- credentials: this.credentials,
428
- integrity: this.integrity,
429
- keepalive: this.keepalive,
430
- mode: this.mode,
431
- redirect: this.redirect,
432
- referrer: this.referrer,
433
- referrerPolicy: this.referrerPolicy,
434
- signal: this.signal
435
- };
436
- return [this.__url__.href, requestInit];
638
+ // The original response
639
+ get res() {
640
+ return this[ContextOrchestratorProperty].context.res;
641
+ }
642
+ // The original response
643
+ set res(value) {
644
+ this[ContextOrchestratorProperty].context.res = value;
645
+ }
646
+ // The request response
647
+ get response() {
648
+ return this[ContextOrchestratorProperty].context.response;
649
+ }
650
+ // The properties extends by middleware
651
+ get data() {
652
+ return this[ContextOrchestratorProperty].context.data;
437
653
  }
438
654
  };
439
655
 
656
+ // src/context/shared-context.ts
657
+ import mitt from "mitt";
658
+
440
659
  // src/context/utils/watch-object.ts
441
660
  function watchObject(obj, listeners) {
442
661
  return new Proxy(obj, {
@@ -657,223 +876,6 @@ var KeqSharedContext = class {
657
876
  }
658
877
  };
659
878
 
660
- // src/exception/type.exception.ts
661
- var TypeException = class extends Exception {
662
- constructor(msg) {
663
- super(msg || "Invalid Type");
664
- }
665
- };
666
-
667
- // src/exception/abort.exception.ts
668
- var AbortException = class _AbortException extends DOMException {
669
- constructor(msg) {
670
- super(msg, "AbortError");
671
- Object.setPrototypeOf(this, _AbortException.prototype);
672
- }
673
- };
674
-
675
- // src/exception/timeout.exception.ts
676
- var TimeoutException = class extends AbortException {
677
- constructor(msg) {
678
- super(msg);
679
- }
680
- };
681
-
682
- // src/exception/http-exceptions/request.exception.ts
683
- var RequestException = class extends Exception {
684
- constructor(statusCode, message, retry = true) {
685
- super(message);
686
- __publicField(this, "statusCode");
687
- __publicField(this, "retry");
688
- this.statusCode = statusCode;
689
- this.retry = retry;
690
- Object.defineProperty(this, "name", { value: "RequestException" });
691
- }
692
- };
693
-
694
- // src/exception/http-exceptions/bad-request.exception.ts
695
- var BadRequestException = class extends RequestException {
696
- constructor(message = "Bad Request", retry = false) {
697
- super(400, message, retry);
698
- Object.defineProperty(this, "name", { value: "BadRequestException" });
699
- }
700
- };
701
-
702
- // src/exception/http-exceptions/unauthorized.exception.ts
703
- var UnauthorizedException = class extends RequestException {
704
- constructor(message = "Unauthorized", retry = false) {
705
- super(401, message, retry);
706
- Object.defineProperty(this, "name", { value: "UnauthorizedException" });
707
- }
708
- };
709
-
710
- // src/exception/http-exceptions/forbidden.exception.ts
711
- var ForbiddenException = class extends RequestException {
712
- constructor(message = "Forbidden", retry = false) {
713
- super(403, message, retry);
714
- Object.defineProperty(this, "name", { value: "ForbiddenException" });
715
- }
716
- };
717
-
718
- // src/exception/http-exceptions/not-founded.exception.ts
719
- var NotFoundedException = class extends RequestException {
720
- constructor(message = "Not Founded", retry = false) {
721
- super(404, message, retry);
722
- Object.defineProperty(this, "name", { value: "NotFoundedException" });
723
- }
724
- };
725
-
726
- // src/exception/http-exceptions/method-not-allowed.exception.ts
727
- var MethodNotAllowedException = class extends RequestException {
728
- constructor(message = "Method Not Allowed", retry = false) {
729
- super(405, message, retry);
730
- Object.defineProperty(this, "name", {
731
- value: "MethodNotAllowedException"
732
- });
733
- }
734
- };
735
-
736
- // src/exception/http-exceptions/not-acceptable.exception.ts
737
- var NotAcceptableException = class extends RequestException {
738
- constructor(message = "Not Acceptable", retry = false) {
739
- super(406, message, retry);
740
- Object.defineProperty(this, "name", { value: "NotAcceptableException" });
741
- }
742
- };
743
-
744
- // src/exception/http-exceptions/proxy-authentication-required.exception.ts
745
- var ProxyAuthenticationRequiredException = class extends RequestException {
746
- constructor(message = "Proxy Authentication Required", retry = false) {
747
- super(407, message, retry);
748
- Object.defineProperty(this, "name", {
749
- value: "ProxyAuthenticationRequiredException"
750
- });
751
- }
752
- };
753
-
754
- // src/exception/http-exceptions/request-timeout.exception.ts
755
- var RequestTimeoutException = class extends RequestException {
756
- constructor(message = "Request Timeout", retry = true) {
757
- super(408, message, retry);
758
- Object.defineProperty(this, "name", {
759
- value: "RequestTimeoutException"
760
- });
761
- }
762
- };
763
-
764
- // src/exception/http-exceptions/conflict.exception.ts
765
- var ConflictException = class extends RequestException {
766
- constructor(message = "Conflict", retry = false) {
767
- super(409, message, retry);
768
- Object.defineProperty(this, "name", { value: "ConflictException" });
769
- }
770
- };
771
-
772
- // src/exception/http-exceptions/precondition-failed.exception.ts
773
- var PreconditionFailedException = class extends RequestException {
774
- constructor(message = "Precondition Failed", retry = false) {
775
- super(412, message, retry);
776
- Object.defineProperty(this, "name", {
777
- value: "PreconditionFailedException"
778
- });
779
- }
780
- };
781
-
782
- // src/exception/http-exceptions/content-too-large.exception.ts
783
- var ContentTooLargeException = class extends RequestException {
784
- constructor(message = "Content Too Large", retry = false) {
785
- super(413, message, retry);
786
- Object.defineProperty(this, "name", {
787
- value: "ContentTooLargeException"
788
- });
789
- }
790
- };
791
-
792
- // src/exception/http-exceptions/uri-too-long.exception.ts
793
- var UriTooLongException = class extends RequestException {
794
- constructor(message = "URI Too Long", retry = false) {
795
- super(414, message, retry);
796
- Object.defineProperty(this, "name", { value: "UriTooLongException" });
797
- }
798
- };
799
-
800
- // src/exception/http-exceptions/unsupported-media-type.exception.ts
801
- var UnsupportedMediaTypeException = class extends RequestException {
802
- constructor(message = "Unsupported Media Type", retry = false) {
803
- super(415, message, retry);
804
- Object.defineProperty(this, "name", { value: "UnsupportedMediaTypeException" });
805
- }
806
- };
807
-
808
- // src/exception/http-exceptions/im-a-teapot.exception.ts
809
- var ImATeapotException = class extends RequestException {
810
- constructor(message = "I'm a teapot", retry = false) {
811
- super(418, message, retry);
812
- Object.defineProperty(this, "name", { value: "ImATeapotException" });
813
- }
814
- };
815
-
816
- // src/exception/http-exceptions/too-many-requests.exception.ts
817
- var TooManyRequestsException = class extends RequestException {
818
- constructor(message = "Too Many Requests", retry = true) {
819
- super(429, message, retry);
820
- Object.defineProperty(this, "name", { value: "TooManyRequestsException" });
821
- }
822
- };
823
-
824
- // src/exception/http-exceptions/internal-server-error.exception.ts
825
- var InternalServerErrorException = class extends RequestException {
826
- constructor(message = "Internal Server Error", retry = true) {
827
- super(500, message, retry);
828
- Object.defineProperty(this, "name", {
829
- value: "InternalServerErrorException"
830
- });
831
- }
832
- };
833
-
834
- // src/exception/http-exceptions/not-implemented.exception.ts
835
- var NotImplementedException = class extends RequestException {
836
- constructor(message = "Not Implemented", retry = false) {
837
- super(501, message, retry);
838
- Object.defineProperty(this, "name", { value: "NotImplementedException" });
839
- }
840
- };
841
-
842
- // src/exception/http-exceptions/bad-gateway.exception.ts
843
- var BadGatewayException = class extends RequestException {
844
- constructor(message = "Bad Gateway", retry = true) {
845
- super(502, message, retry);
846
- Object.defineProperty(this, "name", { value: "BadGatewayException" });
847
- }
848
- };
849
-
850
- // src/exception/http-exceptions/service-unavailable.exception.ts
851
- var ServiceUnavailableException = class extends RequestException {
852
- constructor(message = "Service Unavailable", retry = true) {
853
- super(503, message, retry);
854
- Object.defineProperty(this, "name", {
855
- value: "ServiceUnavailableException"
856
- });
857
- }
858
- };
859
-
860
- // src/exception/http-exceptions/gateway-timeout.exception.ts
861
- var GatewayTimeoutException = class extends RequestException {
862
- constructor(message = "Gateway Timeout", retry = true) {
863
- super(504, message, retry);
864
- Object.defineProperty(this, "name", { value: "GatewayTimeoutException" });
865
- }
866
- };
867
-
868
- // src/utils/base64.ts
869
- var base64Encode = globalThis.btoa || ((str) => Buffer.from(str).toString("base64"));
870
- var base64Decode = globalThis.atob || ((str) => Buffer.from(str, "base64").toString("utf8"));
871
-
872
- // src/utils/sleep.ts
873
- function sleep(ms) {
874
- return new Promise((resolve) => setTimeout(resolve, ms));
875
- }
876
-
877
879
  // src/middleware/utils/get-middleware-name.ts
878
880
  function getMiddlewareName(middleware) {
879
881
  return middleware.__keqMiddlewareName__ || middleware.name || "anonymous";
@@ -1090,6 +1092,22 @@ function getLocationId(depth = 0) {
1090
1092
  return stackLine.trim();
1091
1093
  }
1092
1094
 
1095
+ // src/request/utils/resolve-with.ts
1096
+ import { EventSourceParserStream } from "eventsource-parser/stream";
1097
+ async function resolveWith(response, mode) {
1098
+ if (mode === "response") return response.clone();
1099
+ if (mode === "text") return await response.text();
1100
+ if (mode === "json") return unwrap(await response.json());
1101
+ if (mode === "form-data") return await response.formData();
1102
+ if (mode === "blob") return await response.blob();
1103
+ if (mode === "array-buffer") return await response.arrayBuffer();
1104
+ if (mode === "sse") {
1105
+ if (!response.body) return response.body;
1106
+ return response.clone().body.pipeThrough(new TextDecoderStream()).pipeThrough(new EventSourceParserStream());
1107
+ }
1108
+ return void 0;
1109
+ }
1110
+
1093
1111
  // src/request/utils/intelligent-parse-response.ts
1094
1112
  async function intelligentParseResponse(response) {
1095
1113
  if (!response) return void 0;
@@ -1098,15 +1116,18 @@ async function intelligentParseResponse(response) {
1098
1116
  }
1099
1117
  const contentType = response.headers.get("content-type") || "";
1100
1118
  try {
1101
- if (contentType.includes("application/json")) {
1102
- return unwrap(await response.json());
1119
+ if (contentType.match(/^application\/(.+\+)?json/)) {
1120
+ return resolveWith(response, "json");
1103
1121
  } else if (contentType.includes("multipart/form-data")) {
1104
- return await response.formData();
1122
+ return resolveWith(response, "form-data");
1105
1123
  } else if (contentType.includes("plain/text")) {
1106
- return await response.text();
1124
+ return resolveWith(response, "text");
1125
+ } else if (contentType.includes("text/event-stream")) {
1126
+ return resolveWith(response, "sse");
1107
1127
  }
1108
- } catch (e) {
1109
- console.warn("Failed to auto parse response body", e);
1128
+ } catch (err) {
1129
+ console.warn("Failed to intelligent parse response body: ".concat(response.url), err);
1130
+ throw err;
1110
1131
  }
1111
1132
  return void 0;
1112
1133
  }
@@ -1312,33 +1333,26 @@ var Core = class {
1312
1333
  async end() {
1313
1334
  var _a6;
1314
1335
  const coreContext = await this.run();
1315
- if (coreContext.options.resolveWith === "response") {
1336
+ const resolveWithMode = coreContext.options.resolveWith;
1337
+ if (resolveWithMode === "response") {
1316
1338
  return (_a6 = coreContext.response) == null ? void 0 : _a6.clone();
1317
1339
  }
1318
1340
  const response = coreContext.response;
1319
- if (coreContext.options.resolveWith && coreContext.options.resolveWith !== "intelligent" && !response) {
1341
+ if (!resolveWithMode || resolveWithMode === "intelligent") {
1342
+ const output = coreContext.output;
1343
+ if (output !== void 0) {
1344
+ return output;
1345
+ }
1346
+ return await intelligentParseResponse(response);
1347
+ }
1348
+ if (!response) {
1320
1349
  throw new Exception([
1321
- "Unable to process the response with '".concat(coreContext.options.resolveWith, "'. Possible causes:"),
1350
+ "Unable to process the response with '".concat(resolveWithMode, "'. Possible causes:"),
1322
1351
  "1. The request was never initiated or sent",
1323
1352
  "2. The request failed before a response was received."
1324
1353
  ].join("\n"));
1325
1354
  }
1326
- if (coreContext.options.resolveWith === "text") {
1327
- return await response.text();
1328
- } else if (coreContext.options.resolveWith === "json") {
1329
- return unwrap(await response.json());
1330
- } else if (coreContext.options.resolveWith === "form-data") {
1331
- return await response.formData();
1332
- } else if (coreContext.options.resolveWith === "blob") {
1333
- return await response.blob();
1334
- } else if (coreContext.options.resolveWith === "array-buffer") {
1335
- return await response.arrayBuffer();
1336
- }
1337
- const output = coreContext.output;
1338
- if (output !== void 0) {
1339
- return output;
1340
- }
1341
- return await intelligentParseResponse(response);
1355
+ return await resolveWith(response, resolveWithMode);
1342
1356
  }
1343
1357
  /**
1344
1358
  * Attaches callbacks for the resolution and/or rejection of the Promise.
@@ -1584,11 +1598,11 @@ function keqPathnameRoute(pathname) {
1584
1598
  }
1585
1599
 
1586
1600
  // src/router/keq-router.ts
1587
- var KeqRouter = class {
1601
+ var KeqRouter = class _KeqRouter {
1588
1602
  constructor(middlewares = []) {
1589
1603
  this.middlewares = middlewares;
1590
1604
  }
1591
- buildMiddleware(route, middlewares) {
1605
+ static buildMiddleware(route, middlewares) {
1592
1606
  if (middlewares.length === 0) {
1593
1607
  throw new TypeException("At least one middleware is required to build a route middleware");
1594
1608
  }
@@ -1599,42 +1613,72 @@ var KeqRouter = class {
1599
1613
  };
1600
1614
  return middleware;
1601
1615
  }
1602
- route(route, ...middlewares) {
1603
- const middleware = this.buildMiddleware(route, middlewares);
1604
- middleware.__keqMiddlewareName__ = "route(".concat(route.__keqRouteName__ || route.name, ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1605
- this.middlewares.push(middleware);
1616
+ route(route, middleware, ...additionalMiddlewares) {
1617
+ const mid = _KeqRouter.route(route, middleware, ...additionalMiddlewares);
1618
+ this.middlewares.push(mid);
1606
1619
  return this;
1607
1620
  }
1608
- host(host, ...middlewares) {
1609
- const middleware = this.buildMiddleware(keqHostRoute(host), middlewares);
1610
- middleware.__keqMiddlewareName__ = "host(".concat(JSON.stringify(host), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1611
- this.middlewares.push(middleware);
1621
+ host(host, middleware, ...additionalMiddlewares) {
1622
+ const mid = _KeqRouter.host(host, middleware, ...additionalMiddlewares);
1623
+ this.middlewares.push(mid);
1612
1624
  return this;
1613
1625
  }
1614
- method(method, ...middlewares) {
1615
- const middleware = this.buildMiddleware(keqMethodRoute(method), middlewares);
1616
- middleware.__keqMiddlewareName__ = "method(".concat(JSON.stringify(method), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1617
- this.middlewares.push(middleware);
1626
+ method(method, middleware, ...additionalMiddlewares) {
1627
+ const mid = _KeqRouter.method(method, middleware, ...additionalMiddlewares);
1628
+ this.middlewares.push(mid);
1618
1629
  return this;
1619
1630
  }
1620
- pathname(pathname, ...middlewares) {
1621
- const middleware = this.buildMiddleware(keqPathnameRoute(pathname), middlewares);
1622
- middleware.__keqMiddlewareName__ = "pathname(".concat(JSON.stringify(pathname), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1623
- this.middlewares.push(middleware);
1631
+ pathname(pathname, middleware, ...additionalMiddlewares) {
1632
+ const mid = _KeqRouter.pathname(pathname, middleware, ...additionalMiddlewares);
1633
+ this.middlewares.push(mid);
1624
1634
  return this;
1625
1635
  }
1626
- location(...middlewares) {
1627
- const middleware = this.buildMiddleware(keqLocationRoute(), middlewares);
1628
- middleware.__keqMiddlewareName__ = "location(".concat(middlewares.map(getMiddlewareName).join(", "), ")");
1629
- this.middlewares.push(middleware);
1636
+ location(middleware, ...additionalMiddlewares) {
1637
+ const mid = _KeqRouter.location(middleware, ...additionalMiddlewares);
1638
+ this.middlewares.push(mid);
1630
1639
  return this;
1631
1640
  }
1632
- module(moduleName, ...middlewares) {
1633
- const middleware = this.buildMiddleware(keqModuleRoute(moduleName), middlewares);
1634
- middleware.__keqMiddlewareName__ = "module(".concat(JSON.stringify(moduleName), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1635
- this.middlewares.push(middleware);
1641
+ module(moduleName, middleware, ...additionalMiddlewares) {
1642
+ const mid = _KeqRouter.module(moduleName, middleware, ...additionalMiddlewares);
1643
+ this.middlewares.push(mid);
1636
1644
  return this;
1637
1645
  }
1646
+ static route(route, middleware, ...additionalMiddlewares) {
1647
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1648
+ const mid = _KeqRouter.buildMiddleware(route, list);
1649
+ mid.__keqMiddlewareName__ = "route(".concat(route.__keqRouteName__ || route.name, ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1650
+ return mid;
1651
+ }
1652
+ static host(host, middleware, ...additionalMiddlewares) {
1653
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1654
+ const mid = _KeqRouter.buildMiddleware(keqHostRoute(host), list);
1655
+ mid.__keqMiddlewareName__ = "host(".concat(JSON.stringify(host), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1656
+ return mid;
1657
+ }
1658
+ static method(method, middleware, ...additionalMiddlewares) {
1659
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1660
+ const mid = _KeqRouter.buildMiddleware(keqMethodRoute(method), list);
1661
+ mid.__keqMiddlewareName__ = "method(".concat(JSON.stringify(method), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1662
+ return mid;
1663
+ }
1664
+ static pathname(pathname, middleware, ...additionalMiddlewares) {
1665
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1666
+ const mid = _KeqRouter.buildMiddleware(keqPathnameRoute(pathname), list);
1667
+ mid.__keqMiddlewareName__ = "pathname(".concat(JSON.stringify(pathname), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1668
+ return mid;
1669
+ }
1670
+ static location(middleware, ...additionalMiddlewares) {
1671
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1672
+ const mid = _KeqRouter.buildMiddleware(keqLocationRoute(), list);
1673
+ mid.__keqMiddlewareName__ = "location(".concat(list.map(getMiddlewareName).join(", "), ")");
1674
+ return mid;
1675
+ }
1676
+ static module(moduleName, middleware, ...additionalMiddlewares) {
1677
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1678
+ const mid = _KeqRouter.buildMiddleware(keqModuleRoute(moduleName), list);
1679
+ mid.__keqMiddlewareName__ = "module(".concat(JSON.stringify(moduleName), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1680
+ return mid;
1681
+ }
1638
1682
  };
1639
1683
 
1640
1684
  // src/middlewares/timeout-middleware/index.ts
@@ -1836,6 +1880,14 @@ var KeqRequest = class {
1836
1880
  useRouter() {
1837
1881
  return new KeqRouter(this.preMiddlewares);
1838
1882
  }
1883
+ on(event, listener) {
1884
+ const middleware = async (context, next) => {
1885
+ context.emitter.on(event, listener);
1886
+ await next();
1887
+ };
1888
+ this.use(middleware);
1889
+ return this;
1890
+ }
1839
1891
  };
1840
1892
  var request = new KeqRequest();
1841
1893
 
@@ -1879,11 +1931,6 @@ export {
1879
1931
  composeRoute,
1880
1932
  createProxyResponse,
1881
1933
  createRequest,
1882
- keqHostRoute,
1883
- keqLocationRoute,
1884
- keqMethodRoute,
1885
- keqModuleRoute,
1886
- keqPathnameRoute,
1887
1934
  request
1888
1935
  };
1889
1936
  //# sourceMappingURL=index.mjs.map