From 8f3cde11d2501f97ddb77ed9f8938703293647d9 Mon Sep 17 00:00:00 2001 From: Ell Bradshaw Date: Thu, 26 Sep 2024 12:31:24 -0700 Subject: [PATCH] Warn about common option misspellings (#6489) * 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 Co-authored-by: Jay --- lib/core/Axios.js | 5 +++++ lib/helpers/validator.js | 8 ++++++++ test/specs/options.spec.js | 16 ++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/lib/core/Axios.js b/lib/core/Axios.js index 8eebdc4..6dd3c2c 100644 --- a/lib/core/Axios.js +++ b/lib/core/Axios.js @@ -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(); diff --git a/lib/helpers/validator.js b/lib/helpers/validator.js index 14b4696..1270568 100644 --- a/lib/helpers/validator.js +++ b/lib/helpers/validator.js @@ -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 * diff --git a/test/specs/options.spec.js b/test/specs/options.spec.js index 9879fdc..482838e 100644 --- a/test/specs/options.spec.js +++ b/test/specs/options.spec.js @@ -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/'