mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Fixing baseURL not working in interceptors (#950)
* Fixing baseURL not working in interceptors * add test for modify base URL in request interceptor
This commit is contained in:
@@ -4,8 +4,6 @@ var defaults = require('./../defaults');
|
|||||||
var utils = require('./../utils');
|
var utils = require('./../utils');
|
||||||
var InterceptorManager = require('./InterceptorManager');
|
var InterceptorManager = require('./InterceptorManager');
|
||||||
var dispatchRequest = require('./dispatchRequest');
|
var dispatchRequest = require('./dispatchRequest');
|
||||||
var isAbsoluteURL = require('./../helpers/isAbsoluteURL');
|
|
||||||
var combineURLs = require('./../helpers/combineURLs');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new instance of Axios
|
* Create a new instance of Axios
|
||||||
@@ -37,11 +35,6 @@ Axios.prototype.request = function request(config) {
|
|||||||
config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
|
config = utils.merge(defaults, this.defaults, { method: 'get' }, config);
|
||||||
config.method = config.method.toLowerCase();
|
config.method = config.method.toLowerCase();
|
||||||
|
|
||||||
// Support baseURL config
|
|
||||||
if (config.baseURL && !isAbsoluteURL(config.url)) {
|
|
||||||
config.url = combineURLs(config.baseURL, config.url);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hook up interceptors middleware
|
// Hook up interceptors middleware
|
||||||
var chain = [dispatchRequest, undefined];
|
var chain = [dispatchRequest, undefined];
|
||||||
var promise = Promise.resolve(config);
|
var promise = Promise.resolve(config);
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ var utils = require('./../utils');
|
|||||||
var transformData = require('./transformData');
|
var transformData = require('./transformData');
|
||||||
var isCancel = require('../cancel/isCancel');
|
var isCancel = require('../cancel/isCancel');
|
||||||
var defaults = require('../defaults');
|
var defaults = require('../defaults');
|
||||||
|
var isAbsoluteURL = require('./../helpers/isAbsoluteURL');
|
||||||
|
var combineURLs = require('./../helpers/combineURLs');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throws a `Cancel` if cancellation has been requested.
|
* Throws a `Cancel` if cancellation has been requested.
|
||||||
@@ -23,6 +25,11 @@ function throwIfCancellationRequested(config) {
|
|||||||
module.exports = function dispatchRequest(config) {
|
module.exports = function dispatchRequest(config) {
|
||||||
throwIfCancellationRequested(config);
|
throwIfCancellationRequested(config);
|
||||||
|
|
||||||
|
// Support baseURL config
|
||||||
|
if (config.baseURL && !isAbsoluteURL(config.url)) {
|
||||||
|
config.url = combineURLs(config.baseURL, config.url);
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure headers exist
|
// Ensure headers exist
|
||||||
config.headers = config.headers || {};
|
config.headers = config.headers || {};
|
||||||
|
|
||||||
|
|||||||
@@ -252,4 +252,22 @@ describe('interceptors', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should modify base URL in request interceptor', function (done) {
|
||||||
|
var instance = axios.create({
|
||||||
|
baseURL: 'http://test.com/'
|
||||||
|
});
|
||||||
|
|
||||||
|
instance.interceptors.request.use(function (config) {
|
||||||
|
config.baseURL = 'http://rebase.com/';
|
||||||
|
return config;
|
||||||
|
});
|
||||||
|
|
||||||
|
instance.get('/foo');
|
||||||
|
|
||||||
|
getAjaxRequest().then(function (request) {
|
||||||
|
expect(request.url).toBe('http://rebase.com/foo');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user