Optional Refresh Token Handling
RequestFlag System - Type-Safe Request Configuration
Overview
The Vexana NetworkManager now supports a type-safe request configuration system using the RequestFlag enum, replacing magic strings in the extra map. This enhances code maintainability, IDE support, and prevents runtime errors caused by typos.
📌 Related Issue: #129 - Add Optional Refresh Token Handling to NetworkManager
RequestFlag Enum
The RequestFlag enum currently includes:
RequestFlag.disableRefreshToken: Disables automatic refresh token handling for a specific request.
Use cases:
- Login/logout flows (prevent infinite loops)
- Public API calls without authentication
- One-off authentication flows
// Example: disable refresh token for the login endpoint
final result = await networkManager.send<LoginRequest, LoginResponse>(
'/auth/login',
parseModel: LoginResponse(),
method: RequestType.POST,
data: loginData,
requestFlags: {RequestFlag.disableRefreshToken},
);
Updated API Usage
Both send and sendRequest methods now accept an optional requestFlags parameter:
// send method
tFuture<IResponseModel<R?, E?>> send<T extends INetworkModel<T>, R>(
String path, {
required T parseModel,
required RequestType method,
// ... other parameters
Set<RequestFlag>? requestFlags,
});
// sendRequest method
Future<NetworkResult<R, E>> sendRequest<T extends INetworkModel<T>, R>(
String path, {
required T parseModel,
required RequestType method,
// ... other parameters
Set<RequestFlag>? requestFlags,
});
Additional utility extensions are available via RequestFlagExtension.
Benefits
- Type Safety: Compile-time checks prevent typos, IDE autocomplete support.
- Extensibility: Easily add new flags, maintain backward compatibility.
- Developer Experience: Self-documenting code, easy refactoring.
- Maintainability: Eliminates scattered magic strings, centralizes flag definitions.
Future Enhancements
Potential additional flags:
disableRetry: Disable automatic retry mechanismdisableErrorDialog: Disable error dialog displaydisableCache: Disable caching for specific requestsenableDebugLogging: Enable detailed logging per request
Best Practices
- Always use a
Set<RequestFlag>for consistency. - Document the reason for each flag use in comments.
- Avoid unnecessary flags to keep code clean.
// ✅ Good
requestFlags: {RequestFlag.disableRefreshToken}
// ❌ Avoid
requestFlags: [RequestFlag.disableRefreshToken].toSet()
Description This PR improves refresh token handling by removing the global handleRefreshToken parameter and introducing a per-request disableRefreshToken flag:
The disableRefreshToken parameter can now be set for each request in the send and sendRequest methods. When provided, disableRefreshToken is automatically added to options.extra['disableRefreshToken'] internally, so users do not need to manage options.extra themselves. This change prevents side effects and race conditions that could occur when using a global parameter for concurrent requests.
Summary:
-
Removed global handleRefreshToken.
-
Added per-request disableRefreshToken parameter.
-
The library handles setting options.extra['disableRefreshToken'] automatically.
-
Safer and more predictable refresh token handling for concurrent requests.
Pull Request Overview
This PR introduces an optional refresh token handling mechanism in the NetworkManager, allowing developers to toggle token refresh for individual requests.
- Adds a new nullable boolean parameter in multiple methods to control refresh token handling
- Propagates the new parameter via request options and integrates it with error interception
- Updates tests and interface definitions to support the new behavior
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file Comments suppressed due to low confidence (2)
Insignificant comment. He seems confused by previous statements.
Can you review the final version? @VB10