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:
@@ -13,7 +13,8 @@ import { stub } from "@std/testing/mock";
|
||||
import { type HandlerByMethod, type HandlerFn, page } from "../../handlers.ts";
|
||||
import type { Method } from "../../router.ts";
|
||||
import { parseHtml } from "../../../tests/test_utils.tsx";
|
||||
import type { FreshContext } from "fresh";
|
||||
import type { FreshContext } from "../../context.ts";
|
||||
import { HttpError } from "../../error.ts";
|
||||
|
||||
async function createServer<T>(
|
||||
files: Record<string, string | Uint8Array | FreshFsItem<T>>,
|
||||
@@ -193,13 +194,13 @@ Deno.test("fsRoutes - middleware", async () => {
|
||||
Deno.test("fsRoutes - nested middlewares", async () => {
|
||||
const server = await createServer<{ text: string }>({
|
||||
"routes/_middleware.ts": {
|
||||
handler: (ctx) => {
|
||||
handler: function A(ctx) {
|
||||
ctx.state.text = "A";
|
||||
return ctx.next();
|
||||
},
|
||||
},
|
||||
"routes/foo/_middleware.ts": {
|
||||
handler: (ctx) => {
|
||||
handler: function B(ctx) {
|
||||
ctx.state.text += "B";
|
||||
return ctx.next();
|
||||
},
|
||||
@@ -332,18 +333,22 @@ Deno.test("fsRoutes - nested _layout", async () => {
|
||||
default: () => <>foo</>,
|
||||
},
|
||||
"routes/foo/_layout.tsx": {
|
||||
default: (ctx) => (
|
||||
<>
|
||||
layout_foo_bar/<ctx.Component />
|
||||
</>
|
||||
),
|
||||
default: function fooBar(ctx) {
|
||||
return (
|
||||
<>
|
||||
layout_foo_bar/<ctx.Component />
|
||||
</>
|
||||
);
|
||||
},
|
||||
},
|
||||
"routes/_layout.tsx": {
|
||||
default: (ctx) => (
|
||||
<>
|
||||
layout/<ctx.Component />
|
||||
</>
|
||||
),
|
||||
default: function rootLayout(ctx) {
|
||||
return (
|
||||
<>
|
||||
layout/<ctx.Component />
|
||||
</>
|
||||
);
|
||||
},
|
||||
},
|
||||
"routes/_app.tsx": {
|
||||
default: (ctx) => (
|
||||
@@ -806,7 +811,7 @@ Deno.test("fsRoutes - _error render component", async () => {
|
||||
},
|
||||
"routes/foo/index.tsx": {
|
||||
handlers: () => {
|
||||
throw new Error("ok");
|
||||
throw new Error("failing");
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -816,7 +821,8 @@ Deno.test("fsRoutes - _error render component", async () => {
|
||||
expect(doc.body.firstChild?.textContent).toEqual("ok");
|
||||
});
|
||||
|
||||
Deno.test("fsRoutes - _error render on 404", async () => {
|
||||
// 404 pages go to the root error page if available
|
||||
Deno.test.ignore("fsRoutes - _error render on 404", async () => {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
let error: any = null;
|
||||
const server = await createServer({
|
||||
@@ -828,7 +834,7 @@ Deno.test("fsRoutes - _error render on 404", async () => {
|
||||
},
|
||||
},
|
||||
"routes/foo/_error.tsx": {
|
||||
default: (ctx) => {
|
||||
default: function foo2(ctx) {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
error = ctx.error as any;
|
||||
return <p>ok foo</p>;
|
||||
@@ -1110,34 +1116,23 @@ Deno.test(
|
||||
"fsRoutes - returns response code from error route",
|
||||
async () => {
|
||||
const server = await createServer<{ text: string }>({
|
||||
"routes/_app.tsx": {
|
||||
default: (ctx) => {
|
||||
return (
|
||||
<div>
|
||||
_app/<ctx.Component />
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
"routes/_error.tsx": {
|
||||
default: () => <div>fail</div>,
|
||||
},
|
||||
"routes/index.tsx": {
|
||||
default: () => <div>index</div>,
|
||||
},
|
||||
"routes/bar.tsx": {
|
||||
default: () => <div>index</div>,
|
||||
},
|
||||
"routes/foo/index.tsx": {
|
||||
default: () => <div>foo/index</div>,
|
||||
},
|
||||
"routes/foo/_error.tsx": {
|
||||
"routes/fail.tsx": {
|
||||
default: () => {
|
||||
throw new Error("fail");
|
||||
},
|
||||
},
|
||||
"routes/foo/bar.tsx": {
|
||||
default: () => <div>foo/index</div>,
|
||||
"routes/foo/_error.tsx": {
|
||||
default: () => {
|
||||
throw new HttpError(501);
|
||||
},
|
||||
},
|
||||
"routes/foo/fail.tsx": {
|
||||
default: () => {
|
||||
throw new Error("fail");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1145,9 +1140,13 @@ Deno.test(
|
||||
await res.body?.cancel();
|
||||
expect(res.status).toEqual(404);
|
||||
|
||||
res = await server.get("/foo/asdf");
|
||||
res = await server.get("/fail");
|
||||
await res.body?.cancel();
|
||||
expect(res.status).toEqual(500);
|
||||
|
||||
res = await server.get("/foo/fail");
|
||||
await res.body?.cancel();
|
||||
expect(res.status).toEqual(501);
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user