Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add dio extension for flexible interseptor setup and update dio and mutex packages #47

Merged
merged 14 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.0] - 2023-11-13

- Added Dio extension for interceptor setup
- Updated the mutex package to version ^3.1.0

### Breaking Changes

- Updated the dio package to version ^5.0.0

## [0.2.8] - 2023-09-13

- Adds 1.18 to the list of supported FDI versions
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ void setup() {
}
```

Or use instance method instead.

```dart
import 'package:supertokens_flutter/dio.dart';

void setup() {
Dio dio = Dio(); // Create a Dio instance.
dio.addSupertokensInterceptor();
}
```
Comment on lines +92 to +98
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a better way over the existing method? If so, do you think we should recommend this method on our website docs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I was inspired by the Flutter Sentry SDK and they do the same thing in their SDK. So you can use the cascade operator and make something like this

Dio(options) ..addSentry() ..addSupertokensInterceptor()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha! thanks. Keeping this comment open cause we will add it to our docs.


#### Making network requests

```dart
Expand Down
1 change: 1 addition & 0 deletions lib/dio.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/dio-interceptor-wrapper.dart' show SuperTokensInterceptorWrapper;
export 'src/supertokens_dio_extension.dart' show SuperTokensDioExtension;
16 changes: 8 additions & 8 deletions lib/src/dio-interceptor-wrapper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class SuperTokensInterceptorWrapper extends Interceptor {
void onRequest(
RequestOptions options, RequestInterceptorHandler handler) async {
if (!SuperTokens.isInitCalled) {
handler.reject(DioError(
handler.reject(DioException(
requestOptions: options,
type: DioErrorType.other,
type: DioExceptionType.unknown,
error: SuperTokensException(
"SuperTokens.init must be called before using Client"),
));
Expand All @@ -52,7 +52,7 @@ class SuperTokensInterceptorWrapper extends Interceptor {
}

SuperTokensTokenTransferMethod tokenTransferMethod =
SuperTokens.config.tokenTransferMethod!;
SuperTokens.config.tokenTransferMethod;
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
options.headers["st-auth-mode"] = tokenTransferMethod.getValue();

options = await _setAuthorizationHeaderIfRequired(options);
Expand Down Expand Up @@ -131,10 +131,10 @@ class SuperTokensInterceptorWrapper extends Interceptor {
} else {
if (shouldRetry.exception != null) {
handler.reject(
DioError(
DioException(
requestOptions: response.requestOptions,
error: SuperTokensException(shouldRetry.exception!.message),
type: DioErrorType.other),
type: DioExceptionType.unknown),
);
return;
} else {
Expand All @@ -146,13 +146,13 @@ class SuperTokensInterceptorWrapper extends Interceptor {
_refreshAPILock.release();
return handler.next(response);
}
} on DioError catch (e) {
} on DioException catch (e) {
handler.reject(e);
} catch (e) {
handler.reject(
DioError(
DioException(
requestOptions: response.requestOptions,
type: DioErrorType.other,
type: DioExceptionType.unknown,
error: e),
);
} finally {
Expand Down
20 changes: 20 additions & 0 deletions lib/src/supertokens_dio_extension.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:dio/dio.dart';
import 'package:supertokens_flutter/src/dio-interceptor-wrapper.dart';

/// Dio extension for flexible Dio instance setup.
///
/// Usage:
/// ```dart
/// import "package:supertokens_flutter/dio.dart";
///
/// final dio = Dio()
/// ..addSupertokensInterceptor()
/// ..addSentry()
/// // ...
/// ```
extension SuperTokensDioExtension on Dio {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
/// Adds the SuperTokens interceptor to the Dio instance.
void addSupertokensInterceptor() {
interceptors.add(SuperTokensInterceptorWrapper(client: this));
}
}
2 changes: 1 addition & 1 deletion lib/src/version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Version {
static List<String> supported_fdi = ["1.16", "1.17", "1.18"];
static String sdkVersion = "0.2.8";
static String sdkVersion = "0.3.0";
}
2 changes: 1 addition & 1 deletion lib/supertokens.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
library supertokens;

export "src/supertokens.dart";
export "src/errors.dart";
export "src/errors.dart";
16 changes: 8 additions & 8 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,18 @@ packages:
dependency: "direct main"
description:
name: dio
sha256: "7d328c4d898a61efc3cd93655a0955858e29a0aa647f0f9e02d59b3bb275e2e8"
sha256: "417e2a6f9d83ab396ec38ff4ea5da6c254da71e4db765ad737a42af6930140b7"
url: "https://pub.dev"
source: hosted
version: "4.0.6"
version: "5.3.3"
dio_http_formatter:
dependency: "direct dev"
description:
name: dio_http_formatter
sha256: b2ce51f686e0b2739a10698cf8d518b0780f0e54fa3b23321782064694081259
sha256: "3ac0f427cbe1ac9dfaf14231bf2cccdc49c9015e67b786bde6bd9bacd148218d"
url: "https://pub.dev"
source: hosted
version: "2.1.1"
version: "3.2.1"
fake_async:
dependency: transitive
description:
Expand Down Expand Up @@ -204,10 +204,10 @@ packages:
dependency: transitive
description:
name: logger
sha256: "7ad7215c15420a102ec687bb320a7312afd449bac63bfb1c60d9787c27b9767f"
sha256: "6bbb9d6f7056729537a4309bda2e74e18e5d9f14302489cc1e93f33b3fe32cac"
url: "https://pub.dev"
source: hosted
version: "1.4.0"
version: "2.0.2+1"
logging:
dependency: transitive
description:
Expand Down Expand Up @@ -252,10 +252,10 @@ packages:
dependency: "direct main"
description:
name: mutex
sha256: "03116a4e46282a671b46c12de649d72c0ed18188ffe12a8d0fc63e83f4ad88f4"
sha256: "8827da25de792088eb33e572115a5eb0d61d61a3c01acbc8bcbe76ed78f1a1f2"
url: "https://pub.dev"
source: hosted
version: "3.0.1"
version: "3.1.0"
node_preamble:
dependency: transitive
description:
Expand Down
9 changes: 4 additions & 5 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: supertokens_flutter
description: SuperTokens SDK for Flutter apps
version: 0.2.8
version: 0.3.0
homepage: https://supertokens.com/
repository: https://github.com/supertokens/supertokens-flutter
issue_tracker: https://github.com/supertokens/supertokens-flutter/issues
Expand All @@ -18,18 +18,17 @@ dependencies:
sdk: flutter
http: "<1.2.0"
shared_preferences: ">=2.0.0 <3.0.0"
mutex: ^3.0.0
dio: ^4.0.0
mutex: ^3.1.0
dio: ^5.0.0

dev_dependencies:
flutter_test:
sdk: flutter
test: ^1.21.4
dio_http_formatter: ^2.1.0
dio_http_formatter: ^3.2.1

# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

5 changes: 2 additions & 3 deletions test/accesstoken_dio_test.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:dio/dio.dart';
import 'package:dio_http_formatter/dio_http_formatter.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:supertokens_flutter/src/dio-interceptor-wrapper.dart';
Expand Down Expand Up @@ -28,8 +27,8 @@ void main() {
Dio dio = Dio(
BaseOptions(
baseUrl: apiBasePath,
connectTimeout: 5000,
receiveTimeout: 500,
connectTimeout: Duration(seconds: 5),
receiveTimeout: Duration(milliseconds: 500),
),
);
dio.interceptors.add(SuperTokensInterceptorWrapper(client: dio));
Expand Down
5 changes: 0 additions & 5 deletions test/accesstoken_http_test.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import 'dart:convert';
import 'dart:ffi';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:supertokens_flutter/http.dart' as http;
import 'package:supertokens_flutter/src/anti-csrf.dart';
import 'package:supertokens_flutter/src/front-token.dart';
import 'package:supertokens_flutter/src/utilities.dart';
import 'package:supertokens_flutter/supertokens.dart';

import 'test-utils.dart';
Expand Down
4 changes: 2 additions & 2 deletions test/concurrency_dio_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ void main() {
Dio dio = Dio(
BaseOptions(
baseUrl: apiBasePath,
connectTimeout: 5000,
receiveTimeout: 500,
connectTimeout: Duration(seconds: 5),
receiveTimeout: Duration(milliseconds: 500),
),
);
dio.interceptors.add(SuperTokensInterceptorWrapper(client: dio));
Expand Down
12 changes: 6 additions & 6 deletions test/dioInterceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void main() {
Dio dio = Dio(
BaseOptions(
baseUrl: apiBasePath,
connectTimeout: 5000,
receiveTimeout: 500,
connectTimeout: Duration(seconds: 5),
receiveTimeout: Duration(milliseconds: 500),
),
);
dio.interceptors.add(SuperTokensInterceptorWrapper(client: dio));
Expand Down Expand Up @@ -403,8 +403,8 @@ void main() {
Dio dio = Dio(
BaseOptions(
baseUrl: apiBasePath,
connectTimeout: 5000,
receiveTimeout: 500,
connectTimeout: Duration(seconds: 5),
receiveTimeout: Duration(milliseconds: 500),
),
);
dio.interceptors.add(SuperTokensInterceptorWrapper(client: dio));
Expand All @@ -421,8 +421,8 @@ void main() {
Dio dio = Dio(
BaseOptions(
baseUrl: apiBasePath,
connectTimeout: 5000,
receiveTimeout: 500,
connectTimeout: Duration(seconds: 5),
receiveTimeout: Duration(milliseconds: 500),
),
);
dio.interceptors.add(HttpFormatter());
Expand Down
Loading
Loading