snaps icon indicating copy to clipboard operation
snaps copied to clipboard

Mock endowments for `fetch` and other common functions

Open Mrtenz opened this issue 2 years ago • 0 comments

Currently when you try to use fetch without the endowment:network-access permission, you receive the following error:

fetch is not a function

This may be confusing to Snap developers, since fetch is a global function in normal browser contexts. Instead, I suggest we inject a mock endowment for fetch and other commonly used global functions which are only usable with a permission. For example, if the Snap does not have the endowment:network-access permission, it could inject the following function instead:

function fetch(url: string) {
  throw new Error(`Failed to fetch "${url}": Using this function requires the "endowment:network-access" permission. Refer to the documentation for more information: https://docs.metamask.io/...`);
}

This gives the developer more information about how to resolve this problem.

Mrtenz avatar Nov 29 '23 12:11 Mrtenz