Comments (9)
Okay, I think I know what's happening here.
While it's true that Ky tries to pass any unknown options through to fetch (such as this non-standard proxy
option), we do so via the Request class. We don't actually pass options to fetch()
directly, but rather we pass options to new Request()
and then pass the request instance to fetch()
. I think this is what's causing the problem.
I suspect that this won't work:
const request = new Request(targetUrl, { proxy });
const response = await fetch(request);
console. log(await response.json());
That is essentially what Ky is doing. The Bun team probably didn't anticipate it would need to be passed that way.
I see two possible solutions. Both should probably be implemented but either would suffice for your particular case.
- Bun adds
proxy
support to its Request class. This would be convenient for their users more broadly, for the same reasons that Ky uses Request internally. - Ky starts passing unknown options directly to
fetch
instead of relying on the Request class to do it for us. This is the right thing for Ky to do but might prove to be a little tricky. We would need to either pass all options tofetch
, which could inadvertently override some of our options processing logic that's done with the Request class, or have some kind of filtering system to only pass unknown options to fetch, which would presumably involve some kind of hardcoded list of known options (which would have to include all options that the Request class understands) and that doesn't sound fun.
Solution 1 is likely easier to implement, so I think we should start there. File a feature request with Bun asking for proxy
support in the Request class and paste a link to the issue here.
from ky.
Ky just passes the option through to Fetch
:
The input and options are the same as fetch, with some exceptions:
from ky.
Ky just passes the option through to
Fetch
:The input and options are the same as fetch, with some exceptions:
Yes, it is, only proxy is not a native option it is a development of the Bun team, maybe that's why it doesn't work in Ky.
Anyway, please check it, it would be great if you release an update with a solution to this problem and support for Bun's fetch types in ky.
https://codesandbox.io/p/sandbox/ky-bun-3rg5wm
from ky.
Just a hunch... does it work if you don't use .json()? Ky does a bit of extra processing on the options if you use .json().
from ky.
Just a hunch... does it work if you don't use .json()? Ky does a bit of extra processing on the options if you use .json().
Without calling .text()|.json(), I can't check the response to be sure.
@sindresorhus Are you planning to address this issue)?
from ky.
What I should have said is to use response.json() instead of the ky().json() shortcut. They are different.
from ky.
What I should have said is to use response.json() instead of the ky().json() shortcut. They are different.
Show me an example, it's not clear.
from ky.
Your code example does this:
const json = await ky(targetUrl, { proxy }).json();
console.log(json);
^^ that is what we refer to as the "json shortcut". It's a shortcut because you are calling .json()
on a promise, which is normally not possible. Promises, including the one returned by fetch()
, do not have a .json()
method. But ky()
returns a special promise that has an added .json()
method for convenience.
Compare that with fetch()
, notice how you first have to await
its promise for a response before calling .json()
on the response and then you have to await
that too, since json()
returns another promise.
const response = await fetch(targetUrl, { proxy });
console.log(await response.json());
Ky's json shortcut is optional. You can still the await the response and call its .json()
method, just as you do with fetch()
. Here's what it looks like using Ky without the json shortcut:
const response = await ky(targetUrl, { proxy });
console.log(await response.json());
In theory, there should not be much difference between all of the above constructs. But Ky does do a bit of extra work when you use the json shortcut, including adding an Accept
header to the request.
I would like to know if using Ky without the json shortcut changes the result of your test, so that we can rule that out as a cause.
from ky.
I tried this option and unfortunately nothing changed, the proxies still don't work.
const response = await ky(targetUrl, { proxy });
console. log(await response.json());
from ky.
Related Issues (20)
- POST request fails in Astro.js API route HOT 5
- Header not set on node version 18.18.2 HOT 14
- [help] trying to understand why headers are not set properly HOT 1
- 1.1.1 breaks multipart form boundary HOT 6
- Passing { next: { revalidate, tags } } options to ky does not work in NextJS edge (at least with dev server) HOT 10
- Tests are hanging HOT 2
- Best way to handle Fetch errors such as Failed to fetch, NetworkError when attempting to fetch resource, etc. HOT 1
- Retries on timeout HOT 1
- TypeError: signal.throwIfAborted is not a function HOT 9
- Support for SSL client certificate in Nodejs undici HOT 1
- How to abort retry before hitting the retry limit?
- Cannot use import statement outside a module, Jest HOT 1
- Types for headers HOT 2
- How to handle HTTP Delete Method (204) with ky?
- [BUG] When Response is a string(not valid JSON), `json` method occurs error
- Cannot find module 'undici-types' or its corresponding type declarations
- ky response is an empty string sometimes HOT 3
- response.clone(...).arrayBuffer is not a function
- Content-Type detection HOT 3
- In Next.js app: Only absolute URLs are supported HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ky.