future_interceptor 0.0.1 copy "future_interceptor: ^0.0.1" to clipboard
future_interceptor: ^0.0.1 copied to clipboard

Integrate Interceptors into an asynchronous operation, inspired by Dio.

Integrate Interceptor into an asynchronous operation, inspired by Dio.

Motivation #

Enables the breakdown of tasks into smaller, independent operational units (Interceptor).

Easily adds new functionality to the application by creating new Interceptors.

Easy to modify and upgrade as Interceptors are not dependent on each other.

Increases code reuse.

Auto catch all exception.

Features #

  • onRequest: allows monitoring and modification of the input request.
  • onResponse: allows monitoring and modification of the response data.
  • onError: allows monitoring and modification of the error.
  • onTransform: allow modification of the FutureResponse before returning to the caller.
  • extra: Custom field that can be retrieved later.

Usage #


/// Create your Interceptor

class LoggingInterceptor extends Interceptor {

  @override
  InterceptorRequestCallback? get onRequest => (options) {
    print("onRequest");
    return options;
  };

  @override
  InterceptorDataCallback? get onResponse => (options, data) {
    print("onResponse with data $data");
    return data;
  };

  @override
  InterceptorErrorCallback? get onError => (options, error) {
    print("onError $error");
    return error;
  };

  @override
  InterceptorTransformCallback? get onTransform => (response) {
    print("onTransform response $response");
    return response;
  };
}

/// Create a class that setup interceptors and expose fetch method

class MyClass {
  late FutureInterceptor _futureInterceptor;

  MyClass() {
    _futureInterceptor = FutureInterceptor();
    _futureInterceptor.interceptors.add(LoggingInterceptor());
  }

  Future<FutureResponse<dynamic>> fetch(FetchMethod request) async {
    return _futureInterceptor.fetch(FutureRequestOptions(
        request: request,
        extra: {'key' : 'val'},
    ));
  }
}

/// Finally

Future test() async {
  final myClass = MyClass();
  final res = await myClass.fetch(() async {
    // Do your work here
    return "my result";
  });
  if (res.error != null) {
    print("Error: ${res.error}");
  } else {
    print("Response: ${res.data}");
  }
}

Some rules #

  • FutureInterceptor.fetch() will never throw an exception.
  • The operation of each Interceptor is not affected by other Interceptors. However, the Interceptor's input can be changed by the Interceptor before it.
  • onRequest and onTransform callbacks will always be called.
  • If request return a result, all onResponse callbacks will be called.
  • If request throws an exception, all onError callbacks will be called.
  • If a callback throws an exception, it will be ignored.
1
likes
0
points
61
downloads

Publisher

unverified uploader

Weekly Downloads

Integrate Interceptors into an asynchronous operation, inspired by Dio.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on future_interceptor