developit / unfetch

🐕 Bare minimum 500b fetch polyfill.
https://npm.im/unfetch
MIT License
5.71k stars 200 forks source link

got url.replace is not a function #140

Closed Edorka closed 4 years ago

Edorka commented 4 years ago

I'm getting this error while doing a yarn build on a preact-cli project Complete message is

✖ ERROR TypeError: url.replace is not a function
    at /home/edorka/develop/nsp/node_modules/isomorphic-unfetch/index.js:3:36

Probably this is because code doesn't expect to get URL as an object this would be a quick fix:

module.exports = global.fetch = global.fetch || (
    typeof process=='undefined' ? (require('unfetch').default || require('unfetch')) : (function(url, opts) {
                const target = url.href ? url.href : url;
        return require('node-fetch')(target.replace(/^\/\//g,'https://'), opts);
    })
);
developit commented 4 years ago

Hiya! You're right, Unfetch isn't properly anticipating a URL object.

The fix is actually even smaller than checking .href - URL.toString() returns the URL string, so we can just do url += '' to cast it to a string.