Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
0e2f4f14
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,发现更多精彩内容 >>
提交
0e2f4f14
编写于
6月 01, 2016
作者:
M
Matt Zabriskie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Moving Axios class into core/
上级
203cbc2d
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
173 addition
and
101 deletion
+173
-101
lib/axios.js
lib/axios.js
+7
-101
lib/core/Axios.js
lib/core/Axios.js
+110
-0
lib/utils.js
lib/utils.js
+22
-0
test/specs/utils/extend.spec.js
test/specs/utils/extend.spec.js
+34
-0
未找到文件。
lib/axios.js
浏览文件 @
0e2f4f14
...
...
@@ -2,91 +2,21 @@
var
defaults
=
require
(
'
./defaults
'
);
var
utils
=
require
(
'
./utils
'
);
var
dispatchRequest
=
require
(
'
./core/dispatchRequest
'
);
var
transformData
=
require
(
'
./core/transformData
'
);
var
InterceptorManager
=
require
(
'
./core/InterceptorManager
'
);
var
isAbsoluteURL
=
require
(
'
./helpers/isAbsoluteURL
'
);
var
combineURLs
=
require
(
'
./helpers/combineURLs
'
);
var
bind
=
require
(
'
./helpers/bind
'
);
function
Axios
(
defaultConfig
)
{
this
.
defaults
=
utils
.
merge
({},
defaultConfig
);
this
.
interceptors
=
{
request
:
new
InterceptorManager
(),
response
:
new
InterceptorManager
()
};
}
Axios
.
prototype
.
request
=
function
request
(
config
)
{
/*eslint no-param-reassign:0*/
// Allow for axios('example/url'[, config]) a la fetch API
if
(
typeof
config
===
'
string
'
)
{
config
=
utils
.
merge
({
url
:
arguments
[
0
]
},
arguments
[
1
]);
}
config
=
utils
.
merge
(
defaults
,
this
.
defaults
,
{
method
:
'
get
'
},
config
);
// Support baseURL config
if
(
config
.
baseURL
&&
!
isAbsoluteURL
(
config
.
url
))
{
config
.
url
=
combineURLs
(
config
.
baseURL
,
config
.
url
);
}
// Don't allow overriding defaults.withCredentials
config
.
withCredentials
=
config
.
withCredentials
||
this
.
defaults
.
withCredentials
;
// Transform request data
config
.
data
=
transformData
(
config
.
data
,
config
.
headers
,
config
.
transformRequest
);
// Flatten headers
config
.
headers
=
utils
.
merge
(
config
.
headers
.
common
||
{},
config
.
headers
[
config
.
method
]
||
{},
config
.
headers
||
{}
);
utils
.
forEach
(
[
'
delete
'
,
'
get
'
,
'
head
'
,
'
post
'
,
'
put
'
,
'
patch
'
,
'
common
'
],
function
cleanHeaderConfig
(
method
)
{
delete
config
.
headers
[
method
];
}
);
// Hook up interceptors middleware
var
chain
=
[
dispatchRequest
,
undefined
];
var
promise
=
Promise
.
resolve
(
config
);
this
.
interceptors
.
request
.
forEach
(
function
unshiftRequestInterceptors
(
interceptor
)
{
chain
.
unshift
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
this
.
interceptors
.
response
.
forEach
(
function
pushResponseInterceptors
(
interceptor
)
{
chain
.
push
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
while
(
chain
.
length
)
{
promise
=
promise
.
then
(
chain
.
shift
(),
chain
.
shift
());
}
return
promise
;
};
var
Axios
=
require
(
'
./core/Axios
'
);
var
defaultInstance
=
new
Axios
(
defaults
);
var
axios
=
module
.
exports
=
bind
(
Axios
.
prototype
.
request
,
defaultInstance
);
axios
.
request
=
bind
(
Axios
.
prototype
.
request
,
defaultInstance
);
// Copy Axios.prototype to axios instance
utils
.
extend
(
axios
,
Axios
.
prototype
,
defaultInstance
);
// Copy defaultInstance to axios instance
utils
.
extend
(
axios
,
defaultInstance
);
// Expose Axios class to allow class inheritance
axios
.
Axios
=
Axios
;
// Expose properties from defaultInstance
axios
.
defaults
=
defaultInstance
.
defaults
;
axios
.
interceptors
=
defaultInstance
.
interceptors
;
// Factory for creating new instances
axios
.
create
=
function
create
(
defaultConfig
)
{
return
new
Axios
(
defaultConfig
);
...
...
@@ -97,27 +27,3 @@ axios.all = function all(promises) {
return
Promise
.
all
(
promises
);
};
axios
.
spread
=
require
(
'
./helpers/spread
'
);
// Provide aliases for supported request methods
utils
.
forEach
([
'
delete
'
,
'
get
'
,
'
head
'
],
function
forEachMethodNoData
(
method
)
{
/*eslint func-names:0*/
Axios
.
prototype
[
method
]
=
function
(
url
,
config
)
{
return
this
.
request
(
utils
.
merge
(
config
||
{},
{
method
:
method
,
url
:
url
}));
};
axios
[
method
]
=
bind
(
Axios
.
prototype
[
method
],
defaultInstance
);
});
utils
.
forEach
([
'
post
'
,
'
put
'
,
'
patch
'
],
function
forEachMethodWithData
(
method
)
{
/*eslint func-names:0*/
Axios
.
prototype
[
method
]
=
function
(
url
,
data
,
config
)
{
return
this
.
request
(
utils
.
merge
(
config
||
{},
{
method
:
method
,
url
:
url
,
data
:
data
}));
};
axios
[
method
]
=
bind
(
Axios
.
prototype
[
method
],
defaultInstance
);
});
lib/core/Axios.js
0 → 100644
浏览文件 @
0e2f4f14
'
use strict
'
;
var
defaults
=
require
(
'
./../defaults
'
);
var
utils
=
require
(
'
./../utils
'
);
var
InterceptorManager
=
require
(
'
./InterceptorManager
'
);
var
dispatchRequest
=
require
(
'
./dispatchRequest
'
);
var
transformData
=
require
(
'
./transformData
'
);
var
isAbsoluteURL
=
require
(
'
./../helpers/isAbsoluteURL
'
);
var
combineURLs
=
require
(
'
./../helpers/combineURLs
'
);
/**
* Create a new instance of Axios
*
* @param {Object} defaultConfig The default config for the instance
*/
function
Axios
(
defaultConfig
)
{
this
.
defaults
=
utils
.
merge
({},
defaultConfig
);
this
.
interceptors
=
{
request
:
new
InterceptorManager
(),
response
:
new
InterceptorManager
()
};
}
/**
* Dispatch a request
*
* @param {Object} config The config specific for this request (merged with this.defaults)
*/
Axios
.
prototype
.
request
=
function
request
(
config
)
{
/*eslint no-param-reassign:0*/
// Allow for axios('example/url'[, config]) a la fetch API
if
(
typeof
config
===
'
string
'
)
{
config
=
utils
.
merge
({
url
:
arguments
[
0
]
},
arguments
[
1
]);
}
config
=
utils
.
merge
(
defaults
,
this
.
defaults
,
{
method
:
'
get
'
},
config
);
// Support baseURL config
if
(
config
.
baseURL
&&
!
isAbsoluteURL
(
config
.
url
))
{
config
.
url
=
combineURLs
(
config
.
baseURL
,
config
.
url
);
}
// Don't allow overriding defaults.withCredentials
config
.
withCredentials
=
config
.
withCredentials
||
this
.
defaults
.
withCredentials
;
// Transform request data
config
.
data
=
transformData
(
config
.
data
,
config
.
headers
,
config
.
transformRequest
);
// Flatten headers
config
.
headers
=
utils
.
merge
(
config
.
headers
.
common
||
{},
config
.
headers
[
config
.
method
]
||
{},
config
.
headers
||
{}
);
utils
.
forEach
(
[
'
delete
'
,
'
get
'
,
'
head
'
,
'
post
'
,
'
put
'
,
'
patch
'
,
'
common
'
],
function
cleanHeaderConfig
(
method
)
{
delete
config
.
headers
[
method
];
}
);
// Hook up interceptors middleware
var
chain
=
[
dispatchRequest
,
undefined
];
var
promise
=
Promise
.
resolve
(
config
);
this
.
interceptors
.
request
.
forEach
(
function
unshiftRequestInterceptors
(
interceptor
)
{
chain
.
unshift
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
this
.
interceptors
.
response
.
forEach
(
function
pushResponseInterceptors
(
interceptor
)
{
chain
.
push
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
while
(
chain
.
length
)
{
promise
=
promise
.
then
(
chain
.
shift
(),
chain
.
shift
());
}
return
promise
;
};
// Provide aliases for supported request methods
utils
.
forEach
([
'
delete
'
,
'
get
'
,
'
head
'
],
function
forEachMethodNoData
(
method
)
{
/*eslint func-names:0*/
Axios
.
prototype
[
method
]
=
function
(
url
,
config
)
{
return
this
.
request
(
utils
.
merge
(
config
||
{},
{
method
:
method
,
url
:
url
}));
};
});
utils
.
forEach
([
'
post
'
,
'
put
'
,
'
patch
'
],
function
forEachMethodWithData
(
method
)
{
/*eslint func-names:0*/
Axios
.
prototype
[
method
]
=
function
(
url
,
data
,
config
)
{
return
this
.
request
(
utils
.
merge
(
config
||
{},
{
method
:
method
,
url
:
url
,
data
:
data
}));
};
});
module
.
exports
=
Axios
;
lib/utils.js
浏览文件 @
0e2f4f14
'
use strict
'
;
var
bind
=
require
(
'
./helpers/bind
'
);
/*global toString:true*/
// utils is a library of generic helper functions non-specific to axios
...
...
@@ -255,6 +257,25 @@ function merge(/* obj1, obj2, obj3, ... */) {
return
result
;
}
/**
* Extends object a by mutably adding to it the properties of object b.
*
* @param {Object} a The object to be extended
* @param {Object} b The object to copy properties from
* @param {Object} thisArg The object to bind function to
* @return {Object} The resulting value of object a
*/
function
extend
(
a
,
b
,
thisArg
)
{
forEach
(
b
,
function
assignValue
(
val
,
key
)
{
if
(
thisArg
&&
typeof
val
===
'
function
'
)
{
a
[
key
]
=
bind
(
val
,
thisArg
);
}
else
{
a
[
key
]
=
val
;
}
});
return
a
;
}
module
.
exports
=
{
isArray
:
isArray
,
isArrayBuffer
:
isArrayBuffer
,
...
...
@@ -273,5 +294,6 @@ module.exports = {
isStandardBrowserEnv
:
isStandardBrowserEnv
,
forEach
:
forEach
,
merge
:
merge
,
extend
:
extend
,
trim
:
trim
};
test/specs/utils/extend.spec.js
0 → 100644
浏览文件 @
0e2f4f14
var
extend
=
require
(
'
../../../lib/utils
'
).
extend
;
describe
(
'
utils::extend
'
,
function
()
{
it
(
'
should be mutable
'
,
function
()
{
var
a
=
{};
var
b
=
{
foo
:
123
};
extend
(
a
,
b
);
expect
(
a
.
foo
).
toEqual
(
b
.
foo
);
});
it
(
'
should extend properties
'
,
function
()
{
var
a
=
{
foo
:
123
,
bar
:
456
};
var
b
=
{
bar
:
789
};
a
=
extend
(
a
,
b
);
expect
(
a
.
foo
).
toEqual
(
123
);
expect
(
a
.
bar
).
toEqual
(
789
);
});
it
(
'
should bind to thisArg
'
,
function
()
{
var
a
=
{};
var
b
=
{
getFoo
:
function
getFoo
()
{
return
this
.
foo
;
}};
var
thisArg
=
{
foo
:
'
barbaz
'
};
extend
(
a
,
b
,
thisArg
);
expect
(
typeof
a
.
getFoo
).
toEqual
(
'
function
'
);
expect
(
a
.
getFoo
()).
toEqual
(
thisArg
.
foo
);
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录