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.js CHANGED
@@ -67,111 +67,232 @@ __export(index_exports, {
67
67
  composeRoute: () => composeRoute,
68
68
  createProxyResponse: () => createProxyResponse,
69
69
  createRequest: () => createRequest,
70
- keqHostRoute: () => keqHostRoute,
71
- keqLocationRoute: () => keqLocationRoute,
72
- keqMethodRoute: () => keqMethodRoute,
73
- keqModuleRoute: () => keqModuleRoute,
74
- keqPathnameRoute: () => keqPathnameRoute,
75
70
  request: () => request
76
71
  });
77
72
  module.exports = __toCommonJS(index_exports);
78
73
 
79
- // src/context/orchestrator-context.ts
80
- var OrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.orchestrator");
81
- var ExecutorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.executor");
82
- var _a, _b;
83
- _b = OrchestratorProperty, _a = ExecutorProperty;
84
- var KeqOrchestratorContext = class {
85
- constructor(orchestrator, executor) {
86
- __publicField(this, _b);
87
- __publicField(this, _a);
88
- this[OrchestratorProperty] = orchestrator;
89
- this[ExecutorProperty] = executor;
74
+ // src/exception/exception.ts
75
+ var import_ts_custom_error = require("ts-custom-error");
76
+ var Exception = class extends import_ts_custom_error.CustomError {
77
+ constructor(message) {
78
+ super(message);
79
+ Object.defineProperty(this, "name", { value: "KeqError" });
90
80
  }
91
- get middlewares() {
92
- return this[OrchestratorProperty].executors.map((exe) => exe.context);
81
+ };
82
+
83
+ // src/exception/type.exception.ts
84
+ var TypeException = class extends Exception {
85
+ constructor(msg) {
86
+ super(msg || "Invalid Type");
93
87
  }
94
- // NOTE: For Future
95
- // get executing(): KeqMiddlewareContext {
96
- // const current = this[OrchestratorProperty].current
97
- // const executors = this[OrchestratorProperty].executors
98
- // const executor = executors[current]
99
- // return executor.context
100
- // }
101
- /**
102
- * The middleware context of the current middleware
103
- */
104
- get middleware() {
105
- return this[ExecutorProperty].context;
88
+ };
89
+
90
+ // src/exception/abort.exception.ts
91
+ var AbortException = class _AbortException extends DOMException {
92
+ constructor(msg) {
93
+ super(msg, "AbortError");
94
+ Object.setPrototypeOf(this, _AbortException.prototype);
106
95
  }
107
- fork() {
108
- return this[OrchestratorProperty].fork();
96
+ };
97
+
98
+ // src/exception/timeout.exception.ts
99
+ var TimeoutException = class extends AbortException {
100
+ constructor(msg) {
101
+ super(msg);
109
102
  }
110
- merge(source) {
111
- this[OrchestratorProperty].merge(source);
103
+ };
104
+
105
+ // src/exception/http-exceptions/request.exception.ts
106
+ var RequestException = class extends Exception {
107
+ constructor(statusCode, message, options) {
108
+ super(message);
109
+ __publicField(this, "statusCode");
110
+ __publicField(this, "retry");
111
+ __publicField(this, "response");
112
+ this.statusCode = statusCode;
113
+ this.retry = !(options == null ? void 0 : options.fatal);
114
+ this.response = options == null ? void 0 : options.response;
115
+ Object.defineProperty(this, "name", { value: "RequestException" });
112
116
  }
113
117
  };
114
118
 
115
- // src/context/execution-context.ts
116
- var ContextOrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestrator");
117
- var ContextOrchestrationProperty = /* @__PURE__ */ Symbol("protected context.orchestration");
118
- var _a2, _b2;
119
- _b2 = ContextOrchestratorProperty, _a2 = ContextOrchestrationProperty;
120
- var KeqExecutionContext = class {
121
- constructor(orchestrator, executor) {
122
- __publicField(this, _b2);
123
- __publicField(this, _a2);
124
- this[ContextOrchestratorProperty] = orchestrator;
125
- this[ContextOrchestrationProperty] = new KeqOrchestratorContext(orchestrator, executor);
119
+ // src/exception/http-exceptions/bad-request.exception.ts
120
+ var BadRequestException = class extends RequestException {
121
+ constructor(message = "Bad Request", options) {
122
+ super(400, message, { fatal: true, ...options });
123
+ Object.defineProperty(this, "name", { value: "BadRequestException" });
126
124
  }
127
- get orchestration() {
128
- return this[ContextOrchestrationProperty];
125
+ };
126
+
127
+ // src/exception/http-exceptions/unauthorized.exception.ts
128
+ var UnauthorizedException = class extends RequestException {
129
+ constructor(message = "Unauthorized", options) {
130
+ super(401, message, { fatal: true, ...options });
131
+ Object.defineProperty(this, "name", { value: "UnauthorizedException" });
129
132
  }
130
- /**
131
- * The unique identifier of the request's location in the code
132
- */
133
- get locationId() {
134
- return this[ContextOrchestratorProperty].context.locationId;
133
+ };
134
+
135
+ // src/exception/http-exceptions/forbidden.exception.ts
136
+ var ForbiddenException = class extends RequestException {
137
+ constructor(message = "Forbidden", options) {
138
+ super(403, message, { fatal: true, ...options });
139
+ Object.defineProperty(this, "name", { value: "ForbiddenException" });
135
140
  }
136
- get request() {
137
- return this[ContextOrchestratorProperty].context.request;
141
+ };
142
+
143
+ // src/exception/http-exceptions/not-founded.exception.ts
144
+ var NotFoundedException = class extends RequestException {
145
+ constructor(message = "Not Founded", options) {
146
+ super(404, message, { fatal: true, ...options });
147
+ Object.defineProperty(this, "name", { value: "NotFoundedException" });
138
148
  }
139
- get global() {
140
- return this[ContextOrchestratorProperty].context.global;
149
+ };
150
+
151
+ // src/exception/http-exceptions/method-not-allowed.exception.ts
152
+ var MethodNotAllowedException = class extends RequestException {
153
+ constructor(message = "Method Not Allowed", options) {
154
+ super(405, message, { fatal: true, ...options });
155
+ Object.defineProperty(this, "name", {
156
+ value: "MethodNotAllowedException"
157
+ });
141
158
  }
142
- get emitter() {
143
- return this[ContextOrchestratorProperty].context.emitter;
159
+ };
160
+
161
+ // src/exception/http-exceptions/not-acceptable.exception.ts
162
+ var NotAcceptableException = class extends RequestException {
163
+ constructor(message = "Not Acceptable", options) {
164
+ super(406, message, { fatal: true, ...options });
165
+ Object.defineProperty(this, "name", { value: "NotAcceptableException" });
144
166
  }
145
- get options() {
146
- return this[ContextOrchestratorProperty].context.options;
167
+ };
168
+
169
+ // src/exception/http-exceptions/proxy-authentication-required.exception.ts
170
+ var ProxyAuthenticationRequiredException = class extends RequestException {
171
+ constructor(message = "Proxy Authentication Required", options) {
172
+ super(407, message, { fatal: true, ...options });
173
+ Object.defineProperty(this, "name", {
174
+ value: "ProxyAuthenticationRequiredException"
175
+ });
147
176
  }
148
- // The result get by user if resolveWith is set to 'intelligent' or not set
149
- set output(value) {
150
- if (this.options.resolveWith && this.options.resolveWith !== "intelligent") {
151
- console.warn("The request is configured to resolve with ".concat(this.options.resolveWith, ", so setting context.output maybe no effect."));
152
- }
153
- this[ContextOrchestratorProperty].context.output = value;
177
+ };
178
+
179
+ // src/exception/http-exceptions/request-timeout.exception.ts
180
+ var RequestTimeoutException = class extends RequestException {
181
+ constructor(message = "Request Timeout", options) {
182
+ super(408, message, { fatal: false, ...options });
183
+ Object.defineProperty(this, "name", {
184
+ value: "RequestTimeoutException"
185
+ });
154
186
  }
155
- // The original response
156
- get res() {
157
- return this[ContextOrchestratorProperty].context.res;
187
+ };
188
+
189
+ // src/exception/http-exceptions/conflict.exception.ts
190
+ var ConflictException = class extends RequestException {
191
+ constructor(message = "Conflict", options) {
192
+ super(409, message, { fatal: true, ...options });
193
+ Object.defineProperty(this, "name", { value: "ConflictException" });
158
194
  }
159
- // The original response
160
- set res(value) {
161
- this[ContextOrchestratorProperty].context.res = value;
195
+ };
196
+
197
+ // src/exception/http-exceptions/precondition-failed.exception.ts
198
+ var PreconditionFailedException = class extends RequestException {
199
+ constructor(message = "Precondition Failed", options) {
200
+ super(412, message, { fatal: true, ...options });
201
+ Object.defineProperty(this, "name", {
202
+ value: "PreconditionFailedException"
203
+ });
162
204
  }
163
- // The request response
164
- get response() {
165
- return this[ContextOrchestratorProperty].context.response;
205
+ };
206
+
207
+ // src/exception/http-exceptions/content-too-large.exception.ts
208
+ var ContentTooLargeException = class extends RequestException {
209
+ constructor(message = "Content Too Large", options) {
210
+ super(413, message, { fatal: true, ...options });
211
+ Object.defineProperty(this, "name", {
212
+ value: "ContentTooLargeException"
213
+ });
166
214
  }
167
- // The properties extends by middleware
168
- get data() {
169
- return this[ContextOrchestratorProperty].context.data;
215
+ };
216
+
217
+ // src/exception/http-exceptions/uri-too-long.exception.ts
218
+ var UriTooLongException = class extends RequestException {
219
+ constructor(message = "URI Too Long", options) {
220
+ super(414, message, { fatal: true, ...options });
221
+ Object.defineProperty(this, "name", { value: "UriTooLongException" });
170
222
  }
171
223
  };
172
224
 
173
- // src/context/shared-context.ts
174
- var import_mitt = __toESM(require("mitt"));
225
+ // src/exception/http-exceptions/unsupported-media-type.exception.ts
226
+ var UnsupportedMediaTypeException = class extends RequestException {
227
+ constructor(message = "Unsupported Media Type", options) {
228
+ super(415, message, { fatal: true, ...options });
229
+ Object.defineProperty(this, "name", { value: "UnsupportedMediaTypeException" });
230
+ }
231
+ };
232
+
233
+ // src/exception/http-exceptions/im-a-teapot.exception.ts
234
+ var ImATeapotException = class extends RequestException {
235
+ constructor(message = "I'm a teapot", options) {
236
+ super(418, message, { fatal: true, ...options });
237
+ Object.defineProperty(this, "name", { value: "ImATeapotException" });
238
+ }
239
+ };
240
+
241
+ // src/exception/http-exceptions/too-many-requests.exception.ts
242
+ var TooManyRequestsException = class extends RequestException {
243
+ constructor(message = "Too Many Requests", options) {
244
+ super(429, message, { fatal: false, ...options });
245
+ Object.defineProperty(this, "name", { value: "TooManyRequestsException" });
246
+ }
247
+ };
248
+
249
+ // src/exception/http-exceptions/internal-server-error.exception.ts
250
+ var InternalServerErrorException = class extends RequestException {
251
+ constructor(message = "Internal Server Error", options) {
252
+ super(500, message, { fatal: false, ...options });
253
+ Object.defineProperty(this, "name", {
254
+ value: "InternalServerErrorException"
255
+ });
256
+ }
257
+ };
258
+
259
+ // src/exception/http-exceptions/not-implemented.exception.ts
260
+ var NotImplementedException = class extends RequestException {
261
+ constructor(message = "Not Implemented", options) {
262
+ super(501, message, { fatal: true, ...options });
263
+ Object.defineProperty(this, "name", { value: "NotImplementedException" });
264
+ }
265
+ };
266
+
267
+ // src/exception/http-exceptions/bad-gateway.exception.ts
268
+ var BadGatewayException = class extends RequestException {
269
+ constructor(message = "Bad Gateway", options) {
270
+ super(502, message, { fatal: false, ...options });
271
+ Object.defineProperty(this, "name", { value: "BadGatewayException" });
272
+ }
273
+ };
274
+
275
+ // src/exception/http-exceptions/service-unavailable.exception.ts
276
+ var ServiceUnavailableException = class extends RequestException {
277
+ constructor(message = "Service Unavailable", options) {
278
+ super(503, message, { fatal: false, ...options });
279
+ Object.defineProperty(this, "name", {
280
+ value: "ServiceUnavailableException"
281
+ });
282
+ }
283
+ };
284
+
285
+ // src/exception/http-exceptions/gateway-timeout.exception.ts
286
+ var GatewayTimeoutException = class extends RequestException {
287
+ constructor(message = "Gateway Timeout", options) {
288
+ super(504, message, { fatal: false, ...options });
289
+ Object.defineProperty(this, "name", { value: "GatewayTimeoutException" });
290
+ }
291
+ };
292
+
293
+ // src/utils/base64.ts
294
+ var base64Encode = globalThis.btoa || ((str) => Buffer.from(str).toString("base64"));
295
+ var base64Decode = globalThis.atob || ((str) => Buffer.from(str, "base64").toString("utf8"));
175
296
 
176
297
  // src/validator/validator.ts
177
298
  var Validator = class _Validator {
@@ -286,14 +407,10 @@ function shallowClone(obj) {
286
407
  return obj;
287
408
  }
288
409
 
289
- // src/exception/exception.ts
290
- var import_ts_custom_error = require("ts-custom-error");
291
- var Exception = class extends import_ts_custom_error.CustomError {
292
- constructor(message) {
293
- super(message);
294
- Object.defineProperty(this, "name", { value: "KeqError" });
295
- }
296
- };
410
+ // src/utils/sleep.ts
411
+ function sleep(ms) {
412
+ return new Promise((resolve) => setTimeout(resolve, ms));
413
+ }
297
414
 
298
415
  // src/request-init/utils/clone-body.ts
299
416
  function cloneBody(obj) {
@@ -409,8 +526,8 @@ function cloneRequestInit(init) {
409
526
 
410
527
  // src/request-init/request-init.ts
411
528
  var AbortControllerProperty = /* @__PURE__ */ Symbol("context.request.abortController");
412
- var _a3;
413
- _a3 = AbortControllerProperty;
529
+ var _a;
530
+ _a = AbortControllerProperty;
414
531
  var KeqRequestInit = class {
415
532
  constructor(options) {
416
533
  __publicField(this, "url");
@@ -426,7 +543,7 @@ var KeqRequestInit = class {
426
543
  __publicField(this, "redirect");
427
544
  __publicField(this, "referrer");
428
545
  __publicField(this, "referrerPolicy");
429
- __publicField(this, _a3, new AbortController());
546
+ __publicField(this, _a, new AbortController());
430
547
  this.url = new URL(options.url.href);
431
548
  this.pathParameters = shallowClone(options.pathParameters);
432
549
  this.method = options.method;
@@ -463,54 +580,151 @@ var KeqRequestInit = class {
463
580
  if (typeof this.body === "object") return "application/json";
464
581
  return void 0;
465
582
  }
466
- toFetchBody(contentType) {
467
- const body = this.body;
468
- if (body === void 0) return;
469
- if (body === null) return null;
470
- if (Validator.isBodyInit(body)) return body;
471
- if (!contentType || contentType === "application/json") {
472
- return JSON.stringify(body);
473
- }
474
- if (contentType === "application/x-www-form-urlencoded") {
475
- if (Array.isArray(body)) {
476
- throw new Exception("request.body is an array, that cannot be serialized as application/x-www-form-urlencoded format");
477
- }
478
- return toUrlSearchParams(body);
479
- }
480
- if (contentType === "multipart/form-data") {
481
- if (Array.isArray(body)) {
482
- throw new Exception("FormData cannot send array");
483
- }
484
- return toFormData(body);
583
+ toFetchBody(contentType) {
584
+ const body = this.body;
585
+ if (body === void 0) return;
586
+ if (body === null) return null;
587
+ if (Validator.isBodyInit(body)) return body;
588
+ if (!contentType || contentType === "application/json") {
589
+ return JSON.stringify(body);
590
+ }
591
+ if (contentType === "application/x-www-form-urlencoded") {
592
+ if (Array.isArray(body)) {
593
+ throw new Exception("request.body is an array, that cannot be serialized as application/x-www-form-urlencoded format");
594
+ }
595
+ return toUrlSearchParams(body);
596
+ }
597
+ if (contentType === "multipart/form-data") {
598
+ if (Array.isArray(body)) {
599
+ throw new Exception("FormData cannot send array");
600
+ }
601
+ return toFormData(body);
602
+ }
603
+ throw new Exception("Cannot auto serialize request.body with Content-Type: ".concat(contentType));
604
+ }
605
+ toFetchArguments() {
606
+ const contentType = this.getContentType();
607
+ const headers = cloneHeaders(this.headers);
608
+ if (contentType) headers.set("Content-Type", contentType);
609
+ const body = this.toFetchBody(contentType);
610
+ if (contentType === "multipart/form-data") {
611
+ headers.delete("Content-Type");
612
+ }
613
+ const requestInit = {
614
+ method: this.method.toUpperCase(),
615
+ headers,
616
+ body,
617
+ cache: this.cache,
618
+ credentials: this.credentials,
619
+ integrity: this.integrity,
620
+ keepalive: this.keepalive,
621
+ mode: this.mode,
622
+ redirect: this.redirect,
623
+ referrer: this.referrer,
624
+ referrerPolicy: this.referrerPolicy,
625
+ signal: this.signal
626
+ };
627
+ return [this.__url__.href, requestInit];
628
+ }
629
+ };
630
+
631
+ // src/context/orchestrator-context.ts
632
+ var OrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.orchestrator");
633
+ var ExecutorProperty = /* @__PURE__ */ Symbol("protected context.orchestration.executor");
634
+ var _a2, _b;
635
+ _b = OrchestratorProperty, _a2 = ExecutorProperty;
636
+ var KeqOrchestratorContext = class {
637
+ constructor(orchestrator, executor) {
638
+ __publicField(this, _b);
639
+ __publicField(this, _a2);
640
+ this[OrchestratorProperty] = orchestrator;
641
+ this[ExecutorProperty] = executor;
642
+ }
643
+ get middlewares() {
644
+ return this[OrchestratorProperty].executors.map((exe) => exe.context);
645
+ }
646
+ // NOTE: For Future
647
+ // get executing(): KeqMiddlewareContext {
648
+ // const current = this[OrchestratorProperty].current
649
+ // const executors = this[OrchestratorProperty].executors
650
+ // const executor = executors[current]
651
+ // return executor.context
652
+ // }
653
+ /**
654
+ * The middleware context of the current middleware
655
+ */
656
+ get middleware() {
657
+ return this[ExecutorProperty].context;
658
+ }
659
+ fork() {
660
+ return this[OrchestratorProperty].fork();
661
+ }
662
+ merge(source) {
663
+ this[OrchestratorProperty].merge(source);
664
+ }
665
+ };
666
+
667
+ // src/context/execution-context.ts
668
+ var ContextOrchestratorProperty = /* @__PURE__ */ Symbol("protected context.orchestrator");
669
+ var ContextOrchestrationProperty = /* @__PURE__ */ Symbol("protected context.orchestration");
670
+ var _a3, _b2;
671
+ _b2 = ContextOrchestratorProperty, _a3 = ContextOrchestrationProperty;
672
+ var KeqExecutionContext = class {
673
+ constructor(orchestrator, executor) {
674
+ __publicField(this, _b2);
675
+ __publicField(this, _a3);
676
+ this[ContextOrchestratorProperty] = orchestrator;
677
+ this[ContextOrchestrationProperty] = new KeqOrchestratorContext(orchestrator, executor);
678
+ }
679
+ get orchestration() {
680
+ return this[ContextOrchestrationProperty];
681
+ }
682
+ /**
683
+ * The unique identifier of the request's location in the code
684
+ */
685
+ get locationId() {
686
+ return this[ContextOrchestratorProperty].context.locationId;
687
+ }
688
+ get request() {
689
+ return this[ContextOrchestratorProperty].context.request;
690
+ }
691
+ get global() {
692
+ return this[ContextOrchestratorProperty].context.global;
693
+ }
694
+ get emitter() {
695
+ return this[ContextOrchestratorProperty].context.emitter;
696
+ }
697
+ get options() {
698
+ return this[ContextOrchestratorProperty].context.options;
699
+ }
700
+ // The result get by user if resolveWith is set to 'intelligent' or not set
701
+ set output(value) {
702
+ if (this.options.resolveWith && this.options.resolveWith !== "intelligent") {
703
+ console.warn("The request is configured to resolve with ".concat(this.options.resolveWith, ", so setting context.output maybe no effect."));
485
704
  }
486
- throw new Exception("Cannot auto serialize request.body with Content-Type: ".concat(contentType));
705
+ this[ContextOrchestratorProperty].context.output = value;
487
706
  }
488
- toFetchArguments() {
489
- const contentType = this.getContentType();
490
- const headers = cloneHeaders(this.headers);
491
- if (contentType) headers.set("Content-Type", contentType);
492
- const body = this.toFetchBody(contentType);
493
- if (contentType === "multipart/form-data") {
494
- headers.delete("Content-Type");
495
- }
496
- const requestInit = {
497
- method: this.method.toUpperCase(),
498
- headers,
499
- body,
500
- cache: this.cache,
501
- credentials: this.credentials,
502
- integrity: this.integrity,
503
- keepalive: this.keepalive,
504
- mode: this.mode,
505
- redirect: this.redirect,
506
- referrer: this.referrer,
507
- referrerPolicy: this.referrerPolicy,
508
- signal: this.signal
509
- };
510
- return [this.__url__.href, requestInit];
707
+ // The original response
708
+ get res() {
709
+ return this[ContextOrchestratorProperty].context.res;
710
+ }
711
+ // The original response
712
+ set res(value) {
713
+ this[ContextOrchestratorProperty].context.res = value;
714
+ }
715
+ // The request response
716
+ get response() {
717
+ return this[ContextOrchestratorProperty].context.response;
718
+ }
719
+ // The properties extends by middleware
720
+ get data() {
721
+ return this[ContextOrchestratorProperty].context.data;
511
722
  }
512
723
  };
513
724
 
725
+ // src/context/shared-context.ts
726
+ var import_mitt = __toESM(require("mitt"));
727
+
514
728
  // src/context/utils/watch-object.ts
515
729
  function watchObject(obj, listeners) {
516
730
  return new Proxy(obj, {
@@ -731,223 +945,6 @@ var KeqSharedContext = class {
731
945
  }
732
946
  };
733
947
 
734
- // src/exception/type.exception.ts
735
- var TypeException = class extends Exception {
736
- constructor(msg) {
737
- super(msg || "Invalid Type");
738
- }
739
- };
740
-
741
- // src/exception/abort.exception.ts
742
- var AbortException = class _AbortException extends DOMException {
743
- constructor(msg) {
744
- super(msg, "AbortError");
745
- Object.setPrototypeOf(this, _AbortException.prototype);
746
- }
747
- };
748
-
749
- // src/exception/timeout.exception.ts
750
- var TimeoutException = class extends AbortException {
751
- constructor(msg) {
752
- super(msg);
753
- }
754
- };
755
-
756
- // src/exception/http-exceptions/request.exception.ts
757
- var RequestException = class extends Exception {
758
- constructor(statusCode, message, retry = true) {
759
- super(message);
760
- __publicField(this, "statusCode");
761
- __publicField(this, "retry");
762
- this.statusCode = statusCode;
763
- this.retry = retry;
764
- Object.defineProperty(this, "name", { value: "RequestException" });
765
- }
766
- };
767
-
768
- // src/exception/http-exceptions/bad-request.exception.ts
769
- var BadRequestException = class extends RequestException {
770
- constructor(message = "Bad Request", retry = false) {
771
- super(400, message, retry);
772
- Object.defineProperty(this, "name", { value: "BadRequestException" });
773
- }
774
- };
775
-
776
- // src/exception/http-exceptions/unauthorized.exception.ts
777
- var UnauthorizedException = class extends RequestException {
778
- constructor(message = "Unauthorized", retry = false) {
779
- super(401, message, retry);
780
- Object.defineProperty(this, "name", { value: "UnauthorizedException" });
781
- }
782
- };
783
-
784
- // src/exception/http-exceptions/forbidden.exception.ts
785
- var ForbiddenException = class extends RequestException {
786
- constructor(message = "Forbidden", retry = false) {
787
- super(403, message, retry);
788
- Object.defineProperty(this, "name", { value: "ForbiddenException" });
789
- }
790
- };
791
-
792
- // src/exception/http-exceptions/not-founded.exception.ts
793
- var NotFoundedException = class extends RequestException {
794
- constructor(message = "Not Founded", retry = false) {
795
- super(404, message, retry);
796
- Object.defineProperty(this, "name", { value: "NotFoundedException" });
797
- }
798
- };
799
-
800
- // src/exception/http-exceptions/method-not-allowed.exception.ts
801
- var MethodNotAllowedException = class extends RequestException {
802
- constructor(message = "Method Not Allowed", retry = false) {
803
- super(405, message, retry);
804
- Object.defineProperty(this, "name", {
805
- value: "MethodNotAllowedException"
806
- });
807
- }
808
- };
809
-
810
- // src/exception/http-exceptions/not-acceptable.exception.ts
811
- var NotAcceptableException = class extends RequestException {
812
- constructor(message = "Not Acceptable", retry = false) {
813
- super(406, message, retry);
814
- Object.defineProperty(this, "name", { value: "NotAcceptableException" });
815
- }
816
- };
817
-
818
- // src/exception/http-exceptions/proxy-authentication-required.exception.ts
819
- var ProxyAuthenticationRequiredException = class extends RequestException {
820
- constructor(message = "Proxy Authentication Required", retry = false) {
821
- super(407, message, retry);
822
- Object.defineProperty(this, "name", {
823
- value: "ProxyAuthenticationRequiredException"
824
- });
825
- }
826
- };
827
-
828
- // src/exception/http-exceptions/request-timeout.exception.ts
829
- var RequestTimeoutException = class extends RequestException {
830
- constructor(message = "Request Timeout", retry = true) {
831
- super(408, message, retry);
832
- Object.defineProperty(this, "name", {
833
- value: "RequestTimeoutException"
834
- });
835
- }
836
- };
837
-
838
- // src/exception/http-exceptions/conflict.exception.ts
839
- var ConflictException = class extends RequestException {
840
- constructor(message = "Conflict", retry = false) {
841
- super(409, message, retry);
842
- Object.defineProperty(this, "name", { value: "ConflictException" });
843
- }
844
- };
845
-
846
- // src/exception/http-exceptions/precondition-failed.exception.ts
847
- var PreconditionFailedException = class extends RequestException {
848
- constructor(message = "Precondition Failed", retry = false) {
849
- super(412, message, retry);
850
- Object.defineProperty(this, "name", {
851
- value: "PreconditionFailedException"
852
- });
853
- }
854
- };
855
-
856
- // src/exception/http-exceptions/content-too-large.exception.ts
857
- var ContentTooLargeException = class extends RequestException {
858
- constructor(message = "Content Too Large", retry = false) {
859
- super(413, message, retry);
860
- Object.defineProperty(this, "name", {
861
- value: "ContentTooLargeException"
862
- });
863
- }
864
- };
865
-
866
- // src/exception/http-exceptions/uri-too-long.exception.ts
867
- var UriTooLongException = class extends RequestException {
868
- constructor(message = "URI Too Long", retry = false) {
869
- super(414, message, retry);
870
- Object.defineProperty(this, "name", { value: "UriTooLongException" });
871
- }
872
- };
873
-
874
- // src/exception/http-exceptions/unsupported-media-type.exception.ts
875
- var UnsupportedMediaTypeException = class extends RequestException {
876
- constructor(message = "Unsupported Media Type", retry = false) {
877
- super(415, message, retry);
878
- Object.defineProperty(this, "name", { value: "UnsupportedMediaTypeException" });
879
- }
880
- };
881
-
882
- // src/exception/http-exceptions/im-a-teapot.exception.ts
883
- var ImATeapotException = class extends RequestException {
884
- constructor(message = "I'm a teapot", retry = false) {
885
- super(418, message, retry);
886
- Object.defineProperty(this, "name", { value: "ImATeapotException" });
887
- }
888
- };
889
-
890
- // src/exception/http-exceptions/too-many-requests.exception.ts
891
- var TooManyRequestsException = class extends RequestException {
892
- constructor(message = "Too Many Requests", retry = true) {
893
- super(429, message, retry);
894
- Object.defineProperty(this, "name", { value: "TooManyRequestsException" });
895
- }
896
- };
897
-
898
- // src/exception/http-exceptions/internal-server-error.exception.ts
899
- var InternalServerErrorException = class extends RequestException {
900
- constructor(message = "Internal Server Error", retry = true) {
901
- super(500, message, retry);
902
- Object.defineProperty(this, "name", {
903
- value: "InternalServerErrorException"
904
- });
905
- }
906
- };
907
-
908
- // src/exception/http-exceptions/not-implemented.exception.ts
909
- var NotImplementedException = class extends RequestException {
910
- constructor(message = "Not Implemented", retry = false) {
911
- super(501, message, retry);
912
- Object.defineProperty(this, "name", { value: "NotImplementedException" });
913
- }
914
- };
915
-
916
- // src/exception/http-exceptions/bad-gateway.exception.ts
917
- var BadGatewayException = class extends RequestException {
918
- constructor(message = "Bad Gateway", retry = true) {
919
- super(502, message, retry);
920
- Object.defineProperty(this, "name", { value: "BadGatewayException" });
921
- }
922
- };
923
-
924
- // src/exception/http-exceptions/service-unavailable.exception.ts
925
- var ServiceUnavailableException = class extends RequestException {
926
- constructor(message = "Service Unavailable", retry = true) {
927
- super(503, message, retry);
928
- Object.defineProperty(this, "name", {
929
- value: "ServiceUnavailableException"
930
- });
931
- }
932
- };
933
-
934
- // src/exception/http-exceptions/gateway-timeout.exception.ts
935
- var GatewayTimeoutException = class extends RequestException {
936
- constructor(message = "Gateway Timeout", retry = true) {
937
- super(504, message, retry);
938
- Object.defineProperty(this, "name", { value: "GatewayTimeoutException" });
939
- }
940
- };
941
-
942
- // src/utils/base64.ts
943
- var base64Encode = globalThis.btoa || ((str) => Buffer.from(str).toString("base64"));
944
- var base64Decode = globalThis.atob || ((str) => Buffer.from(str, "base64").toString("utf8"));
945
-
946
- // src/utils/sleep.ts
947
- function sleep(ms) {
948
- return new Promise((resolve) => setTimeout(resolve, ms));
949
- }
950
-
951
948
  // src/middleware/utils/get-middleware-name.ts
952
949
  function getMiddlewareName(middleware) {
953
950
  return middleware.__keqMiddlewareName__ || middleware.name || "anonymous";
@@ -1164,6 +1161,22 @@ function getLocationId(depth = 0) {
1164
1161
  return stackLine.trim();
1165
1162
  }
1166
1163
 
1164
+ // src/request/utils/resolve-with.ts
1165
+ var import_stream = require("eventsource-parser/stream");
1166
+ async function resolveWith(response, mode) {
1167
+ if (mode === "response") return response.clone();
1168
+ if (mode === "text") return await response.text();
1169
+ if (mode === "json") return unwrap(await response.json());
1170
+ if (mode === "form-data") return await response.formData();
1171
+ if (mode === "blob") return await response.blob();
1172
+ if (mode === "array-buffer") return await response.arrayBuffer();
1173
+ if (mode === "sse") {
1174
+ if (!response.body) return response.body;
1175
+ return response.clone().body.pipeThrough(new TextDecoderStream()).pipeThrough(new import_stream.EventSourceParserStream());
1176
+ }
1177
+ return void 0;
1178
+ }
1179
+
1167
1180
  // src/request/utils/intelligent-parse-response.ts
1168
1181
  async function intelligentParseResponse(response) {
1169
1182
  if (!response) return void 0;
@@ -1172,15 +1185,18 @@ async function intelligentParseResponse(response) {
1172
1185
  }
1173
1186
  const contentType = response.headers.get("content-type") || "";
1174
1187
  try {
1175
- if (contentType.includes("application/json")) {
1176
- return unwrap(await response.json());
1188
+ if (contentType.match(/^application\/(.+\+)?json/)) {
1189
+ return resolveWith(response, "json");
1177
1190
  } else if (contentType.includes("multipart/form-data")) {
1178
- return await response.formData();
1191
+ return resolveWith(response, "form-data");
1179
1192
  } else if (contentType.includes("plain/text")) {
1180
- return await response.text();
1193
+ return resolveWith(response, "text");
1194
+ } else if (contentType.includes("text/event-stream")) {
1195
+ return resolveWith(response, "sse");
1181
1196
  }
1182
- } catch (e) {
1183
- console.warn("Failed to auto parse response body", e);
1197
+ } catch (err) {
1198
+ console.warn("Failed to intelligent parse response body: ".concat(response.url), err);
1199
+ throw err;
1184
1200
  }
1185
1201
  return void 0;
1186
1202
  }
@@ -1386,33 +1402,26 @@ var Core = class {
1386
1402
  async end() {
1387
1403
  var _a6;
1388
1404
  const coreContext = await this.run();
1389
- if (coreContext.options.resolveWith === "response") {
1405
+ const resolveWithMode = coreContext.options.resolveWith;
1406
+ if (resolveWithMode === "response") {
1390
1407
  return (_a6 = coreContext.response) == null ? void 0 : _a6.clone();
1391
1408
  }
1392
1409
  const response = coreContext.response;
1393
- if (coreContext.options.resolveWith && coreContext.options.resolveWith !== "intelligent" && !response) {
1410
+ if (!resolveWithMode || resolveWithMode === "intelligent") {
1411
+ const output = coreContext.output;
1412
+ if (output !== void 0) {
1413
+ return output;
1414
+ }
1415
+ return await intelligentParseResponse(response);
1416
+ }
1417
+ if (!response) {
1394
1418
  throw new Exception([
1395
- "Unable to process the response with '".concat(coreContext.options.resolveWith, "'. Possible causes:"),
1419
+ "Unable to process the response with '".concat(resolveWithMode, "'. Possible causes:"),
1396
1420
  "1. The request was never initiated or sent",
1397
1421
  "2. The request failed before a response was received."
1398
1422
  ].join("\n"));
1399
1423
  }
1400
- if (coreContext.options.resolveWith === "text") {
1401
- return await response.text();
1402
- } else if (coreContext.options.resolveWith === "json") {
1403
- return unwrap(await response.json());
1404
- } else if (coreContext.options.resolveWith === "form-data") {
1405
- return await response.formData();
1406
- } else if (coreContext.options.resolveWith === "blob") {
1407
- return await response.blob();
1408
- } else if (coreContext.options.resolveWith === "array-buffer") {
1409
- return await response.arrayBuffer();
1410
- }
1411
- const output = coreContext.output;
1412
- if (output !== void 0) {
1413
- return output;
1414
- }
1415
- return await intelligentParseResponse(response);
1424
+ return await resolveWith(response, resolveWithMode);
1416
1425
  }
1417
1426
  /**
1418
1427
  * Attaches callbacks for the resolution and/or rejection of the Promise.
@@ -1658,11 +1667,11 @@ function keqPathnameRoute(pathname) {
1658
1667
  }
1659
1668
 
1660
1669
  // src/router/keq-router.ts
1661
- var KeqRouter = class {
1670
+ var KeqRouter = class _KeqRouter {
1662
1671
  constructor(middlewares = []) {
1663
1672
  this.middlewares = middlewares;
1664
1673
  }
1665
- buildMiddleware(route, middlewares) {
1674
+ static buildMiddleware(route, middlewares) {
1666
1675
  if (middlewares.length === 0) {
1667
1676
  throw new TypeException("At least one middleware is required to build a route middleware");
1668
1677
  }
@@ -1673,42 +1682,72 @@ var KeqRouter = class {
1673
1682
  };
1674
1683
  return middleware;
1675
1684
  }
1676
- route(route, ...middlewares) {
1677
- const middleware = this.buildMiddleware(route, middlewares);
1678
- middleware.__keqMiddlewareName__ = "route(".concat(route.__keqRouteName__ || route.name, ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1679
- this.middlewares.push(middleware);
1685
+ route(route, middleware, ...additionalMiddlewares) {
1686
+ const mid = _KeqRouter.route(route, middleware, ...additionalMiddlewares);
1687
+ this.middlewares.push(mid);
1680
1688
  return this;
1681
1689
  }
1682
- host(host, ...middlewares) {
1683
- const middleware = this.buildMiddleware(keqHostRoute(host), middlewares);
1684
- middleware.__keqMiddlewareName__ = "host(".concat(JSON.stringify(host), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1685
- this.middlewares.push(middleware);
1690
+ host(host, middleware, ...additionalMiddlewares) {
1691
+ const mid = _KeqRouter.host(host, middleware, ...additionalMiddlewares);
1692
+ this.middlewares.push(mid);
1686
1693
  return this;
1687
1694
  }
1688
- method(method, ...middlewares) {
1689
- const middleware = this.buildMiddleware(keqMethodRoute(method), middlewares);
1690
- middleware.__keqMiddlewareName__ = "method(".concat(JSON.stringify(method), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1691
- this.middlewares.push(middleware);
1695
+ method(method, middleware, ...additionalMiddlewares) {
1696
+ const mid = _KeqRouter.method(method, middleware, ...additionalMiddlewares);
1697
+ this.middlewares.push(mid);
1692
1698
  return this;
1693
1699
  }
1694
- pathname(pathname, ...middlewares) {
1695
- const middleware = this.buildMiddleware(keqPathnameRoute(pathname), middlewares);
1696
- middleware.__keqMiddlewareName__ = "pathname(".concat(JSON.stringify(pathname), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1697
- this.middlewares.push(middleware);
1700
+ pathname(pathname, middleware, ...additionalMiddlewares) {
1701
+ const mid = _KeqRouter.pathname(pathname, middleware, ...additionalMiddlewares);
1702
+ this.middlewares.push(mid);
1698
1703
  return this;
1699
1704
  }
1700
- location(...middlewares) {
1701
- const middleware = this.buildMiddleware(keqLocationRoute(), middlewares);
1702
- middleware.__keqMiddlewareName__ = "location(".concat(middlewares.map(getMiddlewareName).join(", "), ")");
1703
- this.middlewares.push(middleware);
1705
+ location(middleware, ...additionalMiddlewares) {
1706
+ const mid = _KeqRouter.location(middleware, ...additionalMiddlewares);
1707
+ this.middlewares.push(mid);
1704
1708
  return this;
1705
1709
  }
1706
- module(moduleName, ...middlewares) {
1707
- const middleware = this.buildMiddleware(keqModuleRoute(moduleName), middlewares);
1708
- middleware.__keqMiddlewareName__ = "module(".concat(JSON.stringify(moduleName), ", ").concat(middlewares.map(getMiddlewareName).join(", "), ")");
1709
- this.middlewares.push(middleware);
1710
+ module(moduleName, middleware, ...additionalMiddlewares) {
1711
+ const mid = _KeqRouter.module(moduleName, middleware, ...additionalMiddlewares);
1712
+ this.middlewares.push(mid);
1710
1713
  return this;
1711
1714
  }
1715
+ static route(route, middleware, ...additionalMiddlewares) {
1716
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1717
+ const mid = _KeqRouter.buildMiddleware(route, list);
1718
+ mid.__keqMiddlewareName__ = "route(".concat(route.__keqRouteName__ || route.name, ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1719
+ return mid;
1720
+ }
1721
+ static host(host, middleware, ...additionalMiddlewares) {
1722
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1723
+ const mid = _KeqRouter.buildMiddleware(keqHostRoute(host), list);
1724
+ mid.__keqMiddlewareName__ = "host(".concat(JSON.stringify(host), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1725
+ return mid;
1726
+ }
1727
+ static method(method, middleware, ...additionalMiddlewares) {
1728
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1729
+ const mid = _KeqRouter.buildMiddleware(keqMethodRoute(method), list);
1730
+ mid.__keqMiddlewareName__ = "method(".concat(JSON.stringify(method), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1731
+ return mid;
1732
+ }
1733
+ static pathname(pathname, middleware, ...additionalMiddlewares) {
1734
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1735
+ const mid = _KeqRouter.buildMiddleware(keqPathnameRoute(pathname), list);
1736
+ mid.__keqMiddlewareName__ = "pathname(".concat(JSON.stringify(pathname), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1737
+ return mid;
1738
+ }
1739
+ static location(middleware, ...additionalMiddlewares) {
1740
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1741
+ const mid = _KeqRouter.buildMiddleware(keqLocationRoute(), list);
1742
+ mid.__keqMiddlewareName__ = "location(".concat(list.map(getMiddlewareName).join(", "), ")");
1743
+ return mid;
1744
+ }
1745
+ static module(moduleName, middleware, ...additionalMiddlewares) {
1746
+ const list = Array.isArray(middleware) ? middleware : [middleware, ...additionalMiddlewares];
1747
+ const mid = _KeqRouter.buildMiddleware(keqModuleRoute(moduleName), list);
1748
+ mid.__keqMiddlewareName__ = "module(".concat(JSON.stringify(moduleName), ", ").concat(list.map(getMiddlewareName).join(", "), ")");
1749
+ return mid;
1750
+ }
1712
1751
  };
1713
1752
 
1714
1753
  // src/middlewares/timeout-middleware/index.ts
@@ -1910,6 +1949,14 @@ var KeqRequest = class {
1910
1949
  useRouter() {
1911
1950
  return new KeqRouter(this.preMiddlewares);
1912
1951
  }
1952
+ on(event, listener) {
1953
+ const middleware = async (context, next) => {
1954
+ context.emitter.on(event, listener);
1955
+ await next();
1956
+ };
1957
+ this.use(middleware);
1958
+ return this;
1959
+ }
1913
1960
  };
1914
1961
  var request = new KeqRequest();
1915
1962