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";
|
import { ServerContext } from "./context.ts";
|
||||||
export { Status } from "./deps.ts";
|
export { Status } from "./deps.ts";
|
||||||
import { colors, serve } from "./deps.ts";
|
import { colors, serve } from "./deps.ts";
|
||||||
import {
|
import {
|
||||||
AppModule,
|
ErrorHandler,
|
||||||
ErrorPageModule,
|
Handler,
|
||||||
|
Handlers,
|
||||||
IslandModule,
|
IslandModule,
|
||||||
LayoutModule,
|
|
||||||
MiddlewareModule,
|
MiddlewareModule,
|
||||||
RouteModule,
|
RouteConfig,
|
||||||
ServeHandler,
|
ServeHandler,
|
||||||
ServeHandlerInfo,
|
ServeHandlerInfo,
|
||||||
StartOptions,
|
StartOptions,
|
||||||
UnknownPageModule,
|
UnknownHandler,
|
||||||
} from "./types.ts";
|
} from "./types.ts";
|
||||||
|
export { defineApp, defineLayout, defineRoute } from "./defines.ts";
|
||||||
export type {
|
export type {
|
||||||
AppContext,
|
AppContext,
|
||||||
AppProps,
|
AppProps,
|
||||||
@@ -54,12 +57,19 @@ export type { InnerRenderFunction } from "./render.ts";
|
|||||||
export interface Manifest {
|
export interface Manifest {
|
||||||
routes: Record<
|
routes: Record<
|
||||||
string,
|
string,
|
||||||
| RouteModule
|
{
|
||||||
| MiddlewareModule
|
// Use a more loose route definition type because
|
||||||
| AppModule
|
// TS has trouble inferring normal vs aync functions. It cannot infer based on function arity
|
||||||
| LayoutModule
|
default?: (
|
||||||
| ErrorPageModule
|
// deno-lint-ignore no-explicit-any
|
||||||
| UnknownPageModule
|
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>;
|
islands: Record<string, IslandModule>;
|
||||||
baseUrl: string;
|
baseUrl: string;
|
||||||
|
|||||||
@@ -163,9 +163,9 @@ function defaultCsp() {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkAsyncComponent(
|
export function checkAsyncComponent<T>(
|
||||||
component: unknown,
|
component: unknown,
|
||||||
): component is AsyncRoute | AsyncLayout {
|
): component is AsyncRoute<T> | AsyncLayout<T> {
|
||||||
return typeof component === "function" &&
|
return typeof component === "function" &&
|
||||||
component.constructor.name === "AsyncFunction";
|
component.constructor.name === "AsyncFunction";
|
||||||
}
|
}
|
||||||
|
|||||||
Executable
+5
@@ -0,0 +1,5 @@
|
|||||||
|
#!/usr/bin/env -S deno run -A --watch=static/,routes/
|
||||||
|
|
||||||
|
import dev from "$fresh/dev.ts";
|
||||||
|
|
||||||
|
await dev(import.meta.url, "./main.ts");
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
// DO NOT EDIT. This file is generated by Fresh.
|
||||||
|
// This file SHOULD be checked into source version control.
|
||||||
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
|
import * as $0 from "./routes/_app.tsx";
|
||||||
|
import * as $1 from "./routes/_layout.tsx";
|
||||||
|
import * as $2 from "./routes/index.tsx";
|
||||||
|
|
||||||
|
const manifest = {
|
||||||
|
routes: {
|
||||||
|
"./routes/_app.tsx": $0,
|
||||||
|
"./routes/_layout.tsx": $1,
|
||||||
|
"./routes/index.tsx": $2,
|
||||||
|
},
|
||||||
|
islands: {},
|
||||||
|
baseUrl: import.meta.url,
|
||||||
|
};
|
||||||
|
|
||||||
|
export default manifest;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
/// <reference no-default-lib="true" />
|
||||||
|
/// <reference lib="dom" />
|
||||||
|
/// <reference lib="dom.iterable" />
|
||||||
|
/// <reference lib="dom.asynciterable" />
|
||||||
|
/// <reference lib="deno.ns" />
|
||||||
|
|
||||||
|
import { start } from "$fresh/server.ts";
|
||||||
|
import manifest from "./fresh.gen.ts";
|
||||||
|
|
||||||
|
await start(manifest);
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export type State = { something: string };
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
import { defineApp } from "$fresh/server.ts";
|
||||||
|
import { State } from "../other/state.ts";
|
||||||
|
|
||||||
|
export default defineApp<State>((req, ctx) => {
|
||||||
|
ctx.state.something = "foo";
|
||||||
|
return (
|
||||||
|
<div class="app">
|
||||||
|
<ctx.Component />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import { defineLayout } from "$fresh/server.ts";
|
||||||
|
import { State } from "../other/state.ts";
|
||||||
|
|
||||||
|
export default defineLayout<State>((req, ctx) => {
|
||||||
|
return (
|
||||||
|
<div class="layout">
|
||||||
|
<p>
|
||||||
|
Layout: {ctx.state.something === "foo" ? "it works" : "it doesn't work"}
|
||||||
|
</p>
|
||||||
|
<ctx.Component />
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { defineRoute } from "$fresh/server.ts";
|
||||||
|
import { delay } from "$fresh/tests/deps.ts";
|
||||||
|
import { State } from "../other/state.ts";
|
||||||
|
|
||||||
|
export default defineRoute<State>(async (req, ctx) => {
|
||||||
|
await delay(10);
|
||||||
|
return (
|
||||||
|
<div class="page">
|
||||||
|
<p>
|
||||||
|
Page: {ctx.state.something === "foo" ? "it works" : "it doesn't work"}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -171,3 +171,14 @@ Deno.test({
|
|||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("define helpers", async () => {
|
||||||
|
await withFresh(
|
||||||
|
"./tests/fixture_define_helpers/main.ts",
|
||||||
|
async (address) => {
|
||||||
|
const doc = await fetchHtml(`${address}`);
|
||||||
|
assertSelector(doc, "html > body > .app > .layout > .page");
|
||||||
|
assertTextMany(doc, "p", ["Layout: it works", "Page: it works"]);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user