via 1.4.0
via: ^1.4.0 copied to clipboard
A modern, type-safe HTTP client library for Dart/Flutter applications.
🛣️ Via #
A modern, type-safe HTTP & WebSocket client for Dart & Flutter.
Via is a lightweight networking engine built for simplicity and performance. It features a pipeline architecture, built-in resilience, and type-safe response handling.
🚀 Quick Start #
Installation #
Add via to your pubspec.yaml:
dependencies:
via: ^1.3.0
Simple GET Request #
import 'package:via/via.dart';
void main() async {
final via = Via(base: Uri.parse('https://api.example.com'));
final result = await via.get('/users/1');
final user = await result.asMap<String, dynamic>();
print('User Name: ${user['name']}');
}
Multipart File Upload #
final result = await via.post(
'/upload',
{'type': 'avatar'},
files: {
'image': ViaFile.fromBytes(myBytes, filename: 'avatar.png'),
},
);
WebSocket Support #
final socket = await via.socket('/ws');
socket.stream.listen((message) => print('Received: $message'));
socket.send('Hello!');
🔥 Key Features #
🛠️ Pipeline Architecture #
Use pipelines for logging, authentication, and more.
ViaLoggerPipeline: Records history inlogslist. OverrideonLogfor custom printing.ViaCachePipeline: In-memory caching withmaxEntries(FIFO) limit.
final via = Via(
executor: ViaExecutor(
pipelines: [
ViaLoggerPipeline(),
ViaCachePipeline(duration: Duration(minutes: 5), maxEntries: 100),
],
),
);
🛡️ Retry Logic #
Automatic retry on failures. Customize what constitutes an error with errorIf.
final via = Via(
executor: ViaExecutor(
errorIf: (result) => !result.isSuccess,
retry: ViaRetry(maxAttempts: 3),
),
);
🛑 Request Cancellation & cURL #
Easily cancel requests or convert them to cURL commands for debugging.
final cancelToken = CancelToken();
via.get('/data', cancelToken: cancelToken);
cancelToken.cancel();
// cURL debugging
print(result.request.toCurl());
🌊 Streaming Responses #
Simply access the .stream getter on any request to process large responses in real-time. This automatically bypasses any global isolate runners.
// .stream returns Stream<List<int>>
via.get('/large-video.mp4').stream.listen((chunk) {
print('Received ${chunk.length} bytes');
});
💎 Custom Result Types #
Extend ViaResult and use a pipeline to create your own type-safe response models.
class MyResponse extends ViaResult {
MyResponse({required super.request, required super.response});
Future<bool> get hasError async => (await body).contains('error');
}
final via = Via<MyResponse>(
executor: ViaExecutor(
pipelines: [MyResponsePipeline()], // Transforms ViaResult -> MyResponse
),
);
⚖️ License #
This project is licensed under the MIT License.