Router cleanup (#2099)

This commit is contained in:
Marvin Hagemeister
2023-11-30 14:08:05 +01:00
committed by GitHub
parent a0a756a1ec
commit 8ddc21eb90
15 changed files with 398 additions and 496 deletions
+25 -40
View File
@@ -4,13 +4,13 @@ import {
AsyncLayout,
AsyncRoute,
ErrorPage,
FreshContext,
LayoutRoute,
Plugin,
PluginRenderFunctionResult,
PluginRenderResult,
RenderFunction,
Route,
RouteContext,
UnknownPage,
} from "./types.ts";
import { NONE, UNSAFE_INLINE } from "../runtime/csp.ts";
@@ -28,23 +28,16 @@ export const DEFAULT_RENDER_FN: RenderFunction = (_ctx, render) => {
export interface RenderOptions<Data> {
request: Request;
// deno-lint-ignore no-explicit-any
context: any;
context: FreshContext;
route: Route<Data> | UnknownPage | ErrorPage;
plugins: Plugin[];
app: AppModule;
layouts: LayoutRoute[];
imports: string[];
dependenciesFn: (path: string) => string[];
url: URL;
params: Record<string, string | string[]>;
renderFn: RenderFunction;
data?: Data;
state?: Record<string, unknown>;
error?: unknown;
codeFrame?: string;
lang?: string;
basePath: string;
}
export type InnerRenderFunction = () => string;
@@ -148,15 +141,17 @@ export async function render<Data>(
layouts = [];
}
const { params, data, state, error, url, basePath } = opts.context;
const props: Record<string, unknown> = {
params: opts.params,
url: opts.url,
params,
url,
route: opts.route.pattern,
data: opts.data,
state: opts.state,
data,
state,
};
if (opts.error) {
props.error = opts.error;
if (error) {
props.error = error;
}
if (opts.codeFrame) {
props.codeFrame = opts.codeFrame;
@@ -174,22 +169,12 @@ export async function render<Data>(
const ctx = new RenderContext(
crypto.randomUUID(),
opts.url,
url,
opts.route.pattern,
opts.lang ?? "en",
);
const context: RouteContext = {
localAddr: opts.context.localAddr,
remoteAddr: opts.context.remoteAddr,
renderNotFound: opts.context.renderNotFound,
url: opts.url,
route: opts.route.pattern,
params: opts.params as Record<string, string>,
state: opts.state ?? {},
data: opts.data,
isPartial: opts.context.isPartial,
};
const context = opts.context;
// Prepare render order
// deno-lint-ignore no-explicit-any
@@ -253,16 +238,16 @@ export async function render<Data>(
// data.
const renderState = new RenderState(
{
url: opts.url,
url,
route: opts.route.pattern,
data: opts.data,
state: opts.state,
params: opts.params,
basePath: opts.basePath,
data,
state,
params,
basePath,
},
componentStack,
csp,
opts.error,
error,
);
let bodyHtml: string | null = null;
@@ -370,20 +355,20 @@ export async function render<Data>(
});
// Append error overlay
const devErrorUrl = withBase(DEV_ERROR_OVERLAY_URL, opts.basePath);
if (opts.error !== undefined && opts.url.pathname !== devErrorUrl) {
const devErrorUrl = withBase(DEV_ERROR_OVERLAY_URL, basePath);
if (error !== undefined && url.pathname !== devErrorUrl) {
const url = new URL(devErrorUrl, "https://localhost/");
if (opts.error instanceof Error) {
let message = opts.error.message;
if (error instanceof Error) {
let message = error.message;
const idx = message.indexOf("\n");
if (idx > -1) message = message.slice(0, idx);
url.searchParams.append("message", message);
if (opts.error.stack) {
const stack = colors.stripAnsiCode(opts.error.stack);
if (error.stack) {
const stack = colors.stripAnsiCode(error.stack);
url.searchParams.append("stack", stack);
}
} else {
url.searchParams.append("message", String(opts.error));
url.searchParams.append("message", String(error));
}
if (opts.codeFrame) {
const codeFrame = colors.stripAnsiCode(opts.codeFrame);