Skip to content

Commit 95a68f3

Browse files
Hotelldevelopit
andauthored
feat: add relative URI support for baseURL (#51)
* feat: add relative URI support for baseURL * Use nested URL for size * i am smarter than typescript * try a regex (why not) Co-authored-by: Jason Miller <[email protected]>
1 parent a56c877 commit 95a68f3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ export default (function create(/** @type {Options} */ defaults) {
179179
}
180180

181181
if (options.baseURL) {
182-
url = new URL(url, options.baseURL) + '';
182+
url = url.replace(/^(?!.*\/\/)\/?(.*)$/, options.baseURL + '/$1');
183183
}
184184

185185
if (options.params) {

test/index.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,31 @@ describe('redaxios', () => {
9898
window.fetch = oldFetch;
9999
}
100100
});
101+
102+
it('should resolve baseURL for relative URIs', async () => {
103+
const oldFetch = window.fetch;
104+
try {
105+
window.fetch = jasmine
106+
.createSpy('fetch')
107+
.and.returnValue(Promise.resolve({ ok: true, status: 200, text: () => Promise.resolve('') }));
108+
const req = axios.get('/bar', {
109+
baseURL: '/foo'
110+
});
111+
expect(window.fetch).toHaveBeenCalledTimes(1);
112+
expect(window.fetch).toHaveBeenCalledWith(
113+
'/foo/bar',
114+
jasmine.objectContaining({
115+
method: 'get',
116+
headers: {},
117+
body: undefined
118+
})
119+
);
120+
const res = await req;
121+
expect(res.status).toEqual(200);
122+
} finally {
123+
window.fetch = oldFetch;
124+
}
125+
});
101126
});
102127

103128
describe('options.headers', () => {

0 commit comments

Comments
 (0)