@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@server/next",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "An experimental reimplementation of server.js focused on the DX",
5
5
  "homepage": "https://node-server.com/",
6
6
  "repository": "https://github.com/franciscop/server-next.git",
package/src/bucket.js CHANGED
@@ -21,7 +21,6 @@ export default function (root) {
21
21
  path: root,
22
22
  read: (name, type = "utf8") => {
23
23
  const fullPath = absolute(name);
24
- console.log(name, fullPath);
25
24
  return fsp.readFile(fullPath, type);
26
25
  },
27
26
  write: (name, value, type = "utf8") => {
@@ -20,10 +20,23 @@ export default async (request, options = {}) => {
20
20
  Object.fromEntries(url.searchParams.entries())
21
21
  );
22
22
 
23
- if (request.body) {
24
- const type = ctx.headers["content-type"];
25
- ctx.body = await parseBody(request, type, options.uploads);
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
  };
@@ -0,0 +1,6 @@
1
+ export default class StatusError extends Error {
2
+ constructor(msg, status = 500) {
3
+ super(msg);
4
+ this.status = status;
5
+ }
6
+ }
@@ -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
- const out = await parseResponse(cb, ctx);
15
- if (out) return out;
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
+ }
package/src/reply.js CHANGED
@@ -64,7 +64,6 @@ Reply.prototype.file = async function (path) {
64
64
 
65
65
  Reply.prototype.view = async function (path) {
66
66
  return async (ctx) => {
67
- console.log(ctx);
68
67
  if (!ctx.options.views) {
69
68
  throw new Error("Views not enabled");
70
69
  }