Skip to content
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

Pass fetch options to browser providers #3895

Open
stephenlacy opened this issue Mar 13, 2023 · 4 comments
Open

Pass fetch options to browser providers #3895

stephenlacy opened this issue Mar 13, 2023 · 4 comments
Labels
enhancement New feature or improvement. minor-bump Planned for the next minor version bump. next-patch Issues scheduled for the next arch release. on-deck This Enhancement or Bug is currently being worked on. v6 Issues regarding v6

Comments

@stephenlacy
Copy link

stephenlacy commented Mar 13, 2023

Describe the Feature

Currently when communicating with RPC endpoints (or gateways) additional fetch options are not able to be passed in:

For instance, cors or credentials (fetch credentials vs Authorization header) options.

Code Example

const fetchReq = new ethers.FetchRequest(URL, { credentials: "include" });
const provider = new ethers.JsonRpcProvider(fetchReq);
@stephenlacy stephenlacy added the enhancement New feature or improvement. label Mar 13, 2023
@corbanbrook
Copy link

Im in the same boat, migrating a project from ethers 5.7 to 6.

previously was sending the below fetchOptions and not sure how to do this now with the new FetchRequest:

     fetchOptions: {
       mode: 'cors',
       cache: 'force-cache',
       credentials: 'same-origin',
       redirect: 'follow',
       referrer: 'client'
     }

@ricmoo
Copy link
Member

ricmoo commented Feb 22, 2024

In v6 the defaults are used, and redirects are handled by the FetchRequest class, but it definitely makes sense to allow overriding those in the createGetUrlFunc.

I’ll mark this for the next minor patch and include a code snippet on using it.

@ricmoo ricmoo added on-deck This Enhancement or Bug is currently being worked on. minor-bump Planned for the next minor version bump. v6 Issues regarding v6 next-patch Issues scheduled for the next arch release. labels Feb 22, 2024
@ricmoo
Copy link
Member

ricmoo commented Jan 23, 2025

This has been added into the WIP branch for 6.14.

To use custom fetch init options, you can either:

// Modify only one request object, and its clones (such as used by JsonRpcProvider)
const request = new FetchRequest(url);
request.getUrlFunc = FetchRequest.createGetUrlFunc({ mode: "cors", ... });
const provider = new JsonRpcProvider(request);

Or:

// Modify ALL requests made by anything through FethcRequest
FetchRequest.registerGetUrl(FetchRequest.createGetUrlFunc({ mode: "cors", ... }))

The init properties method, headers, body and signal cannot be overridden, as getUrl uses these internally, but provides API means to modify these in FetchRequest.

Any feedback is welcome. :)

@tornadocontrib
Copy link

@ricmoo Would be great if you could also export entire fetch function as well #4929.

Also, please make a look to this old issue #4686 as it sends duplicated requests on BSC ( and slow downs sending transactions )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or improvement. minor-bump Planned for the next minor version bump. next-patch Issues scheduled for the next arch release. on-deck This Enhancement or Bug is currently being worked on. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

4 participants