UNPKG

rwsdk

Version:

Build fast, server-driven webapps on Cloudflare with SSR, RSC, and realtime

81 lines (80 loc) 3.32 kB
type Interruptor<TArgs extends any[] = any[]> = (context: { request: Request; ctx: Record<string, any>; args: TArgs; }) => Promise<Response | void> | Response | void; type ServerFunction<TArgs extends any[] = any[], TResult = any> = (...args: TArgs) => Promise<TResult>; type ServerFunctionOptions = { method?: "GET" | "POST"; }; type WrappedServerFunction<TArgs extends any[] = any[], TResult = any> = { (...args: TArgs): Promise<TResult>; method?: "GET" | "POST"; }; export type ServerFunctionWrap = (fn: Function, args: any[], type: "action" | "query") => Promise<any>; /** * Register a wrapper that runs around every server action and query handler. * * Call this once in your worker entry point. The wrapper receives the main * handler function, its arguments, and the type ("action" or "query"). * Interruptors run *outside* the wrapper. * * Only one wrapper is active at a time; the most recent call wins. * * @example * ```ts * import { registerServerFunctionWrap } from "rwsdk/worker"; * import * as Sentry from "@sentry/cloudflare"; * * registerServerFunctionWrap((fn, args, type) => * Sentry.startSpan( * { name: fn.name, op: `function.rsc_${type}` }, * () => fn(...args) * ) * ); * ``` */ export declare function registerServerFunctionWrap(wrap: ServerFunctionWrap): void; /** * @internal Reset the global wrap — only for tests. */ export declare function __resetServerFunctionWrap(): void; /** * Wrap a function to be used as a server query. * * - **Method**: Defaults to `GET`. can be changed via `options`. * - **Behavior**: When called from the client, it returns data-only and does **not** rehydrate or re-render the React page. * - **Location**: Must be defined in a file with `"use server"`. We recommend `queries.ts` colocated with components. * - **Middleware**: You can pass an array of functions as the first argument to act as interruptors (e.g. for auth). * * @example * ```ts * // getters.ts * "use server" * * export const getUser = serverQuery(async (id: string) => { * return db.user.findUnique({ where: { id } }) * }) * ``` */ export declare function serverQuery<TArgs extends any[], TResult>(fnsOrFn: ServerFunction<TArgs, TResult> | [...Interruptor<TArgs>[], ServerFunction<TArgs, TResult>], options?: ServerFunctionOptions): WrappedServerFunction<TArgs, TResult>; /** * Wrap a function to be used as a server action. * * - **Method**: Defaults to `POST`. can be changed via `options`. * - **Behavior**: When called from the client, it **will** rehydrate and re-render the React page with the new server state. * - **Location**: Must be defined in a file with `"use server"`. We recommend `actions.ts` colocated with components. * - **Middleware**: You can pass an array of functions as the first argument to act as interruptors (e.g. for auth). * * @example * ```ts * // actions.ts * "use server" * * export const updateUser = serverAction(async (id: string, data: any) => { * return db.user.update({ where: { id }, data }) * }) * ``` */ export declare function serverAction<TArgs extends any[], TResult>(fnsOrFn: ServerFunction<TArgs, TResult> | [...Interruptor<TArgs>[], ServerFunction<TArgs, TResult>], options?: ServerFunctionOptions): WrappedServerFunction<TArgs, TResult>; export {};