Skip to content

Commit

Permalink
Merge pull request #2 from urusai88/new-response
Browse files Browse the repository at this point in the history
2.0 with new response system
  • Loading branch information
urusai88 authored Nov 20, 2024
2 parents 15bc13b + a71bca6 commit 80a4f62
Show file tree
Hide file tree
Showing 23 changed files with 287 additions and 244 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.0.0
- Added TioResponse.map and when methods similar to freezed map/when
- **BREAKING**: TioResponse now is not exhausted. Use map/when methods
- TioResponse splitted for base TioResponse and TioHttpResponse with dio.Response response property
- Empty factory removed, factories now is typedefs

## 1.1.0
- added Future<TioResponse>.map extension method

Expand Down
25 changes: 21 additions & 4 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
analyzer:
exclude:
- "**.g.dart"
- "**.chopper.dart"
- "**.freezed.dart"
language:
strict-casts: true
strict-inference: true
Expand All @@ -8,18 +12,22 @@ analyzer:
avoid_annotating_with_dynamic: ignore
avoid_catches_without_on_clauses: ignore
avoid_multiple_declarations_per_line: ignore
avoid_print: ignore
avoid_private_typedef_functions: ignore
avoid_redundant_argument_values: ignore
cascade_invocations: ignore
diagnostic_describe_all_properties: ignore
do_not_use_environment: ignore
document_ignores: ignore
lines_longer_than_80_chars: ignore
sort_pub_dependencies: ignore
one_member_abstracts: ignore
parameter_assignments: ignore
prefer_asserts_with_message: ignore
prefer_expression_function_bodies: ignore
public_member_api_docs: ignore
sort_pub_dependencies: ignore
unreachable_from_main: ignore
unused_catch_stack: ignore
unused_element: ignore
unused_local_variable: ignore

Expand All @@ -28,6 +36,8 @@ linter:
- always_declare_return_types
- always_put_control_body_on_new_line
- always_put_required_named_parameters_first
# - always_specify_types # incompatible
# - always_use_package_imports # incompatible with prefer_relative_imports
- annotate_overrides
- annotate_redeclares
- avoid_annotating_with_dynamic
Expand All @@ -41,13 +51,12 @@ linter:
- avoid_equals_and_hash_code_on_mutable_classes
- avoid_escaping_inner_quotes
- avoid_field_initializers_in_const_classes
- avoid_final_parameters
# - avoid_final_parameters # useless lint
- avoid_function_literals_in_foreach_calls
- avoid_implementing_value_types
- avoid_init_to_null
- avoid_js_rounded_ints
- avoid_multiple_declarations_per_line
- avoid_null_checks_in_equality_operators
- avoid_positional_boolean_parameters
- avoid_print
- avoid_private_typedef_functions
Expand Down Expand Up @@ -90,6 +99,7 @@ linter:
- directives_ordering
- discarded_futures
- do_not_use_environment
- document_ignores
- empty_catches
- empty_constructor_bodies
- empty_statements
Expand All @@ -102,6 +112,7 @@ linter:
- implicit_call_tearoffs
- implicit_reopen
- invalid_case_patterns
- invalid_runtime_check_with_js_interop_types
- join_return_with_assignment
- leading_newlines_in_multiline_strings
- library_annotations
Expand All @@ -111,6 +122,7 @@ linter:
- lines_longer_than_80_chars
- literal_only_boolean_expressions
- matching_super_parameters
- missing_code_block_language_in_doc_comment
- missing_whitespace_between_adjacent_strings
- no_adjacent_strings_in_list
- no_default_cases
Expand Down Expand Up @@ -145,10 +157,12 @@ linter:
- prefer_const_literals_to_create_immutables
- prefer_constructors_over_static_methods
- prefer_contains
# - prefer_double_quotes # incompatible with prefer_single_quites
- prefer_expression_function_bodies
- prefer_final_fields
- prefer_final_in_for_each
- prefer_final_locals
# - prefer_final_parameters # useless lint
- prefer_for_elements_to_map_fromIterable
- prefer_foreach
- prefer_function_declarations_over_variables
Expand Down Expand Up @@ -190,15 +204,18 @@ linter:
- type_init_formals
- type_literal_in_constant_pattern
- unawaited_futures
- unintended_html_in_doc_comment
- unnecessary_await_in_return
- unnecessary_brace_in_string_interps
- unnecessary_breaks
- unnecessary_const
- unnecessary_constructor_name
# - unnecessary_final # useless lint
- unnecessary_getters_setters
- unnecessary_lambdas
- unnecessary_late
- unnecessary_library_directive
- unnecessary_library_name
- unnecessary_new
- unnecessary_null_aware_assignments
- unnecessary_null_aware_operator_on_extension_on_nullable
Expand Down Expand Up @@ -236,4 +253,4 @@ linter:
- use_test_throws_matchers
- use_to_and_as_if_applicable
- valid_regexps
- void_checks
- void_checks
35 changes: 19 additions & 16 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
// 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 @@ -57,4 +48,16 @@ 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,
);
}
2 changes: 1 addition & 1 deletion lib/src/api.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:dio/dio.dart';

import 'factory_config.dart';
import 'response.dart';
import 'responses/response.dart';
import 'tio.dart';
import 'tio_mixin.dart';
import 'tio_transform_mixin.dart';
Expand Down
22 changes: 11 additions & 11 deletions lib/src/factory_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ import 'typedefs.dart';

class TioFactoryGroup<E> {
const TioFactoryGroup({
required this.empty,
required this.string,
required this.json,
});

final TioEmptyFactory<E> empty;
final TioStringFactory<E> string;
final TioJsonFactory<E> json;
}

class TioFactoryConfig<E> {
const TioFactoryConfig(
List<TioJsonFactory<dynamic>> factories, {
required this.errorGroup,
}) : _factories = factories;
const TioFactoryConfig({
this.jsonFactories = const {},
required this.errorStringFactory,
required this.errorJsonFactory,
});

final Set<TioJsonFactory<dynamic>> jsonFactories;

final List<TioJsonFactory<dynamic>> _factories;
final TioFactoryGroup<E> errorGroup;
final TioStringFactory<E> errorStringFactory;
final TioJsonFactory<E> errorJsonFactory;

List<Type> get containsFactories =>
_factories.map(_genericTypeFactory).toList();
List<Type> get containsFactories => jsonFactories.map(_genericTypeFactory).toList();

TioJsonFactory<T>? get<T>() =>
_factories.whereType<TioJsonFactory<T>>().firstOrNull;
jsonFactories.whereType<TioJsonFactory<T>>().firstOrNull;

bool contains<T>() => get<T>() != null;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/src/interceptors/auth_interceptor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:logging/logging.dart';

import '../errors.dart';
import '../interceptor.dart';
import '../response.dart';
import '../responses/response.dart';
import '../x.dart';

abstract interface class TioStorageKey<T> {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/interfaces/tio_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:typed_data';

import 'package:dio/dio.dart';

import '../response.dart';
import '../responses/response.dart';
import '../typedefs.dart';

abstract interface class TioBase<E> {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/request_proxy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:typed_data';
import 'package:dio/dio.dart';

import 'interfaces/tio_base.dart';
import 'response.dart';
import 'responses/response.dart';
import 'tio.dart';
import 'typedefs.dart';

Expand Down
80 changes: 0 additions & 80 deletions lib/src/response.dart

This file was deleted.

Loading

0 comments on commit 80a4f62

Please sign in to comment.