refactor: changed Handlers type to MultiHandler (#1360)
This commit is contained in:
@@ -16,7 +16,7 @@ interface Project {
|
||||
stars: number;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Project> = {
|
||||
export const handler: MultiHandler<Project> = {
|
||||
async GET(_req, ctx) {
|
||||
const project = await db.projects.findOne({ id: ctx.params.id });
|
||||
if (!project) {
|
||||
@@ -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`, `MultiHandler`, `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.
|
||||
|
||||
@@ -30,9 +30,9 @@ example when the route did match, but the requested resource does not exist.
|
||||
This can be achieved with `ctx.renderNotFound`.
|
||||
|
||||
```tsx
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
async GET(req, ctx) {
|
||||
const blogpost = await fetchBlogpost(ctx.params.slug);
|
||||
if (!blogpost) {
|
||||
|
||||
@@ -20,9 +20,9 @@ submissions:
|
||||
|
||||
```tsx
|
||||
// routes/subscribe.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) {
|
||||
return await ctx.render();
|
||||
},
|
||||
|
||||
@@ -33,7 +33,7 @@ export async function handler(
|
||||
|
||||
```ts
|
||||
// routes/myHandler.ts
|
||||
export const handler: Handlers<any, { data: string }> = {
|
||||
export const handler: MultiHandler<any, { data: string }> = {
|
||||
GET(_req, ctx) {
|
||||
return new Response(`middleware data is ${ctx.state.data}`);
|
||||
},
|
||||
|
||||
@@ -27,9 +27,9 @@ Let's look at a basic route that returns a plain text string:
|
||||
```tsx
|
||||
// routes/plain.tsx
|
||||
|
||||
import { HandlerContext, Handlers } from "$fresh/server.ts";
|
||||
import { HandlerContext, MultiHandler } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET(_req: Request, _ctx: HandlerContext) {
|
||||
return new Response("Hello World");
|
||||
},
|
||||
@@ -73,9 +73,9 @@ response after rendering the page component.
|
||||
```tsx
|
||||
// routes/html.tsx
|
||||
|
||||
import { HandlerContext, Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { HandlerContext, MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
async GET(_req: Request, ctx: HandlerContext) {
|
||||
const resp = await ctx.render();
|
||||
resp.headers.set("X-Custom-Header", "Hello World");
|
||||
|
||||
@@ -12,9 +12,9 @@ through
|
||||
```tsx
|
||||
// routes/index.tsx
|
||||
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { MultiHandler } from "$fresh/server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
async POST(req) {
|
||||
const form = await req.formData();
|
||||
|
||||
|
||||
@@ -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), {
|
||||
|
||||
@@ -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}`);
|
||||
|
||||
@@ -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") || "";
|
||||
|
||||
@@ -22,6 +22,7 @@ export type {
|
||||
Handlers,
|
||||
MiddlewareHandler,
|
||||
MiddlewareHandlerContext,
|
||||
MultiHandler,
|
||||
PageProps,
|
||||
Plugin,
|
||||
PluginAsyncRenderContext,
|
||||
|
||||
+8
-3
@@ -102,14 +102,19 @@ export type Handler<T = any, State = Record<string, unknown>> = (
|
||||
) => Response | Promise<Response>;
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export type Handlers<T = any, State = Record<string, unknown>> = {
|
||||
export type MultiHandler<T = any, State = Record<string, unknown>> = {
|
||||
[K in router.KnownMethod]?: Handler<T, State>;
|
||||
};
|
||||
|
||||
/**
|
||||
* @deprecated This type has been deprecated and is replaced with MultiHandler type.
|
||||
*/
|
||||
export type Handlers<T> = MultiHandler<T>;
|
||||
|
||||
export interface RouteModule {
|
||||
default?: ComponentType<PageProps>;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler?: Handler<any, any> | Handlers<any, any>;
|
||||
handler?: Handler<any, any> | MultiHandler<any, any>;
|
||||
config?: RouteConfig;
|
||||
}
|
||||
|
||||
@@ -119,7 +124,7 @@ export interface Route<Data = any> {
|
||||
url: string;
|
||||
name: string;
|
||||
component?: ComponentType<PageProps<Data>>;
|
||||
handler: Handler<Data> | Handlers<Data>;
|
||||
handler: Handler<Data> | MultiHandler<Data>;
|
||||
csp: boolean;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Handlers } from "../../../server.ts";
|
||||
import { MultiHandler } from "../../../server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET(_req, ctx) {
|
||||
return new Response((ctx.remoteAddr as Deno.NetAddr).hostname);
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
|
||||
interface Data extends Record<string, unknown> {
|
||||
info: string;
|
||||
@@ -8,7 +8,7 @@ export default function Page({ data }: PageProps<Data>) {
|
||||
return <div>{data.info}</div>;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data> = {
|
||||
export const handler: MultiHandler<Data> = {
|
||||
GET(req, { render }) {
|
||||
if (req.headers.get("accept")?.includes("text/html")) {
|
||||
return render({
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Handlers } from "../../../../../server.ts";
|
||||
import { MultiHandler } from "../../../../../server.ts";
|
||||
|
||||
interface State {
|
||||
root: string;
|
||||
@@ -6,7 +6,7 @@ interface State {
|
||||
layer3: string;
|
||||
}
|
||||
|
||||
export const handler: Handlers<undefined, State> = {
|
||||
export const handler: MultiHandler<undefined, State> = {
|
||||
GET(_req: Request, { state }) {
|
||||
return new Response(JSON.stringify(state));
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Handlers } from "../../../../../server.ts";
|
||||
import { MultiHandler } from "../../../../../server.ts";
|
||||
|
||||
interface State {
|
||||
root: string;
|
||||
@@ -6,7 +6,7 @@ interface State {
|
||||
layer2: string;
|
||||
}
|
||||
|
||||
export const handler: Handlers<undefined, State> = {
|
||||
export const handler: MultiHandler<undefined, State> = {
|
||||
GET(_req: Request, { state }) {
|
||||
return new Response(JSON.stringify(state));
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Handlers } from "../../../../../server.ts";
|
||||
import { MultiHandler } from "../../../../../server.ts";
|
||||
|
||||
interface State {
|
||||
root: string;
|
||||
@@ -6,7 +6,7 @@ interface State {
|
||||
layer2: string;
|
||||
}
|
||||
|
||||
export const handler: Handlers<undefined, State> = {
|
||||
export const handler: MultiHandler<undefined, State> = {
|
||||
GET(_req: Request, { state }) {
|
||||
return new Response(JSON.stringify(state));
|
||||
},
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Handlers } from "../../../../../../server.ts";
|
||||
import { MultiHandler } from "../../../../../../server.ts";
|
||||
|
||||
interface State {
|
||||
root: string;
|
||||
@@ -7,7 +7,7 @@ interface State {
|
||||
layer3: string;
|
||||
}
|
||||
|
||||
export const handler: Handlers<undefined, State> = {
|
||||
export const handler: MultiHandler<undefined, State> = {
|
||||
GET(_req: Request, { state }) {
|
||||
return new Response(JSON.stringify(state));
|
||||
},
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { Handlers } from "../../../server.ts";
|
||||
import { MultiHandler } from "../../../server.ts";
|
||||
|
||||
interface State {
|
||||
root: string;
|
||||
}
|
||||
|
||||
export const handler: Handlers<undefined, State> = {
|
||||
export const handler: MultiHandler<undefined, State> = {
|
||||
GET(_req: Request, { state }) {
|
||||
return new Response(JSON.stringify(state));
|
||||
},
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Handlers } from "../../../server.ts";
|
||||
import { MultiHandler } from "../../../server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET(_req, ctx) {
|
||||
return ctx.renderNotFound({
|
||||
hello: "Dino",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Handlers } from "../../../server.ts";
|
||||
import { MultiHandler } from "../../../server.ts";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET(_req, ctx) {
|
||||
return new Response(ctx.params.all);
|
||||
},
|
||||
|
||||
@@ -3,13 +3,13 @@ import Header from "../components/Header.tsx";
|
||||
import ComponentGallery from "../islands/ComponentGallery.tsx";
|
||||
|
||||
import { asset, Head } from "$fresh/runtime.ts";
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
|
||||
function getSource(path: string) {
|
||||
return Deno.readTextFile(new URL(path, import.meta.url));
|
||||
}
|
||||
|
||||
export const handler: Handlers<HomeProps> = {
|
||||
export const handler: MultiHandler<HomeProps> = {
|
||||
async GET(req, ctx) {
|
||||
const props: HomeProps = {
|
||||
sources: {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { asset, Head } from "$fresh/runtime.ts";
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
import { frontMatter, gfm } from "../../utils/markdown.ts";
|
||||
|
||||
import Header from "../../components/Header.tsx";
|
||||
@@ -21,7 +21,7 @@ interface Page extends TableOfContentsEntry {
|
||||
data: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export const handler: Handlers<Data> = {
|
||||
export const handler: MultiHandler<Data> = {
|
||||
async GET(_req, ctx) {
|
||||
const slug = ctx.params.slug;
|
||||
if (slug === "") {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { MultiHandler } from "$fresh/server.ts";
|
||||
import { gfm } from "../utils/markdown.ts";
|
||||
|
||||
// TODO(lucacasonato): hash the file and use the hash as the filename, and serve
|
||||
@@ -36,7 +36,7 @@ ol.nested li:before {
|
||||
}
|
||||
`;
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET: () => {
|
||||
return new Response(CSS, {
|
||||
headers: {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { asset, Head } from "$fresh/runtime.ts";
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
import Counter from "../islands/Counter.tsx";
|
||||
import LemonDrop from "../islands/LemonDrop.tsx";
|
||||
import Footer from "../components/Footer.tsx";
|
||||
@@ -18,7 +18,7 @@ function isOpenGraphUA(header: string | null): boolean {
|
||||
return header.startsWith("Twitterbot") || header.startsWith("Slackbot");
|
||||
}
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET(req, ctx) {
|
||||
const accept = req.headers.get("accept");
|
||||
const userAgent = req.headers.get("user-agent");
|
||||
|
||||
+2
-2
@@ -1,11 +1,11 @@
|
||||
import { RouteConfig } from "$fresh/server.ts";
|
||||
import { Handlers } from "$fresh/server.ts";
|
||||
import { MultiHandler } from "$fresh/server.ts";
|
||||
import { parse } from "$std/semver/mod.ts";
|
||||
import VERSIONS from "../../versions.json" assert { type: "json" };
|
||||
|
||||
const BASE_URL = "https://raw.githubusercontent.com/denoland/fresh/";
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
async GET(req, ctx) {
|
||||
const accept = req.headers.get("Accept");
|
||||
const isHTML = accept?.includes("text/html");
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||
import { MultiHandler, PageProps } from "$fresh/server.ts";
|
||||
import VERSIONS from "../../versions.json" assert { type: "json" };
|
||||
|
||||
export const handler: Handlers = {
|
||||
export const handler: MultiHandler = {
|
||||
GET(req) {
|
||||
const accept = req.headers.get("accept");
|
||||
let path = "/docs/concepts/updating";
|
||||
|
||||
Reference in New Issue
Block a user