-
-
Notifications
You must be signed in to change notification settings - Fork 382
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
prefixUrl is unneededly applied to absolute urls #291
Comments
We could, but my concern is it will then be confusing if it still applies the prefix to input URLs like In the past, I've proposed adding a You're probably thinking this makes sense but is still a bit kludgy and I agree. One thought I've been kicking around is to let Open to other suggestions! |
@ux-engineer @sholladay What if we keep the current behavior of const api = ky.extend({
prefixUrl: "https://api",
resolveUrl(url, prefix) {
return url.startsWith("http") ? url : prefix + url;
}
});
api.get("foo"); //=> GET https://api/foo
api.get("http://bar"); //=> GET https://bar |
A resolver function is a decent idea, although to me this is just a more verbose way to invoke the browser's URL resolution logic, compared to a @sindresorhus what if we add |
If we were to add
|
Are there any alternatives that we haven't thought of yet? For example, could we add an option that would would make |
The implementation would essentially do Both of the below would fetch ky('b', {baseUrl: 'https://foo.com/a/'});
ky('https://foo.com/a/b', {baseUrl: 'https://other.com'})
Agreed. I think the docs should recommend
Historically it's been a bit hard to determine if a URL is absolute or not in a clean and cross-platform way. That said, I recently came up with this trick, which I don't entirely hate... const isAbsoluteUrl = (url) => {
return (new URL(url, 'invalid:/')).protocol !== 'invalid:';
}; I suppose we could also borrow the implementation from is-absolute-url. So yeah, it's definitely possible to have special behavior for absolute URLs. |
These days, the way I see it is that people in a situation like sindresorhus/got#783 should use an
I'm still glad that we made the change to But since then, I have figured out a simple and reliable way to do URL resolution without those problems... const resolveUrl = (from, to) => {
const url = new URL(to || '', new URL(from || '', 'invalid:/'));
return url.protocol === 'invalid:' ? url.pathname + url.search + url.hash : url.href;
}; If we accept that sindresorhus/got#783 is a bit unusual, then maybe we could just have an option specific to that. So, we'd have |
At Got we don't allow leading slashes. Just
This might create confusion. In my opinion, the lesser options to modify the URL, the better. Or this could be done in the resolveUrl function.
How is it different from the below? if (prefixUrl) {
return new URL(input, prefixUrl);
}
return new URL(input); |
Currently, Ky also enforces a lack of a leading slash in
The way you've written it, |
Good point! I'm +1 on the |
I think we should first improve It seems we can do the following to make
Then they will only have two differences ( One concern with having two options is that if someone makes a custom Got instance, it's ambiguous at the callsite which options method is used. |
👍
Given
|
@szmarczak None. I would expect a well formed, prefixed URL: |
I agree with @haggen. |
I'm currently working on my own fetch wrapper¹, probably because I suffer from severe NIH syndrome, but mostly because none of the options I've seen fully satisfied me and I came up with a URL resolution that felt powerful and intuitive at least for me. It might work for ky as well, so here's what I thought. In my own lib I don't discriminate
Finally you can always use a hook to modify this behavior. What y'all think?
|
@haggen It's ok to link the URL resolution code in your wrapper for comparison purposes. If you mean promotion, then Ky's issue tracker is not the right place. Your proposal is the same as |
@szmarczak Oh thanks my bad. First time I read it it wasn't clear to me but now it makes sense. Then I guess I agree with the |
when I use const apiUrl = '/api' as const;
export const API: KyInstance = ky.create({
prefixUrl: apiUrl,
headers: {
...
},
});
|
Change Why? URLs that start with a slash are origin-relative. Meaning, they replace the entire path rather than add to it. |
@sholladay thx, I will try. It is vite solidjs spa, and maybe I must conf vite build |
@sholladay not working on vite proxy, I must move from ky to |
Actually, it should probably be |
Man this library is awesome, but this is the one thing arguably the most important to get right. I just changed hundreds of lines of code trying to switch from Axios and I'm stuck on this. I even removed the leading slash from everything (which felt very wrong btw), but there's no workaround for absolute URLs. I tried overriding prefixUrl to |
@alexgleason I have a branch that is intended to solve this problem and make everyone happy. Please give it a try and send us feedback. See PR #606. It is not ready to be merged yet, but it works and is fully tested. I just need to find some time to work on the docs and TS types. Here is how it works:
Having both options provides quite a bit of flexibility. However, we want to avoid any confusion between these two options, so please let me know what you think of the names and whether there's any ambiguity. |
I'm having a problem with
prefixUrl
option:Our backend API is returning absolute download paths which already include the base url. In those cases we end up with request urls having prefixed twice with the base url.
Could we have
prefixUrl
option's behavior altered so that it checks if the url already starts with this value, and if so it would not prefix it there?I tried to create a beforeRequest hook to modify request.url, but it seems the hook cannot modify that property. That approach would have averted having to check and modify urls in possibly multiple parts of the codebase.
The text was updated successfully, but these errors were encountered: