UNPKG

@redwoodjs/sdk

Version:

A full-stack webapp toolkit designed for TypeScript, Vite, and React Server Components

70 lines (69 loc) 3.32 kB
import { ExecutionContext } from "@cloudflare/workers-types"; export type HandlerOptions<TAppContext = Record<string, any>> = { request: Request; env: Env; cf: ExecutionContext; appContext: TAppContext; headers: Headers; rw: RwContext<TAppContext>; }; export type RouteOptions<TAppContext = Record<string, any>, TParams = any> = { cf: ExecutionContext; request: Request; params: TParams; env: Env; appContext: TAppContext; headers: Headers; rw: RwContext<TAppContext>; }; export type PageProps<TAppContext> = Omit<RouteOptions<TAppContext>, "request" | "headers" | "rw" | "cf"> & { rw: { nonce: string; }; }; export type DocumentProps<TAppContext> = PageProps<TAppContext> & { children: React.ReactNode; }; export type RenderPageParams<TAppContext> = { Page: React.FC<Record<string, any>>; props: PageProps<TAppContext>; actionResult: unknown; Document: React.FC<DocumentProps<TAppContext>>; }; export type RenderPage<TAppContext> = (params: RenderPageParams<TAppContext>) => Promise<Response>; export type RwContext<TAppContext> = { nonce: string; Document: React.FC<DocumentProps<TAppContext>>; renderPage: RenderPage<TAppContext>; handleAction: (opts: RouteOptions<TAppContext>) => Promise<unknown>; }; export type RouteMiddleware<TAppContext = any> = (opts: RouteOptions<TAppContext>) => Response | Promise<Response> | void | Promise<void> | Promise<Response | void>; type RouteFunction<TAppContext, TParams> = (opts: RouteOptions<TAppContext, TParams>) => Response | Promise<Response>; type RouteComponent<TAppContext, TParams> = (opts: RouteOptions<TAppContext, TParams>) => JSX.Element | Promise<JSX.Element>; type RouteHandler<TAppContext, TParams> = RouteFunction<TAppContext, TParams> | RouteComponent<TAppContext, TParams> | [ ...RouteMiddleware<TAppContext>[], (RouteFunction<TAppContext, TParams> | RouteComponent<TAppContext, TParams>) ]; export type Route<TAppContext> = RouteMiddleware<TAppContext> | RouteDefinition<TAppContext> | Array<Route<TAppContext>>; export type RouteDefinition<TAppContext = Record<string, any>, TParams = any> = { path: string; handler: RouteHandler<TAppContext, TParams>; }; export declare function defineRoutes<TAppContext = Record<string, any>>(routes: Route<TAppContext>[]): { routes: Route<TAppContext>[]; handle: ({ cf, request, appContext, env, rw, headers, }: { cf: ExecutionContext; request: Request; appContext: TAppContext; env: Env; rw: RwContext<TAppContext>; headers: Headers; }) => Response | Promise<Response>; }; export declare function route<TAppContext = any, TParams = any>(path: string, handler: RouteHandler<TAppContext, TParams>): RouteDefinition<TAppContext, TParams>; export declare function index<TAppContext = any, TParams = any>(handler: RouteHandler<TAppContext, TParams>): RouteDefinition<TAppContext, TParams>; export declare function prefix<TAppContext = any, TParams = any>(prefix: string, routes: ReturnType<typeof route<TAppContext, TParams>>[]): RouteDefinition<TAppContext, TParams>[]; export declare function render<TAppContext = any>(Document: React.FC<{ children: React.ReactNode; }>, routes: Route<TAppContext>[]): Route<TAppContext>[]; export {};