Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
9e3830cf
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,发现更多精彩内容 >>
提交
9e3830cf
编写于
1月 23, 2015
作者:
M
mzabriskie
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Adding support for removing interceptors
上级
7b05902b
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
110 addition
and
24 deletion
+110
-24
lib/axios.js
lib/axios.js
+13
-24
lib/helpers/InterceptorManager.js
lib/helpers/InterceptorManager.js
+51
-0
test/specs/interceptors.spec.js
test/specs/interceptors.spec.js
+46
-0
未找到文件。
lib/axios.js
浏览文件 @
9e3830cf
var
Promise
=
require
(
'
es6-promise
'
).
Promise
;
var
defaults
=
require
(
'
./defaults
'
);
var
utils
=
require
(
'
./utils
'
);
var
InterceptorManager
=
require
(
'
./helpers/InterceptorManager
'
);
var
axios
=
module
.
exports
=
function
axios
(
config
)
{
config
=
utils
.
merge
({
...
...
@@ -13,7 +14,7 @@ var axios = module.exports = function axios(config) {
// Don't allow overriding defaults.withCredentials
config
.
withCredentials
=
config
.
withCredentials
||
defaults
.
withCredentials
;
var
serverRequest
=
function
(
config
)
{
function
dispatchRequest
(
config
)
{
return
new
Promise
(
function
(
resolve
,
reject
)
{
try
{
// For browsers use XHR adapter
...
...
@@ -41,24 +42,22 @@ var axios = module.exports = function axios(config) {
console
.
warn
(
'
For more information about usage see
'
+
docs
);
}
}
catch
(
e
)
{}
}
}
;
var
chain
=
[
serverRequest
,
undefined
];
// Hook up interceptors middleware
var
chain
=
[
dispatchRequest
,
undefined
];
var
promise
=
Promise
.
resolve
(
config
);
utils
.
forEach
(
axios
.
interceptors
.
request
.
handlers
,
function
(
interceptor
)
{
chain
.
unshift
(
interceptor
.
request
,
interceptor
.
requestError
);
axios
.
interceptors
.
request
.
forEach
(
function
(
interceptor
)
{
chain
.
unshift
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
utils
.
forEach
(
axios
.
interceptors
.
response
.
handlers
,
function
(
interceptor
)
{
chain
.
push
(
interceptor
.
response
,
interceptor
.
responseError
);
axios
.
interceptors
.
response
.
forEach
(
function
(
interceptor
)
{
chain
.
push
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
while
(
chain
.
length
)
{
var
thenFn
=
chain
.
shift
();
var
rejectFn
=
chain
.
shift
();
promise
=
promise
.
then
(
thenFn
,
rejectFn
);
promise
=
promise
.
then
(
chain
.
shift
(),
chain
.
shift
());
}
// Provide alias for success
...
...
@@ -93,20 +92,10 @@ axios.all = function (promises) {
};
axios
.
spread
=
require
(
'
./helpers/spread
'
);
// interceptors
//
Expose
interceptors
axios
.
interceptors
=
{
request
:
{
handlers
:
[],
use
:
function
(
thenFn
,
rejectFn
)
{
axios
.
interceptors
.
request
.
handlers
.
push
({
request
:
thenFn
,
requestError
:
rejectFn
});
}
},
response
:
{
handlers
:
[],
use
:
function
(
thenFn
,
rejectFn
)
{
axios
.
interceptors
.
response
.
handlers
.
push
({
response
:
thenFn
,
responseError
:
rejectFn
});
}
}
request
:
new
InterceptorManager
(),
response
:
new
InterceptorManager
()
};
// Provide aliases for supported request methods
...
...
lib/helpers/InterceptorManager.js
0 → 100644
浏览文件 @
9e3830cf
var
utils
=
require
(
'
../utils
'
);
function
InterceptorManager
()
{
this
.
handlers
=
[];
};
/**
* Add a new interceptor to the stack
*
* @param {Function} fulfilled The function to handle `then` for a `Promise`
* @param {Function} rejected The function to handle `reject` for a `Promise`
*
* @return {Number} An ID used to remove interceptor later
*/
InterceptorManager
.
prototype
.
use
=
function
(
fulfilled
,
rejected
)
{
this
.
handlers
.
push
({
fulfilled
:
fulfilled
,
rejected
:
rejected
});
return
this
.
handlers
.
length
-
1
;
};
/**
* Remove an interceptor from the stack
*
* @param {Number} id The ID that was returned by `use`
*/
InterceptorManager
.
prototype
.
eject
=
function
(
id
)
{
if
(
this
.
handlers
[
id
])
{
this
.
handlers
[
id
]
=
null
;
}
};
/**
* Iterate over all the registered interceptors
*
* This method is particularly useful for skipping over any
* interceptors that may have become `null` calling `remove`.
*
* @param {Function} fn The function to call for each interceptor
*/
InterceptorManager
.
prototype
.
forEach
=
function
(
fn
)
{
utils
.
forEach
(
this
.
handlers
,
function
(
h
)
{
if
(
h
!==
null
)
{
fn
(
h
);
}
});
};
module
.
exports
=
InterceptorManager
;
test/specs/interceptors.spec.js
浏览文件 @
9e3830cf
...
...
@@ -296,4 +296,50 @@ describe('interceptors', function () {
expect
(
response
.
data
).
toBe
(
'
OK123
'
);
});
});
it
(
'
should allow removing interceptors
'
,
function
()
{
var
request
,
response
,
intercept
;
runs
(
function
()
{
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
1
'
;
return
data
;
});
intercept
=
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
2
'
;
return
data
;
});
axios
.
interceptors
.
response
.
use
(
function
(
data
)
{
data
.
data
=
data
.
data
+
'
3
'
;
return
data
;
});
axios
.
interceptors
.
response
.
eject
(
intercept
);
axios
({
url
:
'
/foo
'
}).
then
(
function
(
data
)
{
response
=
data
;
});
});
waitsFor
(
function
()
{
return
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
},
'
waiting for the request
'
,
100
);
runs
(
function
()
{
request
.
response
({
status
:
200
,
responseText
:
'
OK
'
});
});
waitsFor
(
function
()
{
return
response
;
},
'
waiting for the response
'
,
100
);
runs
(
function
()
{
expect
(
response
.
data
).
toBe
(
'
OK13
'
);
});
});
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录