Skip to content

Commit

Permalink
Adding baseURL to be used in getUri(), also removing question mark tr…
Browse files Browse the repository at this point in the history
…imming since there seems to be no obvious reason for it. (#3737)

Co-authored-by: Jay <[email protected]>
  • Loading branch information
sakarit and jasonsaayman authored Mar 7, 2022
1 parent 195c8e5 commit bdb7d76
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/core/Axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/specs/api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down
37 changes: 37 additions & 0 deletions test/specs/instance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('instance', function () {
'isCancel',
'all',
'spread',
'getUri',
'isAxiosError',
'VERSION',
'default'].indexOf(prop) > -1) {
Expand Down Expand Up @@ -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');
});
});

0 comments on commit bdb7d76

Please sign in to comment.