Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
xjbieke
axios
提交
c03e51ce
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,发现更多精彩内容 >>
提交
c03e51ce
编写于
9月 18, 2014
作者:
M
Matt Zabriskie
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10 from mathbruyen/arraybuffer
Allow ArrayBuffer and related views as data
上级
d8f687dc
095a204c
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
124 addition
and
9 deletion
+124
-9
README.md
README.md
+2
-0
dist/axios.js
dist/axios.js
+40
-4
dist/axios.map
dist/axios.map
+1
-1
lib/adapters/http.js
lib/adapters/http.js
+5
-0
lib/adapters/xhr.js
lib/adapters/xhr.js
+4
-0
lib/defaults.js
lib/defaults.js
+10
-4
lib/utils.js
lib/utils.js
+26
-0
test/specs/wrapper.spec.js
test/specs/wrapper.spec.js
+36
-0
未找到文件。
README.md
浏览文件 @
c03e51ce
...
...
@@ -150,6 +150,7 @@ This is the available config options for making requests. Only the `url` is requ
// `transformRequest` allows changes to the request data before it is sent to the server
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
// The last function in the array must return a string or an ArrayBuffer
transformRequest
:
[
function
(
data
)
{
// Do whatever you want to transform the data
...
...
@@ -174,6 +175,7 @@ This is the available config options for making requests. Only the `url` is requ
// `data` is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no `transformRequest` is set, must be a string, an ArrayBuffer or a hash
data
:
{
firstName
:
'
Fred
'
},
...
...
dist/axios.js
浏览文件 @
c03e51ce
...
...
@@ -161,10 +161,16 @@ var axios =
module
.
exports
=
{
transformRequest
:
[
function
(
data
)
{
return
utils
.
isObject
(
data
)
&&
!
utils
.
isFile
(
data
)
&&
!
utils
.
isBlob
(
data
)
?
JSON
.
stringify
(
data
)
:
data
;
if
(
utils
.
isArrayBuffer
(
data
))
{
return
data
;
}
if
(
utils
.
isArrayBufferView
(
data
))
{
return
data
.
buffer
;
}
if
(
utils
.
isObject
(
data
)
&&
!
utils
.
isFile
(
data
)
&&
!
utils
.
isBlob
(
data
))
{
return
JSON
.
stringify
(
data
);
}
return
data
;
}],
transformResponse
:
[
function
(
data
)
{
...
...
@@ -208,6 +214,30 @@ var axios =
return
toString
.
call
(
val
)
===
'
[object Array]
'
;
}
/**
* Determine if a value is an ArrayBuffer
*
* @param {Object} val The value to test
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
*/
function
isArrayBuffer
(
val
)
{
return
toString
.
call
(
val
)
===
'
[object ArrayBuffer]
'
;
}
/**
* Determine if a value is a view on an ArrayBuffer
*
* @param {Object} val The value to test
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
*/
function
isArrayBufferView
(
val
)
{
if
((
typeof
ArrayBuffer
!==
'
undefined
'
)
&&
(
ArrayBuffer
.
isView
))
{
return
ArrayBuffer
.
isView
(
val
);
}
else
{
return
(
val
)
&&
(
val
.
buffer
)
&&
(
val
.
buffer
instanceof
ArrayBuffer
);
}
}
/**
* Determine if a value is a String
*
...
...
@@ -349,6 +379,8 @@ var axios =
module
.
exports
=
{
isArray
:
isArray
,
isArrayBuffer
:
isArrayBuffer
,
isArrayBufferView
:
isArrayBufferView
,
isString
:
isString
,
isNumber
:
isNumber
,
isObject
:
isObject
,
...
...
@@ -483,6 +515,10 @@ var axios =
}
}
if
(
utils
.
isArrayBuffer
(
data
))
{
data
=
new
DataView
(
data
);
}
// Send the request
request
.
send
(
data
);
};
...
...
dist/axios.map
浏览文件 @
c03e51ce
此差异已折叠。
点击以展开。
lib/adapters/http.js
浏览文件 @
c03e51ce
...
...
@@ -4,6 +4,7 @@ var transformData = require('./../transformData');
var
utils
=
require
(
'
./../utils
'
);
var
http
=
require
(
'
http
'
);
var
url
=
require
(
'
url
'
);
var
Buffer
=
require
(
'
buffer
'
).
Buffer
;
module
.
exports
=
function
httpAdapter
(
resolve
,
reject
,
config
)
{
// Transform request data
...
...
@@ -13,6 +14,10 @@ module.exports = function httpAdapter(resolve, reject, config) {
config
.
transformRequest
);
if
(
utils
.
isArrayBuffer
(
data
))
{
data
=
new
Buffer
(
new
Uint8Array
(
data
));
}
// Merge headers
var
headers
=
utils
.
merge
(
defaults
.
headers
.
common
,
...
...
lib/adapters/xhr.js
浏览文件 @
c03e51ce
...
...
@@ -87,6 +87,10 @@ module.exports = function xhrAdapter(resolve, reject, config) {
}
}
if
(
utils
.
isArrayBuffer
(
data
))
{
data
=
new
DataView
(
data
);
}
// Send the request
request
.
send
(
data
);
};
\ No newline at end of file
lib/defaults.js
浏览文件 @
c03e51ce
...
...
@@ -11,10 +11,16 @@ var CONTENT_TYPE_APPLICATION_JSON = {
module
.
exports
=
{
transformRequest
:
[
function
(
data
)
{
return
utils
.
isObject
(
data
)
&&
!
utils
.
isFile
(
data
)
&&
!
utils
.
isBlob
(
data
)
?
JSON
.
stringify
(
data
)
:
data
;
if
(
utils
.
isArrayBuffer
(
data
))
{
return
data
;
}
if
(
utils
.
isArrayBufferView
(
data
))
{
return
data
.
buffer
;
}
if
(
utils
.
isObject
(
data
)
&&
!
utils
.
isFile
(
data
)
&&
!
utils
.
isBlob
(
data
))
{
return
JSON
.
stringify
(
data
);
}
return
data
;
}],
transformResponse
:
[
function
(
data
)
{
...
...
lib/utils.js
浏览文件 @
c03e51ce
...
...
@@ -12,6 +12,30 @@ function isArray(val) {
return
toString
.
call
(
val
)
===
'
[object Array]
'
;
}
/**
* Determine if a value is an ArrayBuffer
*
* @param {Object} val The value to test
* @returns {boolean} True if value is an ArrayBuffer, otherwise false
*/
function
isArrayBuffer
(
val
)
{
return
toString
.
call
(
val
)
===
'
[object ArrayBuffer]
'
;
}
/**
* Determine if a value is a view on an ArrayBuffer
*
* @param {Object} val The value to test
* @returns {boolean} True if value is a view on an ArrayBuffer, otherwise false
*/
function
isArrayBufferView
(
val
)
{
if
((
typeof
ArrayBuffer
!==
'
undefined
'
)
&&
(
ArrayBuffer
.
isView
))
{
return
ArrayBuffer
.
isView
(
val
);
}
else
{
return
(
val
)
&&
(
val
.
buffer
)
&&
(
val
.
buffer
instanceof
ArrayBuffer
);
}
}
/**
* Determine if a value is a String
*
...
...
@@ -153,6 +177,8 @@ function merge(obj1/*, obj2, obj3, ...*/) {
module
.
exports
=
{
isArray
:
isArray
,
isArrayBuffer
:
isArrayBuffer
,
isArrayBufferView
:
isArrayBufferView
,
isString
:
isString
,
isNumber
:
isNumber
,
isObject
:
isObject
,
...
...
test/specs/wrapper.spec.js
浏览文件 @
c03e51ce
...
...
@@ -45,6 +45,42 @@ describe('wrapper', function () {
}
});
it
(
'
should support binary data as array buffer
'
,
function
()
{
var
input
=
new
Int8Array
(
2
);
input
[
0
]
=
1
;
input
[
1
]
=
2
;
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
input
.
buffer
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
var
output
=
new
Int8Array
(
request
.
params
.
buffer
);
expect
(
output
.
length
).
toEqual
(
2
);
expect
(
output
[
0
]).
toEqual
(
1
);
expect
(
output
[
1
]).
toEqual
(
2
);
});
it
(
'
should support binary data as array buffer view
'
,
function
()
{
var
input
=
new
Int8Array
(
2
);
input
[
0
]
=
1
;
input
[
1
]
=
2
;
axios
({
method
:
'
post
'
,
url
:
'
/foo
'
,
data
:
input
});
var
request
=
jasmine
.
Ajax
.
requests
.
mostRecent
();
var
output
=
new
Int8Array
(
request
.
params
.
buffer
);
expect
(
output
.
length
).
toEqual
(
2
);
expect
(
output
[
0
]).
toEqual
(
1
);
expect
(
output
[
1
]).
toEqual
(
2
);
});
it
(
'
should remove content-type if data is empty
'
,
function
()
{
axios
({
method
:
'
post
'
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录