Skip to content

Commit

Permalink
update example
Browse files Browse the repository at this point in the history
added Future.maybeUnwrap method
added TioResponse.maybeResult and maybeError getters
2.3.0
  • Loading branch information
urusai88 committed Nov 28, 2024
1 parent c1a64fe commit 2d8f5a6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
36 changes: 20 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,29 @@ 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<String, dynamic> json) : id = json['id'] as int;
User.fromJson(JsonMap json) : id = json['id'] as int;
final int id;
}
class MyError {
const MyError.fromString(this.errorMessage);
const MyError.empty() : errorMessage = 'Unknown message';
MyError.fromJson(Map<String, dynamic> json)
: errorMessage = json['message'] as String;
MyError.fromJson(JsonMap json) : errorMessage = json['message'] as String;
final String errorMessage;
}
const factoryConfig = TioFactoryConfig<MyError>(
[
TioJsonFactory<User>(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();
Expand Down Expand Up @@ -77,7 +68,20 @@ void main() async {
case TioFailure<User, MyError>(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
Expand Down
9 changes: 6 additions & 3 deletions example/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 14 additions & 11 deletions lib/src/responses/response.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:equatable/equatable.dart';

import '../errors.dart';
import '../typedefs.dart';

mixin TioResponseMixin<R, E> {
Expand All @@ -23,6 +22,20 @@ mixin TioResponseMixin<R, E> {
final TioFailure<R, E> 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<R, E> with EquatableMixin, TioResponseMixin<R, E> {
Expand All @@ -34,16 +47,6 @@ abstract class TioResponse<R, E> with EquatableMixin, TioResponseMixin<R, E> {

TioResponse<T, E> withSuccess<T>(TioResultTransformer<R, E, T> 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;
Expand Down
14 changes: 8 additions & 6 deletions lib/src/x.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ extension DioX on Dio {
}

extension FutureTioResponseX<R, E> on Future<TioResponse<R, E>> {
Future<T> map<T>({
required T Function(TioSuccess<R, E> success) success,
required T Function(TioFailure<R, E> failure) failure,
Future<K> map<K>({
required K Function(TioSuccess<R, E> success) success,
required K Function(TioFailure<R, E> failure) failure,
}) =>
then((response) => response.map(success: success, failure: failure));

Future<T> when<T>({
required T Function(R result) success,
required T Function(E error) failure,
Future<K> when<K>({
required K Function(R result) success,
required K Function(E error) failure,
}) =>
then((response) => response.when(success: success, failure: failure));

Future<R> unwrap() => then((response) => response.requireResult);

Future<R?> maybeUnwrap() => then((response) => response.maybeResult);
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: tio
version: 2.2.0
version: 2.3.0
repository: https://github.com/urusai88/tio

description: |
Expand Down

0 comments on commit 2d8f5a6

Please sign in to comment.