vafast 0.1.2 → 0.1.3
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 +38 -24
- 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
|
|
@@ -55528,33 +55534,41 @@ function autoResponse(result) {
|
|
|
55528
55534
|
if (result instanceof Response) {
|
|
55529
55535
|
return result;
|
|
55530
55536
|
}
|
|
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
55537
|
if (result === null || result === undefined) {
|
|
55555
55538
|
return new Response("", { status: 204 });
|
|
55556
55539
|
}
|
|
55557
|
-
|
|
55540
|
+
switch (typeof result) {
|
|
55541
|
+
case "string":
|
|
55542
|
+
return new Response(result, {
|
|
55543
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
55544
|
+
});
|
|
55545
|
+
case "number":
|
|
55546
|
+
case "boolean":
|
|
55547
|
+
return new Response(String(result), {
|
|
55548
|
+
headers: { "Content-Type": "text/plain; charset=utf-8" }
|
|
55549
|
+
});
|
|
55550
|
+
case "object":
|
|
55551
|
+
if ("data" in result) {
|
|
55552
|
+
const { data, status = 200, headers = {} } = result;
|
|
55553
|
+
if (data === null || data === undefined) {
|
|
55554
|
+
return new Response("", {
|
|
55555
|
+
status: status === 200 ? 204 : status,
|
|
55556
|
+
headers
|
|
55557
|
+
});
|
|
55558
|
+
}
|
|
55559
|
+
if (typeof data === "string" || typeof data === "number" || typeof data === "boolean") {
|
|
55560
|
+
const finalHeaders = {
|
|
55561
|
+
"Content-Type": "text/plain; charset=utf-8",
|
|
55562
|
+
...headers
|
|
55563
|
+
};
|
|
55564
|
+
return new Response(String(data), { status, headers: finalHeaders });
|
|
55565
|
+
}
|
|
55566
|
+
return json(data, status, headers);
|
|
55567
|
+
}
|
|
55568
|
+
return json(result);
|
|
55569
|
+
default:
|
|
55570
|
+
return new Response("", { status: 204 });
|
|
55571
|
+
}
|
|
55558
55572
|
}
|
|
55559
55573
|
function createRouteHandler(config, handler) {
|
|
55560
55574
|
const hasBodySchema = config.body !== undefined;
|