refactor: changed Handlers type to MultiHandler (#1360)

This commit is contained in:
Aushwin
2023-06-28 12:52:48 +00:00
committed by GitHub
parent a30c6bdf5c
commit 851f8d60aa
26 changed files with 64 additions and 58 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.
Handlers are functions in the form of `Request => Response` or
MultiHandler 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`. 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.
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.
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 { Handlers } from "$fresh/server.ts";
import { MultiHandler } from "$fresh/server.ts";
export const handler: Handlers = {
export const handler: MultiHandler = {
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 { Handlers } from "$fresh/server.ts";
import { MultiHandler } from "$fresh/server.ts";
export const handler: Handlers = {
export const handler: MultiHandler = {
GET(_req) {
const uuid = crypto.randomUUID();
return new Response(JSON.stringify(uuid), {
+2 -2
View File
@@ -23,7 +23,7 @@ renders it in a page component.
```tsx
// routes/github/[username].tsx
import { Handlers, PageProps } from "$fresh/server.ts";
import { MultiHandler, PageProps } from "$fresh/server.ts";
interface User {
login: string;
@@ -31,7 +31,7 @@ interface User {
avatar_url: string;
}
export const handler: Handlers<User | null> = {
export const handler: MultiHandler<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 { Handlers, PageProps } from "$fresh/server.ts";
import { MultiHandler, PageProps } from "$fresh/server.ts";
const NAMES = ["Alice", "Bob", "Charlie", "Dave", "Eve", "Frank"];
@@ -41,7 +41,7 @@ interface Data {
query: string;
}
export const handler: Handlers<Data> = {
export const handler: MultiHandler<Data> = {
GET(req, ctx) {
const url = new URL(req.url);
const query = url.searchParams.get("q") || "";