Skip to content

Commit

Permalink
Warn about common option misspellings (axios#6489)
Browse files Browse the repository at this point in the history
* chore(tests): add failing tests for baseUrl

* chore(tests): simplify to just warning

* feat: warn about likely-misspelled options

* chore: add semi-colon

* chore: add missing semi-colons

---------

Co-authored-by: Ell Bradshaw <[email protected]>
Co-authored-by: Jay <[email protected]>
  • Loading branch information
3 people authored Sep 26, 2024
1 parent 00de614 commit 8f3cde1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/core/Axios.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class Axios {
}
}

validator.assertOptions(config, {
baseUrl: validators.spelling('baseURL'),
withXsrfToken: validators.spelling('withXSRFToken')
}, true);

// Set config.method
config.method = (config.method || this.defaults.method || 'get').toLowerCase();

Expand Down
8 changes: 8 additions & 0 deletions lib/helpers/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ validators.transitional = function transitional(validator, version, message) {
};
};

validators.spelling = function spelling(correctSpelling) {
return (value, opt) => {
// eslint-disable-next-line no-console
console.warn(`${opt} is likely a misspelling of ${correctSpelling}`);
return true;
}
};

/**
* Assert object's properties type
*
Expand Down
16 changes: 16 additions & 0 deletions test/specs/options.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,22 @@ describe('options', function () {
});
});

it('should warn about baseUrl', function (done) {
spyOn(window.console, 'warn');

const instance = axios.create({
baseUrl: 'http://example.com/'
});

instance.get('/foo');

getAjaxRequest().then(function (request) {
expect(window.console.warn).toHaveBeenCalledWith('baseUrl is likely a misspelling of baseURL');
expect(request.url).toBe('/foo');
done();
});
});

it('should ignore base URL if request URL is absolute', function (done) {
const instance = axios.create({
baseURL: 'http://someurl.com/'
Expand Down

0 comments on commit 8f3cde1

Please sign in to comment.