-
-
Notifications
You must be signed in to change notification settings - Fork 362
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
Add support for relative URLs and searchParams even in non-browsers #271
Merged
sindresorhus
merged 2 commits into
sindresorhus:master
from
sholladay:remove-whatwg-url
Jul 17, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import test from 'ava'; | ||
import ky from '..'; | ||
|
||
test.serial('relative URLs are passed to fetch unresolved', async t => { | ||
const originalFetch = global.fetch; | ||
global.fetch = async input => { | ||
t.true(input.url.startsWith('/')); | ||
return new Response(input.url); | ||
}; | ||
|
||
t.is(await ky('/unicorn').text(), '/unicorn'); | ||
t.is(await ky('/unicorn', {searchParams: {foo: 'bar'}}).text(), '/unicorn?foo=bar'); | ||
t.is(await ky('/unicorn#hash', {searchParams: 'foo'}).text(), '/unicorn?foo=#hash'); | ||
t.is(await ky('/unicorn?old', {searchParams: 'new'}).text(), '/unicorn?new='); | ||
t.is(await ky('/unicorn?old#hash', {searchParams: 'new'}).text(), '/unicorn?new=#hash'); | ||
t.is(await ky('unicorn', {prefixUrl: '/api/'}).text(), '/api/unicorn'); | ||
global.fetch = originalFetch; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Sorry I'm late to the party. Does this change anything?
this.request.url
is an absolute URL anyway... I'm just asking because I don't like RegExps.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 don't like regex either.
this.request.url
can be a relative URL in some environments, e.g. Node and Deno.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.
Then why not this?
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 believe your code would remove the hash fragment from a URL, unlike the
new URL()
code we had before.Granted, the hash fragment shouldn't be sent to the server anyway, so that shouldn't really matter. But I figure we should be spec compliant and unobtrusive by only modifying the search params and nothing more. It's possible that a custom
fetch
function might care about the hash fragment, for example.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.
Hmm... Fair point. We could also
indexOf('#')
but that would add more complexity. The regex solution is much clearer.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.
Yep, my thoughts exactly. I did add tests that assert we don't modify the hash fragment. So if someone comes up with a clean non-regex solution, go ahead and give it a shot. It'll be checked thoroughly by the tests.