sinew_network
SinewHttp (Dio): the auth layer, processApiCall, retry, polling and NetworkChecker.
An HTTP client that handles what every app rewrites: attaching the token only to your backend, refreshing it once for any number of concurrent 401s, mapping responses to typed failures, and retrying and polling with backoff. Use the preset, or add only the interceptors you need to your own Dio.
Features
SinewHttp.dio: aDiowith timeouts, headers, auth, error bodies and logging from oneSinewConfigSinewAuthInterceptor: the token goes only tobaseUrl's host, outside the anonymous paths, with a single-flight refresh throughTokenSourceandSessionTokenSourceprocessApiCallandprocessOptionalApiCall: map anEnvelopeto aResultand every status to a typedSinewExceptionretryWithBackoffandpoll,NetworkChecker,pinCertificateAuthorities, andSinewHttpHooksfor the devtools
Getting started
flutter pub add sinew_network
Usage
final config = SinewConfig(
baseUrl: 'https://api.example.com/v1',
locale: () => 'en',
anonymousPaths: {'/auth/login'},
);
final dio = SinewHttp.dio(config, tokenSource);
// In a repository: any failure becomes a typed SinewException.
Future<Result<Order>> cancel(String id) =>
processApiCall('ORD', 'CANCEL', () => api.cancel(id));
API classes use paths relative to baseUrl (get('orders')), since a leading / would replace its own path. On sign-out, call SinewHttp.resetAuth(dio) and tokenSource.clear().
Related packages
Sinew is one architecture for Flutter and Kotlin, released as a set of packages at one version.
Documentation
- Usage docs: sinew.srctool.com
- API reference: pub.dev/documentation/sinew_network
- Source and issues: srctool/sinew-dart
- Design and decisions: the Guide
License
Apache 2.0, see LICENSE.
Libraries
- sinew_network
- SinewHttp (Dio), the auth layer, processApiCall, retry, polling and NetworkChecker.