node-fetch-cache icon indicating copy to clipboard operation
node-fetch-cache copied to clipboard

Use native fetch api

Open hoikin opened this issue 2 years ago • 9 comments

Hello there,

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

hoikin avatar May 07 '23 04:05 hoikin

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?

mistval avatar May 07 '23 10:05 mistval

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

hoikin avatar May 08 '23 00:05 hoikin

I guess this is the way to do it:

  • https://github.com/mistval/node-fetch-cache/issues/34

glensc avatar Jan 25 '24 17:01 glensc

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).

mistval avatar Jan 28 '24 18:01 mistval

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.

glensc avatar Jan 30 '24 21:01 glensc

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.

mistval avatar Jan 30 '24 21:01 mistval

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

  • https://github.com/glensc/comics-mailer/blob/90c29c38791139822cf96e3deb3550a806c59676/src/core/types.ts

it's used like this:

  • https://github.com/glensc/comics-mailer/blob/90c29c38791139822cf96e3deb3550a806c59676/src/core/HttpClient.ts

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

  • https://github.com/oven-sh/bun/issues/8487

glensc avatar Jan 31 '24 07:01 glensc