From adfdec548abec24b021c3b405e9a2dfe0e3e65b6 Mon Sep 17 00:00:00 2001 From: Leo Kettmeir Date: Sun, 6 Mar 2022 20:27:02 +0100 Subject: [PATCH] refactor: `HandlerContext.match` to `HandlerContext.params` (#118) --- src/server/context.ts | 14 ++++---- src/server/types.ts | 2 +- tests/fixture/fresh.gen.ts | 62 +++++++++++++++++---------------- tests/fixture/routes/params.tsx | 14 ++++++++ tests/main_test.ts | 10 ++++++ 5 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 tests/fixture/routes/params.tsx diff --git a/src/server/context.ts b/src/server/context.ts index 350a32f8..3953e517 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -358,15 +358,15 @@ export class ServerContext { for (const page of this.#pages) { const createRender = genRender(page, 200); if (typeof page.handler === "function") { - routes[page.route] = (req, match) => + routes[page.route] = (req, params) => (page.handler as Handler)(req, { - match, - render: createRender(req, match), + params, + render: createRender(req, params), }); } else { for (const [method, handler] of Object.entries(page.handler)) { - routes[`${method}@${page.route}`] = (req, match) => - handler(req, { match, render: createRender(req, match) }); + routes[`${method}@${page.route}`] = (req, params) => + handler(req, { params, render: createRender(req, params) }); } } } @@ -456,8 +456,8 @@ export class ServerContext { * constants.INTERNAL_PREFIX */ #bundleAssetRoute = (): router.MatchHandler => { - return async (_req, match) => { - const path = `/${match.path}`; + return async (_req, params) => { + const path = `/${params.path}`; const file = await this.#bundler.get(path); let res; if (file) { diff --git a/src/server/types.ts b/src/server/types.ts index a0befe60..ef906771 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -33,7 +33,7 @@ export interface IslandModule { } export interface HandlerContext { - match: Record; + params: Record; render: (data?: T) => Response; } diff --git a/tests/fixture/fresh.gen.ts b/tests/fixture/fresh.gen.ts index 6733857c..79364b54 100644 --- a/tests/fixture/fresh.gen.ts +++ b/tests/fixture/fresh.gen.ts @@ -2,40 +2,42 @@ // This file SHOULD be checked into source version control. // To update this file, run `fresh routes`. -import * as $0 from "./routes/_middleware.ts"; -import * as $1 from "./routes/_404.tsx"; -import * as $2 from "./routes/failure.ts"; -import * as $3 from "./routes/api/get_only.ts"; -import * as $4 from "./routes/books/[id].tsx"; -import * as $5 from "./routes/_render.ts"; -import * as $6 from "./routes/props/[id].tsx"; -import * as $7 from "./routes/_app.tsx"; -import * as $8 from "./routes/static.tsx"; -import * as $9 from "./routes/intercept_args.tsx"; -import * as $10 from "./routes/wildcard.tsx"; -import * as $11 from "./routes/intercept.tsx"; -import * as $12 from "./routes/[name].tsx"; -import * as $13 from "./routes/_500.tsx"; -import * as $14 from "./routes/index.tsx"; +import * as $0 from "./routes/index.tsx"; +import * as $1 from "./routes/params.tsx"; +import * as $2 from "./routes/static.tsx"; +import * as $3 from "./routes/intercept_args.tsx"; +import * as $4 from "./routes/_render.ts"; +import * as $5 from "./routes/props/[id].tsx"; +import * as $6 from "./routes/failure.ts"; +import * as $7 from "./routes/_middleware.ts"; +import * as $8 from "./routes/books/[id].tsx"; +import * as $9 from "./routes/wildcard.tsx"; +import * as $10 from "./routes/api/get_only.ts"; +import * as $11 from "./routes/_500.tsx"; +import * as $12 from "./routes/intercept.tsx"; +import * as $13 from "./routes/_404.tsx"; +import * as $14 from "./routes/[name].tsx"; +import * as $15 from "./routes/_app.tsx"; import * as $$0 from "./islands/Test.tsx"; const manifest = { routes: { - "./routes/_middleware.ts": $0, - "./routes/_404.tsx": $1, - "./routes/failure.ts": $2, - "./routes/api/get_only.ts": $3, - "./routes/books/[id].tsx": $4, - "./routes/_render.ts": $5, - "./routes/props/[id].tsx": $6, - "./routes/_app.tsx": $7, - "./routes/static.tsx": $8, - "./routes/intercept_args.tsx": $9, - "./routes/wildcard.tsx": $10, - "./routes/intercept.tsx": $11, - "./routes/[name].tsx": $12, - "./routes/_500.tsx": $13, - "./routes/index.tsx": $14, + "./routes/index.tsx": $0, + "./routes/params.tsx": $1, + "./routes/static.tsx": $2, + "./routes/intercept_args.tsx": $3, + "./routes/_render.ts": $4, + "./routes/props/[id].tsx": $5, + "./routes/failure.ts": $6, + "./routes/_middleware.ts": $7, + "./routes/books/[id].tsx": $8, + "./routes/wildcard.tsx": $9, + "./routes/api/get_only.ts": $10, + "./routes/_500.tsx": $11, + "./routes/intercept.tsx": $12, + "./routes/_404.tsx": $13, + "./routes/[name].tsx": $14, + "./routes/_app.tsx": $15, }, islands: { "./islands/Test.tsx": $$0, diff --git a/tests/fixture/routes/params.tsx b/tests/fixture/routes/params.tsx new file mode 100644 index 00000000..c6481406 --- /dev/null +++ b/tests/fixture/routes/params.tsx @@ -0,0 +1,14 @@ +/** @jsx h */ + +import { h, PageConfig } from "../client_deps.ts"; +import { HandlerContext } from "../../../server.ts"; + +export const handler = { + GET(_req: Request, { params }: HandlerContext) { + return new Response(params.path); + }, +}; + +export const config: PageConfig = { + routeOverride: "/params/:path*", +}; diff --git a/tests/main_test.ts b/tests/main_test.ts index 91010b91..6c7c687e 100644 --- a/tests/main_test.ts +++ b/tests/main_test.ts @@ -184,3 +184,13 @@ Deno.test("static file", async () => { ); assertEquals(resp2.status, 304); }); + +Deno.test("/params/:path*", async () => { + const resp = await router( + new Request("https://fresh.deno.dev/params/bar/baz"), + ); + assert(resp); + assertEquals(resp.status, 200); + const body = await resp.text(); + assertEquals(body, "bar/baz"); +});