From f3578f863ff4c0462fa3122aafef227c40045eb2 Mon Sep 17 00:00:00 2001 From: Christos Hrousis Date: Sat, 2 Dec 2023 22:14:37 +1100 Subject: [PATCH] docs: Remove references to deprecated types (#2125) As per the [1.6 release](https://deno.com/blog/fresh-1.6#simplified-typings), and commentary by @csvn [here](https://github.com/denoland/fresh/issues/2108#issuecomment-1836625789), this PR aims to replace the references to deprecated typing in the documentation. Please see inline commentary for parts I'm unclear on. --- docs/latest/concepts/app-wrapper.md | 10 +++++----- docs/latest/concepts/data-fetching.md | 8 ++++---- docs/latest/concepts/error-pages.md | 12 ++++++------ docs/latest/concepts/layouts.md | 8 ++++---- docs/latest/concepts/middleware.md | 19 +++++++++++-------- docs/latest/concepts/partials.md | 4 ++-- docs/latest/examples/dealing-with-cors.md | 8 ++++---- .../examples/handling-complex-routes.md | 8 ++++---- docs/latest/examples/init-the-server.md | 4 ++-- docs/latest/examples/using-csp.md | 4 ++-- 10 files changed, 44 insertions(+), 41 deletions(-) diff --git a/docs/latest/concepts/app-wrapper.md b/docs/latest/concepts/app-wrapper.md index 13b51ff3..8c187684 100644 --- a/docs/latest/concepts/app-wrapper.md +++ b/docs/latest/concepts/app-wrapper.md @@ -14,9 +14,9 @@ template which can be conditioned based on state and params. Note that any state set by middleware is available via `props.state`. ```tsx routes/_app.tsx -import { AppProps } from "$fresh/server.ts"; +import { PageProps } from "$fresh/server.ts"; -export default function App({ Component, state }: AppProps) { +export default function App({ Component, state }: PageProps) { // do something with state here return ( @@ -37,12 +37,12 @@ export default function App({ Component, state }: AppProps) { Similar to routes and layouts, the app wrapper can be made asynchronous. This changes the function signature so that the first argument is the `Request` -instance and the second one is the `AppContext`. +instance and the second one is the `FreshContext`. ```tsx routes/_app.tsx -import { AppContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; -export default async function App(req: Request, ctx: AppContext) { +export default async function App(req: Request, ctx: FreshContext) { const data = await loadData(); return ( diff --git a/docs/latest/concepts/data-fetching.md b/docs/latest/concepts/data-fetching.md index 2504b597..2ddc4abc 100644 --- a/docs/latest/concepts/data-fetching.md +++ b/docs/latest/concepts/data-fetching.md @@ -36,7 +36,7 @@ export default function ProjectPage(props: PageProps) { } ``` -The type parameter on the `PageProps`, `Handlers`, `Handler`, and -`HandlerContext` can be used to enforce a TypeScript type to use for the render -data. Fresh enforces during type checking that the types in all of these fields -are compatible within a single page. +The type parameter on the `PageProps`, `Handlers`, `Handler`, and `FreshContext` +can be used to enforce a TypeScript type to use for the render data. Fresh +enforces during type checking that the types in all of these fields are +compatible within a single page. diff --git a/docs/latest/concepts/error-pages.md b/docs/latest/concepts/error-pages.md index e657d7df..20852b04 100644 --- a/docs/latest/concepts/error-pages.md +++ b/docs/latest/concepts/error-pages.md @@ -12,12 +12,12 @@ throws an error respectively. The 404 page can be customized by creating a `_404.tsx` file in the `routes/` folder. The file must have a default export that is a regular Preact component. -A props object of type `UnknownPageProps` is passed in as an argument. +A props object of type `PageProps` is passed in as an argument. ```tsx routes/_404.tsx -import { UnknownPageProps } from "$fresh/server.ts"; +import { PageProps } from "$fresh/server.ts"; -export default function NotFoundPage({ url }: UnknownPageProps) { +export default function NotFoundPage({ url }: PageProps) { return

404 not found: {url.pathname}

; } ``` @@ -71,12 +71,12 @@ export const handler: Handlers = { The 500 page can be customized by creating a `_500.tsx` file in the `routes/` folder. The file must have a default export that is a regular Preact component. -A props object of type `ErrorPageProps` is passed in as an argument. +A props object of type `PageProps` is passed in as an argument. ```tsx routes/_500.tsx -import { ErrorPageProps } from "$fresh/server.ts"; +import { PageProps } from "$fresh/server.ts"; -export default function Error500Page({ error }: ErrorPageProps) { +export default function Error500Page({ error }: PageProps) { return

500 internal error: {(error as Error).message}

; } ``` diff --git a/docs/latest/concepts/layouts.md b/docs/latest/concepts/layouts.md index e407e7e2..df4617a3 100644 --- a/docs/latest/concepts/layouts.md +++ b/docs/latest/concepts/layouts.md @@ -25,9 +25,9 @@ template which can be conditioned based on state and params. Note that any state set by middleware is available via `props.state`. ```tsx routes/sub/_layout.tsx -import { LayoutProps } from "$fresh/server.ts"; +import { PageProps } from "$fresh/server.ts"; -export default function Layout({ Component, state }: LayoutProps) { +export default function Layout({ Component, state }: PageProps) { // do something with state here return (
@@ -43,9 +43,9 @@ In case you need to fetch data asynchronously before rendering the layout, you can use an async layout to do so. ```tsx routes/sub/_layout.tsx -import { LayoutProps } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; -export default async function Layout(req: Request, ctx: LayoutContext) { +export default async function Layout(req: Request, ctx: FreshContext) { // do something with state here const data = await loadData(); diff --git a/docs/latest/concepts/middleware.md b/docs/latest/concepts/middleware.md index 6191678a..050a5837 100644 --- a/docs/latest/concepts/middleware.md +++ b/docs/latest/concepts/middleware.md @@ -18,7 +18,7 @@ properties, e.g. `ctx.state.loggedIn = true`, but you can also replace the entire object like `ctx.state = { loggedIn: true }`. ```ts routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; interface State { data: string; @@ -26,7 +26,7 @@ interface State { export async function handler( req: Request, - ctx: MiddlewareHandlerContext, + ctx: FreshContext, ) { ctx.state.data = "myData"; const resp = await ctx.next(); @@ -100,9 +100,9 @@ It should be noted that `middleware` has access to route parameters. If you're running a fictitious `routes/[tenant]/admin/_middleware.ts` like this: ```ts routes/[tenant]/admin/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; -export async function handler(_req: Request, ctx: MiddlewareHandlerContext) { +export async function handler(_req: Request, ctx: FreshContext) { const currentTenant = ctx.params.tenant; //do something with the tenant const resp = await ctx.next(); @@ -115,10 +115,12 @@ the value of `acme` in your middleware. ## Middleware Destination -To set the stage for this section, `MiddlewareHandlerContext` looks like this: +To set the stage for this section, let's focus on the part of `FreshContext` +that looks like this: ```ts -export interface MiddlewareHandlerContext> { +export interface FreshContext> { + ... next: () => Promise; state: State; destination: router.DestinationKind; @@ -127,6 +129,7 @@ export interface MiddlewareHandlerContext> { hostname: string; port: number; }; + ... } ``` @@ -145,9 +148,9 @@ Initiate a new Fresh project (`deno run -A -r https://fresh.deno.dev/`) and then create a `_middleware.ts` file in the `routes` folder like this: ```ts routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; -export async function handler(req: Request, ctx: MiddlewareHandlerContext) { +export async function handler(req: Request, ctx: FreshContext) { console.log(ctx.destination); console.log(req.url); const resp = await ctx.next(); diff --git a/docs/latest/concepts/partials.md b/docs/latest/concepts/partials.md index 96077e62..bfe7a93e 100644 --- a/docs/latest/concepts/partials.md +++ b/docs/latest/concepts/partials.md @@ -18,10 +18,10 @@ The quickest way to get started is to enable partials for every page in `routes/_app.tsx` by making the following changes. ```diff routes/_app.tsx - import { AppProps } from "$fresh/server.ts"; + import { PageProps } from "$fresh/server.ts"; + import { Partial } from "$fresh/runtime.ts"; - export default function App({ Component }: AppProps) { + export default function App({ Component }: PageProps) { return ( diff --git a/docs/latest/examples/dealing-with-cors.md b/docs/latest/examples/dealing-with-cors.md index 0f9061a7..4fec6ce6 100644 --- a/docs/latest/examples/dealing-with-cors.md +++ b/docs/latest/examples/dealing-with-cors.md @@ -16,9 +16,9 @@ requests. You can CORS enable all the routes affected by some `middleware` by doing the following: ```ts routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; -export async function handler(req: Request, ctx: MiddlewareHandlerContext) { +export async function handler(req: Request, ctx: FreshContext) { const origin = req.headers.get("Origin") || "*"; const resp = await ctx.next(); const headers = resp.headers; @@ -45,9 +45,9 @@ deal with "preflight requests". Let's imagine you're trying to support a `DELETE` route. Then you'd need to do something like this: ```ts routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; -export async function handler(_req: Request, ctx: MiddlewareHandlerContext) { +export async function handler(_req: Request, ctx: FreshContext) { if (_req.method == "OPTIONS") { const resp = new Response(null, { status: 204, diff --git a/docs/latest/examples/handling-complex-routes.md b/docs/latest/examples/handling-complex-routes.md index ccd975ba..757c5ded 100644 --- a/docs/latest/examples/handling-complex-routes.md +++ b/docs/latest/examples/handling-complex-routes.md @@ -17,10 +17,10 @@ Let's look at the example from the routing page more closely. We'll flesh out the handler so that we end up with something like the following: ```ts routes/x.tsx -import { HandlerContext, RouteConfig } from "$fresh/server.ts"; +import { FreshContext, RouteConfig } from "$fresh/server.ts"; export const handler = { - GET(_req: Request, { params }: HandlerContext) { + GET(_req: Request, { params }: FreshContext) { console.log(params); return new Response(params.path); }, @@ -48,10 +48,10 @@ show the following: Let's look at something a bit more complex: ```ts routes/api.tsx -import { HandlerContext, RouteConfig } from "$fresh/server.ts"; +import { FreshContext, RouteConfig } from "$fresh/server.ts"; export const handler = { - GET(_req: Request, { params }: HandlerContext) { + GET(_req: Request, { params }: FreshContext) { console.log(params); return new Response(params.path); }, diff --git a/docs/latest/examples/init-the-server.md b/docs/latest/examples/init-the-server.md index ef37047e..f7fc7e58 100644 --- a/docs/latest/examples/init-the-server.md +++ b/docs/latest/examples/init-the-server.md @@ -35,7 +35,7 @@ export default defineConfig({ But what's going on in this new `_middleware.ts` we've created? ```ts routes/_middleware.ts -import { MiddlewareHandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; export interface State { context: Context; @@ -64,7 +64,7 @@ export class Context { export async function handler( _req: Request, - ctx: MiddlewareHandlerContext, + ctx: FreshContext, ) { ctx.state.context = Context.instance(); if (ctx.destination === "route") { diff --git a/docs/latest/examples/using-csp.md b/docs/latest/examples/using-csp.md index 5e3b5ee6..7393104b 100644 --- a/docs/latest/examples/using-csp.md +++ b/docs/latest/examples/using-csp.md @@ -324,10 +324,10 @@ export const config: RouteConfig = { ``` ```ts routes/reportHandler.ts -import { HandlerContext } from "$fresh/server.ts"; +import { FreshContext } from "$fresh/server.ts"; export const handler = { - async POST(req: Request, _ctx: HandlerContext) { + async POST(req: Request, _ctx: FreshContext) { const body = await req.json(); const report = JSON.stringify(body, null, 2);