@server/next 0.19.0 → 0.20.0
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/package.json +1 -1
- package/src/bucket.js +0 -1
- package/src/context/node.js +17 -4
- package/src/helpers/StatusError.js +6 -0
- package/src/helpers/handleRequest.js +11 -3
- package/src/helpers/validate.js +35 -0
- package/src/reply.js +0 -1
package/package.json
CHANGED
package/src/bucket.js
CHANGED
package/src/context/node.js
CHANGED
|
@@ -20,10 +20,23 @@ export default async (request, options = {}) => {
|
|
|
20
20
|
Object.fromEntries(url.searchParams.entries())
|
|
21
21
|
);
|
|
22
22
|
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
await new Promise((resolve, reject) => {
|
|
24
|
+
const body = [];
|
|
25
|
+
request
|
|
26
|
+
.on("data", (chunk) => {
|
|
27
|
+
body.push(chunk);
|
|
28
|
+
})
|
|
29
|
+
.on("end", async () => {
|
|
30
|
+
const type = ctx.headers["content-type"];
|
|
31
|
+
ctx.body = await parseBody(
|
|
32
|
+
Buffer.concat(body).toString(),
|
|
33
|
+
type,
|
|
34
|
+
options.uploads
|
|
35
|
+
);
|
|
36
|
+
resolve();
|
|
37
|
+
})
|
|
38
|
+
.on("error", reject);
|
|
39
|
+
});
|
|
27
40
|
|
|
28
41
|
return ctx;
|
|
29
42
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import parseResponse from "../parseResponse.js";
|
|
2
2
|
import pathPattern from "../pathPattern.js";
|
|
3
3
|
import define from "./define.js";
|
|
4
|
+
import validate from "./validate.js";
|
|
4
5
|
|
|
5
6
|
export default async function handleRequest(handlers, ctx) {
|
|
6
7
|
for (let [matcher, ...cbs] of handlers[ctx.method]) {
|
|
@@ -11,10 +12,17 @@ export default async function handleRequest(handlers, ctx) {
|
|
|
11
12
|
define(ctx.url, "params", () => match);
|
|
12
13
|
|
|
13
14
|
for (let cb of cbs) {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
try {
|
|
16
|
+
validate(ctx, cb);
|
|
17
|
+
if (typeof cb === "function") {
|
|
18
|
+
const out = await parseResponse(cb, ctx);
|
|
19
|
+
if (out) return out;
|
|
20
|
+
}
|
|
21
|
+
} catch (error) {
|
|
22
|
+
return new Response(error.message, { status: error.status || 500 });
|
|
23
|
+
}
|
|
16
24
|
}
|
|
17
25
|
}
|
|
18
26
|
|
|
19
|
-
return Response("Not Found", { status: 404 });
|
|
27
|
+
return new Response("Not Found", { status: 404 });
|
|
20
28
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import StatusError from "./StatusError.js";
|
|
2
|
+
|
|
3
|
+
export default function (ctx, schema) {
|
|
4
|
+
if (!schema || typeof schema !== "object") return;
|
|
5
|
+
|
|
6
|
+
let base;
|
|
7
|
+
try {
|
|
8
|
+
if (typeof schema?.body === "function") {
|
|
9
|
+
base = "body";
|
|
10
|
+
schema.body(ctx.body || {});
|
|
11
|
+
}
|
|
12
|
+
if (typeof schema?.body?.parse === "function") {
|
|
13
|
+
base = "body";
|
|
14
|
+
schema.body.parse(ctx.body || {});
|
|
15
|
+
}
|
|
16
|
+
if (typeof schema?.query === "function") {
|
|
17
|
+
base = "query";
|
|
18
|
+
schema.query(ctx.url.query || {});
|
|
19
|
+
}
|
|
20
|
+
if (typeof schema?.query?.parse === "function") {
|
|
21
|
+
base = "query";
|
|
22
|
+
schema.query.parse(ctx.url.query || {});
|
|
23
|
+
}
|
|
24
|
+
} catch (error) {
|
|
25
|
+
if (error.constructor.name === "ZodError") {
|
|
26
|
+
console.log(error);
|
|
27
|
+
const message = error.issues
|
|
28
|
+
.map(({ path, message }) => `[${base}.${path.join(".")}]: ${message}`)
|
|
29
|
+
.sort()
|
|
30
|
+
.join("\n");
|
|
31
|
+
throw new StatusError(message, 422);
|
|
32
|
+
}
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|