31 lines
872 B
Dart
31 lines
872 B
Dart
import 'dart:convert';
|
|
import 'dart:core';
|
|
|
|
import 'package:backend_dart/domain/errors/app_error.dart';
|
|
import 'package:backend_dart/domain/interface/error.dart';
|
|
import 'package:fpdart/fpdart.dart';
|
|
import 'package:shelf/shelf.dart';
|
|
|
|
TaskEither<IError, Map<String, dynamic>> requestToJson(Request request) {
|
|
return TaskEither.tryCatch(
|
|
() async {
|
|
final body = await request.readAsString();
|
|
return jsonDecode(body);
|
|
},
|
|
(error, stack) => AppError.inputError(
|
|
message: 'Failed to decode JSON: ${error.toString()}',
|
|
),
|
|
);
|
|
}
|
|
|
|
TaskEither<IError, T> decodeJson<T>(
|
|
Map<String, dynamic> json, T Function(Map<String, dynamic>) fromJson) {
|
|
return TaskEither.tryCatch(
|
|
() async {
|
|
return fromJson(json);
|
|
},
|
|
(error, stack) => AppError.inputError(
|
|
message: 'Failed to decode JSON: ${error.toString()}',
|
|
),
|
|
);
|
|
} |