Expand description
It is the boundary between shared screens and the server. The same call runs
during SSR, in the browser, and in a native guest; leptoskit_rpc_codegen
turns the specification into TypeScript, Python, Go, Rust, Dart, and CLI
clients.
A leptos server function is already a typed, isomorphic RPC: #[server]
generates a struct whose FromArg/await call handles serialization,
transport, and routing on both sides. What it leaves to you is everything
around the call — retrying flaky networks, classifying which failures are
worth retrying, and wrapping results so loading and failure states are one
type. This crate adds exactly that layer:
- [
query] turns any server-fn call into a reactiveQuerywith a retry policy, ready for<Await>-style rendering; - [
mutation] andmutation_with_retrywrap calls in a leptosActionwith the same error handling; FnErrorclassifies failures so “offline” and “your input was invalid” stop looking identical in the UI;- [
query] andmutationdeclare operations that run on the database as the caller, called by their argument structs from every platform through [client::call] (featuresclient,web,guest) and served atPOST /api/<name>by [server::Server] (featuresserver,db, andpostgresorsqlite); apipublishes a server function to generated clients — the same RPC, described, with docs — soleptoskit rpc generatecan write typed clients for TypeScript, Python, Go, Rust, Dart, and the command line (see the [spec] module, behind thessrfeature).
use leptos::prelude::*;
use leptoskit_rpc::FnError;
use leptoskit_rpc::RetryPolicy;
use leptoskit_rpc::mutation;
use leptoskit_rpc::query;
// Reads retry automatically and stay reactive.
let todos = query(RetryPolicy::aggressive(), || {
async { list_todos().await.map_err(FnError::from) }
});
// Mutations only retry when asked, because re-sending a POST blindly is
// usually wrong.
let add = mutation(|title: &String| {
let title = title.clone();
async move { create_todo(title).await.map_err(FnError::from) }
});§Publishing the RPC
Put #[api] above #[server] and the function becomes part of the app’s
published RPC: a stable /api/<name> route, a JSON Schema description of
its arguments and result, and its doc comment — including the
# Arguments section — as documentation. #[api(internal)] keeps a
function callable but takes it out of generated clients, and
#[api(scope = Scope::PostsRead)] lets apps that sign in with yours call
it with a token carrying that scope (see scopes!). Enable the ssr
feature next to leptos/ssr, call
leptoskit_rpc::spec::export_if_requested() first in main, and
leptoskit rpc generate writes typed clients for any language that speak
the RPC’s own protocol. See the book’s “Sharing the RPC” chapter.
Re-exports§
pub use scopes::ScopeInfo;pub use scopes::Scopes;pub use scopes::is_scope_name;
Modules§
- live
- The live protocol: the frames a client and a server exchange over a WebSocket, and the patches that keep a 2,000-message channel’s update at about one message’s size.
- scopes
- Typed OAuth scopes: what a third-party app may do with a person’s account once they let it sign in with yours.
Macros§
- scopes
- Declares an enum of OAuth scopes. Each variant names its scope and documents what it allows; the doc comment is what a consent screen shows.
Structs§
- Backoff
- How long a job waits before its next attempt: exponential, from
mindoubling up tomax, each wait a random part of the upper half of that, so jobs that failed together don’t all return together. - Cancel
Job - Asks a job to stop: a waiting one never starts, and a running one sees
cx.cancelled()at its next step. Only the person who enqueued it may. - Failed
- What an
on_failuremutation receives: the job’s arguments, and why it gave up. - Inbox
- The signed-in person’s inbox: the newest notices, and how many are unread.
- Inbox
Args - Reads the signed-in person’s inbox: the query behind
use_inbox. - Inbox
Notice - One notice in someone’s inbox.
- JobOptions
- How a job runs, from
#[rpc::job(...)]. - JobStatus
- A job’s state and result, as a client reads it.
- JobStatus
Args - The arguments of the job-status query: the job’s id.
- Mark
AllRead - Marks every notice in the signed-in person’s inbox read.
- Mark
Read - Marks one notice read.
- Notice
- What a notice says, and what tapping it does.
- Notice
Action - A button on a notice: a mutation and its arguments. Tapping it runs the mutation as the person tapping, under their own policies.
- Page
- One page of a paginated query’s rows.
- Page
Args - Which rows one page of a paginated query reads.
- Retry
Policy - How a retrying call backs off between attempts.
Enums§
- Error
- Why an operation failed, as its body reports it.
- FnError
- One classified server-fn failure.
- Kind
- What kind of operation something is, which decides how it runs and how clients may call it.
- Notice
Kind - How a notice looks.
- Visibility
- Who may read a job besides the person who enqueued it, from
#[rpc::job(visible_to = ...)].
Constants§
- MAX_
PAGE_ ROWS - The most rows a page reads, whatever its
limitasks for: the arguments come from the client. - MAX_
PINNED_ ROWS - The most rows a pinned page reads. It grows as rows are inserted into its
range, so it gets room beyond
MAX_PAGE_ROWS; past this, its oldest rows in page order are left off.
Traits§
- Channel
- One kind of channel: its join arguments, the messages it carries, and the presence each member shows.
- Job
- A job that runs in the background: the struct
#[rpc::job]generates from the function’s arguments. - Operation
- An operation’s arguments, as one value: the struct
#[rpc::query]and#[rpc::mutation]generate from the function’s arguments, named inPascalCase. - Paginated
- A query that reads a page at a time: the arguments carry a
PageArgs, and the result is aPage.#[rpc::query]implements it for a query with aPageArgsargument that returnsrpc::Page<T>. - Resumable
- An item of a resumable stream: where the stream reached with it.
- Stream
Operation - A stream’s arguments, as one value: the struct
#[rpc::stream]generates from the function’s arguments.
Functions§
- call_
with_ retry - Calls
fetch, retrying retryable failures underpolicy. - mutation
- Wraps a server-fn call in a leptos action with unified errors.
- mutation_
with_ retry - Wraps an idempotent server-fn call in a retrying action.
- query
- Turns a server-fn call into a reactive query under a retry policy.
Type Aliases§
- Mutation
- A reactive server-fn mutation.
- Query
- A reactive server-fn query: retrying, error-carrying, ready to render.
- Result
- What an operation’s body returns.
- Retry
Future - The future returned by
call_with_retry.
Attribute Macros§
- action
- Declares an action: an
async fnthat may call anything, published under its name. - api
- Publishes a server function to generated clients.
- channel
- Declares a mutation: an
async fnthat writes in one transaction, published under its name. - cron
- Enqueues an argument-less job on a schedule: a five-field cron
expression in UTC, or
@hourly,@daily,@weekly,@monthly, or@yearly. - job
- Declares a job: work that runs later, in the background.
- mutation
- query
- Declares a query: an
async fnthat reads, published under its name. - stream
- Declares a stream: an
async fnreturning items over time, sent over the live connection only as fast as the client takes them.