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.
This commit is contained in:
Christos Hrousis
2023-12-02 11:14:37 +00:00
committed by GitHub
parent 6eccdcf1a1
commit f3578f863f
10 changed files with 44 additions and 41 deletions
+5 -5
View File
@@ -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`. set by middleware is available via `props.state`.
```tsx routes/_app.tsx ```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 // do something with state here
return ( return (
<html> <html>
@@ -37,12 +37,12 @@ export default function App({ Component, state }: AppProps) {
Similar to routes and layouts, the app wrapper can be made asynchronous. This 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` 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 ```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(); const data = await loadData();
return ( return (
+4 -4
View File
@@ -36,7 +36,7 @@ export default function ProjectPage(props: PageProps<Project>) {
} }
``` ```
The type parameter on the `PageProps`, `Handlers`, `Handler`, and The type parameter on the `PageProps`, `Handlers`, `Handler`, and `FreshContext`
`HandlerContext` can be used to enforce a TypeScript type to use for the render can be used to enforce a TypeScript type to use for the render data. Fresh
data. Fresh enforces during type checking that the types in all of these fields enforces during type checking that the types in all of these fields are
are compatible within a single page. compatible within a single page.
+6 -6
View File
@@ -12,12 +12,12 @@ throws an error respectively.
The 404 page can be customized by creating a `_404.tsx` file in the `routes/` 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. 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 ```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 <p>404 not found: {url.pathname}</p>; return <p>404 not found: {url.pathname}</p>;
} }
``` ```
@@ -71,12 +71,12 @@ export const handler: Handlers = {
The 500 page can be customized by creating a `_500.tsx` file in the `routes/` 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. 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 ```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 <p>500 internal error: {(error as Error).message}</p>; return <p>500 internal error: {(error as Error).message}</p>;
} }
``` ```
+4 -4
View File
@@ -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`. set by middleware is available via `props.state`.
```tsx routes/sub/_layout.tsx ```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 // do something with state here
return ( return (
<div class="layout"> <div class="layout">
@@ -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. can use an async layout to do so.
```tsx routes/sub/_layout.tsx ```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 // do something with state here
const data = await loadData(); const data = await loadData();
+11 -8
View File
@@ -18,7 +18,7 @@ properties, e.g. `ctx.state.loggedIn = true`, but you can also replace the
entire object like `ctx.state = { loggedIn: true }`. entire object like `ctx.state = { loggedIn: true }`.
```ts routes/_middleware.ts ```ts routes/_middleware.ts
import { MiddlewareHandlerContext } from "$fresh/server.ts"; import { FreshContext } from "$fresh/server.ts";
interface State { interface State {
data: string; data: string;
@@ -26,7 +26,7 @@ interface State {
export async function handler( export async function handler(
req: Request, req: Request,
ctx: MiddlewareHandlerContext<State>, ctx: FreshContext<State>,
) { ) {
ctx.state.data = "myData"; ctx.state.data = "myData";
const resp = await ctx.next(); 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: running a fictitious `routes/[tenant]/admin/_middleware.ts` like this:
```ts routes/[tenant]/admin/_middleware.ts ```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; const currentTenant = ctx.params.tenant;
//do something with the tenant //do something with the tenant
const resp = await ctx.next(); const resp = await ctx.next();
@@ -115,10 +115,12 @@ the value of `acme` in your middleware.
## Middleware Destination ## 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 ```ts
export interface MiddlewareHandlerContext<State = Record<string, unknown>> { export interface FreshContext<State = Record<string, unknown>> {
...
next: () => Promise<Response>; next: () => Promise<Response>;
state: State; state: State;
destination: router.DestinationKind; destination: router.DestinationKind;
@@ -127,6 +129,7 @@ export interface MiddlewareHandlerContext<State = Record<string, unknown>> {
hostname: string; hostname: string;
port: number; 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: create a `_middleware.ts` file in the `routes` folder like this:
```ts routes/_middleware.ts ```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(ctx.destination);
console.log(req.url); console.log(req.url);
const resp = await ctx.next(); const resp = await ctx.next();
+2 -2
View File
@@ -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. `routes/_app.tsx` by making the following changes.
```diff routes/_app.tsx ```diff routes/_app.tsx
import { AppProps } from "$fresh/server.ts"; import { PageProps } from "$fresh/server.ts";
+ import { Partial } from "$fresh/runtime.ts"; + import { Partial } from "$fresh/runtime.ts";
export default function App({ Component }: AppProps) { export default function App({ Component }: PageProps) {
return ( return (
<html> <html>
<head> <head>
+4 -4
View File
@@ -16,9 +16,9 @@ requests. You can CORS enable all the routes affected by some `middleware` by
doing the following: doing the following:
```ts routes/_middleware.ts ```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 origin = req.headers.get("Origin") || "*";
const resp = await ctx.next(); const resp = await ctx.next();
const headers = resp.headers; 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: `DELETE` route. Then you'd need to do something like this:
```ts routes/_middleware.ts ```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") { if (_req.method == "OPTIONS") {
const resp = new Response(null, { const resp = new Response(null, {
status: 204, status: 204,
@@ -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: the handler so that we end up with something like the following:
```ts routes/x.tsx ```ts routes/x.tsx
import { HandlerContext, RouteConfig } from "$fresh/server.ts"; import { FreshContext, RouteConfig } from "$fresh/server.ts";
export const handler = { export const handler = {
GET(_req: Request, { params }: HandlerContext) { GET(_req: Request, { params }: FreshContext) {
console.log(params); console.log(params);
return new Response(params.path); return new Response(params.path);
}, },
@@ -48,10 +48,10 @@ show the following:
Let's look at something a bit more complex: Let's look at something a bit more complex:
```ts routes/api.tsx ```ts routes/api.tsx
import { HandlerContext, RouteConfig } from "$fresh/server.ts"; import { FreshContext, RouteConfig } from "$fresh/server.ts";
export const handler = { export const handler = {
GET(_req: Request, { params }: HandlerContext) { GET(_req: Request, { params }: FreshContext) {
console.log(params); console.log(params);
return new Response(params.path); return new Response(params.path);
}, },
+2 -2
View File
@@ -35,7 +35,7 @@ export default defineConfig({
But what's going on in this new `_middleware.ts` we've created? But what's going on in this new `_middleware.ts` we've created?
```ts routes/_middleware.ts ```ts routes/_middleware.ts
import { MiddlewareHandlerContext } from "$fresh/server.ts"; import { FreshContext } from "$fresh/server.ts";
export interface State { export interface State {
context: Context; context: Context;
@@ -64,7 +64,7 @@ export class Context {
export async function handler( export async function handler(
_req: Request, _req: Request,
ctx: MiddlewareHandlerContext<State>, ctx: FreshContext<State>,
) { ) {
ctx.state.context = Context.instance(); ctx.state.context = Context.instance();
if (ctx.destination === "route") { if (ctx.destination === "route") {
+2 -2
View File
@@ -324,10 +324,10 @@ export const config: RouteConfig = {
``` ```
```ts routes/reportHandler.ts ```ts routes/reportHandler.ts
import { HandlerContext } from "$fresh/server.ts"; import { FreshContext } from "$fresh/server.ts";
export const handler = { export const handler = {
async POST(req: Request, _ctx: HandlerContext) { async POST(req: Request, _ctx: FreshContext) {
const body = await req.json(); const body = await req.json();
const report = JSON.stringify(body, null, 2); const report = JSON.stringify(body, null, 2);