feat: add helpers to make Fresh less TypeScripty (#1627)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { ComponentChildren } from "preact";
|
||||
import {
|
||||
AsyncLayout,
|
||||
AsyncRoute,
|
||||
LayoutContext,
|
||||
RouteContext,
|
||||
} from "./types.ts";
|
||||
import { checkAsyncComponent } from "./render.ts";
|
||||
import { AppContext } from "$fresh/server.ts";
|
||||
|
||||
// Route creation helpers
|
||||
export function defineRoute<
|
||||
T,
|
||||
>(
|
||||
fn: (
|
||||
req: Request,
|
||||
ctx: RouteContext<void, T>,
|
||||
) => ComponentChildren | Response | Promise<ComponentChildren | Response>,
|
||||
): AsyncRoute<void, T> {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
if (checkAsyncComponent(fn)) return fn as any;
|
||||
// deno-lint-ignore require-await
|
||||
return async (req, ctx) => fn(req, ctx);
|
||||
}
|
||||
|
||||
// Layout creation helper
|
||||
export function defineLayout<T>(
|
||||
fn: (
|
||||
req: Request,
|
||||
ctx: LayoutContext<void, T>,
|
||||
) => ComponentChildren | Response | Promise<ComponentChildren | Response>,
|
||||
): AsyncLayout<void, T> {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
if (checkAsyncComponent(fn)) return fn as any;
|
||||
// deno-lint-ignore require-await
|
||||
return async (req, ctx) => fn(req, ctx);
|
||||
}
|
||||
|
||||
// App creation helper
|
||||
export function defineApp<T>(
|
||||
fn: (
|
||||
req: Request,
|
||||
ctx: AppContext<void, T>,
|
||||
) => ComponentChildren | Response | Promise<ComponentChildren | Response>,
|
||||
): AsyncLayout<void, T> {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
if (checkAsyncComponent(fn)) return fn as any;
|
||||
// deno-lint-ignore require-await
|
||||
return async (req, ctx) => fn(req, ctx);
|
||||
}
|
||||
+21
-11
@@ -1,18 +1,21 @@
|
||||
import { LayoutConfig } from "$fresh/server.ts";
|
||||
import { ComponentChildren } from "preact";
|
||||
import { ServerContext } from "./context.ts";
|
||||
export { Status } from "./deps.ts";
|
||||
import { colors, serve } from "./deps.ts";
|
||||
import {
|
||||
AppModule,
|
||||
ErrorPageModule,
|
||||
ErrorHandler,
|
||||
Handler,
|
||||
Handlers,
|
||||
IslandModule,
|
||||
LayoutModule,
|
||||
MiddlewareModule,
|
||||
RouteModule,
|
||||
RouteConfig,
|
||||
ServeHandler,
|
||||
ServeHandlerInfo,
|
||||
StartOptions,
|
||||
UnknownPageModule,
|
||||
UnknownHandler,
|
||||
} from "./types.ts";
|
||||
export { defineApp, defineLayout, defineRoute } from "./defines.ts";
|
||||
export type {
|
||||
AppContext,
|
||||
AppProps,
|
||||
@@ -54,12 +57,19 @@ export type { InnerRenderFunction } from "./render.ts";
|
||||
export interface Manifest {
|
||||
routes: Record<
|
||||
string,
|
||||
| RouteModule
|
||||
| MiddlewareModule
|
||||
| AppModule
|
||||
| LayoutModule
|
||||
| ErrorPageModule
|
||||
| UnknownPageModule
|
||||
{
|
||||
// Use a more loose route definition type because
|
||||
// TS has trouble inferring normal vs aync functions. It cannot infer based on function arity
|
||||
default?: (
|
||||
// deno-lint-ignore no-explicit-any
|
||||
propsOrRequest: any,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
ctx: any,
|
||||
) => Promise<ComponentChildren | Response> | ComponentChildren;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler?: Handler<any, any> | Handlers<any, any> | UnknownHandler;
|
||||
config?: RouteConfig | LayoutConfig | ErrorHandler;
|
||||
} | MiddlewareModule
|
||||
>;
|
||||
islands: Record<string, IslandModule>;
|
||||
baseUrl: string;
|
||||
|
||||
@@ -163,9 +163,9 @@ function defaultCsp() {
|
||||
};
|
||||
}
|
||||
|
||||
function checkAsyncComponent(
|
||||
export function checkAsyncComponent<T>(
|
||||
component: unknown,
|
||||
): component is AsyncRoute | AsyncLayout {
|
||||
): component is AsyncRoute<T> | AsyncLayout<T> {
|
||||
return typeof component === "function" &&
|
||||
component.constructor.name === "AsyncFunction";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user