[]previous and next page

serverFnStub

Creates the browser function that stands for a server function.

Creates the browser function that stands for a server function. The build calls this; you rarely do.

Arguments are encoded with serialize (dates, maps, sets, bigint and undefined survive; functions and class instances, Error included, throw a TypeError). The answer is decoded the same way. Failures do not travel as Error instances: the server sends the status, name and the public message in the answer's error member, and the stub rebuilds the error from that. A stack or a cause never crosses the wire, and in production the message of anything but a client error (or an HttpError created with expose: true) is the standard reason phrase.

Import#

ts
import { serverFnStub } from "@shadow-js/dusk/client";

Signature#

ts
function serverFnStub<Input = unknown, Output = unknown>(
  id: string,
  meta: ServerFnStubMeta,
): ClientServerFn<Input, Output>

Parameters#

Name Description
id The stable id of the server function.
meta Method, form support, mount base, batching and single flight, all known at build time.

Returns#

A callable with .id, .url and .with(options).

Throws#

  • HttpError for every HttpError the server function threw, with its status, and its message and data as the server allows (4xx: as thrown; 5xx: the reason phrase unless expose: true), and for a failed input validation (400, data.issues).
  • ServerFnError for any other error on the server (status 500, requestId), an unknown method, or an aborted request.
  • ServerFnProtocolError when the answer is not a server function answer.

Examples#

ts
const getPost = serverFnStub("q3Zr0b1g2yTt0m5Qk9w3xA", { method: "GET", form: false, base: "" });
const post = await getPost({ id: "7" });