mirror of
https://github.com/tenrok/axios.git
synced 2026-05-15 11:59:42 +03:00
Adding initial source
This commit is contained in:
Vendored
+902
File diff suppressed because one or more lines are too long
Vendored
+902
File diff suppressed because one or more lines are too long
Vendored
+903
File diff suppressed because one or more lines are too long
Vendored
+903
File diff suppressed because one or more lines are too long
+129
@@ -0,0 +1,129 @@
|
||||
var Promise = require('es6-promise').Promise;
|
||||
|
||||
function axios(options) {
|
||||
options = merge({
|
||||
method: 'get'
|
||||
}, options);
|
||||
|
||||
var promise = new Promise(function (resolve, reject) {
|
||||
var request = new(XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
|
||||
|
||||
function onload() {
|
||||
if (request.status >= 200 && request.status < 300) {
|
||||
resolve(parse(request.responseText));
|
||||
} else {
|
||||
onerror();
|
||||
}
|
||||
}
|
||||
|
||||
function onerror() {
|
||||
reject(new Error('Can\'t connect to ' + JSON.stringify(options.url)));
|
||||
}
|
||||
|
||||
try {
|
||||
request.open(options.method, options.url, true);
|
||||
request.onreadystatechange = function () {
|
||||
if (request.readyState === 4) {
|
||||
onload();
|
||||
}
|
||||
};
|
||||
|
||||
request.onload = request.load = onload;
|
||||
request.onerror = request.error = onerror;
|
||||
|
||||
var headers = merge(
|
||||
defaults.headers.common,
|
||||
defaults.headers[options.method] || {},
|
||||
options.headers || {}
|
||||
);
|
||||
|
||||
for (var key in headers) {
|
||||
request.setRequestHeader(key, headers[key]);
|
||||
}
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
|
||||
request.send(options.data || null);
|
||||
});
|
||||
|
||||
promise.success = function (fn) {
|
||||
promise.then(function(response) {
|
||||
fn(response);
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
promise.error = function(fn) {
|
||||
promise.then(null, function(response) {
|
||||
fn(response);
|
||||
});
|
||||
return promise;
|
||||
};
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
var CONTENT_TYPE_APPLICATION_JSON = {'Content-Type': 'application/json;charset=utf-8'};
|
||||
var defaults = axios.defaults = {
|
||||
headers: {
|
||||
common: {'Accept': 'application/json, text/plain, */*'},
|
||||
patch: merge(CONTENT_TYPE_APPLICATION_JSON),
|
||||
post: merge(CONTENT_TYPE_APPLICATION_JSON),
|
||||
put: merge(CONTENT_TYPE_APPLICATION_JSON)
|
||||
}
|
||||
};
|
||||
|
||||
function parse(response) {
|
||||
try {
|
||||
return JSON.parse(response);
|
||||
} catch(e) {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
|
||||
function merge() {
|
||||
var result = {};
|
||||
forEach(arguments, function (obj) {
|
||||
for (var key in obj) {
|
||||
if (obj.hasOwnProperty(key)) {
|
||||
result[key] = obj[key];
|
||||
}
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
function forEach(arr, fn) {
|
||||
for (var i=0, l=arr.length; i<l; i++) {
|
||||
fn.call(null, arr[i], i, arr);
|
||||
}
|
||||
}
|
||||
|
||||
function createShortMethods() {
|
||||
forEach(arguments, function (method) {
|
||||
axios[method] = function (url, options) {
|
||||
return axios(merge(options || {}, {
|
||||
method: method,
|
||||
url: url
|
||||
}));
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
function createShortMethodsWithData() {
|
||||
forEach(arguments, function (method) {
|
||||
axios[method] = function (url, data, options) {
|
||||
return axios(merge(options || {}, {
|
||||
method: method,
|
||||
url: url,
|
||||
data: data
|
||||
}));
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
createShortMethods('delete', 'get', 'head');
|
||||
createShortMethodsWithData('post', 'put', 'patch');
|
||||
|
||||
module.exports = axios;
|
||||
@@ -0,0 +1,102 @@
|
||||
describe('axios', function () {
|
||||
describe('api', function () {
|
||||
it('should have request method helpers', function () {
|
||||
expect(typeof axios.get).toEqual('function');
|
||||
expect(typeof axios.head).toEqual('function');
|
||||
expect(typeof axios.delete).toEqual('function');
|
||||
expect(typeof axios.post).toEqual('function');
|
||||
expect(typeof axios.put).toEqual('function');
|
||||
expect(typeof axios.patch).toEqual('function');
|
||||
});
|
||||
|
||||
it('should have promise method helpers', function () {
|
||||
var promise = axios();
|
||||
|
||||
expect(typeof promise.then).toEqual('function');
|
||||
expect(typeof promise.catch).toEqual('function');
|
||||
expect(typeof promise.success).toEqual('function');
|
||||
expect(typeof promise.error).toEqual('function');
|
||||
});
|
||||
|
||||
it('should have defaults', function () {
|
||||
expect(typeof axios.defaults).toEqual('object');
|
||||
expect(typeof axios.defaults.headers).toEqual('object');
|
||||
});
|
||||
});
|
||||
|
||||
describe('wrapper', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
it('should make an http request', function () {
|
||||
axios({
|
||||
url: '/foo'
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.url).toBe('/foo');
|
||||
});
|
||||
|
||||
it('should default common headers', function () {
|
||||
axios();
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var headers = axios.defaults.headers.common;
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should add extra headers for post', function () {
|
||||
axios({
|
||||
method: 'post'
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
var headers = axios.defaults.headers.post;
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
expect(request.requestHeaders[key]).toEqual(headers[key]);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('options', function () {
|
||||
beforeEach(function () {
|
||||
jasmine.Ajax.install();
|
||||
});
|
||||
|
||||
it('should default method to get', function () {
|
||||
axios();
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.method).toBe('get');
|
||||
});
|
||||
|
||||
it('should accept headers', function () {
|
||||
axios({
|
||||
headers: {
|
||||
'X-Requested-With': 'XMLHttpRequest'
|
||||
}
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.requestHeaders['X-Requested-With']).toEqual('XMLHttpRequest');
|
||||
});
|
||||
|
||||
it('should allow overriding default headers', function () {
|
||||
axios({
|
||||
headers: {
|
||||
'Accept': 'foo/bar'
|
||||
}
|
||||
});
|
||||
|
||||
var request = jasmine.Ajax.requests.mostRecent();
|
||||
expect(request.requestHeaders['Accept']).toEqual('foo/bar');
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user