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);
|
||||
}
|
||||
Reference in New Issue
Block a user