UNPKG

rwsdk

Version:

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

53 lines (52 loc) 2.43 kB
type Interruptor<TArgs extends any[] = any[], TResult = any> = (context: { request: Request; ctx: Record<string, any>; args: TArgs; }) => Promise<Response | void | TResult> | Response | void | TResult; 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"; }; /** * 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[] = any[], TResult = any>(fnsOrFn: ServerFunction<TArgs, TResult> | [...Interruptor<TArgs, TResult>[], 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[] = any[], TResult = any>(fnsOrFn: ServerFunction<TArgs, TResult> | [...Interruptor<TArgs, TResult>[], ServerFunction<TArgs, TResult>], options?: ServerFunctionOptions): WrappedServerFunction<TArgs, TResult>; export {};