@server/next 0.20.4 → 0.20.6
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/index.js +1 -1
- package/src/reply.js +15 -5
- package/src/router.test.js +47 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -135,7 +135,7 @@ server.prototype.use = function (...middleware) {
|
|
|
135
135
|
};
|
|
136
136
|
|
|
137
137
|
server.prototype.router = function (basePath, router) {
|
|
138
|
-
basePath = "/" + basePath
|
|
138
|
+
basePath = ("/" + basePath + "/").replace(/^\/+/, "/").replace(/\/+$/, "/");
|
|
139
139
|
for (const method in router.handlers) {
|
|
140
140
|
router.handlers[method].forEach(([method, path, ...callbacks]) => {
|
|
141
141
|
this.handle(method, basePath + path.replace(/^\//, ""), ...callbacks);
|
package/src/reply.js
CHANGED
|
@@ -57,11 +57,16 @@ Reply.prototype.json = function (body) {
|
|
|
57
57
|
};
|
|
58
58
|
|
|
59
59
|
Reply.prototype.file = async function (path) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
60
|
+
try {
|
|
61
|
+
const data = await fs.readFile(path);
|
|
62
|
+
const ext = path.split(".").pop();
|
|
63
|
+
return this.type(ext).send(data);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
if (error.code === "ENOENT") {
|
|
66
|
+
return status(404).send();
|
|
67
|
+
}
|
|
68
|
+
throw error;
|
|
69
|
+
}
|
|
65
70
|
};
|
|
66
71
|
|
|
67
72
|
Reply.prototype.view = async function (path) {
|
|
@@ -89,6 +94,11 @@ Reply.prototype.send = function (body = "") {
|
|
|
89
94
|
return new Response(body, { status, headers });
|
|
90
95
|
}
|
|
91
96
|
|
|
97
|
+
if (body?.constructor?.name === "Buffer") {
|
|
98
|
+
const headers = this.generateHeaders();
|
|
99
|
+
return new Response(body, { status, headers });
|
|
100
|
+
}
|
|
101
|
+
|
|
92
102
|
// This is a bit loopy, send({}) => json({}) => send('{}')
|
|
93
103
|
return this.json(body);
|
|
94
104
|
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import server, { router } from "./index.js";
|
|
2
|
+
|
|
3
|
+
const url = (path, options = {}) =>
|
|
4
|
+
new Request("http://localhost:3000" + path, options);
|
|
5
|
+
|
|
6
|
+
describe("can route properly", () => {
|
|
7
|
+
const api = router()
|
|
8
|
+
.get("/hello", (ctx) => "Hello " + ctx.url.pathname)
|
|
9
|
+
.put("/hello", (ctx) => "Hello " + ctx.url.pathname)
|
|
10
|
+
.post("/hello", (ctx) => "Hello " + ctx.url.pathname);
|
|
11
|
+
|
|
12
|
+
const app = server()
|
|
13
|
+
.router("/", api)
|
|
14
|
+
.router("/api/", api)
|
|
15
|
+
.get("/", () => "Fallback");
|
|
16
|
+
|
|
17
|
+
// INTERNAL - so this might change in the future
|
|
18
|
+
it("has the correct structure", () => {
|
|
19
|
+
const registeredPaths = app.handlers.get.map((h) => h[1]);
|
|
20
|
+
expect(registeredPaths).toEqual(["/hello", "/api/hello", "/"]);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("can get fallback when nothing matches", async () => {
|
|
24
|
+
const res = await app.fetch(url("/"));
|
|
25
|
+
expect(await res.text()).toBe("Fallback");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("can get the base get", async () => {
|
|
29
|
+
const res = await app.fetch(url("/hello"));
|
|
30
|
+
expect(await res.text()).toBe("Hello /hello");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("can get the nested get", async () => {
|
|
34
|
+
const res = await app.fetch(url("/api/hello"));
|
|
35
|
+
expect(await res.text()).toBe("Hello /api/hello");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("can post to the base get", async () => {
|
|
39
|
+
const res = await app.fetch(url("/hello", { method: "POST" }));
|
|
40
|
+
expect(await res.text()).toBe("Hello /hello");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("can post to the nested get", async () => {
|
|
44
|
+
const res = await app.fetch(url("/api/hello", { method: "POST" }));
|
|
45
|
+
expect(await res.text()).toBe("Hello /api/hello");
|
|
46
|
+
});
|
|
47
|
+
});
|