mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding support for custom adapters
This commit is contained in:
@@ -167,14 +167,14 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
// `url` is the server URL that will be used for the request
|
// `url` is the server URL that will be used for the request
|
||||||
url: '/user',
|
url: '/user',
|
||||||
|
|
||||||
|
// `method` is the request method to be used when making the request
|
||||||
|
method: 'get', // default
|
||||||
|
|
||||||
// `baseURL` will be prepended to `url` unless `url` is absolute.
|
// `baseURL` will be prepended to `url` unless `url` is absolute.
|
||||||
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
|
// It can be convenient to set `baseURL` for an instance of axios to pass relative URLs
|
||||||
// to methods of that instance.
|
// to methods of that instance.
|
||||||
baseURL: 'https://some-domain.com/api/',
|
baseURL: 'https://some-domain.com/api/',
|
||||||
|
|
||||||
// `method` is the request method to be used when making the request
|
|
||||||
method: 'get', // default
|
|
||||||
|
|
||||||
// `transformRequest` allows changes to the request data before it is sent to the server
|
// `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'
|
// This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
|
||||||
// The last function in the array must return a string or an ArrayBuffer
|
// The last function in the array must return a string or an ArrayBuffer
|
||||||
@@ -221,6 +221,12 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
// should be made using credentials
|
// should be made using credentials
|
||||||
withCredentials: false, // default
|
withCredentials: false, // default
|
||||||
|
|
||||||
|
// `adapter` allows custom handling of requests which makes testing easier.
|
||||||
|
// Call `resolve` or `reject` and supply a valid response (see [response docs](#response-api)).
|
||||||
|
adapter: function (resolve, reject, config) {
|
||||||
|
/* ... */
|
||||||
|
},
|
||||||
|
|
||||||
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
|
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
|
||||||
// This will set an `Authorization` header, overwriting any existing
|
// This will set an `Authorization` header, overwriting any existing
|
||||||
// `Authorization` custom headers you have set using `headers`.
|
// `Authorization` custom headers you have set using `headers`.
|
||||||
|
|||||||
@@ -10,12 +10,21 @@
|
|||||||
module.exports = function dispatchRequest(config) {
|
module.exports = function dispatchRequest(config) {
|
||||||
return new Promise(function executor(resolve, reject) {
|
return new Promise(function executor(resolve, reject) {
|
||||||
try {
|
try {
|
||||||
if ((typeof XMLHttpRequest !== 'undefined') || (typeof ActiveXObject !== 'undefined')) {
|
var adapter;
|
||||||
|
|
||||||
|
if (typeof config.adapter === 'function') {
|
||||||
|
// For custom adapter support
|
||||||
|
adapter = config.adapter;
|
||||||
|
} else if (typeof XMLHttpRequest !== 'undefined') {
|
||||||
// For browsers use XHR adapter
|
// For browsers use XHR adapter
|
||||||
require('../adapters/xhr')(resolve, reject, config);
|
adapter = require('../adapters/xhr');
|
||||||
} else if (typeof process !== 'undefined') {
|
} else if (typeof process !== 'undefined') {
|
||||||
// For node use HTTP adapter
|
// For node use HTTP adapter
|
||||||
require('../adapters/http')(resolve, reject, config);
|
adapter = require('../adapters/http');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof adapter === 'function') {
|
||||||
|
adapter(resolve, reject, config);
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
reject(e);
|
reject(e);
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
var axios = require('../../index');
|
||||||
|
|
||||||
|
describe('adapter', function () {
|
||||||
|
it('should support custom adapter', function (done) {
|
||||||
|
var called = false;
|
||||||
|
|
||||||
|
axios({
|
||||||
|
url: '/foo',
|
||||||
|
adapter: function (resolve, reject, config) {
|
||||||
|
called = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
setTimeout(function () {
|
||||||
|
expect(called).toBe(true);
|
||||||
|
done();
|
||||||
|
}, 0);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
Reference in New Issue
Block a user