feat(server): add isPartial to context types (#1977)

This commit is contained in:
Reed von Redwitz
2023-10-26 22:26:36 +02:00
committed by GitHub
parent 7ad4610e3a
commit 63ac1e6362
13 changed files with 333 additions and 121 deletions
+4 -5
View File
@@ -1,4 +1,4 @@
import { ErrorHandler, FinalHandler, InternalRoute } from "./router.ts";
import { ErrorHandler, FinalHandler, RouteResult } from "./router.ts";
import {
BaseRoute,
MiddlewareHandlerContext,
@@ -55,10 +55,7 @@ export function composeMiddlewares(
errorHandler: ErrorHandler<RouterState>,
paramsAndRoute: (
url: string,
) => {
route: InternalRoute<RouterState> | undefined;
params: Record<string, string>;
},
) => RouteResult<RouterState>,
) {
return (
req: Request,
@@ -103,6 +100,7 @@ export function composeMiddlewares(
},
destination: "route",
params: paramsAndRouteResult.params,
isPartial: paramsAndRouteResult.isPartial,
};
for (const { module } of mws) {
@@ -124,6 +122,7 @@ export function composeMiddlewares(
set state(v) {
state = v;
},
isPartial: paramsAndRouteResult.isPartial,
};
const { destination, handler } = inner(
req,
+3 -1
View File
@@ -163,7 +163,9 @@ export class ServerContext {
return response;
} else if (url.pathname === DEV_CLIENT_URL) {
return bundleAssetRoute(req, connInfo, { path: "client.js" });
return bundleAssetRoute(req, { ...connInfo, isPartial: false }, {
path: "client.js",
});
}
}
+1
View File
@@ -184,6 +184,7 @@ export async function render<Data>(
params: opts.params as Record<string, string>,
state: opts.state ?? {},
data: opts.data,
isPartial: opts.context.isPartial,
};
// Prepare render order
+15 -5
View File
@@ -1,6 +1,9 @@
import { PARTIAL_SEARCH_PARAM } from "$fresh/src/constants.ts";
import { BaseRoute, ErrorHandlerContext, ServeHandlerInfo } from "./types.ts";
type HandlerContext<T = unknown> = T & ServeHandlerInfo;
type HandlerContext<T = unknown> = T & ServeHandlerInfo & {
isPartial: boolean;
};
export type Handler<T = unknown> = (
req: Request,
@@ -136,9 +139,10 @@ function processRoutes<T>(
}
}
interface RouteResult<T> {
export interface RouteResult<T> {
route: InternalRoute<T> | undefined;
params: Record<string, string>;
isPartial: boolean;
}
export function getParamsAndRoute<T>(
@@ -158,7 +162,8 @@ export function getParamsAndRoute<T>(
const statics = new Map<string, RouteResult<T>>();
return (url: string) => {
const pathname = new URL(url).pathname;
const urlObject = new URL(url);
const pathname = urlObject.pathname;
const cached = statics.get(pathname);
if (cached !== undefined) {
return cached;
@@ -174,7 +179,7 @@ export function getParamsAndRoute<T>(
if (typeof route.pattern === "string") {
if (route.pattern === pathname) {
processedRoutes[i] = null;
const res = { route: route, params: {} };
const res = { route: route, params: {}, isPartial: false };
statics.set(route.pattern, res);
return res;
}
@@ -195,12 +200,17 @@ export function getParamsAndRoute<T>(
groups[key] = decodeURIComponent(value);
}
}
return { route: route, params: groups };
return {
route: route,
params: groups,
isPartial: urlObject.searchParams.has(PARTIAL_SEARCH_PARAM),
};
}
}
return {
route: undefined,
params: {},
isPartial: false,
};
};
}
+3
View File
@@ -207,6 +207,7 @@ export type RouteContext<T = any, S = Record<string, unknown>> = {
params: Record<string, string>;
state: S;
data: T;
isPartial: boolean;
};
export interface RouteConfig {
@@ -268,6 +269,7 @@ export interface HandlerContext<
) => Response | Promise<Response>;
renderNotFound: (data?: NotFoundData) => Response | Promise<Response>;
state: State;
isPartial: boolean;
}
// deno-lint-ignore no-explicit-any
@@ -489,6 +491,7 @@ export interface MiddlewareHandlerContext<State = Record<string, unknown>>
state: State;
destination: router.DestinationKind;
params: Record<string, string>;
isPartial: boolean;
}
export interface MiddlewareRoute {