Migration from 0.1 topic
Migration from 0.1
0.2 builds the interceptor on the new token_manager core. Existing 0.1
code compiles and works, with deprecation warnings; the behaviour changes
below are the only things that can surprise you.
import 'package:dio/dio.dart';
import 'package:token_manager/token_manager.dart';
import 'package:token_manager_dio/token_manager_dio.dart';
// 0.1
final class OldDelegate extends RefreshInterceptorDelegate<TokenSet> {
const OldDelegate();
@override
// ignore: deprecated_member_use, shown for comparison.
void applyTokensToRequest(RequestOptions options, TokenSet? tokens) {
options.headers['Authorization'] = 'Bearer ${tokens?.accessToken}';
}
}
// 0.2
final class NewDelegate extends RefreshInterceptorDelegate<TokenSet> {
const NewDelegate();
@override
void applyTo(RequestOptions options, TokenSet tokens) {
options.headers['Authorization'] = 'Bearer ${tokens.accessToken}';
}
}
Renames
| 0.1 | 0.2 |
|---|---|
RefreshInterceptor<T> |
DioTokenInterceptor<T> (the old name is a deprecated subclass) |
applyTokensToRequest(options, tokens) |
applyTo(options, tokens); tokens is non-null |
extra['_auth_tokens'] |
extra['token_manager_dio.tokens'] |
Both delegate methods are concrete. A delegate that overrides only
applyTokensToRequest keeps working; one that overrides neither fails with a
StateError.
Behaviour changes
- Logged out — a request fails with a
DioExceptionwhoseerrorisNotAuthenticatedand is not sent. 0.1 sent it without credentials. - Refresh failures — no longer swallowed:
errorisInvalidGrant(the session ended) orTransient(the session is kept), with the original 401 asresponse. - Replay at most once — a replay is marked with
extra['token_manager_dio.replayed']. This ends the endless loop of 0.1 when theDioowning the interceptor was passed asdioand the server kept answering 401. - Same tokens — if the manager returns the tokens that were rejected, the original error is returned instead of sending the same request again.
- Stream bodies — not replayed and no refresh is started.
- Refresh timing — tokens close to expiry (
skew, 30 seconds by default) are refreshed before the request; a refresh in flight is joined. - Replay request — sent with
Dio.fetchon a copy: thebaseUrlofdiois kept (0.1 overwrote it), the response type is kept, aFormDatabody is cloned.
Requirements
token_manager0.2.0, whose deprecated API (tokens,waitForFresh,executeRefresh,initialize) the interceptor no longer uses.dio5.3.1 or newer, forFormData.clone.
Conventions
- Override
applyToin new delegates; dropapplyTokensToRequest. - Pass
dio:so the replay goes through your client. - Handle
NotAuthenticated,InvalidGrantandTransientinDioException.error.
Classes
-
RefreshInterceptor<
T> Migration from 0.1 - The former name of DioTokenInterceptor.