connect_kit 0.1.0 copy "connect_kit: ^0.1.0" to clipboard
connect_kit: ^0.1.0 copied to clipboard

A Connect RPC runtime for Dart: a transport factory with pinned roots and keep-alive, an interceptor chain with retry, metadata and compression middlewares, and the protobuf well-known types.

example/example.dart

// ignore_for_file: avoid_print, an example prints to show the result.

import 'package:connect_kit/connect_kit.dart';
import 'package:connectrpc/connect.dart';

/// A middleware that attaches a bearer token to every call.
class AuthMiddleware extends ConnectMiddleware {
  const AuthMiddleware(this.token);

  final String token;

  @override
  ConnectMiddlewareHandler handle(ConnectMiddlewareHandler invoker) => (path, metadata) async {
    metadata['authorization'] = 'Bearer $token';
    await invoker(path, metadata);
  };
}

Future<void> main() async {
  final httpClient = createRpcHttpClient(config: const ConnectTransportConfig(pingInterval: Duration(minutes: 1)));

  final transport = createConnectTransport(
    Uri.parse('https://api.example.com'),
    httpClient: httpClient,
    interceptors: [
      ConnectMetadataMiddleware(metadata: const {'x-app-version': '1.0.0'}).call,
      const AuthMiddleware('token').call,
      ConnectRetryMiddleware(backoff: const RetryBackoff(maxRetries: 3)).call,
    ],
  );

  // A generated client takes the transport; with a hand-written Spec it looks like this.
  const spec = Spec<Empty, Empty>('/example.v1.ExampleService/Ping', .unary, Empty.create, Empty.create);
  try {
    await Client(transport).unary(spec, Empty());
  } on ConnectException catch (e) {
    print(e.detail('Ping failed'));
  } finally {
    await httpClient.close();
  }
}
1
likes
150
points
--
downloads

Documentation

API reference

Publisher

verified publisherdmitrii.app

Weekly Downloads

A Connect RPC runtime for Dart: a transport factory with pinned roots and keep-alive, an interceptor chain with retry, metadata and compression middlewares, and the protobuf well-known types.

Repository (GitHub)
View/report issues

Topics

#connectrpc #grpc #protobuf #rpc #transport

License

MIT (license)

Dependencies

async, collection, connectrpc, core_model, fixnum, meta, protobuf

More

Packages that depend on connect_kit