[]previous and next page

ServerFn

A server function: callable on the server like a normal async function, and callable from the client through the RPC endpoint once the build registers it.

A server function: callable on the server like a normal async function, and callable from the client through the RPC endpoint once the build registers it.

Import#

ts
import type { ServerFn } from "@shadow-js/dusk/server";

Signature#

ts
interface ServerFn<Input, Output, Parsed = Input>
  extends ServerFnDefinition<Parsed, Output> {
  /**
   * Calls the function inside the current request (validation and middleware run). The input is
   * optional when its type allows `undefined` (no schema, or a schema of an optional value).
   */
  (...input: [...CallArguments<Input>, options?: ServerFnCallOptions]): Promise<Output>;
  /**
   * The same call with options, as on the browser stub. On the server the options are ignored (a
   * direct call has no network and runs in the current request); the signature exists so code that
   * runs on both sides, such as a component, type-checks against this definition.
   */
  with(options: ServerFnCallOptions): (...input: CallArguments<Input>) => Promise<Output>;
  /** Calls the function with an explicit event, where `getRequestEvent()` is unavailable. */
  invoke(input: Input, event: RequestEvent): Promise<Output>;
  /**
   * The stable id the build gave the function: the same one its browser stub has.
   * @throws DuskError `SERVER_FN_NOT_BUILT` when read on a function the build did not process
   *   (a unit test that imports the module without `dusk()`).
   */
  readonly id: string;
  /**
   * The endpoint, for example `/_dusk/fn/q3Zr…`: the `action` of a `<form>` for a function
   * defined with `form: true`. Rendered on the server it equals the browser stub's `url`.
   * @throws DuskError `SERVER_FN_NOT_BUILT` as {@link ServerFn.id}.
   */
  readonly url: string;
}

Members#

Name Type Description
Input Input
Output Output
Parsed Parsed = Input
with with(options: ServerFnCallOptions): (...input: CallArguments<Input>) => Promise<Output> The same call with options, as on the browser stub. On the server the options are ignored (a direct call has no network and runs in the current request); the signature exists so code that runs on both sides, such as a component, type-checks against this definition.
invoke invoke(input: Input, event: RequestEvent): Promise<Output> Calls the function with an explicit event, where getRequestEvent() is unavailable.
id readonly id: string The stable id the build gave the function: the same one its browser stub has.
url readonly url: string The endpoint, for example /_dusk/fn/q3Zr…: the action of a <form> for a function defined with form: true. Rendered on the server it equals the browser stub's url.