hg-pyun / axios-logger

Beautify Axios Logging Messages
MIT License
173 stars 33 forks source link

`combineURLs` produces wrong absolute URL when `baseURL` has `path` part #148

Closed xak2000 closed 8 months ago

xak2000 commented 9 months ago

Describe the bug When baseURL is an absolute URL with path part included (e.g. http://example.com/foo) and url is relative URL (e.g. /bar) the logged URL does not include the path part of the baseURL (i.e. /foo) while Axios calls the correct URL.

In other words: Axios calls: http://example.com/foo/bar The log shows: http://example.com/bar (/foo is missing)

To Reproduce

const api = axios.create({
  baseURL: 'http://example.com/foo'
})
api({
  url: '/bar'
})

Expected behavior

[Axios][Request] GET http://example.com/foo/bar

Actual behavior

[Axios][Request] GET http://example.com/bar

Additional Context

Related bug: #140 (probably should be fixed together as they both caused by the same URL-combining function).

I think new URL(relativeURL, baseURL) is wrong tool to combine URLs to produce the same URL as Axios does.

A failing test

test('combineURLs should combine the base URL with the relative URL when base URL has a path', () => {
    const sb = new StringBuilder(getGlobalConfig());
    const result = sb.combineURLs('https://github.com/users', '/hg-pyun');

    expect(result).toBe('https://github.com/users/hg-pyun');
});
antirek commented 9 months ago

same problem

antirek commented 9 months ago

@hg-pyun add it please to 2.7.2

hg-pyun commented 9 months ago

I will check it soon. Thx 🙇‍♂️