refactor: HandlerContext.match to HandlerContext.params (#118)

This commit is contained in:
Leo Kettmeir
2022-03-06 20:27:02 +01:00
committed by GitHub
parent cf48eafa9a
commit adfdec548a
5 changed files with 64 additions and 38 deletions
+7 -7
View File
@@ -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) {
+1 -1
View File
@@ -33,7 +33,7 @@ export interface IslandModule {
}
export interface HandlerContext<T = unknown> {
match: Record<string, string>;
params: Record<string, string>;
render: (data?: T) => Response;
}
+32 -30
View File
@@ -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,
+14
View File
@@ -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*",
};
+10
View File
@@ -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");
});