Adding baseURL to be used in getUri(), also removing question mark trimming since there seems to be no obvious reason for it. (#3737)

Co-authored-by: Jay <jasonsaayman@gmail.com>
This commit is contained in:
sakarit 2022-03-07 20:09:23 +02:00 committed by GitHub
parent 195c8e5ff5
commit bdb7d76d40
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 1 deletions

View File

@ -5,6 +5,7 @@ var buildURL = require('../helpers/buildURL');
var InterceptorManager = require('./InterceptorManager');
var dispatchRequest = require('./dispatchRequest');
var mergeConfig = require('./mergeConfig');
var buildFullPath = require('./buildFullPath');
var validator = require('../helpers/validator');
var validators = validator.validators;
@ -119,7 +120,8 @@ Axios.prototype.request = function request(configOrUrl, config) {
Axios.prototype.getUri = function getUri(config) {
config = mergeConfig(this.defaults, config);
return buildURL(config.url, config.params, config.paramsSerializer).replace(/^\?/, '');
var fullPath = buildFullPath(config.baseURL, config.url);
return buildURL(fullPath, config.params, config.paramsSerializer);
};
// Provide aliases for supported request methods

View File

@ -42,6 +42,10 @@ describe('static api', function () {
expect(typeof axios.isCancel).toEqual('function');
});
it('should have getUri method', function() {
expect(typeof axios.getUri).toEqual('function');
});
it('should have isAxiosError properties', function () {
expect(typeof axios.isAxiosError).toEqual('function');
});

View File

@ -19,6 +19,7 @@ describe('instance', function () {
'isCancel',
'all',
'spread',
'getUri',
'isAxiosError',
'VERSION',
'default'].indexOf(prop) > -1) {
@ -112,4 +113,40 @@ describe('instance', function () {
}, 100);
});
});
it('should have getUri on the instance', function() {
var instance = axios.create({
baseURL: 'https://api.example.com'
});
var options = {
url: 'foo/bar',
params: {
name: 'axios'
}
};
expect(instance.getUri(options)).toBe('https://api.example.com/foo/bar?name=axios');
});
it('should correctly build url without baseURL', function () {
var instance = axios.create();
var options = {
url: 'foo/bar?foo=bar',
params: {
name: 'axios'
}
};
expect(instance.getUri(options)).toBe('foo/bar?foo=bar&name=axios');
});
it('should correctly discard url hash mark', function () {
var instance = axios.create();
var options = {
baseURL: 'https://api.example.com',
url: 'foo/bar?foo=bar#hash',
params: {
name: 'axios'
}
};
expect(instance.getUri(options)).toBe('https://api.example.com/foo/bar?foo=bar&name=axios');
});
});