diff --git a/CHANGELOG.md b/CHANGELOG.md index b6c169a..7c3ee3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 2.3.0 +- Update example +- Added Future.maybeUnwrap method +- Added TioResponse.maybeResult and maybeError getters + ## 2.2.0 - Added TioResponse.requireError getter - Added FutureX.unwrap method diff --git a/README.md b/README.md index 084ea9d..dc28fe4 100644 --- a/README.md +++ b/README.md @@ -18,12 +18,11 @@ Inspired by [chopper](https://pub.dev/packages/chopper). #### Basic usage: ```dart -// ignore_for_file: avoid_print import 'package:dio/dio.dart'; import 'package:tio/tio.dart'; class User { - User.fromJson(Map json) : id = json['id'] as int; + User.fromJson(JsonMap json) : id = json['id'] as int; final int id; } @@ -31,25 +30,17 @@ class User { class MyError { const MyError.fromString(this.errorMessage); - const MyError.empty() : errorMessage = 'Unknown message'; - - MyError.fromJson(Map json) - : errorMessage = json['message'] as String; + MyError.fromJson(JsonMap json) : errorMessage = json['message'] as String; final String errorMessage; } const factoryConfig = TioFactoryConfig( - [ - TioJsonFactory(User.fromJson), - ], - // Factory for error transformation - errorGroup: TioFactoryGroup( - // when response body is empty (or empty string) - empty: TioEmptyFactory(MyError.empty), - string: TioStringFactory(MyError.fromString), // string - json: TioJsonFactory(MyError.fromJson), // or json - ), + jsonFactories: { + User.fromJson, + }, + errorJsonFactory: MyError.fromJson, + errorStringFactory: MyError.fromString, ); final dio = Dio(); @@ -77,7 +68,20 @@ void main() async { case TioFailure(error: final error): print('error acquired ${error.errorMessage}'); } + + // ignore: omit_local_variable_types + final User? user = await getUser(2).map( + success: (success) => success.result, + failure: (failure) => null, + ); + + // ignore: omit_local_variable_types + final User? user2 = await getUser(3).when( + success: (user) => user, + failure: (error) => null, + ); } + ``` ## Guide diff --git a/example/main.dart b/example/main.dart index b26adf6..5e27243 100644 --- a/example/main.dart +++ b/example/main.dart @@ -50,14 +50,17 @@ void main() async { } // ignore: omit_local_variable_types - final User? user = await getUser(2).map( + final User? user = await getUser(1).map( success: (success) => success.result, failure: (failure) => null, ); // ignore: omit_local_variable_types - final User? user2 = await getUser(3).when( - success: (user) => user, + final User? user2 = await getUser(2).when( + success: (result) => result, failure: (error) => null, ); + + // ignore: omit_local_variable_types + final User? user3 = (await getUser(3)).maybeResult; } diff --git a/lib/src/responses/response.dart b/lib/src/responses/response.dart index b472b2d..a4ce88e 100644 --- a/lib/src/responses/response.dart +++ b/lib/src/responses/response.dart @@ -1,6 +1,5 @@ import 'package:equatable/equatable.dart'; -import '../errors.dart'; import '../typedefs.dart'; mixin TioResponseMixin { @@ -23,6 +22,20 @@ mixin TioResponseMixin { final TioFailure value => failure(value.error), _ => throw StateError('Bad state'), }; + + R? get maybeResult => when(success: (result) => result, failure: (_) => null); + + E? get maybeError => when(success: (_) => null, failure: (error) => error); + + R get requireResult => when( + success: (result) => result, + failure: (_) => throw StateError('Response is not succeed'), + ); + + E get requireError => when( + success: (_) => throw StateError('Response is not failures'), + failure: (error) => error, + ); } abstract class TioResponse with EquatableMixin, TioResponseMixin { @@ -34,16 +47,6 @@ abstract class TioResponse with EquatableMixin, TioResponseMixin { TioResponse withSuccess(TioResultTransformer builder); - R get requireResult => map( - success: (success) => success.result, - failure: (_) => throw StateError('Response is not succeed'), - ); - - E get requireError => map( - success: (_) => throw StateError('Response is not failures'), - failure: (failure) => failure.error, - ); - bool get isSuccess => this is TioSuccess; bool get isFailure => this is TioFailure; diff --git a/lib/src/x.dart b/lib/src/x.dart index 40dac21..fc253a5 100644 --- a/lib/src/x.dart +++ b/lib/src/x.dart @@ -50,17 +50,19 @@ extension DioX on Dio { } extension FutureTioResponseX on Future> { - Future map({ - required T Function(TioSuccess success) success, - required T Function(TioFailure failure) failure, + Future map({ + required K Function(TioSuccess success) success, + required K Function(TioFailure failure) failure, }) => then((response) => response.map(success: success, failure: failure)); - Future when({ - required T Function(R result) success, - required T Function(E error) failure, + Future when({ + required K Function(R result) success, + required K Function(E error) failure, }) => then((response) => response.when(success: success, failure: failure)); Future unwrap() => then((response) => response.requireResult); + + Future maybeUnwrap() => then((response) => response.maybeResult); } diff --git a/pubspec.yaml b/pubspec.yaml index 99c6c4e..5a5c9a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,5 +1,5 @@ name: tio -version: 2.2.0 +version: 2.3.0 repository: https://github.com/urusai88/tio description: |