ServerFnDefinition
A server function as the build output registers it.
A server function as the build output registers it. serverFn creates these.
I is the type the handler receives. The default, unknown, is what the executor passes: the
output of the function's schema, or the untrusted input when it has none. A typed definition (a
handler that takes { id: string }) is assignable to ServerFnDefinition (a manifest entry)
without a cast because handler is declared as a method, whose parameters are checked
bivariantly; the schema the definition was built with is what makes that input safe.
Import#
ts
import type { ServerFnDefinition } from "@shadow-js/dusk/server";
Signature#
ts
interface ServerFnDefinition<I = unknown, O = unknown> {
/** `"POST"` (the default) or `"GET"` for idempotent reads. */
readonly method: "GET" | "POST";
/** Input schema, if any. Input is untrusted until it has passed it. */
readonly input: StandardSchemaV1 | undefined;
readonly middleware: readonly ServerFnMiddleware[];
/** Whether the function also accepts HTML form posts (`application/x-www-form-urlencoded` or multipart). */
readonly form: boolean;
/** Whether the answer also carries the refreshed loader data of the current page (see `serverFn`). */
readonly singleFlight?: boolean;
/** Delivers an AsyncIterable as typed SSE items; requires `rpc({ streams: {} })`. */
readonly stream?: boolean;
/** `cache-control` of a successful answer. Default `no-store` (see `cache-control.ts`). */
readonly cacheControl?: CacheControlOption;
/** The largest body a call of this function reads, in bytes (see `serverFn({ maxBodyBytes })`). Absent: the app's limit. */
readonly maxBodyBytes?: number;
/** Guards that run before the input is validated (see {@link Guard}). Absent: none. */
readonly guard?: readonly Guard[];
/** Rate limit charged to every call that arrives over HTTP, alone or in a batch. Absent: none. */
readonly rateLimit?: ServerFnRateLimit;
/** The handler. A method, so typed definitions are assignable to the erased default (see above). */
handler(input: I, event: RequestEvent): O | Promise<O>;
}
Members#
| Name | Type | Description |
|---|---|---|
I |
I = unknown |
|
O |
O = unknown |
|
method |
readonly method: "GET" | "POST" |
"POST" (the default) or "GET" for idempotent reads. |
input |
readonly input: StandardSchemaV1 | undefined |
Input schema, if any. Input is untrusted until it has passed it. |
middleware |
readonly middleware: readonly ServerFnMiddleware[] |
|
form |
readonly form: boolean |
Whether the function also accepts HTML form posts (application/x-www-form-urlencoded or multipart). |
singleFlight? |
readonly singleFlight?: boolean |
Whether the answer also carries the refreshed loader data of the current page (see serverFn). |
stream? |
readonly stream?: boolean |
Delivers an AsyncIterable as typed SSE items; requires rpc({ streams: {} }). |
cacheControl? |
readonly cacheControl?: CacheControlOption |
cache-control of a successful answer. Default no-store (see cache-control.ts). |
maxBodyBytes? |
readonly maxBodyBytes?: number |
The largest body a call of this function reads, in bytes (see serverFn({ maxBodyBytes })). Absent: the app's limit. |
guard? |
readonly guard?: readonly Guard[] |
Guards that run before the input is validated (see Guard). Absent: none. |
rateLimit? |
readonly rateLimit?: ServerFnRateLimit |
Rate limit charged to every call that arrives over HTTP, alone or in a batch. Absent: none. |
handler |
handler(input: I, event: RequestEvent): O | Promise<O> |
The handler. A method, so typed definitions are assignable to the erased default (see above). |