Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2abe9ed

Browse files
Nathy-bajojkelleyrtp
andauthoredJun 20, 2025··
Fix/server macro defaults (#3403)
* Clarify and improve #[server] macro docs --------- Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
1 parent 097bc1d commit 2abe9ed

File tree

1 file changed

+72
-10
lines changed
  • packages/server-macro/src

1 file changed

+72
-10
lines changed
 

‎packages/server-macro/src/lib.rs

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,85 @@ use syn::{__private::ToTokens, parse_quote};
3131
///
3232
/// ## Named Arguments
3333
///
34-
/// You can any combination of the following named arguments:
34+
/// You can use any combination of the following named arguments:
3535
/// - `name`: sets the identifier for the server function’s type, which is a struct created
36-
/// to hold the arguments (defaults to the function identifier in PascalCase)
37-
/// - `prefix`: a prefix at which the server function handler will be mounted (defaults to `/api`)
36+
/// to hold the arguments (defaults to the function identifier in PascalCase).
37+
/// Example: `name = MyServerFunction`.
38+
/// - `prefix`: a prefix at which the server function handler will be mounted (defaults to `/api`).
39+
/// Example: `prefix = "/my_api"`.
3840
/// - `endpoint`: specifies the exact path at which the server function handler will be mounted,
39-
/// relative to the prefix (defaults to the function name followed by unique hash)
40-
/// - `input`: the encoding for the arguments (defaults to `PostUrl`)
41-
/// - `output`: the encoding for the response (defaults to `Json`)
42-
/// - `client`: a custom `Client` implementation that will be used for this server fn
41+
/// relative to the prefix (defaults to the function name followed by unique hash).
42+
/// Example: `endpoint = "my_fn"`.
43+
/// - `input`: the encoding for the arguments (defaults to `PostUrl`).
44+
/// - The `input` argument specifies how the function arguments are encoded for transmission.
45+
/// - Acceptable values include:
46+
/// - `PostUrl`: A `POST` request with URL-encoded arguments, suitable for form-like submissions.
47+
/// - `Json`: A `POST` request where the arguments are encoded as JSON. This is a common choice for modern APIs.
48+
/// - `Cbor`: A `POST` request with CBOR-encoded arguments, useful for binary data transmission with compact encoding.
49+
/// - `GetUrl`: A `GET` request with URL-encoded arguments, suitable for simple queries or when data fits in the URL.
50+
/// - `GetCbor`: A `GET` request with CBOR-encoded arguments, useful for query-style APIs when the payload is binary.
51+
/// - `output`: the encoding for the response (defaults to `Json`).
52+
/// - The `output` argument specifies how the server should encode the response data.
53+
/// - Acceptable values include:
54+
/// - `Json`: A response encoded as JSON (default). This is ideal for most web applications.
55+
/// - `Cbor`: A response encoded in the CBOR format for efficient, binary-encoded data.
56+
/// - `client`: a custom `Client` implementation that will be used for this server function. This allows
57+
/// customization of the client-side behavior if needed.
4358
/// - `encoding`: (legacy, may be deprecated in future) specifies the encoding, which may be one
44-
/// of the following (not case sensitive)
59+
/// of the following (not case sensitive):
4560
/// - `"Url"`: `POST` request with URL-encoded arguments and JSON response
4661
/// - `"GetUrl"`: `GET` request with URL-encoded arguments and JSON response
4762
/// - `"Cbor"`: `POST` request with CBOR-encoded arguments and response
4863
/// - `"GetCbor"`: `GET` request with URL-encoded arguments and CBOR response
49-
/// - `req` and `res` specify the HTTP request and response types to be used on the server (these
50-
/// should usually only be necessary if you are integrating with a server other than Actix/Axum)
64+
/// - `req` and `res`: specify the HTTP request and response types to be used on the server. These
65+
/// are typically necessary if you are integrating with a custom server framework (other than Actix/Axum).
66+
/// Example: `req = SomeRequestType`, `res = SomeResponseType`.
67+
///
68+
/// ## Advanced Usage of `input` and `output` Fields
69+
///
70+
/// The `input` and `output` fields allow you to customize how arguments and responses are encoded and decoded.
71+
/// These fields impose specific trait bounds on the types you use. Here are detailed examples for different scenarios:
72+
///
73+
/// ### `output = StreamingJson`
74+
///
75+
/// Setting the `output` type to `StreamingJson` requires the return type to implement `From<JsonStream<T>>`,
76+
/// where `T` implements `serde::Serialize` and `serde::de::DeserializeOwned`.
77+
///
78+
/// ```rust,ignore
79+
/// #[server(output = StreamingJson)]
80+
/// pub async fn json_stream_fn() -> Result<JsonStream<String>, ServerFnError> {
81+
/// todo!()
82+
/// }
83+
/// ```
84+
///
85+
/// ### `output = StreamingText`
86+
///
87+
/// Setting the `output` type to `StreamingText` requires the return type to implement `From<TextStream>`.
88+
///
89+
/// ```rust,ignore
90+
/// #[server(output = StreamingText)]
91+
/// pub async fn text_stream_fn() -> Result<TextStream, ServerFnError> {
92+
/// todo!()
93+
/// }
94+
/// ```
95+
///
96+
/// ### `output = PostUrl`
97+
///
98+
/// Setting the `output` type to `PostUrl` requires the return type to implement `Serialize` and `Deserialize`.
99+
/// Note that this uses `serde_qs`, which imposes the following constraints:
100+
/// - The structure must be less than 5 levels deep.
101+
/// - The structure must not contain any `serde(flatten)` attributes.
102+
///
103+
/// ```rust,ignore
104+
/// #[server(output = PostUrl)]
105+
/// pub async fn form_fn() -> Result<TextStream, ServerFnError> {
106+
/// todo!()
107+
/// }
108+
/// ```
109+
///
110+
/// These examples illustrate how the `output` type impacts the bounds and expectations for your server function. Ensure your return types comply with these requirements.
111+
///
112+
///
51113
/// ```rust,ignore
52114
/// #[server(
53115
/// name = SomeStructName,

0 commit comments

Comments
 (0)
Please sign in to comment.