feat: add helpers to make Fresh less TypeScripty (#1627)

This commit is contained in:
Marvin Hagemeister
2023-08-14 22:45:13 +02:00
committed by GitHub
parent 6436a43dce
commit 726740ed39
11 changed files with 157 additions and 13 deletions
+50
View File
@@ -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);
}