fresh_http
0.2.0, released 9 days ago by felangel.dev
An http.Client wrapper that automatically refreshes authentication tokens and retries requests.
WASM-ready
What it does
Fresh_http intercepts outgoing HTTP calls, attaches the current auth token, and refreshes it when it expires or a 401 response is received. It also provides a stream to observe authentication state changes.
AI-written summary of version 0.2.0, based on the package's README and metadata. Report an inaccuracy
Use it when
- You need token‑based authentication with automatic refresh for http package calls
- You want a drop‑in replacement for http.Client without rewriting request code
Skip it when
- You already use a different HTTP client like dio with its own refresh support
- Your app requires complex token handling that Fresh_http doesn't expose
Strengths
- Transparent integration with package:http
- Handles concurrent refreshes with single‑flight logic
- Provides authenticationStatus stream for UI reactions
Weaknesses
- Limited to http.Client; not a full‑featured networking stack
- Customization beyond provided hooks may require extra code
Setup
- Kind
- Pure Dart
- Effort
- Low
- Code generation
- No
- Direct dependencies
- 2
final client = Fresh.oAuth2(
tokenStorage: InMemoryTokenStorage<OAuth2Token>(),
refreshToken: (token, client) async {
final response = await client.post(
Uri.parse('https://api.example.com/auth/refresh'),
body: {'refresh_token': token?.refreshToken},
);
final body = jsonDecode(response.body) as Map<String, dynamic>;
return OAuth2Token(
accessToken: body['access_token'],
refreshToken: body['refresh_token'],
expiresIn: body['expires_in'],
issuedAt: DateTime.now(),
);
},
);Alternatives
Packages that do the same job and could replace fresh_http.
Works with
Packages commonly used together with fresh_http: ones it builds on, ones built on it, and its own family.
- freshfresh_http depends on itCore token refresh library for managing authentication token lifecycles.
- fresh_dioSame familyA dio interceptor for automatic token refresh and authentication management.
- fresh_graphqlSame familyA GraphQL link for package:graphql that manages authentication tokens transparently.