A Flutter package for making typed HTTP requests with automatic JSON deserialization, built-in error handling, and optional auth token refresh.
- Typed HTTP client — responses are deserialized directly into your Dart models
- Structured API responses with
success/failedstatus discrimination Result<T, ApiError>return type (viaoption_result) — no uncaught exceptions- Auto-generated
JsonTypefactories via the@AutoFactoryannotation andbuild_runner - Optional
createClientfor shared headers, auth token injection, and automatic token refresh on 401
Add to your pubspec.yaml:
dependencies:
valync: ^0.0.1
dev_dependencies:
build_runner: anyAnnotate your model with @AutoFactory and implement JsonType<T>:
import 'package:valync/valync.dart';
@AutoFactory()
class User implements JsonType<User> {
final String id;
final String name;
User({required this.id, required this.name});
@override
User fromJson(dynamic json) => User(
id: json['id'] as String,
name: json['name'] as String,
);
}Run the generator:
flutter pub run build_runner buildfinal result = await valync<User>('https://api.example.com/users/1');
result.match(
ok: (user) => print(user.name),
err: (error) => print('Error: $error'),
);final client = createClient(
config: ValyncClientConfig(
getAuthHeaders: () => {'Authorization': 'Bearer $token'},
isUnauthorized: (error) => error.code.unwrapOr('') == '401',
onUnauthorized: () async => await refreshToken(),
),
);
// GET
final result = await client<User>('https://api.example.com/me');
// POST
final result = await client<User>(
'https://api.example.com/users',
method: HttpMethod.post,
body: {'name': 'Alice'},
);HttpMethod.get, HttpMethod.post, HttpMethod.put, HttpMethod.patch, HttpMethod.delete
The package expects responses in this shape:
{ "status": "success", "data": { ... } }
{ "status": "failed", "error": { "name": "...", "message": "...", "code": "..." } }- Issues and contributions: open a GitHub issue or pull request
- Built on
option_result,http, andsource_gen