Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
68b1b37f
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,发现更多精彩内容 >>
提交
68b1b37f
编写于
10月 27, 2015
作者:
M
Matt Zabriskie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #123 from nickuraltsev/master
Make axios instantiable
上级
4d432cf1
4ac0fbd1
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
194 addition
and
42 deletion
+194
-42
axios.d.ts
axios.d.ts
+21
-8
lib/axios.js
lib/axios.js
+40
-18
test/specs/api.spec.js
test/specs/api.spec.js
+29
-1
test/specs/instance.spec.js
test/specs/instance.spec.js
+41
-0
test/typescript/axios.ts
test/typescript/axios.ts
+63
-15
未找到文件。
axios.d.ts
浏览文件 @
68b1b37f
...
@@ -6,18 +6,26 @@
...
@@ -6,18 +6,26 @@
declare
var
axios
:
axios
.
AxiosStatic
declare
var
axios
:
axios
.
AxiosStatic
declare
module
axios
{
declare
module
axios
{
interface
AxiosStatic
{
interface
AxiosRequestMethods
{
(
options
:
axios
.
RequestOptions
):
axios
.
Promise
;
get
(
url
:
string
,
config
?:
any
):
axios
.
Promise
;
get
(
url
:
string
,
config
?:
any
):
axios
.
Promise
;
delete
(
url
:
string
,
config
?:
any
):
axios
.
Promise
;
delete
(
url
:
string
,
config
?:
any
):
axios
.
Promise
;
head
(
url
:
string
,
config
?:
any
):
axios
.
Promise
;
head
(
url
:
string
,
config
?:
any
):
axios
.
Promise
;
post
(
url
:
string
,
data
:
any
,
config
?:
any
):
axios
.
Promise
;
post
(
url
:
string
,
data
:
any
,
config
?:
any
):
axios
.
Promise
;
put
(
url
:
string
,
data
:
any
,
config
?:
any
):
axios
.
Promise
;
put
(
url
:
string
,
data
:
any
,
config
?:
any
):
axios
.
Promise
;
patch
(
url
:
string
,
data
:
any
,
config
?:
any
):
axios
.
Promise
;
patch
(
url
:
string
,
data
:
any
,
config
?:
any
):
axios
.
Promise
;
}
interface
AxiosStatic
extends
AxiosRequestMethods
{
(
options
:
axios
.
RequestOptions
):
axios
.
Promise
;
create
(
defaultOptions
?:
axios
.
InstanceOptions
):
AxiosInstance
;
all
(
iterable
:
any
):
axios
.
Promise
;
all
(
iterable
:
any
):
axios
.
Promise
;
spread
(
callback
:
any
):
axios
.
Promise
;
spread
(
callback
:
any
):
axios
.
Promise
;
}
}
interface
AxiosInstance
extends
AxiosRequestMethods
{
request
(
options
:
axios
.
RequestOptions
):
axios
.
Promise
;
}
interface
Response
{
interface
Response
{
data
?:
any
;
data
?:
any
;
status
?:
number
;
status
?:
number
;
...
@@ -31,18 +39,23 @@ declare module axios {
...
@@ -31,18 +39,23 @@ declare module axios {
catch
(
onRejected
:(
response
:
axios
.
Response
)
=>
void
):
axios
.
Promise
;
catch
(
onRejected
:(
response
:
axios
.
Response
)
=>
void
):
axios
.
Promise
;
}
}
interface
RequestOptions
{
interface
InstanceOptions
{
url
:
string
;
method
?:
string
;
transformRequest
?:
(
data
:
any
)
=>
any
;
transformRequest
?:
(
data
:
any
)
=>
any
;
transformResponse
?:
(
data
:
any
)
=>
any
;
headers
?:
any
;
headers
?:
any
;
params
?:
any
;
timeout
?:
number
;
data
?:
any
;
withCredentials
?:
boolean
;
withCredentials
?:
boolean
;
responseType
?:
string
;
responseType
?:
string
;
xsrfCookieName
?:
string
;
xsrfCookieName
?:
string
;
xsrfHeaderName
?:
string
;
xsrfHeaderName
?:
string
;
}
}
interface
RequestOptions
extends
InstanceOptions
{
url
:
string
;
method
?:
string
;
params
?:
any
;
data
?:
any
;
}
}
}
declare
module
"
axios
"
{
declare
module
"
axios
"
{
...
...
lib/axios.js
浏览文件 @
68b1b37f
...
@@ -5,7 +5,21 @@ var utils = require('./utils');
...
@@ -5,7 +5,21 @@ var utils = require('./utils');
var
dispatchRequest
=
require
(
'
./core/dispatchRequest
'
);
var
dispatchRequest
=
require
(
'
./core/dispatchRequest
'
);
var
InterceptorManager
=
require
(
'
./core/InterceptorManager
'
);
var
InterceptorManager
=
require
(
'
./core/InterceptorManager
'
);
var
axios
=
module
.
exports
=
function
(
config
)
{
function
Axios
(
defaultConfig
)
{
this
.
defaultConfig
=
utils
.
merge
({
headers
:
{},
timeout
:
defaults
.
timeout
,
transformRequest
:
defaults
.
transformRequest
,
transformResponse
:
defaults
.
transformResponse
},
defaultConfig
);
this
.
interceptors
=
{
request
:
new
InterceptorManager
(),
response
:
new
InterceptorManager
()
};
}
Axios
.
prototype
.
request
=
function
(
config
)
{
// Allow for axios('example/url'[, config]) a la fetch API
// Allow for axios('example/url'[, config]) a la fetch API
if
(
typeof
config
===
'
string
'
)
{
if
(
typeof
config
===
'
string
'
)
{
config
=
utils
.
merge
({
config
=
utils
.
merge
({
...
@@ -13,13 +27,7 @@ var axios = module.exports = function (config) {
...
@@ -13,13 +27,7 @@ var axios = module.exports = function (config) {
},
arguments
[
1
]);
},
arguments
[
1
]);
}
}
config
=
utils
.
merge
({
config
=
utils
.
merge
(
this
.
defaultConfig
,
{
method
:
'
get
'
},
config
);
method
:
'
get
'
,
headers
:
{},
timeout
:
defaults
.
timeout
,
transformRequest
:
defaults
.
transformRequest
,
transformResponse
:
defaults
.
transformResponse
},
config
);
// Don't allow overriding defaults.withCredentials
// Don't allow overriding defaults.withCredentials
config
.
withCredentials
=
config
.
withCredentials
||
defaults
.
withCredentials
;
config
.
withCredentials
=
config
.
withCredentials
||
defaults
.
withCredentials
;
...
@@ -28,11 +36,11 @@ var axios = module.exports = function (config) {
...
@@ -28,11 +36,11 @@ var axios = module.exports = function (config) {
var
chain
=
[
dispatchRequest
,
undefined
];
var
chain
=
[
dispatchRequest
,
undefined
];
var
promise
=
Promise
.
resolve
(
config
);
var
promise
=
Promise
.
resolve
(
config
);
axio
s
.
interceptors
.
request
.
forEach
(
function
(
interceptor
)
{
thi
s
.
interceptors
.
request
.
forEach
(
function
(
interceptor
)
{
chain
.
unshift
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
chain
.
unshift
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
});
axio
s
.
interceptors
.
response
.
forEach
(
function
(
interceptor
)
{
thi
s
.
interceptors
.
response
.
forEach
(
function
(
interceptor
)
{
chain
.
push
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
chain
.
push
(
interceptor
.
fulfilled
,
interceptor
.
rejected
);
});
});
...
@@ -43,6 +51,14 @@ var axios = module.exports = function (config) {
...
@@ -43,6 +51,14 @@ var axios = module.exports = function (config) {
return
promise
;
return
promise
;
};
};
var
defaultInstance
=
new
Axios
();
var
axios
=
module
.
exports
=
bind
(
Axios
.
prototype
.
request
,
defaultInstance
);
axios
.
create
=
function
(
defaultConfig
)
{
return
new
Axios
(
defaultConfig
);
};
// Expose defaults
// Expose defaults
axios
.
defaults
=
defaults
;
axios
.
defaults
=
defaults
;
...
@@ -53,36 +69,42 @@ axios.all = function (promises) {
...
@@ -53,36 +69,42 @@ axios.all = function (promises) {
axios
.
spread
=
require
(
'
./helpers/spread
'
);
axios
.
spread
=
require
(
'
./helpers/spread
'
);
// Expose interceptors
// Expose interceptors
axios
.
interceptors
=
{
axios
.
interceptors
=
defaultInstance
.
interceptors
;
request
:
new
InterceptorManager
(),
response
:
new
InterceptorManager
()
};
// Provide aliases for supported request methods
// Provide aliases for supported request methods
(
function
()
{
(
function
()
{
function
createShortMethods
()
{
function
createShortMethods
()
{
utils
.
forEach
(
arguments
,
function
(
method
)
{
utils
.
forEach
(
arguments
,
function
(
method
)
{
axios
[
method
]
=
function
(
url
,
config
)
{
Axios
.
prototype
[
method
]
=
function
(
url
,
config
)
{
return
axios
(
utils
.
merge
(
config
||
{},
{
return
this
.
request
(
utils
.
merge
(
config
||
{},
{
method
:
method
,
method
:
method
,
url
:
url
url
:
url
}));
}));
};
};
axios
[
method
]
=
bind
(
Axios
.
prototype
[
method
],
defaultInstance
);
});
});
}
}
function
createShortMethodsWithData
()
{
function
createShortMethodsWithData
()
{
utils
.
forEach
(
arguments
,
function
(
method
)
{
utils
.
forEach
(
arguments
,
function
(
method
)
{
axios
[
method
]
=
function
(
url
,
data
,
config
)
{
Axios
.
prototype
[
method
]
=
function
(
url
,
data
,
config
)
{
return
axios
(
utils
.
merge
(
config
||
{},
{
return
this
.
request
(
utils
.
merge
(
config
||
{},
{
method
:
method
,
method
:
method
,
url
:
url
,
url
:
url
,
data
:
data
data
:
data
}));
}));
};
};
axios
[
method
]
=
bind
(
Axios
.
prototype
[
method
],
defaultInstance
);
});
});
}
}
createShortMethods
(
'
delete
'
,
'
get
'
,
'
head
'
);
createShortMethods
(
'
delete
'
,
'
get
'
,
'
head
'
);
createShortMethodsWithData
(
'
post
'
,
'
put
'
,
'
patch
'
);
createShortMethodsWithData
(
'
post
'
,
'
put
'
,
'
patch
'
);
})();
})();
// Helpers
function
bind
(
fn
,
thisArg
)
{
return
function
()
{
return
fn
.
apply
(
thisArg
,
Array
.
prototype
.
slice
.
call
(
arguments
));
};
}
test/specs/api.spec.js
浏览文件 @
68b1b37f
var
axios
=
require
(
'
../../index
'
);
var
axios
=
require
(
'
../../index
'
);
describe
(
'
api
'
,
function
()
{
describe
(
'
static
api
'
,
function
()
{
it
(
'
should have request method helpers
'
,
function
()
{
it
(
'
should have request method helpers
'
,
function
()
{
expect
(
typeof
axios
.
get
).
toEqual
(
'
function
'
);
expect
(
typeof
axios
.
get
).
toEqual
(
'
function
'
);
expect
(
typeof
axios
.
head
).
toEqual
(
'
function
'
);
expect
(
typeof
axios
.
head
).
toEqual
(
'
function
'
);
...
@@ -22,8 +22,36 @@ describe('api', function () {
...
@@ -22,8 +22,36 @@ describe('api', function () {
expect
(
typeof
axios
.
defaults
.
headers
).
toEqual
(
'
object
'
);
expect
(
typeof
axios
.
defaults
.
headers
).
toEqual
(
'
object
'
);
});
});
it
(
'
should have interceptors
'
,
function
()
{
expect
(
typeof
axios
.
interceptors
.
request
).
toEqual
(
'
object
'
);
expect
(
typeof
axios
.
interceptors
.
response
).
toEqual
(
'
object
'
);
});
it
(
'
should have all/spread helpers
'
,
function
()
{
it
(
'
should have all/spread helpers
'
,
function
()
{
expect
(
typeof
axios
.
all
).
toEqual
(
'
function
'
);
expect
(
typeof
axios
.
all
).
toEqual
(
'
function
'
);
expect
(
typeof
axios
.
spread
).
toEqual
(
'
function
'
);
expect
(
typeof
axios
.
spread
).
toEqual
(
'
function
'
);
});
});
it
(
'
should have factory method
'
,
function
()
{
expect
(
typeof
axios
.
create
).
toEqual
(
'
function
'
);
});
});
describe
(
'
instance api
'
,
function
()
{
var
instance
=
axios
.
create
();
it
(
'
should have request methods
'
,
function
()
{
expect
(
typeof
instance
.
request
).
toEqual
(
'
function
'
);
expect
(
typeof
instance
.
get
).
toEqual
(
'
function
'
);
expect
(
typeof
instance
.
head
).
toEqual
(
'
function
'
);
expect
(
typeof
instance
.
delete
).
toEqual
(
'
function
'
);
expect
(
typeof
instance
.
post
).
toEqual
(
'
function
'
);
expect
(
typeof
instance
.
put
).
toEqual
(
'
function
'
);
expect
(
typeof
instance
.
patch
).
toEqual
(
'
function
'
);
});
it
(
'
should have interceptors
'
,
function
()
{
expect
(
typeof
instance
.
interceptors
.
request
).
toEqual
(
'
object
'
);
expect
(
typeof
instance
.
interceptors
.
response
).
toEqual
(
'
object
'
);
});
});
});
test/specs/instance.spec.js
0 → 100644
浏览文件 @
68b1b37f
var
axios
=
require
(
'
../../index
'
);
describe
(
'
instance
'
,
function
()
{
beforeEach
(
function
()
{
jasmine
.
Ajax
.
install
();
});
afterEach
(
function
()
{
jasmine
.
Ajax
.
uninstall
();
});
it
(
'
should make an http request
'
,
function
(
done
)
{
var
instance
=
axios
.
create
();
instance
.
request
({
url
:
'
/foo
'
});
setTimeout
(
function
()
{
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
url
).
toBe
(
'
/foo
'
);
done
();
},
0
);
});
it
(
'
should use instance options
'
,
function
(
done
)
{
var
instance
=
axios
.
create
({
timeout
:
1000
});
instance
.
request
({
url
:
'
/foo
'
});
setTimeout
(
function
()
{
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
expect
(
request
.
timeout
).
toBe
(
1000
);
done
();
},
0
);
});
});
test/typescript/axios.ts
浏览文件 @
68b1b37f
...
@@ -109,3 +109,51 @@ axios({
...
@@ -109,3 +109,51 @@ axios({
xsrfCookieName
:
'
XSRF-TOKEN
'
,
// default
xsrfCookieName
:
'
XSRF-TOKEN
'
,
// default
xsrfHeaderName
:
'
X-XSRF-TOKEN
'
// default
xsrfHeaderName
:
'
X-XSRF-TOKEN
'
// default
});
});
var
instance
=
axios
.
create
();
axios
.
create
({
transformRequest
:
(
data
)
=>
{
return
data
.
doSomething
();
},
transformResponse
:
(
data
)
=>
{
return
data
.
doSomethingElse
();
},
headers
:
{
'
X-Requested-With
'
:
'
XMLHttpRequest
'
},
timeout
:
1000
,
withCredentials
:
false
,
// default
responseType
:
'
json
'
,
// default
xsrfCookieName
:
'
XSRF-TOKEN
'
,
// default
xsrfHeaderName
:
'
X-XSRF-TOKEN
'
// default
});
instance
.
request
({
method
:
'
get
'
,
url
:
'
/user/12345
'
})
.
then
(
function
(
response
)
{
console
.
log
(
response
);
})
.
catch
(
function
(
response
)
{
console
.
log
(
response
);
});
instance
.
get
(
'
/user?ID=12345
'
)
.
then
(
function
(
response
)
{
console
.
log
(
response
);
})
.
catch
(
function
(
response
)
{
console
.
log
(
response
);
});
instance
.
get
(
'
/user
'
,
{
params
:
{
ID
:
12345
}
})
.
then
(
function
(
response
)
{
console
.
log
(
response
);
})
.
catch
(
function
(
response
)
{
console
.
log
(
response
);
});
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录