From 5161be54a2c60047d70352159a48377c9ddabd49 Mon Sep 17 00:00:00 2001 From: Alex Li Date: Thu, 11 Jan 2024 11:12:24 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20`Dio.clone`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dio/lib/src/dio.dart | 11 +++++++++++ dio/lib/src/dio_mixin.dart | 14 ++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/dio/lib/src/dio.dart b/dio/lib/src/dio.dart index e0a13f3fd..bd6af0d8b 100644 --- a/dio/lib/src/dio.dart +++ b/dio/lib/src/dio.dart @@ -294,4 +294,15 @@ abstract class Dio { /// The eventual method to submit requests. All callers for requests should /// eventually go through this method. Future> fetch(RequestOptions requestOptions); + + /// Creates a new [Dio] instance with: + /// - Current [options] + /// - [interceptors] + /// - [httpClientAdapter] + /// - [transformer] + Dio clone({ + Interceptors? interceptors, + HttpClientAdapter? httpClientAdapter, + Transformer? transformer, + }); } diff --git a/dio/lib/src/dio_mixin.dart b/dio/lib/src/dio_mixin.dart index 8a4b84c7c..3095a2d57 100644 --- a/dio/lib/src/dio_mixin.dart +++ b/dio/lib/src/dio_mixin.dart @@ -740,6 +740,20 @@ abstract class DioMixin implements Dio { } return response; } + + @override + Dio clone({ + Interceptors? interceptors, + HttpClientAdapter? httpClientAdapter, + Transformer? transformer, + }) { + final dio = Dio(options); + dio.interceptors.removeImplyContentTypeInterceptor(); + dio.interceptors.addAll(interceptors ?? this.interceptors); + dio.httpClientAdapter = httpClientAdapter ?? this.httpClientAdapter; + dio.transformer = transformer ?? this.transformer; + return dio; + } } /// A null-check function for function parameters in Null Safety enabled code.