feat: allow passing additional args to HandlerContext.render (#82)

Co-authored-by: Luca Casonato <lucacasonato@yahoo.com>
This commit is contained in:
Leo Kettmeir
2022-02-11 01:25:47 +01:00
committed by GitHub
co-authored by Luca Casonato
parent 6abf62c832
commit 788a85c431
7 changed files with 75 additions and 18 deletions
+7 -1
View File
@@ -1,4 +1,4 @@
export interface PageProps {
export interface PageProps<T extends Record<string, unknown>> {
/** The URL of the request that resulted in this page being rendered. */
url: URL;
@@ -15,6 +15,12 @@ export interface PageProps {
* be `{ path: [ 'bar', 'baz' ] }`.
*/
params: Record<string, string | string[]>;
/**
* Additional data passed into `HandlerContext.render`. Not present by
* default.
*/
renderData?: T;
}
export interface PageConfig {
+2 -1
View File
@@ -203,7 +203,7 @@ export class ServerContext {
params: Record<string, string | string[]>,
) => {
if (page.component === undefined) return undefined;
const render = async () => {
const render = async (renderData?: Record<string, unknown>) => {
const preloads = page.runtimeJS
? this.#bundler.getPreloads(bundlePath).map(bundleAssetUrl)
: [];
@@ -214,6 +214,7 @@ export class ServerContext {
renderer: this.#renderer,
url: new URL(req.url),
params,
renderData,
});
const headers: Record<string, string> = {
"content-type": "text/html; charset=utf-8",
+7 -1
View File
@@ -18,6 +18,7 @@ export interface RenderOptions {
url: URL;
params: Record<string, string | string[]>;
renderer: Renderer;
renderData?: Record<string, unknown>;
}
export type RenderFn = () => void;
@@ -114,7 +115,12 @@ function defaultCsp() {
export async function* render(
opts: RenderOptions,
): AsyncIterable<string | [string, ContentSecurityPolicy | undefined]> {
const props = { params: opts.params, url: opts.url, route: opts.page.route };
const props = {
params: opts.params,
url: opts.url,
route: opts.page.route,
renderData: opts.renderData,
};
const csp: ContentSecurityPolicy | undefined = opts.page.csp
? defaultCsp()
+1 -1
View File
@@ -12,7 +12,7 @@ export interface PageModule {
export interface HandlerContext {
req: Request;
match: Record<string, string>;
render?: () => Promise<Response>;
render?: (args?: Record<string, unknown>) => Promise<Response>;
}
export type Handler = (ctx: HandlerContext) => Response | Promise<Response>;
+31
View File
@@ -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
View File
@@ -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,
};
+11
View File
@@ -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", {