@forklaunch/core 0.5.4 → 0.5.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/http/index.d.mts +10 -3
- package/lib/http/index.d.ts +10 -3
- package/lib/http/index.js +31 -8
- package/lib/http/index.js.map +1 -1
- package/lib/http/index.mjs +31 -8
- package/lib/http/index.mjs.map +1 -1
- package/package.json +2 -1
package/lib/http/index.mjs
CHANGED
@@ -491,13 +491,18 @@ import {
|
|
491
491
|
prettyPrintParseErrors
|
492
492
|
} from "@forklaunch/validator";
|
493
493
|
|
494
|
+
// src/http/guards/hasSend.ts
|
495
|
+
function hasSend(res) {
|
496
|
+
return typeof res === "object" && res !== null && "send" in res;
|
497
|
+
}
|
498
|
+
|
494
499
|
// src/http/guards/isResponseShape.ts
|
495
500
|
function isResponseShape(maybeResponseShape) {
|
496
501
|
return maybeResponseShape != null && "body" in maybeResponseShape && "query" in maybeResponseShape && "params" in maybeResponseShape && "headers" in maybeResponseShape;
|
497
502
|
}
|
498
503
|
|
499
504
|
// src/http/middleware/request/parse.middleware.ts
|
500
|
-
function parse(req,
|
505
|
+
function parse(req, res, next) {
|
501
506
|
const request = {
|
502
507
|
params: req.params,
|
503
508
|
query: req.query,
|
@@ -518,10 +523,18 @@ function parse(req, _res, next) {
|
|
518
523
|
switch (req.contractDetails.options?.requestValidation) {
|
519
524
|
default:
|
520
525
|
case "error":
|
521
|
-
|
522
|
-
|
523
|
-
)
|
524
|
-
|
526
|
+
res.type("application/json");
|
527
|
+
res.status(400);
|
528
|
+
if (hasSend(res)) {
|
529
|
+
res.send(
|
530
|
+
`${prettyPrintParseErrors(parsedRequest.errors, "Request")}
|
531
|
+
|
532
|
+
Correlation id: ${req.context.correlationId ?? "No correlation ID"}`
|
533
|
+
);
|
534
|
+
} else {
|
535
|
+
next?.(new Error("Request is not sendable."));
|
536
|
+
}
|
537
|
+
return;
|
525
538
|
case "warning":
|
526
539
|
req.openTelemetryCollector.warn(
|
527
540
|
prettyPrintParseErrors(parsedRequest.errors, "Request")
|
@@ -2224,9 +2237,19 @@ function parse2(req, res, next) {
|
|
2224
2237
|
switch (req.contractDetails.options?.responseValidation) {
|
2225
2238
|
default:
|
2226
2239
|
case "error":
|
2227
|
-
|
2228
|
-
|
2229
|
-
|
2240
|
+
res.type("text/plain");
|
2241
|
+
res.status(400);
|
2242
|
+
if (hasSend(res)) {
|
2243
|
+
res.send(
|
2244
|
+
`Invalid response:
|
2245
|
+
${parseErrors.join("\n\n")}
|
2246
|
+
|
2247
|
+
Correlation id: ${req.context.correlationId ?? "No correlation ID"}`
|
2248
|
+
);
|
2249
|
+
} else {
|
2250
|
+
next?.(new Error("Response is not sendable."));
|
2251
|
+
}
|
2252
|
+
return;
|
2230
2253
|
case "warning":
|
2231
2254
|
req.openTelemetryCollector.warn(
|
2232
2255
|
`Invalid response:
|