vexana icon indicating copy to clipboard operation
vexana copied to clipboard

Optional Refresh Token Handling

Open ahmtydn opened this issue 7 months ago • 3 comments

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

  1. Type Safety: Compile-time checks prevent typos, IDE autocomplete support.
  2. Extensibility: Easily add new flags, maintain backward compatibility.
  3. Developer Experience: Self-documenting code, easy refactoring.
  4. Maintainability: Eliminates scattered magic strings, centralizes flag definitions.

Future Enhancements

Potential additional flags:

  • disableRetry: Disable automatic retry mechanism
  • disableErrorDialog: Disable error dialog display
  • disableCache: Disable caching for specific requests
  • enableDebugLogging: 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()

ahmtydn avatar May 10 '25 21:05 ahmtydn