mistval / node-fetch-cache

Node-fetch with built-in response caching.
MIT License
51 stars 8 forks source link

Use native fetch api #31

Open hoikin opened 1 year ago

hoikin commented 1 year ago

Hello there,

Is that possible to use native fetch api if available instead of node-fetch?

mistval commented 1 year ago

Hi, that's a good question. I did some quick testing and it seems like it should be possible, but might not be straightforward. To confirm, you mean the new native NodeJS fetch API, right? Or browser?

hoikin commented 1 year ago

Hi mistval,

Yes, I refer to the node native fetch API. See would it be possible, thanks a lot!

https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch

glensc commented 6 months ago

I guess this is the way to do it:

mistval commented 6 months ago

Yes that's true, implementing that would also offer a path to resolving this ask.

node-fetch-cache is pretty coupled to node-fetch at the moment (for example calling the constructor of node-fetch's Response class). Probably will need to refactor node-fetch-cache to have its own Response class which is more generic while being substitutable for responses from both node-fetch and the native fetch API in all the important ways (which is an important goal of this library, being drop-in replaceable for node-fetch).

glensc commented 6 months ago

I even had trouble matching types of node-fetch and node-fetch-cache with typescript!

loosely explained is that node-fetch returned fetch-like function with properties attached to the function. and node-fetch-cache returned object that have more functions than needed. (or maybe it was vice versa? )if needed I can dig up the errors.

mistval commented 6 months ago

Sure, I can imagine what you mean but it would be good to see what you're doing exactly. The response from node-fetch-cache should be assignable to the response type of node-fetch, but the fetch function itself might not be assignable to the type of node-fetch's fetch function, though I think it should be possible to make it so.

glensc commented 6 months ago

Looks like I've published the code, so you can just look at the result of the massive hack:

it's used like this:

the typing now works, but the problem is that it doesn't work in the environment I plan to run it (bun):