solana-labs / solana

Web-Scale Blockchain for fast, secure, scalable, decentralized apps and marketplaces.
https://solanalabs.com
Apache License 2.0
13.02k stars 4.19k forks source link

Web3.js - Fetch Middleware custom client #22429

Closed arguiot closed 2 years ago

arguiot commented 2 years ago

Problem

Currently, the fetch client is cross-fetch which is fine in most situations, but in some cases, it can be very problematic. Personally, I'm trying to use web3.js on Cloudflare Workers which has a custom fetch client. In other cases, like React Native, the library may also not work, see: https://github.com/solana-labs/solana/issues/22421

My point is that there should be an option to use another fetch function.

Proposed Solution

Currently, there is a Middleware option in the Connection constructor. The issue with this middleware is that it's very limited as we have to use the provided fetch function as a callback. I propose to have a FetchMiddleware function like that:

const fetchMiddleware = async (url, options): Promise<Response> => {
    // Your code, but here's how you would use it:
   return await fetch(url, options)
}

It really shouldn't be hard to implement it. I can probably do it, but I prefer to wait for feedback.

Haianh9999 commented 2 years ago

@jstarry please check this PR it's very important to our project

steveluscher commented 2 years ago

I think this has been addressed? Please feel free to file another issue if there's more to do here.