Skip to content
AI NEWS JOURNAL
Hacker News Show HNAI NEWS / JOURNAL

Show HN: Dart-TS Bridge

Working with Dart for years now and same goes for Node.JS. While we can use tRPC or oRPC to manage type-safe communication between front-end and backend in web but if you go to mobile with Flutter all that goes to waste, those carefully built routers, procedures and middleware everything.Here's a thing that could help tho :) Comments URL: https://news.ycombinator.com/item?id=50659956 Points: 1 # Comments: 0

End-to-end type safety for tRPC v11 and oRPC backends consumed from Dart and Flutter. The rpc-dart CLI reads your TypeScript router and generates plain, build-tool-free Dart bindings; the trpc_dart runtime calls your server over the real tRPC and oRPC wire protocols with a composable link pipeline and a built-in, state-management-agnostic query cache. Rename a procedure or change a Zod field on the server: the Dart bindings regenerate in milliseconds and the Dart analyzer breaks at every stale call site. No route strings, no hand-written models, no build_runner, no freezed. TypeScript router ──► rpc-dart CLI ──► generated Dart bindings ──► trpc_dart runtime ──► your tRPC server Core Features Zero manual route strings. Nested routers compile to nested typed getters: api.user.getById.query(...). Compile-time safety across languages. Renaming a procedure or changing a Zod input field triggers a Dart analyzer error at every affected call site after regeneration. No Dart build step. Generated code is plain Dart 3: const constructors, copyWith, value equality, fromJson/toJson. AOT-compatible. build_runner, freezed, and json_serializable are never required. Input vs output correctness. Zod .transform(), .default(), and .coerce() are handled correctly: wire input models come from z.input, response models from z.output. Three-state PATCH semantics. RpcOpt distinguishes an omitted key, an explicit null, and a value — no silent merge bugs on partial updates. Unknown-safe enums and unions. Server-side enum additions decode to $unknown sentinels instead of crashing the app; discriminated unions become sealed Dart classes. State-management agnostic. Direct calls (.query(), .mutate(), .safeQuery(), .safeMutate()), structural query keys, a zero-dependency RpcQueryCache with invalidation streams and .watch(), and a native Flutter ValueListenable bridge. Works with setState, BLoC, Provider, Riverpod, or nothing at all. Real-time subscriptions. Generated .subscribe() streams over SSE or WebSocket with tracked() resume, exponential-backoff reconnect, and single-flight auth refresh (HttpSubscriptionLink, WsSubscriptionLink, SplitLink). Composable middleware. A link pipeline (AuthLink, RetryLink, HttpBatchLink, ...) with per-call context: headers, timeouts, cancellation, idempotency keys. Real protocol support. HTTP batching with index-correlated responses, SuperJSON envelopes (Date, BigInt, NaN, ...), SSE subscriptions, the full tRPC error-code table as typed exceptions. oRPC support. The @rpc-dart/orpc-adapter extracts oc contracts and os / implement() routers (Standard Schema v1: Zod, Valibot, ArkType), including handler-inferred outputs, eventIterator subscriptions, and .errors() typed error maps. OrpcLink speaks the RPCHandler wire: {json, meta} envelopes, multipart file maps, and SSE event iterators. Binary & file transfers. RpcFile, Uint8List, and RpcByteStream flow end-to-end with onSendProgress / onReceiveProgress callbacks, cancellation, and zero casts — native multipart on oRPC, a SuperJSON registerCustom convention on tRPC. Quickstart 1. Backend: install the CLI Requires Node.js 20+ and a tRPC v11 router or an oRPC contract/router (with or without Zod — oRPC accepts any Standard Schema v1 library: Zod, Valibot, ArkType). npm install --save-dev rpc-dart # or: pnpm add -D rpc-dart npx rpc-dart init # detects your router, tsconfig, and Flutter target; scaffolds rpc-dart.config.ts npx rpc-dart watch # run next to your dev server; regenerates Dart on router changes A minimal rpc-dart.config.ts: import { defineConfig } from 'rpc-dart'; export default defineConfig({ entry: './src/trpc/router.ts', // module that exports the router exportName: 'appRouter', transformer: 'superjson', // must match your initTRPC transformer dart: { outDir: '../mobile/lib/src/api' }, }); Make sure the router type is exported next to the router itself: const appRouter = t.router({ user: userRouter, posts: postsRouter }); export type AppRouter = typeof appRouter; export { appRouter }; 2. Flutter: add the runtime dependencies: # tRPC backends: trpc_dart: ^0.3.0 # pure Dart runtime (works on server, CLI, web, VM) trpc_flutter: ^0.3.0 # optional: ValueListenable bridge for Flutter UIs # oRPC backends — install the orpc_* facades instead: orpc_dart: ^0.3.0 # oRPC runtime facade over trpc_dart (hides tRPC-only transports) orpc_flutter: ^0.3.0 # optional: orpc_dart + the Flutter ValueListenable bridge oRPC clients install the orpc_* facades and import package:orpc_dart/orpc_dart.dart; the tRPC example below uses trpc_dart. import 'package:trpc_dart/trpc_dart.dart'; import 'api/api.dart'; // generated final endpoint = Uri.parse('https://api.example.com/trpc'); final client = RpcClient( endpoint: endpoint, transformer: RpcTransformer.superjson, // match the server transformer links: [ AuthLink(getHeaders: () async => {'authorization': 'Bearer $token'}), HttpBatchLink(endpoint: endpoint), ], ); final api = Api(client); // generated root Usage Examples Given a backend with user.me, user.getById, user.update, and posts.list procedures: Vanilla Flutter: setState, FutureBuilder, ValueListenableBuilder // One-shot call with setState Future _load() async { setState(() => _loading = true); try { final user = await api.user.getById.query(UserGetByIdInput(id: widget.userId)); setState(() => _user = user); } on RpcUnauthorizedException { Navigator.pushNamed(context, '/login'); } finally { setState(() => _loading = false); } } // Result-typed variant (no throwing): switch over RpcResult final res = await api.user.getById.safeQuery(UserGetByIdInput(id: widget.userId)); switch (res) { case RpcSuccess(:final data): print('hello ${data.id}'); case RpcFailure(:final error): print('failed: ${error.message}'); // error carries zod field errors when present } // Reactive: ValueListenableBuilder over the built-in cache. // The listenable updates itself when this query is invalidated or refetched. ValueListenableBuilder>( valueListenable: api.user.getById.listenable(UserGetByIdInput(id: widget.userId)), builder: (context, state, _) => switch (state) { RpcAsyncLoading() => const CircularProgressIndicator(), RpcAsyncError(:final error) => Text('error: $error'), RpcAsyncData(:final data) => Text(data.id), }, ); // Mutations refresh the cache explicitly — no string heuristics: final updated = await api.user.update.mutate(UserUpdateInput( name: 'Ada', nickname: const RpcNull(), // explicit null on the wire promoCode: 'PROMO2024', // required field )); api.invalidate(api.user.pathKey); // refetches every watched query under 'user' BLoC / Cubit class UserCubit extends Cubit> { UserCubit(this._api, this._userId) : super(const RpcAsyncLoading()); final Api _api; final String _userId; StreamSubscription? _invalidationSub; void watch() { // .watch() yields a Stream> that re-emits on invalidation. _invalidationSub = _api.onInvalidate(_api.user.pathKey).listen((_) {}); _api.user.getById .watch(UserGetByIdInput(id: _userId)) .stream .listen(emit); } Future rename(String name) async { await _api.user.update.mutate(UserUpdateInput(name: name, promoCode: 'PROMO2024')); _api.invalidate(_api.user.pathKey); // the watched cubit state refreshes itself } @override void close() { _invalidationSub?.cancel(); return super.close(); } } Provider (ChangeNotifier) class UserNotifier extends ChangeNotifier { UserNotifier(this._api, this._userId) { _sub = _api.user.getById .watch(UserGetByIdInput(id: _userId)) .stream .listen((state) { _state = state; notifyListeners(); }); } final Api _api; final String _userId; late final StreamSubscription> _sub; RpcAsyncState _state = const RpcAsyncLoading(); RpcAsyncState get state => _state; @override void dispose() { _sub.cancel(); super.dispose(); } } Riverpod // Family provider over the typed call — invalidation via the structural key. final userProvider = FutureProvider.autoDispose .family((ref, id) async { final api = ref.watch(apiProvider); final invalidate = api.onInvalidate(api.user.getById.pathKey); ref.onDispose(invalidate.listen((_) => ref.invalidateSelf()).cancel); return api.user.getById.query(UserGetByIdInput(id: id)); }); // Or keep the built-in cache as the single source of truth: final userStateProvider = StreamProvider.autoDispose .family, String>((ref, id) { final api = ref.watch(apiProvider); return api.user.getById.watch(UserGetByIdInput(id: id)).stream; }); Link Pipeline Requests flow through a composable chain. The last link is the transport. Api call → LoggingLink → AuthLink → ContextLink → RetryLink → HttpBatchLink Per-call overrides ride on RpcCallContext and never touch client configuration: final user = await api.user.getById.query( UserGetByIdInput(id: '1'), context: RpcCallContext( headers: {'x-org-id': 'org_99'}, timeout: Duration(seconds: 5), cancelToken: token, ), ); Custom links are plain functions over the chain: class OrgHeaderLink implements RpcLink { @override Future invoke(RpcRequest request, RpcLinkNext next) { return next(request.withAddedHeader('x-org-id', currentOrgId)); } } CLI Reference Documentation Full website-structured documentation lives in docs/: Getting started — installation, config, quickstart Concepts — how the compiler works, type mapping, links & auth Guides — vanilla Flutter, BLoC, Provider, Riverpod, oRPC quickstart, file & binary transfers, monorepo & CI Reference — CLI reference, Dart runtime API, troubleshooting Roadmap — shipped milestones and upcoming work MIT — see LICENSE.

Sources

Read original ↗

Related topics

← Continue to all updates