fix(server): make AppProps generic (#1449)

This commit is contained in:
Reed von Redwitz
2023-08-04 12:05:15 +02:00
committed by GitHub
parent 76f0f4a84a
commit 630f0a2322
4 changed files with 15 additions and 6 deletions
+3 -1
View File
@@ -162,7 +162,9 @@ export interface RouterState {
// --- APP ---
export interface AppProps extends PageProps {
// deno-lint-ignore no-explicit-any
export interface AppProps<T = any, S = Record<string, unknown>>
extends PageProps<T, S> {
Component: ComponentType<Record<never, never>>;
}
+7 -2
View File
@@ -1,11 +1,16 @@
import { Head } from "$fresh/runtime.ts";
import { AppProps } from "$fresh/server.ts";
export default function App(props: AppProps) {
export type TestState = {
root: string;
stateInProps: string;
};
export default function App(props: AppProps<unknown, TestState>) {
const statefulValue = props.state?.root === "root_mw"
? "The freshest framework!"
: "";
const specialCase = props.state?.stateInProps as string;
const specialCase = props.state?.stateInProps;
return (
<>
<Head>
@@ -1,8 +1,9 @@
import { MiddlewareHandlerContext } from "$fresh/server.ts";
import { TestState } from "../_app.tsx";
export async function handler(
_req: Request,
ctx: MiddlewareHandlerContext,
ctx: MiddlewareHandlerContext<TestState>,
) {
ctx.state.stateInProps = "look, i am set from middleware";
const resp = await ctx.next();
@@ -1,4 +1,5 @@
import { Handlers, PageProps } from "$fresh/server.ts";
import { TestState } from "../_app.tsx";
export const handler: Handlers<boolean> = {
GET(_, ctx) {
@@ -7,8 +8,8 @@ export const handler: Handlers<boolean> = {
},
};
export default function Page(props: PageProps<boolean>) {
let valueFromState = props.state.stateInProps as string;
export default function Page(props: PageProps<boolean, TestState>) {
let valueFromState = props.state.stateInProps;
if (props.data) {
valueFromState = valueFromState.toUpperCase();
}