trpc_client 0.3.1
trpc_client: ^0.3.1 copied to clipboard
A minimal trpc client for Dart and Flutter.
trpc_client #
A simple trpc client for Dart Applications. It's minimal, dependency-free (only depends on dart's own http library), and works perfectly with flutter.
Features #
- Run Query and Mutate operations against your tRPC server (expressjs, Next.js, Cloudflare Worker, ...)
Usage #
You just need to instantiate the TRPCClient
class with the trpc base endpoint. You can optionally pass headers:
// Import package
import "package:trpc_client/trpc_client.dart";
// Instantiate the client
final client = TRPCClient(
baseUri: "https://example.com/trpc",
headers: {"x-trpc-source": "Dart App"},
);
Now you can query, and mutate, with type safety:
final response = await client.query<Map<String, dynamic>>("route", payload: {"userId": "example"});
if (response.isError) {
print(response.errors);
} else {
final data = response.successResponse.data;
}
Usage with Riverpod (Provider) #
I recommend using it with riverpod, to make authentication easier:
@riverpod
TRPCClient trpcClient(TrpcClientRef ref) {
final sessionId = ref.watch(sessionIdProvider);
final client = TRPCClient(
baseUri: "https://example.com/trpc",
headers: {"Authorization": "Bearer $sessionId"},
);
return client;
}
Additional information #
TOOD #
- ❌ Better README.md
- ❌ Support for batching
- ❌ Write tests with real tRPC server
- ❌ Linter Rules
- ❌ Docs?
- ❌ Somehow generate definitions from ts?
PR's are welcome :D