Skip to content

Commit 5d6342b

Browse files
committed
Merge branch 'fix/update-node-fetch' of https://github.com/RishikeshDarandale/unfetch into RishikeshDarandale-fix/update-node-fetch
2 parents 628c907 + 385868d commit 5d6342b

4 files changed

Lines changed: 24 additions & 13 deletions

File tree

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function r(m){return m && m.default || m;}
22
module.exports = global.fetch = global.fetch || (
33
typeof process=='undefined' ? r(require('unfetch')) : (function(url, opts) {
4-
return r(require('node-fetch'))(String(url).replace(/^\/\//g,'https://'), opts);
4+
const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
5+
return r(fetch)(String(url).replace(/^\/\//g,'https://'), opts);
56
})
67
);

packages/isomorphic-unfetch/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"main": "index.js",
1414
"types": "index.d.ts",
1515
"dependencies": {
16-
"node-fetch": "^2.6.1",
16+
"node-fetch": "^3.2.0",
1717
"unfetch": "^4.2.0"
1818
}
1919
}

packages/isomorphic-unfetch/readme.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Switches between [unfetch](https://github.com/developit/unfetch) & [node-fetch](
66

77
This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed.
88

9+
> **Note:** This module uses node-fetch 3.x, which is ES Module and requires Node >= 12.20.0.
10+
911
```sh
1012
$ npm i isomorphic-unfetch
1113
```
@@ -14,36 +16,36 @@ Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](http
1416

1517
```javascript
1618
// using ES6 modules
17-
import fetch from 'isomorphic-unfetch'
19+
import fetch from "isomorphic-unfetch";
1820

1921
// using CommonJS modules
20-
const fetch = require('isomorphic-unfetch')
22+
const fetch = require("isomorphic-unfetch");
2123
```
2224

2325
## Usage
2426

2527
As a [**ponyfill**](https://ponyfill.com):
2628

2729
```js
28-
import fetch from 'isomorphic-unfetch';
30+
import fetch from "isomorphic-unfetch";
2931

30-
fetch('/foo.json')
31-
.then( r => r.json() )
32-
.then( data => {
32+
fetch("/foo.json")
33+
.then((r) => r.json())
34+
.then((data) => {
3335
console.log(data);
3436
});
3537
```
3638

3739
Globally, as a [**polyfill**](https://ponyfill.com/#polyfill):
3840

3941
```js
40-
import 'isomorphic-unfetch';
42+
import "isomorphic-unfetch";
4143

4244
// "fetch" is now installed globally if it wasn't already available
4345

44-
fetch('/foo.json')
45-
.then( r => r.json() )
46-
.then( data => {
46+
fetch("/foo.json")
47+
.then((r) => r.json())
48+
.then((data) => {
4749
console.log(data);
4850
});
4951
```

test/isomorphic.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,15 @@ describe('isomorphic-unfetch', () => {
112112
sandbox.global.process = sandbox.process;
113113
sandbox.module = { exports: sandbox.exports };
114114
let filename = require.resolve('../packages/isomorphic-unfetch');
115-
vm.runInNewContext(fs.readFileSync(filename), sandbox, filename);
115+
vm.runInNewContext(fs.readFileSync(filename), sandbox, {
116+
filename,
117+
importModuleDynamically: () => {
118+
const module = new vm.SyntheticModule(['node-fetch'], function() {
119+
this.setExport('default', 'I AM NODE-FETCH');
120+
});
121+
return module;
122+
}
123+
});
116124

117125
expect(sandbox.module.exports('/')).toBe(modules['node-fetch']('/'));
118126
});

0 commit comments

Comments
 (0)