Type-safe Dart bindings for tRPC backends without build tools.
Project details
rpc-dart provides an end-to-end solution for type-safe communication between Dart applications and tRPC v11 backends. By generating Dart bindings from TypeScript routers, it eliminates the need for manual routing and code generation tools. This ensures compile-time safety and fast regeneration of bindings, while maintaining compatibility with any state management solution in Flutter.
rpc-dart offers a robust solution for achieving end-to-end type safety when interacting with tRPC v11 backends from Dart and Flutter applications. The rpc-dart Command Line Interface (CLI) efficiently reads TypeScript routers to generate straightforward, build-tool-free Dart bindings, which can streamline the development process. The accompanying trpc_dart runtime facilitates server communication over the tRPC wire protocol, incorporating a flexible link pipeline and a built-in, state-management-agnostic query cache.
api.user.getById.query(...).build_runner, freezed, or json_serializable. Generated code includes const constructors, copyWith, and value equality functions, as well as fromJson and toJson methods.z.input and output models from z.output.RpcOpt<T> type ensures clarity in parameter definitions, distinguishing between omitted keys, explicit null, and provided values.$unknown sentinels to prevent application crashes.RpcQueryCache with invalidation streams, compatible with various state management frameworks, including setState, BLoC, Provider, and Riverpod..subscribe() method for real-time updates through Server-Sent Events (SSE) or WebSocket connections, featuring built-in reconnection strategies and authorization handling.For Flutter Applications:
Integrate the trpc_dart package within a Flutter project and utilize its features to make efficient API calls:
final client = RpcClient(
endpoint: Uri.parse('https://api.example.com/trpc'),
transformer: RpcTransformer.superjson,
links: RpcLinkChain([
AuthLink(tokenProvider: myTokenProvider),
HttpBatchLink(),
]),
);
final api = Api(client);
// Fetching user data
final user = await api.user.getById.query(UserGetByIdInput(id: widget.userId));
Handling Async State:
Use Flutter's ValueListenableBuilder for responsive UI updates based on the state of API queries:
ValueListenableBuilder<RpcAsyncState<UserGetByIdOutput>>(
valueListenable: api.user.getById.listenable(UserGetByIdInput(id: widget.userId)),
builder: (context, state, _) {
switch (state) {
case RpcAsyncLoading():
return const CircularProgressIndicator();
case RpcAsyncError(let error):
return Text('error: $error');
case RpcAsyncData(let data):
return Text(data.id);
}
},
);
An API call traverses a composable chain of links, ensuring that requests are processed intelligently:
Api call → LoggingLink → AuthLink → ContextLink → RetryLink → TransformLink → HttpBatchLink
This structure allows for enhanced manageability over aspects like authentication, logging, and error handling.
The CLI offers various commands that assist in the integration and maintenance of your TypeScript and Dart projects, making it possible to initialize the setup, generate Dart bindings, monitor changes incrementally, and validate configurations efficiently.
For comprehensive documentation, including installation steps and advanced usage guides, visit the documentation page.
By implementing rpc-dart, developers can leverage a streamlined, type-safe communication layer for their Dart and Flutter applications with tRPC, enhancing both performance and maintainability.
Comments
0Start the conversation
Share the first comment.