fix: remove Data from FreshContext (#2473)
This is now only on `PageProps`.
This commit is contained in:
+2
-5
@@ -205,21 +205,18 @@ export class App<State> {
|
|||||||
const { params, handlers } = matched;
|
const { params, handlers } = matched;
|
||||||
const ctx = new FreshReqContext<State>(
|
const ctx = new FreshReqContext<State>(
|
||||||
req,
|
req,
|
||||||
|
conn,
|
||||||
|
params,
|
||||||
this.config,
|
this.config,
|
||||||
next,
|
next,
|
||||||
this.#islandRegistry,
|
this.#islandRegistry,
|
||||||
this.#buildCache!,
|
this.#buildCache!,
|
||||||
conn,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
ctx.params = params;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (handlers.length === 1 && handlers[0].length === 1) {
|
if (handlers.length === 1 && handlers[0].length === 1) {
|
||||||
return handlers[0][0](ctx);
|
return handlers[0][0](ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.next = next;
|
|
||||||
return await runMiddlewares(handlers, ctx);
|
return await runMiddlewares(handlers, ctx);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err instanceof HttpError) {
|
if (err instanceof HttpError) {
|
||||||
|
|||||||
+8
-14
@@ -5,31 +5,25 @@ import type { HandlerByMethod } from "../handlers.ts";
|
|||||||
/**
|
/**
|
||||||
* @deprecated Use {@link FreshContext} instead
|
* @deprecated Use {@link FreshContext} instead
|
||||||
*/
|
*/
|
||||||
export type AppProps<Data = unknown, T = unknown> = FreshContext<Data, T>;
|
export type AppProps<_Data = unknown, T = unknown> = FreshContext<T>;
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link FreshContext} instead
|
* @deprecated Use {@link FreshContext} instead
|
||||||
*/
|
*/
|
||||||
export type LayoutProps<Data = unknown, T = unknown> = FreshContext<Data, T>;
|
export type LayoutProps<_Data = unknown, T = unknown> = FreshContext<T>;
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link FreshContext} instead
|
* @deprecated Use {@link FreshContext} instead
|
||||||
*/
|
*/
|
||||||
export type UnknownPageProps<Data = unknown, T = unknown> = FreshContext<
|
export type UnknownPageProps<_Data = unknown, T = unknown> = FreshContext<T>;
|
||||||
Data,
|
|
||||||
T
|
|
||||||
>;
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link FreshContext} instead
|
* @deprecated Use {@link FreshContext} instead
|
||||||
*/
|
*/
|
||||||
export type ErrorPageProps<Data = unknown, T = unknown> = FreshContext<Data, T>;
|
export type ErrorPageProps<_Data = unknown, T = unknown> = FreshContext<T>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated Use {@link FreshContext} instead
|
* @deprecated Use {@link FreshContext} instead
|
||||||
*/
|
*/
|
||||||
// deno-lint-ignore no-explicit-any
|
export type RouteContext<_T = never, S = Record<string, unknown>> =
|
||||||
export type RouteContext<T = any, S = Record<string, unknown>> = FreshContext<
|
FreshContext<S>;
|
||||||
T,
|
|
||||||
S
|
|
||||||
>;
|
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
export type Handlers<T = any, State = Record<string, unknown>> =
|
export type Handlers<T = any, State = Record<string, unknown>> =
|
||||||
@@ -37,10 +31,10 @@ export type Handlers<T = any, State = Record<string, unknown>> =
|
|||||||
|
|
||||||
function defineFn<State>(
|
function defineFn<State>(
|
||||||
fn: (
|
fn: (
|
||||||
ctx: FreshContext<unknown, State>,
|
ctx: FreshContext<State>,
|
||||||
) => Request | VNode | null | Promise<Request | VNode | null>,
|
) => Request | VNode | null | Promise<Request | VNode | null>,
|
||||||
): (
|
): (
|
||||||
ctx: FreshContext<unknown, State>,
|
ctx: FreshContext<State>,
|
||||||
) => Request | VNode | null | Promise<Request | VNode | null> {
|
) => Request | VNode | null | Promise<Request | VNode | null> {
|
||||||
return fn;
|
return fn;
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
import { MODE, type Mode } from "./runtime/server/mod.tsx";
|
import { MODE, type Mode } from "./runtime/server/mod.ts";
|
||||||
|
|
||||||
export interface FreshPlugin {
|
export interface FreshPlugin {
|
||||||
name: string;
|
name: string;
|
||||||
|
|||||||
+59
-22
@@ -1,6 +1,12 @@
|
|||||||
import { type ComponentType, h, isValidElement, type VNode } from "preact";
|
import {
|
||||||
import type { ResolvedFreshConfig } from "./config.ts";
|
type ComponentType,
|
||||||
|
type FunctionComponent,
|
||||||
|
h,
|
||||||
|
isValidElement,
|
||||||
|
type VNode,
|
||||||
|
} from "preact";
|
||||||
import { renderToString } from "preact-render-to-string";
|
import { renderToString } from "preact-render-to-string";
|
||||||
|
import type { ResolvedFreshConfig } from "./config.ts";
|
||||||
import type { BuildCache } from "./build_cache.ts";
|
import type { BuildCache } from "./build_cache.ts";
|
||||||
import {
|
import {
|
||||||
FreshScripts,
|
FreshScripts,
|
||||||
@@ -21,22 +27,21 @@ export type ServerIslandRegistry = Map<ComponentType, Island>;
|
|||||||
/**
|
/**
|
||||||
* The context passed to every middleware. It is unique for every request.
|
* The context passed to every middleware. It is unique for every request.
|
||||||
*/
|
*/
|
||||||
export interface FreshContext<Data = unknown, State = unknown> {
|
export interface FreshContext<State = unknown> {
|
||||||
/** Reference to the resolved Fresh configuration */
|
/** Reference to the resolved Fresh configuration */
|
||||||
readonly config: ResolvedFreshConfig;
|
readonly config: ResolvedFreshConfig;
|
||||||
state: State;
|
readonly state: State;
|
||||||
data: Data;
|
|
||||||
/** The original incoming `Request` object` */
|
/** The original incoming `Request` object` */
|
||||||
req: Request;
|
readonly req: Request;
|
||||||
/**
|
/**
|
||||||
* The request url parsed into an `URL` instance. This is typically used
|
* The request url parsed into an `URL` instance. This is typically used
|
||||||
* to apply logic based on the pathname of the incoming url or when
|
* to apply logic based on the pathname of the incoming url or when
|
||||||
* certain search parameters are set.
|
* certain search parameters are set.
|
||||||
*/
|
*/
|
||||||
url: URL;
|
readonly url: URL;
|
||||||
params: Record<string, string>;
|
readonly params: Record<string, string>;
|
||||||
error: unknown;
|
readonly error: unknown;
|
||||||
info?: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo;
|
readonly info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo;
|
||||||
/**
|
/**
|
||||||
* Return a redirect response to the specified path. This is the
|
* Return a redirect response to the specified path. This is the
|
||||||
* preferred way to do redirects in Fresh.
|
* preferred way to do redirects in Fresh.
|
||||||
@@ -78,32 +83,48 @@ export interface FreshContext<Data = unknown, State = unknown> {
|
|||||||
render(vnode: VNode, init?: ResponseInit): Response | Promise<Response>;
|
render(vnode: VNode, init?: ResponseInit): Response | Promise<Response>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export let getBuildCache: (ctx: FreshContext<unknown, unknown>) => BuildCache;
|
export let getBuildCache: (ctx: FreshContext<unknown>) => BuildCache;
|
||||||
|
|
||||||
export class FreshReqContext<State> implements FreshContext<unknown, State> {
|
export class FreshReqContext<State>
|
||||||
|
implements FreshContext<State>, PageProps<unknown, State> {
|
||||||
|
config: ResolvedFreshConfig;
|
||||||
url: URL;
|
url: URL;
|
||||||
params = {} as Record<string, string>;
|
req: Request;
|
||||||
state = {} as State;
|
params: Record<string, string>;
|
||||||
data = {} as never;
|
state: State = {} as State;
|
||||||
error: Error | null = null;
|
data: unknown = undefined;
|
||||||
|
error: unknown | null = null;
|
||||||
|
info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo;
|
||||||
|
|
||||||
|
next: FreshContext<State>["next"];
|
||||||
|
|
||||||
#islandRegistry: ServerIslandRegistry;
|
#islandRegistry: ServerIslandRegistry;
|
||||||
#buildCache: BuildCache;
|
#buildCache: BuildCache;
|
||||||
|
|
||||||
|
// FIXME: remove after switching to <Slot />
|
||||||
|
Component!: FunctionComponent;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
getBuildCache = (ctx) => (ctx as FreshReqContext<unknown>).#buildCache;
|
getBuildCache = (ctx) => (ctx as FreshReqContext<unknown>).#buildCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public req: Request,
|
req: Request,
|
||||||
public config: ResolvedFreshConfig,
|
info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo,
|
||||||
public next: FreshContext<unknown, State>["next"],
|
params: Record<string, string>,
|
||||||
|
config: ResolvedFreshConfig,
|
||||||
|
next: FreshContext<State>["next"],
|
||||||
islandRegistry: ServerIslandRegistry,
|
islandRegistry: ServerIslandRegistry,
|
||||||
buildCache: BuildCache,
|
buildCache: BuildCache,
|
||||||
public info: Deno.ServeHandlerInfo | Deno.ServeUnixHandlerInfo,
|
|
||||||
) {
|
) {
|
||||||
|
this.url = new URL(req.url);
|
||||||
|
this.req = req;
|
||||||
|
this.info = info;
|
||||||
|
this.params = params;
|
||||||
|
this.config = config;
|
||||||
|
this.next = next;
|
||||||
this.#islandRegistry = islandRegistry;
|
this.#islandRegistry = islandRegistry;
|
||||||
this.#buildCache = buildCache;
|
this.#buildCache = buildCache;
|
||||||
this.url = new URL(req.url);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
redirect(pathOrUrl: string, status = 302): Response {
|
redirect(pathOrUrl: string, status = 302): Response {
|
||||||
@@ -168,9 +189,21 @@ export class FreshReqContext<State> implements FreshContext<unknown, State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Object.defineProperties(FreshReqContext.prototype, {
|
||||||
|
config: { enumerable: true },
|
||||||
|
url: { enumerable: true },
|
||||||
|
req: { enumerable: true },
|
||||||
|
params: { enumerable: true },
|
||||||
|
state: { enumerable: true },
|
||||||
|
data: { enumerable: true },
|
||||||
|
error: { enumerable: true },
|
||||||
|
next: { enumerable: true },
|
||||||
|
info: { enumerable: true },
|
||||||
|
});
|
||||||
|
|
||||||
function preactRender<State, Data>(
|
function preactRender<State, Data>(
|
||||||
vnode: VNode,
|
vnode: VNode,
|
||||||
ctx: FreshContext<Data, State>,
|
ctx: PageProps<Data, State>,
|
||||||
islandRegistry: ServerIslandRegistry,
|
islandRegistry: ServerIslandRegistry,
|
||||||
buildCache: BuildCache,
|
buildCache: BuildCache,
|
||||||
partialId: string,
|
partialId: string,
|
||||||
@@ -201,3 +234,7 @@ function preactRender<State, Data>(
|
|||||||
setRenderState(null);
|
setRenderState(null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type PageProps<Data = unknown, T = unknown> =
|
||||||
|
& Omit<FreshContext<T>, "next" | "render" | "redirect">
|
||||||
|
& { data: Data; Component: FunctionComponent };
|
||||||
|
|||||||
+4
-10
@@ -1,7 +1,7 @@
|
|||||||
import type { AnyComponent } from "preact";
|
import type { AnyComponent } from "preact";
|
||||||
import type { HandlerByMethod, RouteHandler } from "./handlers.ts";
|
import type { HandlerByMethod, RouteHandler } from "./handlers.ts";
|
||||||
import type { FreshContext } from "./context.ts";
|
|
||||||
import type { Middleware } from "./middlewares/mod.ts";
|
import type { Middleware } from "./middlewares/mod.ts";
|
||||||
|
import type { PageProps } from "./context.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A set of define functions that enable better type inference and code
|
* A set of define functions that enable better type inference and code
|
||||||
@@ -57,9 +57,7 @@ export interface Define<State> {
|
|||||||
handlers<
|
handlers<
|
||||||
Data,
|
Data,
|
||||||
Handlers extends RouteHandler<Data, State> = RouteHandler<Data, State>,
|
Handlers extends RouteHandler<Data, State> = RouteHandler<Data, State>,
|
||||||
>(
|
>(handlers: Handlers): typeof handlers;
|
||||||
handlers: Handlers,
|
|
||||||
): typeof handlers;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a page component that will be rendered when a route handler returns
|
* Define a page component that will be rendered when a route handler returns
|
||||||
@@ -110,9 +108,7 @@ export interface Define<State> {
|
|||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
Handler extends RouteHandler<any, State> = never,
|
Handler extends RouteHandler<any, State> = never,
|
||||||
Data = Handler extends HandlerByMethod<infer Data, State> ? Data : never,
|
Data = Handler extends HandlerByMethod<infer Data, State> ? Data : never,
|
||||||
>(
|
>(render: AnyComponent<PageProps<Data, State>>): typeof render;
|
||||||
render: AnyComponent<FreshContext<Data, State> & { Component: () => null }>,
|
|
||||||
): typeof render;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Define a {@link Middleware} that will be used to process requests before
|
* Define a {@link Middleware} that will be used to process requests before
|
||||||
@@ -135,9 +131,7 @@ export interface Define<State> {
|
|||||||
*
|
*
|
||||||
* @typeParam M The type of the middleware function. This will be inferred from the input function. Do not manually specify this type.
|
* @typeParam M The type of the middleware function. This will be inferred from the input function. Do not manually specify this type.
|
||||||
*/
|
*/
|
||||||
middleware<M extends Middleware<State>>(
|
middleware<M extends Middleware<State>>(middleware: M): typeof middleware;
|
||||||
middleware: M,
|
|
||||||
): typeof middleware;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
import { setMode } from "../runtime/server/mod.tsx";
|
import { setMode } from "../runtime/server/mod.ts";
|
||||||
|
|
||||||
export { Builder, type FreshBuilder } from "./builder.ts";
|
export { Builder, type FreshBuilder } from "./builder.ts";
|
||||||
export {
|
export {
|
||||||
|
|||||||
+1
-1
@@ -192,7 +192,7 @@ export function isHandlerByMethod<D, S>(
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export interface HandlerFn<Data, State> {
|
export interface HandlerFn<Data, State> {
|
||||||
(ctx: FreshContext<Data, State>):
|
(ctx: FreshContext<State>):
|
||||||
| Response
|
| Response
|
||||||
| PageResponse<Data>
|
| PageResponse<Data>
|
||||||
| Promise<Response | PageResponse<Data>>;
|
| Promise<Response | PageResponse<Data>>;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import type { FreshContext } from "../context.ts";
|
import type { FreshContext, FreshReqContext } from "../context.ts";
|
||||||
import type { App as _App } from "../app.ts";
|
import type { App as _App } from "../app.ts";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -71,7 +71,7 @@ import type { App as _App } from "../app.ts";
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export type MiddlewareFn<State> = (
|
export type MiddlewareFn<State> = (
|
||||||
ctx: FreshContext<unknown, State>,
|
ctx: FreshContext<State>,
|
||||||
) => Response | Promise<Response>;
|
) => Response | Promise<Response>;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -82,7 +82,7 @@ export type Middleware<State> = MiddlewareFn<State> | MiddlewareFn<State>[];
|
|||||||
|
|
||||||
export function runMiddlewares<State>(
|
export function runMiddlewares<State>(
|
||||||
middlewares: MiddlewareFn<State>[][],
|
middlewares: MiddlewareFn<State>[][],
|
||||||
ctx: FreshContext<unknown, State>,
|
ctx: FreshReqContext<State>,
|
||||||
): Promise<Response> {
|
): Promise<Response> {
|
||||||
let fn = ctx.next;
|
let fn = ctx.next;
|
||||||
let i = middlewares.length;
|
let i = middlewares.length;
|
||||||
|
|||||||
@@ -5,9 +5,8 @@ import { serveMiddleware } from "../test_utils.ts";
|
|||||||
|
|
||||||
Deno.test("trailingSlashes - always", async () => {
|
Deno.test("trailingSlashes - always", async () => {
|
||||||
const middleware = trailingSlashes("always");
|
const middleware = trailingSlashes("always");
|
||||||
const server = serveMiddleware(async (ctx) => {
|
const server = serveMiddleware(middleware, {
|
||||||
ctx.next = async () => new Response("ok");
|
next: async () => new Response("ok"),
|
||||||
return await middleware(ctx);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let res = await server.get("/");
|
let res = await server.get("/");
|
||||||
@@ -33,9 +32,8 @@ Deno.test("trailingSlashes - always", async () => {
|
|||||||
|
|
||||||
Deno.test("trailingSlashes - never", async () => {
|
Deno.test("trailingSlashes - never", async () => {
|
||||||
const middleware = trailingSlashes("never");
|
const middleware = trailingSlashes("never");
|
||||||
const server = serveMiddleware(async (ctx) => {
|
const server = serveMiddleware(middleware, {
|
||||||
ctx.next = async () => new Response("ok");
|
next: async () => new Response("ok"),
|
||||||
return await middleware(ctx);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
let res = await server.get("/");
|
let res = await server.get("/");
|
||||||
|
|||||||
+6
-6
@@ -9,14 +9,14 @@ export {
|
|||||||
type RouteData,
|
type RouteData,
|
||||||
type RouteHandler,
|
type RouteHandler,
|
||||||
} from "./handlers.ts";
|
} from "./handlers.ts";
|
||||||
export { type RouteConfig } from "./types.ts";
|
export type { RouteConfig } from "./types.ts";
|
||||||
export { type Middleware, type MiddlewareFn } from "./middlewares/mod.ts";
|
export type { Middleware, MiddlewareFn } from "./middlewares/mod.ts";
|
||||||
export { staticFiles } from "./middlewares/static_files.ts";
|
export { staticFiles } from "./middlewares/static_files.ts";
|
||||||
export { type Mode, type PageProps } from "./runtime/server/mod.tsx";
|
export type { Mode } from "./runtime/server/mod.ts";
|
||||||
export { type FreshConfig, type ResolvedFreshConfig } from "./config.ts";
|
export type { FreshConfig, ResolvedFreshConfig } from "./config.ts";
|
||||||
export { type FreshContext, type Island } from "./context.ts";
|
export type { FreshContext, Island, PageProps } from "./context.ts";
|
||||||
export { createDefine, type Define } from "./define.ts";
|
export { createDefine, type Define } from "./define.ts";
|
||||||
export { type Method } from "./router.ts";
|
export type { Method } from "./router.ts";
|
||||||
export { HttpError } from "./error.ts";
|
export { HttpError } from "./error.ts";
|
||||||
|
|
||||||
// Compat with older Fresh versions
|
// Compat with older Fresh versions
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import type { WalkEntry } from "@std/fs/walk";
|
|||||||
import * as path from "@std/path";
|
import * as path from "@std/path";
|
||||||
import type { RouteConfig } from "../../types.ts";
|
import type { RouteConfig } from "../../types.ts";
|
||||||
import type { RouteHandler } from "../../handlers.ts";
|
import type { RouteHandler } from "../../handlers.ts";
|
||||||
import type { FreshContext } from "../../context.ts";
|
|
||||||
import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
||||||
import {
|
import {
|
||||||
type AsyncAnyComponent,
|
type AsyncAnyComponent,
|
||||||
@@ -13,9 +12,9 @@ import {
|
|||||||
import { type Method, pathToPattern } from "../../router.ts";
|
import { type Method, pathToPattern } from "../../router.ts";
|
||||||
import { type HandlerFn, isHandlerByMethod } from "../../handlers.ts";
|
import { type HandlerFn, isHandlerByMethod } from "../../handlers.ts";
|
||||||
import { type FsAdapter, fsAdapter } from "../../fs.ts";
|
import { type FsAdapter, fsAdapter } from "../../fs.ts";
|
||||||
import type { PageProps } from "../../runtime/server/mod.tsx";
|
|
||||||
import { HttpError } from "../../error.ts";
|
import { HttpError } from "../../error.ts";
|
||||||
import { parseRootPath } from "../../config.ts";
|
import { parseRootPath } from "../../config.ts";
|
||||||
|
import type { FreshReqContext, PageProps } from "../../context.ts";
|
||||||
|
|
||||||
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
|
const TEST_FILE_PATTERN = /[._]test\.(?:[tj]sx?|[mc][tj]s)$/;
|
||||||
const GROUP_REG = /(^|[/\\\\])\((_[^/\\\\]+)\)[/\\\\]/;
|
const GROUP_REG = /(^|[/\\\\])\((_[^/\\\\]+)\)[/\\\\]/;
|
||||||
@@ -34,12 +33,8 @@ export interface FreshFsItem<State> {
|
|||||||
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
|
handler?: RouteHandler<unknown, State> | HandlerFn<unknown, State>[];
|
||||||
handlers?: RouteHandler<unknown, State>;
|
handlers?: RouteHandler<unknown, State>;
|
||||||
default?:
|
default?:
|
||||||
| AnyComponent<
|
| AnyComponent<PageProps<unknown, State>>
|
||||||
PageProps<unknown, State> & Pick<FreshContext<unknown, State>, "redirect">
|
| AsyncAnyComponent<PageProps<unknown, State>>;
|
||||||
>
|
|
||||||
| AsyncAnyComponent<
|
|
||||||
PageProps<unknown, State> & Pick<FreshContext<unknown, State>, "redirect">
|
|
||||||
>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
@@ -324,7 +319,7 @@ function errorMiddleware<State>(
|
|||||||
try {
|
try {
|
||||||
return await ctx.next();
|
return await ctx.next();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
ctx.error = err;
|
(ctx as FreshReqContext<State>).error = err;
|
||||||
return mid(ctx);
|
return mid(ctx);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { type AnyComponent, h, type RenderableProps, type VNode } from "preact";
|
import { type AnyComponent, h, type RenderableProps, type VNode } from "preact";
|
||||||
import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
||||||
import type { HandlerFn, PageResponse } from "../../handlers.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> = {
|
export type AsyncAnyComponent<P> = {
|
||||||
(
|
(
|
||||||
@@ -30,18 +30,21 @@ export function renderMiddleware<State>(
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
result = res;
|
||||||
result = res as any;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (components.length === 0) {
|
if (components.length === 0) {
|
||||||
throw new Error(`Did not receive any components to render.`);
|
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;
|
let vnode: VNode | null = null;
|
||||||
for (let i = components.length - 1; i >= 0; i--) {
|
for (let i = components.length - 1; i >= 0; i--) {
|
||||||
const child = vnode;
|
const child = vnode;
|
||||||
const Component = () => child;
|
// FIXME: remove when we're using `<Slot />`
|
||||||
|
props.Component = () => child;
|
||||||
|
|
||||||
const fn = components[i];
|
const fn = components[i];
|
||||||
|
|
||||||
@@ -49,23 +52,14 @@ export function renderMiddleware<State>(
|
|||||||
typeof fn === "function" &&
|
typeof fn === "function" &&
|
||||||
fn.constructor.name === "AsyncFunction"
|
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) {
|
if (result instanceof Response) {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
vnode = result;
|
vnode = result;
|
||||||
} else {
|
} else {
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
vnode = h(components[i] as any, {
|
vnode = h(components[i] as any, props) as VNode;
|
||||||
config: ctx.config,
|
|
||||||
url: ctx.url,
|
|
||||||
req: ctx.req,
|
|
||||||
params: ctx.params,
|
|
||||||
state: ctx.state,
|
|
||||||
Component,
|
|
||||||
error: ctx.error,
|
|
||||||
data: result?.data ?? {},
|
|
||||||
}) as VNode;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
/**
|
||||||
|
* The mode Fresh can run in.
|
||||||
|
*/
|
||||||
|
export type Mode = "development" | "build" | "production";
|
||||||
|
export let MODE: Mode = "production";
|
||||||
|
|
||||||
|
export function setMode(mode: Mode) {
|
||||||
|
MODE = mode;
|
||||||
|
}
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
import type { FunctionComponent } from "preact";
|
|
||||||
import type { FreshContext } from "../../context.ts";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The mode Fresh can run in.
|
|
||||||
*/
|
|
||||||
export type Mode = "development" | "build" | "production";
|
|
||||||
export let MODE: Mode = "production";
|
|
||||||
|
|
||||||
export function setMode(mode: Mode) {
|
|
||||||
MODE = mode;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type PageProps<Data = unknown, T = unknown> =
|
|
||||||
& Omit<FreshContext<Data, T>, "next" | "redirect" | "next">
|
|
||||||
& { Component: FunctionComponent };
|
|
||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
} from "preact";
|
} from "preact";
|
||||||
import type { Signal } from "@preact/signals";
|
import type { Signal } from "@preact/signals";
|
||||||
import type { Stringifiers } from "../../jsonify/stringify.ts";
|
import type { Stringifiers } from "../../jsonify/stringify.ts";
|
||||||
import type { FreshContext } from "../../context.ts";
|
import type { PageProps } from "../../context.ts";
|
||||||
import { asset, Partial, type PartialProps } from "../shared.ts";
|
import { asset, Partial, type PartialProps } from "../shared.ts";
|
||||||
import { stringify } from "../../jsonify/stringify.ts";
|
import { stringify } from "../../jsonify/stringify.ts";
|
||||||
import type { ServerIslandRegistry } from "../../context.ts";
|
import type { ServerIslandRegistry } from "../../context.ts";
|
||||||
@@ -88,7 +88,7 @@ export class RenderState {
|
|||||||
hasRuntimeScript = false;
|
hasRuntimeScript = false;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public ctx: FreshContext<unknown, unknown>,
|
public ctx: PageProps<unknown, unknown>,
|
||||||
public islandRegistry: ServerIslandRegistry,
|
public islandRegistry: ServerIslandRegistry,
|
||||||
public buildCache: BuildCache,
|
public buildCache: BuildCache,
|
||||||
public partialId: string,
|
public partialId: string,
|
||||||
@@ -396,14 +396,14 @@ export function FreshScripts() {
|
|||||||
<>
|
<>
|
||||||
{slots.map((slot) => {
|
{slots.map((slot) => {
|
||||||
if (slot === null) return null;
|
if (slot === null) return null;
|
||||||
// FIXME: Wait for https://github.com/preactjs/preact/pull/4334 to be
|
return (
|
||||||
// released
|
<template
|
||||||
return h(
|
key={slot.id}
|
||||||
"template",
|
id={`frsh-${slot.id}-${slot.name}`}
|
||||||
{ key: slot.id, id: `frsh-${slot.id}-${slot.name}` },
|
>
|
||||||
slot.vnode,
|
{slot.vnode}
|
||||||
// deno-lint-ignore no-explicit-any
|
</template>
|
||||||
) as VNode<any>;
|
);
|
||||||
})}
|
})}
|
||||||
<FreshRuntimeScript />
|
<FreshRuntimeScript />
|
||||||
</>
|
</>
|
||||||
|
|||||||
+10
-5
@@ -1,5 +1,4 @@
|
|||||||
import { FreshReqContext } from "./context.ts";
|
import { FreshReqContext } from "./context.ts";
|
||||||
import type { MiddlewareFn } from "./middlewares/mod.ts";
|
|
||||||
import type { FsAdapter } from "./fs.ts";
|
import type { FsAdapter } from "./fs.ts";
|
||||||
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
|
import { type BuildCache, ProdBuildCache } from "./build_cache.ts";
|
||||||
import type { ResolvedFreshConfig } from "./config.ts";
|
import type { ResolvedFreshConfig } from "./config.ts";
|
||||||
@@ -63,22 +62,28 @@ const DEFAULT_CONFIG: ResolvedFreshConfig = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export function serveMiddleware<T>(
|
export function serveMiddleware<T>(
|
||||||
middleware: MiddlewareFn<T>,
|
middleware: (ctx: FreshReqContext<T>) => Response | Promise<Response>,
|
||||||
options: { config?: ResolvedFreshConfig; buildCache?: BuildCache } = {},
|
options: {
|
||||||
|
config?: ResolvedFreshConfig;
|
||||||
|
buildCache?: BuildCache;
|
||||||
|
next?: () => Promise<Response>;
|
||||||
|
} = {},
|
||||||
): FakeServer {
|
): FakeServer {
|
||||||
return new FakeServer(async (req) => {
|
return new FakeServer(async (req) => {
|
||||||
const next = () => new Response("not found", { status: 404 });
|
const next = options.next ??
|
||||||
|
(() => new Response("not found", { status: 404 }));
|
||||||
const config = options.config ?? DEFAULT_CONFIG;
|
const config = options.config ?? DEFAULT_CONFIG;
|
||||||
const buildCache = options.buildCache ??
|
const buildCache = options.buildCache ??
|
||||||
new ProdBuildCache(config, new Map(), new Map(), true);
|
new ProdBuildCache(config, new Map(), new Map(), true);
|
||||||
|
|
||||||
const ctx = new FreshReqContext<T>(
|
const ctx = new FreshReqContext<T>(
|
||||||
req,
|
req,
|
||||||
|
DEFAULT_CONN_INFO,
|
||||||
|
{},
|
||||||
config,
|
config,
|
||||||
() => Promise.resolve(next()),
|
() => Promise.resolve(next()),
|
||||||
new Map(),
|
new Map(),
|
||||||
buildCache,
|
buildCache,
|
||||||
DEFAULT_CONN_INFO,
|
|
||||||
);
|
);
|
||||||
return await middleware(ctx);
|
return await middleware(ctx);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user