From 1fb7f3bea95c796cc10b8d5be466821acd8190ce Mon Sep 17 00:00:00 2001 From: Reed von Redwitz Date: Sat, 5 Aug 2023 22:27:13 +0200 Subject: [PATCH] fix(server): make LayoutProps generic (#1592) --- src/server/types.ts | 4 +- tests/fixture_layouts/fresh.gen.ts | 62 +++++++++++---------- tests/fixture_layouts/routes/_layout.tsx | 6 +- tests/fixture_layouts/routes/_middleware.ts | 13 +++++ tests/layouts_test.ts | 2 + 5 files changed, 55 insertions(+), 32 deletions(-) create mode 100644 tests/fixture_layouts/routes/_middleware.ts diff --git a/src/server/types.ts b/src/server/types.ts index 00470df9..6aaaab44 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -172,7 +172,9 @@ export interface AppModule { default: ComponentType; } -export interface LayoutProps extends PageProps { +// deno-lint-ignore no-explicit-any +export interface LayoutProps> + extends PageProps { Component: ComponentType>; } diff --git a/tests/fixture_layouts/fresh.gen.ts b/tests/fixture_layouts/fresh.gen.ts index 5f23fbba..c8f9f9f2 100644 --- a/tests/fixture_layouts/fresh.gen.ts +++ b/tests/fixture_layouts/fresh.gen.ts @@ -4,41 +4,43 @@ import * as $0 from "./routes/_app.tsx"; import * as $1 from "./routes/_layout.tsx"; -import * as $2 from "./routes/files/js/_layout.js"; -import * as $3 from "./routes/files/js/index.js"; -import * as $4 from "./routes/files/jsx/_layout.jsx"; -import * as $5 from "./routes/files/jsx/index.jsx"; -import * as $6 from "./routes/files/ts/_layout.ts"; -import * as $7 from "./routes/files/ts/index.ts"; -import * as $8 from "./routes/files/tsx/_layout.tsx"; -import * as $9 from "./routes/files/tsx/index.tsx"; -import * as $10 from "./routes/foo/_layout.tsx"; -import * as $11 from "./routes/foo/bar.tsx"; -import * as $12 from "./routes/foo/index.tsx"; -import * as $13 from "./routes/index.tsx"; -import * as $14 from "./routes/other.tsx"; -import * as $15 from "./routes/skip/sub/_layout.tsx"; -import * as $16 from "./routes/skip/sub/index.tsx"; +import * as $2 from "./routes/_middleware.ts"; +import * as $3 from "./routes/files/js/_layout.js"; +import * as $4 from "./routes/files/js/index.js"; +import * as $5 from "./routes/files/jsx/_layout.jsx"; +import * as $6 from "./routes/files/jsx/index.jsx"; +import * as $7 from "./routes/files/ts/_layout.ts"; +import * as $8 from "./routes/files/ts/index.ts"; +import * as $9 from "./routes/files/tsx/_layout.tsx"; +import * as $10 from "./routes/files/tsx/index.tsx"; +import * as $11 from "./routes/foo/_layout.tsx"; +import * as $12 from "./routes/foo/bar.tsx"; +import * as $13 from "./routes/foo/index.tsx"; +import * as $14 from "./routes/index.tsx"; +import * as $15 from "./routes/other.tsx"; +import * as $16 from "./routes/skip/sub/_layout.tsx"; +import * as $17 from "./routes/skip/sub/index.tsx"; const manifest = { routes: { "./routes/_app.tsx": $0, "./routes/_layout.tsx": $1, - "./routes/files/js/_layout.js": $2, - "./routes/files/js/index.js": $3, - "./routes/files/jsx/_layout.jsx": $4, - "./routes/files/jsx/index.jsx": $5, - "./routes/files/ts/_layout.ts": $6, - "./routes/files/ts/index.ts": $7, - "./routes/files/tsx/_layout.tsx": $8, - "./routes/files/tsx/index.tsx": $9, - "./routes/foo/_layout.tsx": $10, - "./routes/foo/bar.tsx": $11, - "./routes/foo/index.tsx": $12, - "./routes/index.tsx": $13, - "./routes/other.tsx": $14, - "./routes/skip/sub/_layout.tsx": $15, - "./routes/skip/sub/index.tsx": $16, + "./routes/_middleware.ts": $2, + "./routes/files/js/_layout.js": $3, + "./routes/files/js/index.js": $4, + "./routes/files/jsx/_layout.jsx": $5, + "./routes/files/jsx/index.jsx": $6, + "./routes/files/ts/_layout.ts": $7, + "./routes/files/ts/index.ts": $8, + "./routes/files/tsx/_layout.tsx": $9, + "./routes/files/tsx/index.tsx": $10, + "./routes/foo/_layout.tsx": $11, + "./routes/foo/bar.tsx": $12, + "./routes/foo/index.tsx": $13, + "./routes/index.tsx": $14, + "./routes/other.tsx": $15, + "./routes/skip/sub/_layout.tsx": $16, + "./routes/skip/sub/index.tsx": $17, }, islands: {}, baseUrl: import.meta.url, diff --git a/tests/fixture_layouts/routes/_layout.tsx b/tests/fixture_layouts/routes/_layout.tsx index 0820eb80..62752ab8 100644 --- a/tests/fixture_layouts/routes/_layout.tsx +++ b/tests/fixture_layouts/routes/_layout.tsx @@ -1,8 +1,12 @@ import { LayoutProps } from "$fresh/server.ts"; +import { LayoutState } from "./_middleware.ts"; -export default function RootLayout({ Component }: LayoutProps) { +export default function RootLayout( + { Component, state }: LayoutProps, +) { return (
+ {state.something === "it works" ? "it works\n" : "it doesn't work\n"}
); diff --git a/tests/fixture_layouts/routes/_middleware.ts b/tests/fixture_layouts/routes/_middleware.ts new file mode 100644 index 00000000..e5cffa07 --- /dev/null +++ b/tests/fixture_layouts/routes/_middleware.ts @@ -0,0 +1,13 @@ +import { MiddlewareHandlerContext } from "$fresh/server.ts"; + +export type LayoutState = { + something: string; +}; + +export const handler = ( + _req: Request, + ctx: MiddlewareHandlerContext, +) => { + ctx.state.something = "it works"; + return ctx.next(); +}; diff --git a/tests/layouts_test.ts b/tests/layouts_test.ts index 157f632a..c735e7d0 100644 --- a/tests/layouts_test.ts +++ b/tests/layouts_test.ts @@ -1,3 +1,4 @@ +import { assert } from "./deps.ts"; import { assertSelector, fetchHtml, withFresh } from "./test_utils.ts"; Deno.test("apply root _layout and _app", async () => { @@ -5,6 +6,7 @@ Deno.test("apply root _layout and _app", async () => { "./tests/fixture_layouts/main.ts", async (address) => { const doc = await fetchHtml(address); + assert(doc.body.textContent?.includes("it works")); assertSelector(doc, ".app .root-layout .home-page"); const doc2 = await fetchHtml(`${address}/other`);