@server/next 0.18.0 → 0.19.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.18.0",
3
+ "version": "0.19.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
@@ -7,6 +7,11 @@ import fsp from "fs/promises";
7
7
  // at the very least a read(id) and write(id, value), both returning
8
8
  // promises. If possible both are also pipeable/streamable.
9
9
  export default function (root) {
10
+ // Already a bucket, no need to do anything with it, just return it:
11
+ if (typeof root !== "string") {
12
+ return root;
13
+ }
14
+
10
15
  const absolute = (name) => {
11
16
  if (!name) throw new Error(`File name is required`);
12
17
  return path.resolve(path.join(root, name));
@@ -16,6 +21,7 @@ export default function (root) {
16
21
  path: root,
17
22
  read: (name, type = "utf8") => {
18
23
  const fullPath = absolute(name);
24
+ console.log(name, fullPath);
19
25
  return fsp.readFile(fullPath, type);
20
26
  },
21
27
  write: (name, value, type = "utf8") => {
@@ -4,6 +4,7 @@ import parseCookies from "./parseCookies.js";
4
4
 
5
5
  export default async (request, options = {}) => {
6
6
  const ctx = {};
7
+ ctx.options = options;
7
8
  ctx.req = request;
8
9
  ctx.res = { status: null, headers: {}, cookies: {} };
9
10
  ctx.method = request.method.toLowerCase();
@@ -4,6 +4,7 @@ import parseCookies from "./parseCookies.js";
4
4
 
5
5
  export default async (request, options = {}) => {
6
6
  const ctx = {};
7
+ ctx.options = options;
7
8
  ctx.req = request;
8
9
  ctx.res = { status: null, headers: {}, cookies: {} };
9
10
  ctx.method = request.method.toLowerCase();
@@ -9,7 +9,7 @@ export default async function parseResponse(handler, ctx) {
9
9
  if (!out && typeof out !== "string") return null;
10
10
 
11
11
  if (typeof out === "function") {
12
- out = out(ctx);
12
+ out = await out(ctx);
13
13
  }
14
14
 
15
15
  // A plain number is a status code
package/src/reply.js CHANGED
@@ -62,6 +62,18 @@ Reply.prototype.file = async function (path) {
62
62
  return status(404).send();
63
63
  };
64
64
 
65
+ Reply.prototype.view = async function (path) {
66
+ return async (ctx) => {
67
+ console.log(ctx);
68
+ if (!ctx.options.views) {
69
+ throw new Error("Views not enabled");
70
+ }
71
+ const data = await ctx.options.views.read(path);
72
+ if (data) return this.type(path.split(".").pop()).send(data);
73
+ return status(404).send();
74
+ };
75
+ };
76
+
65
77
  Reply.prototype.send = function (body = "") {
66
78
  const { status = 200 } = this.res;
67
79
 
@@ -93,3 +105,4 @@ export const cookies = (...args) => new Reply().cookies(...args);
93
105
  export const send = (...args) => new Reply().send(...args);
94
106
  export const json = (...args) => new Reply().json(...args);
95
107
  export const file = (...args) => new Reply().file(...args);
108
+ export const view = (...args) => new Reply().view(...args);