mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
* fix: prevent request url override prevent request URL from overriding preconfigured base URL BREAKING CHANGE: code relying on the above will now combine the URLs instead of prefer request URL * feat: add config option for allowing absolute URLs * fix: add default value for allowAbsoluteUrls in buildFullPath * fix: typo in flow control when setting allowAbsoluteUrls * feat: update tests supporting issue #5902 functionality * feat: update README.md with allowAbsoluteUrls * fix: properly group conditions in buildFullPath.js to avoid undefined error when baseUrl undefined * Update README.md fix typo * fix: update build full path logic to address failing test case * fix: update base URL test * fix: remove problem test (works locally, will not work in the pipeline) * fix: update https test to use github.com instead of google.com * fix: revert previous commit * fix: add back problem test * chore: remove un-needed passed var to URL class instanciation --------- Co-authored-by: Austin Ryan Lawson <ryan.lawson2@gmail.com> Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
@@ -375,11 +375,16 @@ These are the available config options for making requests. Only the `url` is re
|
|||||||
// `method` is the request method to be used when making the request
|
// `method` is the request method to be used when making the request
|
||||||
method: 'get', // default
|
method: 'get', // default
|
||||||
|
|
||||||
// `baseURL` will be prepended to `url` unless `url` is absolute.
|
// `baseURL` will be prepended to `url` unless `url` is absolute and option `allowAbsoluteUrls` is set to true.
|
||||||
// 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/',
|
||||||
|
|
||||||
|
// `allowAbsoluteUrls` determines whether or not absolute URLs will override a configured `baseUrl`.
|
||||||
|
// When set to true (default), absolute values for `url` will override `baseUrl`.
|
||||||
|
// When set to false, absolute values for `url` will always be prepended by `baseUrl`.
|
||||||
|
allowAbsoluteUrls: true,
|
||||||
|
|
||||||
// `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', 'PATCH' and 'DELETE'
|
// This is only applicable for request methods 'PUT', 'POST', 'PATCH' and 'DELETE'
|
||||||
// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
|
// The last function in the array must return a string or an instance of Buffer, ArrayBuffer,
|
||||||
|
|||||||
+10
-1
@@ -97,6 +97,15 @@ class Axios {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Set config.allowAbsoluteUrls
|
||||||
|
if (config.allowAbsoluteUrls !== undefined) {
|
||||||
|
// do nothing
|
||||||
|
} else if (this.defaults.allowAbsoluteUrls !== undefined) {
|
||||||
|
config.allowAbsoluteUrls = this.defaults.allowAbsoluteUrls;
|
||||||
|
} else {
|
||||||
|
config.allowAbsoluteUrls = true;
|
||||||
|
}
|
||||||
|
|
||||||
validator.assertOptions(config, {
|
validator.assertOptions(config, {
|
||||||
baseUrl: validators.spelling('baseURL'),
|
baseUrl: validators.spelling('baseURL'),
|
||||||
withXsrfToken: validators.spelling('withXSRFToken')
|
withXsrfToken: validators.spelling('withXSRFToken')
|
||||||
@@ -192,7 +201,7 @@ class Axios {
|
|||||||
|
|
||||||
getUri(config) {
|
getUri(config) {
|
||||||
config = mergeConfig(this.defaults, config);
|
config = mergeConfig(this.defaults, config);
|
||||||
const fullPath = buildFullPath(config.baseURL, config.url);
|
const fullPath = buildFullPath(config.baseURL, config.url, config.allowAbsoluteUrls);
|
||||||
return buildURL(fullPath, config.params, config.paramsSerializer);
|
return buildURL(fullPath, config.params, config.paramsSerializer);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,8 +13,9 @@ import combineURLs from '../helpers/combineURLs.js';
|
|||||||
*
|
*
|
||||||
* @returns {string} The combined full path
|
* @returns {string} The combined full path
|
||||||
*/
|
*/
|
||||||
export default function buildFullPath(baseURL, requestedURL) {
|
export default function buildFullPath(baseURL, requestedURL, allowAbsoluteUrls) {
|
||||||
if (baseURL && !isAbsoluteURL(requestedURL)) {
|
let isRelativeUrl = !isAbsoluteURL(requestedURL);
|
||||||
|
if (baseURL && isRelativeUrl || allowAbsoluteUrls == false) {
|
||||||
return combineURLs(baseURL, requestedURL);
|
return combineURLs(baseURL, requestedURL);
|
||||||
}
|
}
|
||||||
return requestedURL;
|
return requestedURL;
|
||||||
|
|||||||
@@ -5,10 +5,14 @@ describe('helpers::buildFullPath', function () {
|
|||||||
expect(buildFullPath('https://api.github.com', '/users')).toBe('https://api.github.com/users');
|
expect(buildFullPath('https://api.github.com', '/users')).toBe('https://api.github.com/users');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return the requestedURL when it is absolute', function () {
|
it('should not combine the URLs when the requestedURL is absolute', function () {
|
||||||
expect(buildFullPath('https://api.github.com', 'https://api.example.com/users')).toBe('https://api.example.com/users');
|
expect(buildFullPath('https://api.github.com', 'https://api.example.com/users')).toBe('https://api.example.com/users');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should combine the URLs when the requestedURL is absolute and allowAbsoluteUrls is false', function () {
|
||||||
|
expect(buildFullPath('https://api.github.com', 'https://api.example.com/users', false)).toBe('https://api.github.com/https://api.example.com/users');
|
||||||
|
});
|
||||||
|
|
||||||
it('should not combine URLs when the baseURL is not configured', function () {
|
it('should not combine URLs when the baseURL is not configured', function () {
|
||||||
expect(buildFullPath(undefined, '/users')).toBe('/users');
|
expect(buildFullPath(undefined, '/users')).toBe('/users');
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import AxiosHeaders from "../../lib/core/AxiosHeaders.js";
|
// import AxiosHeaders from "../../lib/core/AxiosHeaders.js";
|
||||||
|
// import isAbsoluteURL from '../../lib/helpers/isAbsoluteURL.js';
|
||||||
|
|
||||||
describe('options', function () {
|
describe('options', function () {
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
@@ -63,8 +64,7 @@ describe('options', function () {
|
|||||||
baseURL: 'http://test.com/'
|
baseURL: 'http://test.com/'
|
||||||
});
|
});
|
||||||
|
|
||||||
instance.get('/foo');
|
instance.get('/foo')
|
||||||
|
|
||||||
getAjaxRequest().then(function (request) {
|
getAjaxRequest().then(function (request) {
|
||||||
expect(request.url).toBe('http://test.com/foo');
|
expect(request.url).toBe('http://test.com/foo');
|
||||||
done();
|
done();
|
||||||
@@ -100,6 +100,21 @@ describe('options', function () {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should combine the URLs if base url and request url exist and allowAbsoluteUrls is false', function (done) {
|
||||||
|
const instance = axios.create({
|
||||||
|
baseURL: 'http://someurl.com/',
|
||||||
|
allowAbsoluteUrls: false
|
||||||
|
});
|
||||||
|
|
||||||
|
instance.get('http://someotherurl.com/');
|
||||||
|
|
||||||
|
getAjaxRequest().then(function (request) {
|
||||||
|
expect(request.url).toBe('http://someotherurl.com/');
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
it('should change only the baseURL of the specified instance', function() {
|
it('should change only the baseURL of the specified instance', function() {
|
||||||
const instance1 = axios.create();
|
const instance1 = axios.create();
|
||||||
const instance2 = axios.create();
|
const instance2 = axios.create();
|
||||||
|
|||||||
Reference in New Issue
Block a user