From 3b3c5e3ca4a3ac73c9a90c90f0cf2bb51788b221 Mon Sep 17 00:00:00 2001 From: Martin Hochel Date: Fri, 31 Jul 2020 17:44:58 +0200 Subject: [PATCH 1/4] feat: add relative URI support for baseURL --- src/index.js | 5 ++++- test/index.test.js | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 39f523b..6d88db4 100644 --- a/src/index.js +++ b/src/index.js @@ -178,7 +178,10 @@ export default (function create(/** @type {Options} */ defaults) { } if (options.baseURL) { - url = new URL(url, options.baseURL) + ''; + const _ = options.baseURL, + isAbsURL = ~_.indexOf('://'); + + url = (new URL(url, isAbsURL ? _ : 'http:' + _) + '').slice(isAbsURL ? 0 : 6); } if (options.params) { diff --git a/test/index.test.js b/test/index.test.js index 2682d3d..13b0d37 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -98,6 +98,31 @@ describe('redaxios', () => { window.fetch = oldFetch; } }); + + it('should resolve baseURL for relative URIs', async () => { + const oldFetch = window.fetch; + try { + window.fetch = jasmine + .createSpy('fetch') + .and.returnValue(Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve('') })); + const req = axios.get('/bar', { + baseURL: '/foo' + }); + expect(window.fetch).toHaveBeenCalledTimes(1); + expect(window.fetch).toHaveBeenCalledWith( + '/foo/bar', + jasmine.objectContaining({ + method: 'get', + headers: {}, + body: undefined + }) + ); + const res = await req; + expect(res.status).toEqual(200); + } finally { + window.fetch = oldFetch; + } + }); }); describe('options.headers', () => { From 9309103a42a53b3a656ba5c2a88531d023e72981 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 9 Oct 2020 10:58:37 -0400 Subject: [PATCH 2/4] Use nested URL for size --- src/index.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index f053a66..ce30bb3 100644 --- a/src/index.js +++ b/src/index.js @@ -179,10 +179,7 @@ export default (function create(/** @type {Options} */ defaults) { } if (options.baseURL) { - const _ = options.baseURL, - isAbsURL = ~_.indexOf('://'); - - url = (new URL(url, isAbsURL ? _ : 'http:' + _) + '').slice(isAbsURL ? 0 : 6); + url = new URL(url, new URL(options.baseURL, location)) + ''; } if (options.params) { From 4698e4207728d67ef6ca1b6586b49e96d7f8bf3e Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 9 Oct 2020 11:00:26 -0400 Subject: [PATCH 3/4] i am smarter than typescript --- src/index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.js b/src/index.js index ce30bb3..198f175 100644 --- a/src/index.js +++ b/src/index.js @@ -179,6 +179,7 @@ export default (function create(/** @type {Options} */ defaults) { } if (options.baseURL) { + // @ts-ignore-next url = new URL(url, new URL(options.baseURL, location)) + ''; } From adaf95ec3690b186e1c7797322305dae35d22280 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 9 Oct 2020 11:20:29 -0400 Subject: [PATCH 4/4] try a regex (why not) --- src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 198f175..27b4433 100644 --- a/src/index.js +++ b/src/index.js @@ -179,8 +179,7 @@ export default (function create(/** @type {Options} */ defaults) { } if (options.baseURL) { - // @ts-ignore-next - url = new URL(url, new URL(options.baseURL, location)) + ''; + url = url.replace(/^(?!.*\/\/)\/?(.*)$/, options.baseURL + '/$1'); } if (options.params) {