Closed taratatach closed 5 years ago
I was trying to come up with alternative solutions as I do not like
but since isomorphic-fetch was already imported by webpack, the polyfill already happens without this change and isomorphic-fetch is already in the bundle. The only change then is that the import is more explicit, which is a good thing. A comment explaning why isomorphic-fetch is imported would be even better.
An alternative solution would be to require the user of the library to have the polyfill. More explicit but more burden on the user. It is what pouchdb-browser does so I think we should have gone this way in cozy-client-js. This is what we do now in cozy-client. This would be a breaking change and I do not think that we want to invest more in cozy-client-js at this point.
LGTM to me.
I added a comment to explain why we import isomorphic-fetch
in offline.js
.
Cozy Desktop uses
cozy-client-js
to interact with the user's Cozy and needs to be able to operate behind a proxy as well as define a custom User-Agent per user so support can follow their requests on the server. Electron offers better support for proxy configuration than Node so Cozy Desktop useselectron-fetch
instead ofisomorphic-fetch
andelectron-proxy-agent
to customize the request's Agent with the proxy configuration. This means that Cozy Desktop needs to be able to replace thefetch
method called bycozy-client-js
as well so all requests made to the server use this custom configuration. The webpack'sprovidePlugin
plugin creates a closure with the given module around all modules calling the specified method. In our case, every module ofcozy-client-js
callingfetch
will be wrapped in a closure bindingfetch
to theisomorphic-fetch
imported bycozy-client-js
. This renders overwritting it completely impossible for Cozy Desktop and thus prevents Cozy Desktop from upgradingcozy-client-js
past version0.15.0
since the change was introduced in version0.15.1
.The proposed solution here is to import
isomorphic-fetch
which defines the globalfetch
variable in everycozy-client-js
module who needs it, like theoffline
module which doesn't make a direct use offetch
but importspouchdb-browser
who needsfetch
to be defined by the importing module or projet.