refactor: HandlerContext.match to HandlerContext.params (#118)
This commit is contained in:
@@ -358,15 +358,15 @@ export class ServerContext {
|
|||||||
for (const page of this.#pages) {
|
for (const page of this.#pages) {
|
||||||
const createRender = genRender(page, 200);
|
const createRender = genRender(page, 200);
|
||||||
if (typeof page.handler === "function") {
|
if (typeof page.handler === "function") {
|
||||||
routes[page.route] = (req, match) =>
|
routes[page.route] = (req, params) =>
|
||||||
(page.handler as Handler)(req, {
|
(page.handler as Handler)(req, {
|
||||||
match,
|
params,
|
||||||
render: createRender(req, match),
|
render: createRender(req, params),
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
for (const [method, handler] of Object.entries(page.handler)) {
|
for (const [method, handler] of Object.entries(page.handler)) {
|
||||||
routes[`${method}@${page.route}`] = (req, match) =>
|
routes[`${method}@${page.route}`] = (req, params) =>
|
||||||
handler(req, { match, render: createRender(req, match) });
|
handler(req, { params, render: createRender(req, params) });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -456,8 +456,8 @@ export class ServerContext {
|
|||||||
* constants.INTERNAL_PREFIX
|
* constants.INTERNAL_PREFIX
|
||||||
*/
|
*/
|
||||||
#bundleAssetRoute = (): router.MatchHandler => {
|
#bundleAssetRoute = (): router.MatchHandler => {
|
||||||
return async (_req, match) => {
|
return async (_req, params) => {
|
||||||
const path = `/${match.path}`;
|
const path = `/${params.path}`;
|
||||||
const file = await this.#bundler.get(path);
|
const file = await this.#bundler.get(path);
|
||||||
let res;
|
let res;
|
||||||
if (file) {
|
if (file) {
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ export interface IslandModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface HandlerContext<T = unknown> {
|
export interface HandlerContext<T = unknown> {
|
||||||
match: Record<string, string>;
|
params: Record<string, string>;
|
||||||
render: (data?: T) => Response;
|
render: (data?: T) => Response;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+32
-30
@@ -2,40 +2,42 @@
|
|||||||
// This file SHOULD be checked into source version control.
|
// This file SHOULD be checked into source version control.
|
||||||
// To update this file, run `fresh routes`.
|
// To update this file, run `fresh routes`.
|
||||||
|
|
||||||
import * as $0 from "./routes/_middleware.ts";
|
import * as $0 from "./routes/index.tsx";
|
||||||
import * as $1 from "./routes/_404.tsx";
|
import * as $1 from "./routes/params.tsx";
|
||||||
import * as $2 from "./routes/failure.ts";
|
import * as $2 from "./routes/static.tsx";
|
||||||
import * as $3 from "./routes/api/get_only.ts";
|
import * as $3 from "./routes/intercept_args.tsx";
|
||||||
import * as $4 from "./routes/books/[id].tsx";
|
import * as $4 from "./routes/_render.ts";
|
||||||
import * as $5 from "./routes/_render.ts";
|
import * as $5 from "./routes/props/[id].tsx";
|
||||||
import * as $6 from "./routes/props/[id].tsx";
|
import * as $6 from "./routes/failure.ts";
|
||||||
import * as $7 from "./routes/_app.tsx";
|
import * as $7 from "./routes/_middleware.ts";
|
||||||
import * as $8 from "./routes/static.tsx";
|
import * as $8 from "./routes/books/[id].tsx";
|
||||||
import * as $9 from "./routes/intercept_args.tsx";
|
import * as $9 from "./routes/wildcard.tsx";
|
||||||
import * as $10 from "./routes/wildcard.tsx";
|
import * as $10 from "./routes/api/get_only.ts";
|
||||||
import * as $11 from "./routes/intercept.tsx";
|
import * as $11 from "./routes/_500.tsx";
|
||||||
import * as $12 from "./routes/[name].tsx";
|
import * as $12 from "./routes/intercept.tsx";
|
||||||
import * as $13 from "./routes/_500.tsx";
|
import * as $13 from "./routes/_404.tsx";
|
||||||
import * as $14 from "./routes/index.tsx";
|
import * as $14 from "./routes/[name].tsx";
|
||||||
|
import * as $15 from "./routes/_app.tsx";
|
||||||
import * as $$0 from "./islands/Test.tsx";
|
import * as $$0 from "./islands/Test.tsx";
|
||||||
|
|
||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
"./routes/_middleware.ts": $0,
|
"./routes/index.tsx": $0,
|
||||||
"./routes/_404.tsx": $1,
|
"./routes/params.tsx": $1,
|
||||||
"./routes/failure.ts": $2,
|
"./routes/static.tsx": $2,
|
||||||
"./routes/api/get_only.ts": $3,
|
"./routes/intercept_args.tsx": $3,
|
||||||
"./routes/books/[id].tsx": $4,
|
"./routes/_render.ts": $4,
|
||||||
"./routes/_render.ts": $5,
|
"./routes/props/[id].tsx": $5,
|
||||||
"./routes/props/[id].tsx": $6,
|
"./routes/failure.ts": $6,
|
||||||
"./routes/_app.tsx": $7,
|
"./routes/_middleware.ts": $7,
|
||||||
"./routes/static.tsx": $8,
|
"./routes/books/[id].tsx": $8,
|
||||||
"./routes/intercept_args.tsx": $9,
|
"./routes/wildcard.tsx": $9,
|
||||||
"./routes/wildcard.tsx": $10,
|
"./routes/api/get_only.ts": $10,
|
||||||
"./routes/intercept.tsx": $11,
|
"./routes/_500.tsx": $11,
|
||||||
"./routes/[name].tsx": $12,
|
"./routes/intercept.tsx": $12,
|
||||||
"./routes/_500.tsx": $13,
|
"./routes/_404.tsx": $13,
|
||||||
"./routes/index.tsx": $14,
|
"./routes/[name].tsx": $14,
|
||||||
|
"./routes/_app.tsx": $15,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/Test.tsx": $$0,
|
"./islands/Test.tsx": $$0,
|
||||||
|
|||||||
@@ -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*",
|
||||||
|
};
|
||||||
@@ -184,3 +184,13 @@ Deno.test("static file", async () => {
|
|||||||
);
|
);
|
||||||
assertEquals(resp2.status, 304);
|
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");
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user