feat: expose layouts and pages on App (#3086)
This PR extracts the functionality from the `fs_routes` plugin and exposes it onto `App`. These are the new APIs: - `app.layout(pattern, route)` - `app.onError(pattern, route)` - `app.notFound(pattern, route)` - `app.route(pattern, route)` This also removes the need for `.use(staticFiles())`. This line can be safely removed. Fixes https://github.com/denoland/fresh/issues/3040 Fixes https://github.com/denoland/fresh/issues/3033 Fixes https://github.com/denoland/fresh/issues/2591 Fixes https://github.com/denoland/fresh/issues/950 Fixes https://github.com/denoland/fresh/issues/1918
This commit is contained in:
+79
-3
@@ -2,6 +2,7 @@ import { expect } from "@std/expect";
|
||||
import { App, getIslandRegistry, setBuildCache } from "./app.ts";
|
||||
import { FakeServer } from "./test_utils.ts";
|
||||
import { ProdBuildCache } from "./build_cache.ts";
|
||||
import { HttpError } from "./error.ts";
|
||||
|
||||
Deno.test("App - .use()", async () => {
|
||||
const app = new App<{ text: string }>()
|
||||
@@ -35,8 +36,6 @@ Deno.test("App - .use() #2", async () => {
|
||||
|
||||
Deno.test("App - .get()", async () => {
|
||||
const app = new App()
|
||||
.post("/", () => new Response("ok"))
|
||||
.post("/foo", () => new Response("ok"))
|
||||
.get("/", () => new Response("ok"))
|
||||
.get("/foo", () => new Response("ok"));
|
||||
|
||||
@@ -323,11 +322,11 @@ Deno.test(
|
||||
"App - .mountApp() self mount, different order",
|
||||
async () => {
|
||||
const innerApp = new App<{ text: string }>()
|
||||
.get("/foo", (ctx) => new Response(ctx.state.text))
|
||||
.use(function B(ctx) {
|
||||
ctx.state.text += "B";
|
||||
return ctx.next();
|
||||
})
|
||||
.get("/foo", (ctx) => new Response(ctx.state.text))
|
||||
.post("/foo", (ctx) => new Response(ctx.state.text));
|
||||
|
||||
const app = new App<{ text: string }>()
|
||||
@@ -392,6 +391,27 @@ Deno.test(
|
||||
},
|
||||
);
|
||||
|
||||
// https://github.com/denoland/fresh/issues/3033
|
||||
Deno.test(
|
||||
"App - .mountApp() self mount with dynamic routes",
|
||||
async () => {
|
||||
const innerApp = new App<{ text: string }>()
|
||||
.get("/", () => new Response("list authors"))
|
||||
.get("/:name", (ctx) => new Response(`show: ${ctx.params.name}`));
|
||||
|
||||
const app = new App<{ text: string }>()
|
||||
.mountApp("/api/authors", innerApp);
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
let res = await server.get("/api/authors");
|
||||
expect(await res.text()).toEqual("list authors");
|
||||
|
||||
res = await server.get("/api/authors/foo");
|
||||
expect(await res.text()).toEqual("show: foo");
|
||||
},
|
||||
);
|
||||
|
||||
Deno.test("App - catches errors", async () => {
|
||||
let thrownErr: unknown | null = null;
|
||||
const app = new App<{ text: string }>()
|
||||
@@ -532,3 +552,59 @@ Deno.test("App - adding Island should convert to valid export names", () => {
|
||||
fn: component3,
|
||||
});
|
||||
});
|
||||
|
||||
Deno.test("App - overwrite default 404 handler", async () => {
|
||||
const app = new App()
|
||||
.notFound(() => new Response("bar", { status: 404 }))
|
||||
.get("/foo", () => new Response("foo"))
|
||||
.get("/thrower", () => {
|
||||
throw new HttpError(404);
|
||||
});
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
let res = await server.get("/invalid");
|
||||
expect(await res.text()).toEqual("bar");
|
||||
|
||||
res = await server.get("/thrower");
|
||||
expect(await res.text()).toEqual("bar");
|
||||
});
|
||||
|
||||
Deno.test("App - uses error route", async () => {
|
||||
const app = new App()
|
||||
.onError("*", () => new Response("error route", { status: 200 }))
|
||||
.get("/thrower", () => {
|
||||
throw new HttpError(404);
|
||||
});
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
let res = await server.get("/invalid");
|
||||
expect(await res.text()).toEqual("error route");
|
||||
|
||||
res = await server.get("/thrower");
|
||||
expect(await res.text()).toEqual("error route");
|
||||
});
|
||||
|
||||
Deno.test("App - uses notFound route on 404", async () => {
|
||||
const app = new App()
|
||||
.notFound(() => new Response("not found route", { status: 404 }))
|
||||
.onError("*", () => new Response("error route", { status: 500 }))
|
||||
.get("/thrower", () => {
|
||||
throw new HttpError(404);
|
||||
})
|
||||
.get("/thrower_2", () => {
|
||||
throw new HttpError(500);
|
||||
});
|
||||
|
||||
const server = new FakeServer(app.handler());
|
||||
|
||||
let res = await server.get("/invalid");
|
||||
expect(await res.text()).toEqual("not found route");
|
||||
|
||||
res = await server.get("/thrower");
|
||||
expect(await res.text()).toEqual("not found route");
|
||||
|
||||
res = await server.get("/thrower_2");
|
||||
expect(await res.text()).toEqual("error route");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user