Last active
February 4, 2024 10:49
-
-
Save CoderNamedHendrick/3d94732b0ae6abf764638b844ac04b6e to your computer and use it in GitHub Desktop.
Predictable data transformer for API responses
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:either_dart/either.dart'; | |
import '../exceptions/exceptions.dart'; | |
Either<AppException, E> transform<E>( | |
dynamic data, E Function(Map<String, dynamic>) transformer) { | |
try { | |
return Right(transformer(data)); | |
} on TypeError { | |
return Left(ObjectParseException()); | |
} catch (e) { | |
return Left(GroundException(e.toString())); | |
} | |
} | |
Either<AppExceptions, E> transformObject<E>( | |
dynamic data, E Function(Object) transformer) { | |
try { | |
return Right(transformer(data)); | |
} on AkuExceptions catch (e) { | |
return Left(e); | |
} on TypeError catch (e) { | |
debugPrint(e.toString()); | |
return const Left(ObjectParseException()); | |
} on Exception catch (e) { | |
return Left(GroundException(e.toString())); | |
} | |
} | |
Either<AppExceptions, E> processData<E, T>( | |
E Function(Map<String, dynamic>) transformer, Either<AppExceptions, T> response) { | |
if (response.isLeft) return Left(response.left); | |
final data = transform(response.right, transformer); | |
return data.either((left) => left, (right) => right); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
typedef EitherExceptionOr<T> = Either<AppException, T>; | |
Future<EitherExceptionOr<E>> processData<E>( | |
E Function(dynamic json) transformer, | |
EitherExceptionOr<Response?> response, | |
) async { | |
if (response.isLeft) return Left(response.left); | |
return await compute<dynamic, EitherExceptionOr<E>>( | |
(message) => _transform(message, (p0) => transformer(p0)), | |
response.right!.data, | |
); | |
} | |
EitherExceptionOr<E> _transform<E>( | |
dynamic data, E Function(dynamic) transformer) { | |
try { | |
final json = data as Map<String, dynamic>; | |
if (json['successful']) { | |
return Right(transformer(json['result'] as Map<String, dynamic>)); | |
} | |
if (json['validationMessages'] case final List messagesList?) { | |
return Left(ValidationMessagesException( | |
messagesList.map((e) => e.toString()).toList(), | |
)); | |
} | |
return Left(ServerException(json['message'] as String)); | |
} on TypeError catch (e) { | |
return Left(ObjectParseException(e.stackTrace)); | |
} on Exception catch (e) { | |
return Left(GroundException(e.toString())); | |
} | |
} |
@usupjr I use the processData function to parse responses from the backend and return an Either<AppException, T(PODO)>. The process data uses flutter compute to run the parse operation
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is an exception