greymass / ual-anchor

Identity and session through ESR using EOSIO/universal-authenticator-library
MIT License
23 stars 16 forks source link

Unable to pass fetch provider to eosio core as option #31

Closed mvdschee closed 2 years ago

mvdschee commented 2 years ago
 ERROR  Cannot start nuxt:  Missing fetch                             22:33:43

  at new FetchProvider (node_modules/@greymass/eosio/lib/eosio-core.js:3510:23)
  at new Anchor (node_modules/ual-anchor/dist/Anchor.js:72:30)
  at node_modules/nuxt3-ual/module.mjs:13:19
  at Array.map (<anonymous>)
  at setup (node_modules/nuxt3-ual/module.mjs:12:28)
  at Object.wrappedModule (node_modules/@nuxt/kit/dist/index.mjs:1249:22)
  at installModule (node_modules/@nuxt/kit/dist/index.mjs:1298:17)
  at async initNuxt (node_modules/nuxt3/dist/index.mjs:917:5)
  at async load (node_modules/nuxi/dist/chunks/dev.mjs:6713:9)
  at async Object.invoke (node_modules/nuxi/dist/chunks/dev.mjs:6752:5)

I'm currently running into an issue that the underlaying package eosio-core.js doesn't work because the fetch provider is missing. It's an option in eosio to provide fetch in the options, but ual-anchor doesn't let us set those options.

It would be nice to have a clean way to pass through those options.

Currently I got it working by setting global.fetch but this pollutes the global scope.

aaroncox commented 2 years ago

It's not in the documentation all that well, but I think you should be able to pass a client to the UAL plugin, which can use a custom fetch provider.

The client that needs to be passed in is an APIClient from the eosio library, which you'd pass a custom FetchProvider to, like so:

import {APIClient, FetchProvider} from '@greymass/eosio'

const fetch = require('node-fetch')

export const provider = new FetchProvider('https://eos.greymass.com', {fetch})
export const client = new APIClient({provider})

node-fetch can be replaced by whichever fetch compatible library you are using.

The client in the example above can then be passed into ual-anchor instance that you're using in UAL. An example of where it's passed in can be found in one of our demos:

https://github.com/greymass/ual-reactjs-renderer-demo-multipass/blob/59525e22ab3162aadddb80aac18907f10aff7e05/src/App.js#L57-L59

mvdschee commented 2 years ago

Thank you that works :)