fix: remove Data from FreshContext (#2473)

This is now only on `PageProps`.
This commit is contained in:
Luca Casonato
2024-05-31 13:50:28 +02:00
committed by GitHub
parent dc283a137b
commit 8d85945da6
16 changed files with 131 additions and 124 deletions
+4 -9
View File
@@ -4,7 +4,6 @@ import type { WalkEntry } from "@std/fs/walk";
import * as path from "@std/path";
import type { RouteConfig } from "../../types.ts";
import type { RouteHandler } from "../../handlers.ts";
import type { FreshContext } from "../../context.ts";
import type { MiddlewareFn } from "../../middlewares/mod.ts";
import {
type AsyncAnyComponent,
@@ -13,9 +12,9 @@ import {
import { type Method, pathToPattern } from "../../router.ts";
import { type HandlerFn, isHandlerByMethod } from "../../handlers.ts";
import { type FsAdapter, fsAdapter } from "../../fs.ts";
import type { PageProps } from "../../runtime/server/mod.tsx";
import { HttpError } from "../../error.ts";
import { parseRootPath } from "../../config.ts";
import type { FreshReqContext, PageProps } from "../../context.ts";
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
const GROUP_REG = /(^|[/\\\\])\((_[^/\\\\]+)\)[/\\\\]/;
@@ -34,12 +33,8 @@ export interface FreshFsItem<State> {
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
handlers?: RouteHandler<unknown, State>;
default?:
| AnyComponent<
PageProps<unknown, State> & Pick<FreshContext<unknown, State>, "redirect">
>
| AsyncAnyComponent<
PageProps<unknown, State> & Pick<FreshContext<unknown, State>, "redirect">
>;
| AnyComponent<PageProps<unknown, State>>
| AsyncAnyComponent<PageProps<unknown, State>>;
}
// deno-lint-ignore no-explicit-any
@@ -324,7 +319,7 @@ function errorMiddleware<State>(
try {
return await ctx.next();
} catch (err) {
ctx.error = err;
(ctx as FreshReqContext<State>).error = err;
return mid(ctx);
}
};
+9 -15
View File
@@ -1,7 +1,7 @@
import { type AnyComponent, h, type RenderableProps, type VNode } from "preact";
import type { MiddlewareFn } from "../../middlewares/mod.ts";
import type { HandlerFn, PageResponse } from "../../handlers.ts";
import type { PageProps } from "../../runtime/server/mod.tsx";
import type { FreshReqContext, PageProps } from "../../context.ts";
export type AsyncAnyComponent<P> = {
(
@@ -30,18 +30,21 @@ export function renderMiddleware<State>(
return res;
}
// deno-lint-ignore no-explicit-any
result = res as any;
result = res;
}
if (components.length === 0) {
throw new Error(`Did not receive any components to render.`);
}
const props = ctx as FreshReqContext<State>;
props.data = result?.data;
let vnode: VNode | null = null;
for (let i = components.length - 1; i >= 0; i--) {
const child = vnode;
const Component = () => child;
// FIXME: remove when we're using `<Slot />`
props.Component = () => child;
const fn = components[i];
@@ -49,23 +52,14 @@ export function renderMiddleware<State>(
typeof fn === "function" &&
fn.constructor.name === "AsyncFunction"
) {
const result = (await fn({ ...ctx, Component })) as VNode | Response;
const result = (await fn(props)) as VNode | Response;
if (result instanceof Response) {
return result;
}
vnode = result;
} else {
// deno-lint-ignore no-explicit-any
vnode = h(components[i] as any, {
config: ctx.config,
url: ctx.url,
req: ctx.req,
params: ctx.params,
state: ctx.state,
Component,
error: ctx.error,
data: result?.data ?? {},
}) as VNode;
vnode = h(components[i] as any, props) as VNode;
}
}