Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
72c66dfd
A
axios
项目概览
xjbieke
/
axios
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
A
axios
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
72c66dfd
编写于
3月 23, 2018
作者:
M
Martti Laine
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor and introduce deepMerge
上级
d1154eae
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
76 addition
and
41 deletion
+76
-41
lib/axios.js
lib/axios.js
+2
-1
lib/core/Axios.js
lib/core/Axios.js
+2
-1
lib/core/mergeConfig.js
lib/core/mergeConfig.js
+44
-0
lib/utils.js
lib/utils.js
+28
-39
未找到文件。
lib/axios.js
浏览文件 @
72c66dfd
...
...
@@ -3,6 +3,7 @@
var
utils
=
require
(
'
./utils
'
);
var
bind
=
require
(
'
./helpers/bind
'
);
var
Axios
=
require
(
'
./core/Axios
'
);
var
mergeConfig
=
require
(
'
./core/mergeConfig
'
);
var
defaults
=
require
(
'
./defaults
'
);
/**
...
...
@@ -32,7 +33,7 @@ axios.Axios = Axios;
// Factory for creating new instances
axios
.
create
=
function
create
(
instanceConfig
)
{
return
createInstance
(
utils
.
mergeConfig
(
axios
.
defaults
,
instanceConfig
));
return
createInstance
(
mergeConfig
(
axios
.
defaults
,
instanceConfig
));
};
// Expose Cancel & CancelToken
...
...
lib/core/Axios.js
浏览文件 @
72c66dfd
...
...
@@ -3,6 +3,7 @@
var
utils
=
require
(
'
./../utils
'
);
var
InterceptorManager
=
require
(
'
./InterceptorManager
'
);
var
dispatchRequest
=
require
(
'
./dispatchRequest
'
);
var
mergeConfig
=
require
(
'
./mergeConfig
'
);
/**
* Create a new instance of Axios
...
...
@@ -32,7 +33,7 @@ Axios.prototype.request = function request(config) {
config
=
config
||
{};
}
config
=
utils
.
mergeConfig
(
this
.
defaults
,
config
);
config
=
mergeConfig
(
this
.
defaults
,
config
);
config
.
method
=
config
.
method
?
config
.
method
.
toLowerCase
()
:
'
get
'
;
// Hook up interceptors middleware
...
...
lib/core/mergeConfig.js
0 → 100644
浏览文件 @
72c66dfd
'
use strict
'
;
var
utils
=
require
(
'
../utils
'
);
/**
* Config-specific merge-function which creates a new config-object
* based on given defaults and instance config.
*
* @param {Object} defaults Defaults
* @param {Object} instanceConfig Instance-specific config
* @returns {Object} New object resulting from merging instanceConfig to defaults
*/
module
.
exports
=
function
mergeConfig
(
defaults
,
instanceConfig
)
{
// eslint-disable-next-line no-param-reassign
instanceConfig
=
instanceConfig
||
{};
var
config
=
{};
utils
.
forEach
([
'
url
'
,
'
method
'
,
'
params
'
,
'
data
'
],
function
valueFromInstanceConfig
(
prop
)
{
config
[
prop
]
=
instanceConfig
[
prop
];
});
utils
.
forEach
([
'
headers
'
,
'
auth
'
,
'
proxy
'
],
function
mergeInstanceConfigWithDefaults
(
prop
)
{
if
(
typeof
instanceConfig
[
prop
]
!==
'
undefined
'
)
{
if
(
typeof
instanceConfig
[
prop
]
===
'
object
'
)
{
config
[
prop
]
=
utils
.
deepMerge
(
defaults
[
prop
],
instanceConfig
[
prop
]);
}
else
{
config
[
prop
]
=
instanceConfig
[
prop
];
}
}
else
if
(
typeof
defaults
[
prop
]
!==
'
undefined
'
)
{
config
[
prop
]
=
utils
.
deepMerge
(
defaults
[
prop
]);
}
});
utils
.
forEach
([
'
baseURL
'
,
'
transformRequest
'
,
'
transformResponse
'
,
'
paramsSerializer
'
,
'
timeout
'
,
'
withCredentials
'
,
'
adapter
'
,
'
responseType
'
,
'
xsrfCookieName
'
,
'
xsrfHeaderName
'
,
'
onUploadProgress
'
,
'
onDownloadProgress
'
,
'
maxContentLength
'
,
'
validateStatus
'
,
'
maxRedirects
'
,
'
httpAgent
'
,
'
httpsAgent
'
,
'
cancelToken
'
],
function
defaultToInstanceConfig
(
prop
)
{
config
[
prop
]
=
typeof
instanceConfig
[
prop
]
===
'
undefined
'
?
defaults
[
prop
]
:
instanceConfig
[
prop
];
});
return
config
;
};
lib/utils.js
浏览文件 @
72c66dfd
...
...
@@ -260,6 +260,32 @@ function merge(/* obj1, obj2, obj3, ... */) {
return
result
;
}
/**
* Function equal to merge with the difference being that no reference
* to original objects is kept.
*
* @see merge
* @param {Object} obj1 Object to merge
* @returns {Object} Result of all merge properties
*/
function
deepMerge
(
/* obj1, obj2, obj3, ... */
)
{
var
result
=
{};
function
assignValue
(
val
,
key
)
{
if
(
typeof
result
[
key
]
===
'
object
'
&&
typeof
val
===
'
object
'
)
{
result
[
key
]
=
merge
(
result
[
key
],
val
);
}
else
if
(
typeof
val
===
'
object
'
)
{
result
[
key
]
=
merge
({},
val
);
}
else
{
result
[
key
]
=
val
;
}
}
for
(
var
i
=
0
,
l
=
arguments
.
length
;
i
<
l
;
i
++
)
{
forEach
(
arguments
[
i
],
assignValue
);
}
return
result
;
}
/**
* Extends object a by mutably adding to it the properties of object b.
*
...
...
@@ -279,43 +305,6 @@ function extend(a, b, thisArg) {
return
a
;
}
/**
* Config-specific merge-function which creates a new config-object
* based on given defaults and instance config.
*
* @param {Object} defaults Defaults
* @param {Object} instanceConfig Instance-specific config
* @returns {Object} New object resulting from merging instanceConfig to defaults
*/
function
mergeConfig
(
defaults
,
instanceConfig
)
{
instanceConfig
=
instanceConfig
||
{};
var
config
=
{};
forEach
([
'
url
'
,
'
method
'
,
'
params
'
,
'
data
'
],
function
valueFromInstanceConfig
(
prop
)
{
config
[
prop
]
=
instanceConfig
[
prop
];
});
forEach
([
'
headers
'
,
'
auth
'
,
'
proxy
'
],
function
mergeInstanceConfigWithDefaults
(
prop
)
{
if
(
!
isUndefined
(
instanceConfig
[
prop
]))
{
if
(
isObject
(
instanceConfig
[
prop
]))
{
config
[
prop
]
=
merge
(
defaults
[
prop
],
instanceConfig
[
prop
]);
}
else
{
config
[
prop
]
=
instanceConfig
[
prop
];
}
}
else
if
(
!
isUndefined
(
defaults
[
prop
]))
{
config
[
prop
]
=
JSON
.
parse
(
JSON
.
stringify
(
defaults
[
prop
]));
}
});
forEach
([
'
baseURL
'
,
'
transformRequest
'
,
'
transformResponse
'
,
'
paramsSerializer
'
,
'
timeout
'
,
'
withCredentials
'
,
'
adapter
'
,
'
responseType
'
,
'
xsrfCookieName
'
,
'
xsrfHeaderName
'
,
'
onUploadProgress
'
,
'
onDownloadProgress
'
,
'
maxContentLength
'
,
'
validateStatus
'
,
'
maxRedirects
'
,
'
httpAgent
'
,
'
httpsAgent
'
,
'
cancelToken
'
],
function
defaultToInstanceConfig
(
prop
)
{
config
[
prop
]
=
isUndefined
(
instanceConfig
[
prop
])
?
defaults
[
prop
]
:
instanceConfig
[
prop
];
});
return
config
;
}
module
.
exports
=
{
isArray
:
isArray
,
isArrayBuffer
:
isArrayBuffer
,
...
...
@@ -335,7 +324,7 @@ module.exports = {
isStandardBrowserEnv
:
isStandardBrowserEnv
,
forEach
:
forEach
,
merge
:
merge
,
deepMerge
:
deepMerge
,
extend
:
extend
,
trim
:
trim
,
mergeConfig
:
mergeConfig
trim
:
trim
};
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录