@@ -65,9 +65,7 @@ export function setDefaultConfig(
6565) : Partial < RequestConfig > {
6666 const sanitized = sanitizeObject ( customConfig ) ;
6767
68- Object . assign ( defaultConfig , sanitized ) ;
69-
70- return defaultConfig ;
68+ return mergeConfigs ( { } , sanitized , defaultConfig ) ;
7169}
7270
7371/**
@@ -208,26 +206,40 @@ function setContentTypeIfNeeded(
208206 }
209207}
210208
209+ /**
210+ * Merges two request configurations, applying overrides from the second config to the first.
211+ * Handles special merging for nested properties like 'retry' and 'headers' (deep merge),
212+ * and concatenates interceptor arrays for 'onRequest', 'onResponse', and 'onError'.
213+ * If a target config is provided, it mutates that object; otherwise, creates a new one.
214+ *
215+ * @param {RequestConfig } baseConfig - The base configuration object to merge from.
216+ * @param {RequestConfig } overrideConfig - The override configuration object to apply on top of the base.
217+ * @param {RequestConfig } [targetConfig={}] - Optional target configuration object to merge into (mutated in place).
218+ * @returns {RequestConfig } The merged configuration object.
219+ *
220+ * @example
221+ * const base = { timeout: 5000, headers: { 'Accept': 'application/json' } };
222+ * const override = { timeout: 10000, headers: { 'Authorization': 'Bearer token' } };
223+ * const merged = mergeConfigs(base, override);
224+ * // Result: { timeout: 10000, headers: { Accept: 'application/json', Authorization: 'Bearer token' } }
225+ */
211226export function mergeConfigs (
212227 baseConfig : RequestConfig ,
213228 overrideConfig : RequestConfig ,
229+ targetConfig : RequestConfig = { } ,
214230) : RequestConfig {
215- const mergedConfig : RequestConfig = Object . assign (
216- { } ,
217- baseConfig ,
218- overrideConfig ,
219- ) ;
231+ Object . assign ( targetConfig , baseConfig , overrideConfig ) ;
220232
221233 // Ensure that retry and headers are merged correctly
222- mergeConfig ( 'retry' , mergedConfig , baseConfig , overrideConfig ) ;
223- mergeConfig ( 'headers' , mergedConfig , baseConfig , overrideConfig ) ;
234+ mergeConfig ( 'retry' , baseConfig , overrideConfig , targetConfig ) ;
235+ mergeConfig ( 'headers' , baseConfig , overrideConfig , targetConfig ) ;
224236
225237 // Merge interceptors efficiently
226- mergeInterceptors ( 'onRequest' , mergedConfig , baseConfig , overrideConfig ) ;
227- mergeInterceptors ( 'onResponse' , mergedConfig , baseConfig , overrideConfig ) ;
228- mergeInterceptors ( 'onError' , mergedConfig , baseConfig , overrideConfig ) ;
238+ mergeInterceptors ( 'onRequest' , baseConfig , overrideConfig , targetConfig ) ;
239+ mergeInterceptors ( 'onResponse' , baseConfig , overrideConfig , targetConfig ) ;
240+ mergeInterceptors ( 'onError' , baseConfig , overrideConfig , targetConfig ) ;
229241
230- return mergedConfig ;
242+ return targetConfig ;
231243}
232244
233245/**
@@ -237,9 +249,9 @@ function mergeInterceptors<
237249 K extends 'onRequest' | 'onResponse' | 'onError' | 'onRetry' ,
238250> (
239251 property : K ,
240- targetConfig : RequestConfig ,
241252 baseConfig : RequestConfig ,
242253 overrideConfig : RequestConfig ,
254+ targetConfig : RequestConfig ,
243255) : void {
244256 const baseInterceptor = baseConfig [ property ] ;
245257 const newInterceptor = overrideConfig [ property ] ;
@@ -274,15 +286,15 @@ function mergeInterceptors<
274286 * Merges the specified property from the base configuration and the override configuration into the target configuration.
275287 *
276288 * @param {K } property - The property key to merge from the base and override configurations. Must be a key of RequestConfig.
277- * @param {RequestConfig } targetConfig - The configuration object that will receive the merged properties.
278289 * @param {RequestConfig } baseConfig - The base configuration object that provides default values.
279290 * @param {RequestConfig } overrideConfig - The override configuration object that contains user-specific settings to merge.
291+ * @param {RequestConfig } targetConfig - The configuration object that will receive the merged properties.
280292 */
281293export function mergeConfig < K extends keyof RequestConfig > (
282294 property : K ,
283- targetConfig : RequestConfig ,
284295 baseConfig : RequestConfig ,
285296 overrideConfig : RequestConfig ,
297+ targetConfig : RequestConfig ,
286298) : void {
287299 if ( overrideConfig [ property ] ) {
288300 targetConfig [ property ] = {
0 commit comments