-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: do not double prepend baseUrl on retry #142
Conversation
Codecov Report
@@ Coverage Diff @@
## master #142 +/- ##
==========================================
- Coverage 96.85% 96.55% -0.31%
==========================================
Files 4 4
Lines 572 580 +8
Branches 96 98 +2
==========================================
+ Hits 554 560 +6
- Misses 17 19 +2
Partials 1 1
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we discussed in air a potential improvement, of switching to a shallow clone for local opts
which removes baseUrl
/baseURL
as a parameter, such that it's not passed on retry.
Everything else looks good to me.
Coming back around ... I better understand the problem now. This issue is this:
I can think of two ways to fix this:
@stephenplusplus the problem here wasn't that the option wasn't retained ... it's that it got prepended once for every time we retried the request. |
src/gaxios.ts
Outdated
@@ -147,7 +147,7 @@ export class Gaxios { | |||
|
|||
// baseUrl has been deprecated, remove in 2.0 | |||
const baseUrl = opts.baseUrl || opts.baseURL; | |||
if (baseUrl) { | |||
if (baseUrl && !opts.url.startsWith(baseUrl)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we go with the approach of simply checking for baseUrl
as a prefix, perhaps we could perform an additional check to ensure that we're not re-appending a scheme:
we never want:
https://www.baseurl.com/foo/http://www.oldurl.com
Perhaps we could do:
const {parse, resolve} = require("url");
if (baseUrl && !opts.url.startsWith(baseUrl)) {
opts.url = resolve(baseUrl, parse(opts.url).path)
}
Or perhaps if a scheme already exists in opts.url
, it should take precedence ... this might be the safest change:
const {resolve} = require('url')
if (baseUrl && !opts.url.startsWith(baseUrl) && !/https?:\/\//.test(opts.url)) {
opts.url = resolve(baseUrl, opts.url)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm fine with the simple approach, my only comment would be that I think we can tighten the logic up a bit, with something like:
const {resolve} = require('url')
if (baseUrl && !opts.url.startsWith(baseUrl) && !/https?:\/\//.test(opts.url)) {
opts.url = resolve(baseUrl, opts.url)
}
If opts.url
is already a fully qualified URL, don't try to append, and use resolve
to append the baseURL and opts.url
, such that http://www.foo.com/bar
appends properly to bar/foo
.
Just a suggestion; Maybe we can just add another parameter to For example,
Then we can use it like this:
|
Fixes #141