vafast 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +42 -27
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -51226,6 +51226,12 @@ function matchPath(pattern, path) {
|
|
|
51226
51226
|
|
|
51227
51227
|
// src/utils/response.ts
|
|
51228
51228
|
function json(data, status = 200, headers = {}) {
|
|
51229
|
+
if (Object.keys(headers).length === 0) {
|
|
51230
|
+
return new Response(JSON.stringify(data), {
|
|
51231
|
+
status,
|
|
51232
|
+
headers: { "Content-Type": "application/json" }
|
|
51233
|
+
});
|
|
51234
|
+
}
|
|
51229
51235
|
const h = new Headers({
|
|
51230
51236
|
"Content-Type": "application/json",
|
|
51231
51237
|
...headers
|
|
@@ -55524,37 +55530,46 @@ var defaultValidationErrorHandler = (error, field, value, schema) => {
|
|
|
55524
55530
|
timestamp: new Date().toISOString()
|
|
55525
55531
|
}, 400);
|
|
55526
55532
|
};
|
|
55527
|
-
|
|
55533
|
+
var TEXT_HEADERS = { "Content-Type": "text/plain; charset=utf-8" };
|
|
55534
|
+
var EMPTY_RESPONSE_204 = new Response(null, { status: 204 });
|
|
55535
|
+
function autoResponseUltra(result) {
|
|
55528
55536
|
if (result instanceof Response) {
|
|
55529
55537
|
return result;
|
|
55530
55538
|
}
|
|
55531
|
-
if (result && typeof result === "object" && "data" in result) {
|
|
55532
|
-
const {
|
|
55533
|
-
data,
|
|
55534
|
-
status = 200,
|
|
55535
|
-
headers = {}
|
|
55536
|
-
} = result;
|
|
55537
|
-
const h = new Headers(headers);
|
|
55538
|
-
if (data === null || data === undefined) {
|
|
55539
|
-
return new Response("", { status: status ?? 204, headers: h });
|
|
55540
|
-
}
|
|
55541
|
-
if (typeof data === "string" || typeof data === "number" || typeof data === "boolean") {
|
|
55542
|
-
if (!h.has("Content-Type")) {
|
|
55543
|
-
h.set("Content-Type", "text/plain; charset=utf-8");
|
|
55544
|
-
}
|
|
55545
|
-
return new Response(String(data), { status, headers: h });
|
|
55546
|
-
}
|
|
55547
|
-
return json(data, status, h);
|
|
55548
|
-
}
|
|
55549
|
-
if (typeof result === "string" || typeof result === "number" || typeof result === "boolean") {
|
|
55550
|
-
return new Response(String(result), {
|
|
55551
|
-
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
55552
|
-
});
|
|
55553
|
-
}
|
|
55554
55539
|
if (result === null || result === undefined) {
|
|
55555
|
-
return
|
|
55540
|
+
return EMPTY_RESPONSE_204;
|
|
55541
|
+
}
|
|
55542
|
+
switch (typeof result) {
|
|
55543
|
+
case "string":
|
|
55544
|
+
return new Response(result, { headers: TEXT_HEADERS });
|
|
55545
|
+
case "number":
|
|
55546
|
+
case "boolean":
|
|
55547
|
+
return new Response(result.toString(), { headers: TEXT_HEADERS });
|
|
55548
|
+
case "object":
|
|
55549
|
+
if ("data" in result) {
|
|
55550
|
+
const { data, status = 200, headers = {} } = result;
|
|
55551
|
+
if (data === null || data === undefined) {
|
|
55552
|
+
return new Response("", {
|
|
55553
|
+
status: status === 200 ? 204 : status,
|
|
55554
|
+
headers
|
|
55555
|
+
});
|
|
55556
|
+
}
|
|
55557
|
+
if (typeof data === "string" || typeof data === "number" || typeof data === "boolean") {
|
|
55558
|
+
const finalHeaders = {
|
|
55559
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
55560
|
+
...headers
|
|
55561
|
+
};
|
|
55562
|
+
return new Response(data.toString(), {
|
|
55563
|
+
status,
|
|
55564
|
+
headers: finalHeaders
|
|
55565
|
+
});
|
|
55566
|
+
}
|
|
55567
|
+
return json(data, status, headers);
|
|
55568
|
+
}
|
|
55569
|
+
return json(result);
|
|
55570
|
+
default:
|
|
55571
|
+
return EMPTY_RESPONSE_204;
|
|
55556
55572
|
}
|
|
55557
|
-
return json(result);
|
|
55558
55573
|
}
|
|
55559
55574
|
function createRouteHandler(config, handler) {
|
|
55560
55575
|
const hasBodySchema = config.body !== undefined;
|
|
@@ -55603,7 +55618,7 @@ function createRouteHandler(config, handler) {
|
|
|
55603
55618
|
cookies,
|
|
55604
55619
|
...extras
|
|
55605
55620
|
});
|
|
55606
|
-
return
|
|
55621
|
+
return autoResponseUltra(result);
|
|
55607
55622
|
} catch (error) {
|
|
55608
55623
|
if (error instanceof Error && error.message.includes("\u9A8C\u8BC1\u5931\u8D25")) {
|
|
55609
55624
|
const field = extractFieldFromError(error);
|