2
0
mirror of https://github.com/tenrok/axios.git synced 2026-06-08 17:22:34 +03:00

Make axios instantiable

This commit is contained in:
Nick Uraltsev
2015-10-03 09:38:16 -07:00
parent 4f732e8caa
commit b10874fa67
5 changed files with 194 additions and 42 deletions
+29 -1
View File
@@ -1,6 +1,6 @@
var axios = require('../../index');
describe('api', function () {
describe('static api', function () {
it('should have request method helpers', function () {
expect(typeof axios.get).toEqual('function');
expect(typeof axios.head).toEqual('function');
@@ -22,8 +22,36 @@ describe('api', function () {
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 () {
expect(typeof axios.all).toEqual('function');
expect(typeof axios.spread).toEqual('function');
});
it('should have factory method', function () {
expect(typeof axios.createNew).toEqual('function');
});
});
describe('instance api', function () {
var instance = axios.createNew();
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');
});
});
+41
View File
@@ -0,0 +1,41 @@
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.createNew();
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.createNew({ timeout: 1000 });
instance.request({
url: '/foo'
});
setTimeout(function () {
request = jasmine.Ajax.requests.mostRecent();
expect(request.timeout).toBe(1000);
done();
}, 0);
});
});
+63 -15
View File
@@ -4,11 +4,11 @@ import axios = require('axios');
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
console.log(response);
})
.catch(function (response) {
console.log(response);
});
console.log(response);
});
axios.get('/user', {
params: {
@@ -96,16 +96,64 @@ axios({
});
axios({
url: "hi",
headers: {'X-Requested-With': 'XMLHttpRequest'},
url: "hi",
headers: {'X-Requested-With': 'XMLHttpRequest'},
params: {
ID: 12345
},
data: {
firstName: 'Fred'
},
withCredentials: false, // default
responseType: 'json', // default
xsrfCookieName: 'XSRF-TOKEN', // default
xsrfHeaderName: 'X-XSRF-TOKEN' // default
});
var instance = axios.createNew();
axios.createNew({
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
},
data: {
firstName: 'Fred'
},
withCredentials: false, // default
responseType: 'json', // default
xsrfCookieName: 'XSRF-TOKEN', // default
xsrfHeaderName: 'X-XSRF-TOKEN' // default
});
}
})
.then(function (response) {
console.log(response);
})
.catch(function (response) {
console.log(response);
});