feat: add render function (#2454)
This commit is contained in:
+21
-8
@@ -18,6 +18,8 @@ export interface Define<State> {
|
||||
* of your route handlers. For example:
|
||||
*
|
||||
* ```ts
|
||||
* import { define } from "../utils.ts";
|
||||
*
|
||||
* export const handler = define.handlers((ctx) => {
|
||||
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
|
||||
* return new Response("Hello, world!");
|
||||
@@ -29,19 +31,22 @@ export interface Define<State> {
|
||||
* For more information, see {@link Define.page}.
|
||||
*
|
||||
* You can also pass an explicit type argument to ensure that all data
|
||||
* returned from the render function is of the correct type:
|
||||
* returned from the handler is of the correct type:
|
||||
*
|
||||
* ```ts
|
||||
* import { page } from "@fresh/core";
|
||||
* import { define } from "../utils.ts";
|
||||
*
|
||||
* export const handler = define.handlers<{ slug: string }>({
|
||||
* async GET(ctx) {
|
||||
* const slug = ctx.params.slug; // slug is inferred to be a string
|
||||
* return { data: { slug } };
|
||||
* return page({ slug });
|
||||
* },
|
||||
*
|
||||
* // This method will cause a type error because the data object is missing
|
||||
* // the required `slug` property.
|
||||
* async POST(ctx) {
|
||||
* return { data: { } };
|
||||
* return page({ });
|
||||
* },
|
||||
* });
|
||||
* ```
|
||||
@@ -64,6 +69,8 @@ export interface Define<State> {
|
||||
* of the data that your page component receives. For example:
|
||||
*
|
||||
* ```ts
|
||||
* import { define } from "../utils.ts";
|
||||
*
|
||||
* export default define.page((props) => {
|
||||
* const slug = props.params.slug; // Because props is inferred to be a FreshContext object, slug is inferred to be a string
|
||||
* return <h1>{slug}</h1>;
|
||||
@@ -75,10 +82,13 @@ export interface Define<State> {
|
||||
* return type of the handler method.
|
||||
*
|
||||
* ```ts
|
||||
* import { page } from "@fresh/core";
|
||||
* import { define } from "../utils.ts";
|
||||
*
|
||||
* export const handler = define.handlers({
|
||||
* async GET(ctx) {
|
||||
* const slug = ctx.params.slug; // slug is inferred to be a string
|
||||
* return { data: { slug } };
|
||||
* return page({ slug });
|
||||
* },
|
||||
* });
|
||||
*
|
||||
@@ -97,7 +107,8 @@ export interface Define<State> {
|
||||
* @typeParam Data The type of data that the page component receives. This will be inferred from the handler methods if not provided. In very advanced use cases, you can specify `never` to the `Handler` type argument and provide the `Data` type explicitly.
|
||||
*/
|
||||
page<
|
||||
Handler extends RouteHandler<unknown, State> = never,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
Handler extends RouteHandler<any, State> = never,
|
||||
Data = Handler extends HandlerByMethod<infer Data, State> ? Data : never,
|
||||
>(
|
||||
render: AnyComponent<FreshContext<Data, State> & { Component: () => null }>,
|
||||
@@ -112,6 +123,8 @@ export interface Define<State> {
|
||||
* of the context object that your middleware receives. For example:
|
||||
*
|
||||
* ```ts
|
||||
* import { define } from "../utils.ts";
|
||||
*
|
||||
* export const middleware = define.middleware((ctx) => {
|
||||
* ctx.url; // ctx is inferred to be a FreshContext object, so this is a URL
|
||||
* return ctx.next();
|
||||
@@ -122,7 +135,7 @@ export interface Define<State> {
|
||||
*
|
||||
* @typeParam M The type of the middleware function. This will be inferred from the input function. Do not manually specify this type.
|
||||
*/
|
||||
defineMiddleware<M extends Middleware<State>>(
|
||||
middleware<M extends Middleware<State>>(
|
||||
middleware: M,
|
||||
): typeof middleware;
|
||||
}
|
||||
@@ -136,7 +149,7 @@ export interface Define<State> {
|
||||
* it to define your routes and middleware using the
|
||||
* {@link Define.handlers|define.handlers},
|
||||
* {@link Define.page|define.page}, and
|
||||
* {@link Define.defineMiddleware|define.middleware} functions.
|
||||
* {@link Define.middleware|define.middleware} functions.
|
||||
*
|
||||
* @typeParam State The type of the state object that is passed to all middleware and route handlers.
|
||||
*/
|
||||
@@ -148,7 +161,7 @@ export function createDefine<State>(): Define<State> {
|
||||
page(render) {
|
||||
return render;
|
||||
},
|
||||
defineMiddleware(middleware) {
|
||||
middleware(middleware) {
|
||||
return middleware;
|
||||
},
|
||||
};
|
||||
|
||||
+45
-4
@@ -1,10 +1,52 @@
|
||||
import type { FreshContext } from "./context.ts";
|
||||
import type { Method } from "./router.ts";
|
||||
|
||||
export interface Render<T> {
|
||||
export interface PageResponse<T> {
|
||||
data: T;
|
||||
headers: Headers;
|
||||
status: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link PageResponse} object that can be returned from a route
|
||||
* handler. This will cause the page component to be rendered with the specified
|
||||
* data and response options.
|
||||
*
|
||||
* ```ts
|
||||
* export const handlers = define.handlers({
|
||||
* GET: (ctx) => {
|
||||
* return page({ message: "Hello, world!" }, {
|
||||
* headers: { "Cache-Control": "public, max-age=3600" },
|
||||
* status: 201,
|
||||
* });
|
||||
* },
|
||||
* });
|
||||
*
|
||||
* export default define.page<typeof handlers>(({ data }) => {
|
||||
* return <h1>{data.message}</h1>;
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* @param data The data to pass to the page component to be rendered.
|
||||
* @param options Additional options to use when constructing the response object.
|
||||
* @returns A {@link PageResponse} object that should be returned from a route handler.
|
||||
*/
|
||||
export function page<T>(data: T, options?: {
|
||||
headers?: HeadersInit;
|
||||
status?: number;
|
||||
}): PageResponse<T>;
|
||||
export function page(): PageResponse<undefined>;
|
||||
export function page<T>(data?: T, options?: {
|
||||
headers?: HeadersInit;
|
||||
status?: number;
|
||||
}): PageResponse<T> {
|
||||
return {
|
||||
data: data ?? undefined as T,
|
||||
headers: options?.headers instanceof Headers
|
||||
? options.headers
|
||||
: new Headers(options?.headers),
|
||||
status: options?.status ?? 200,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -152,9 +194,8 @@ export function isHandlerByMethod<D, S>(
|
||||
export interface HandlerFn<Data, State> {
|
||||
(ctx: FreshContext<Data, State>):
|
||||
| Response
|
||||
| Render<Data>
|
||||
| void
|
||||
| Promise<Response | Render<Data> | void>;
|
||||
| PageResponse<Data>
|
||||
| Promise<Response | PageResponse<Data>>;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+2
-1
@@ -4,7 +4,8 @@ export { fsRoutes, type FsRoutesOptions } from "./plugins/fs_routes/mod.ts";
|
||||
export {
|
||||
type HandlerByMethod,
|
||||
type HandlerFn,
|
||||
type Render,
|
||||
page,
|
||||
type PageResponse,
|
||||
type RouteData,
|
||||
type RouteHandler,
|
||||
} from "./handlers.ts";
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
import { delay, FakeServer } from "../../test_utils.ts";
|
||||
import { createFakeFs } from "../../test_utils.ts";
|
||||
import { expect } from "@std/expect";
|
||||
import type { HandlerByMethod, HandlerFn } from "../../handlers.ts";
|
||||
import { type HandlerByMethod, type HandlerFn, page } from "../../handlers.ts";
|
||||
import type { Method } from "../../router.ts";
|
||||
import { parseHtml } from "../../../tests/test_utils.tsx";
|
||||
|
||||
@@ -544,7 +544,7 @@ Deno.test("fsRoutes - handler return data", async () => {
|
||||
const server = await createServer({
|
||||
"routes/index.tsx": {
|
||||
handler: () => {
|
||||
return { data: "foo", status: 404 };
|
||||
return page("foo", { status: 404 });
|
||||
},
|
||||
default: (ctx) => {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { type AnyComponent, h, type RenderableProps, type VNode } from "preact";
|
||||
import type { MiddlewareFn } from "../../middlewares/mod.ts";
|
||||
import type { HandlerFn, Render } from "../../handlers.ts";
|
||||
import type { HandlerFn, PageResponse } from "../../handlers.ts";
|
||||
import type { PageProps } from "../../runtime/server/mod.tsx";
|
||||
|
||||
export type AsyncAnyComponent<P> = {
|
||||
@@ -22,7 +22,7 @@ export function renderMiddleware<State>(
|
||||
handler: HandlerFn<unknown, State> | undefined,
|
||||
): MiddlewareFn<State> {
|
||||
return async (ctx) => {
|
||||
let result: Render<unknown> | undefined;
|
||||
let result: PageResponse<unknown> | undefined;
|
||||
if (handler !== undefined) {
|
||||
const res = await handler(ctx);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user