fix: change MultiHandler back to Handlers (#1396)

This commit is contained in:
Marvin Hagemeister
2023-07-03 09:57:46 +02:00
committed by GitHub
parent eb53d53ab8
commit 16be99e69b
32 changed files with 135 additions and 66 deletions
+8 -8
View File
@@ -7,7 +7,7 @@ description: |
Routes actually consist of two parts: handlers, and the page component. Up to
now, only the page component has been discussed in this chapter.
MultiHandler are functions in the form of `Request => Response` or
Handlers are functions in the form of `Request => Response` or
`Request => Promise<Response>` that are called when a request is made to a
particular route. There can be one handler that covers all HTTP methods or one
handler per method.
@@ -20,9 +20,9 @@ define a custom handler use a default handler that just renders the page
component.
To define a handler in a route module, one must export it as a named export with
the name `handler`. MultiHandler can have two forms: a plain function (catchall
for all HTTP methods) or a plain object where each property is a function named
by the HTTP method it handles.
the name `handler`. Handlers can have two forms: a plain function (catchall for
all HTTP methods) or a plain object where each property is a function named by
the HTTP method it handles.
Here is an example of a custom `GET` handler that renders the page component and
then adds a custom header to the response before returning it:
@@ -30,9 +30,9 @@ then adds a custom header to the response before returning it:
```tsx
// routes/about.tsx
import { MultiHandler } from "$fresh/server.ts";
import { Handlers } from "$fresh/server.ts";
export const handler: MultiHandler = {
export const handler: Handlers = {
async GET(_req, ctx) {
const resp = await ctx.render();
resp.headers.set("X-Custom-Header", "Hello");
@@ -57,9 +57,9 @@ response:
```ts
// routes/api/random-uuid.ts
import { MultiHandler } from "$fresh/server.ts";
import { Handlers } from "$fresh/server.ts";
export const handler: MultiHandler = {
export const handler: Handlers = {
GET(_req) {
const uuid = crypto.randomUUID();
return new Response(JSON.stringify(uuid), {
+3 -3
View File
@@ -1,7 +1,7 @@
---
description: |
Fetch data for routes dynamically by creating a custom handler and passing
data to the render function.
data to the render function.
---
All of the pages in the demo project so far have not used any dynamic data
@@ -23,7 +23,7 @@ renders it in a page component.
```tsx
// routes/github/[username].tsx
import { MultiHandler, PageProps } from "$fresh/server.ts";
import { Handlers, PageProps } from "$fresh/server.ts";
interface User {
login: string;
@@ -31,7 +31,7 @@ interface User {
avatar_url: string;
}
export const handler: MultiHandler<User | null> = {
export const handler: Handlers<User | null> = {
async GET(_, ctx) {
const { username } = ctx.params;
const resp = await fetch(`https://api.github.com/users/${username}`);
+2 -2
View File
@@ -32,7 +32,7 @@ server side:
```tsx
// routes/search.tsx
import { MultiHandler, PageProps } from "$fresh/server.ts";
import { Handlers, PageProps } from "$fresh/server.ts";
const NAMES = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"];
@@ -41,7 +41,7 @@ interface Data {
query: string;
}
export const handler: MultiHandler<Data> = {
export const handler: Handlers<Data> = {
GET(req, ctx) {
const url = new URL(req.url);
const query = url.searchParams.get("q") || "";