feat: allow passing additional args to HandlerContext.render (#82)
Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
This commit is contained in:
co-authored by
Luca Casonato
parent
6abf62c832
commit
788a85c431
@@ -0,0 +1,31 @@
|
||||
/** @jsx h */
|
||||
|
||||
import { h } from "../deps.ts";
|
||||
import { HandlerContext } from "../../../server.ts";
|
||||
|
||||
interface renderData extends Record<string, unknown> {
|
||||
info: string;
|
||||
}
|
||||
|
||||
export default function Page({
|
||||
renderData,
|
||||
}: {
|
||||
renderData: renderData;
|
||||
}) {
|
||||
return <div>{renderData.info}</div>;
|
||||
}
|
||||
|
||||
export const handler = {
|
||||
GET({ req, render }: HandlerContext) {
|
||||
if (req.headers.get("accept")?.includes("text/html")) {
|
||||
return render!({
|
||||
info: "intercepted",
|
||||
} as renderData);
|
||||
} else {
|
||||
return new Response("This is plain text");
|
||||
}
|
||||
},
|
||||
POST() {
|
||||
return new Response("POST response");
|
||||
},
|
||||
};
|
||||
+16
-14
@@ -2,25 +2,27 @@
|
||||
// This file SHOULD be checked into source version control.
|
||||
// To update this file, run `fresh routes`.
|
||||
|
||||
import * as $0 from "./pages/_middleware.ts";
|
||||
import * as $1 from "./pages/api/get_only.ts";
|
||||
import * as $2 from "./pages/books/[id].tsx";
|
||||
import * as $0 from "./pages/index.tsx";
|
||||
import * as $1 from "./pages/static.tsx";
|
||||
import * as $2 from "./pages/intercept_args.tsx";
|
||||
import * as $3 from "./pages/props/[id].tsx";
|
||||
import * as $4 from "./pages/static.tsx";
|
||||
import * as $5 from "./pages/intercept.tsx";
|
||||
import * as $6 from "./pages/[name].tsx";
|
||||
import * as $7 from "./pages/index.tsx";
|
||||
import * as $4 from "./pages/_middleware.ts";
|
||||
import * as $5 from "./pages/books/[id].tsx";
|
||||
import * as $6 from "./pages/api/get_only.ts";
|
||||
import * as $7 from "./pages/intercept.tsx";
|
||||
import * as $8 from "./pages/[name].tsx";
|
||||
|
||||
const routes = {
|
||||
pages: {
|
||||
"./pages/_middleware.ts": $0,
|
||||
"./pages/api/get_only.ts": $1,
|
||||
"./pages/books/[id].tsx": $2,
|
||||
"./pages/index.tsx": $0,
|
||||
"./pages/static.tsx": $1,
|
||||
"./pages/intercept_args.tsx": $2,
|
||||
"./pages/props/[id].tsx": $3,
|
||||
"./pages/static.tsx": $4,
|
||||
"./pages/intercept.tsx": $5,
|
||||
"./pages/[name].tsx": $6,
|
||||
"./pages/index.tsx": $7,
|
||||
"./pages/_middleware.ts": $4,
|
||||
"./pages/books/[id].tsx": $5,
|
||||
"./pages/api/get_only.ts": $6,
|
||||
"./pages/intercept.tsx": $7,
|
||||
"./pages/[name].tsx": $8,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
@@ -98,6 +98,17 @@ Deno.test("/intercept - DELETE", async () => {
|
||||
assertEquals(resp.status, 405);
|
||||
});
|
||||
|
||||
Deno.test("/intercept_args - GET html", async () => {
|
||||
const req = new Request("https://fresh.deno.dev/intercept_args", {
|
||||
headers: { "accept": "text/html" },
|
||||
});
|
||||
const resp = await router(req);
|
||||
assert(resp);
|
||||
assertEquals(resp.status, 200);
|
||||
const body = await resp.text();
|
||||
assert(body.includes("<div>intercepted</div>"));
|
||||
});
|
||||
|
||||
Deno.test("/api/get_only - NOTAMETHOD", async () => {
|
||||
const resp = await router(
|
||||
new Request("https://fresh.deno.dev/api/get_only", {
|
||||
|
||||
Reference in New Issue
Block a user