File tree Expand file tree Collapse file tree
packages/isomorphic-unfetch Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11function r ( m ) { return m && m . default || m ; }
22module . 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) ;
Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ Switches between [unfetch](https://github.com/developit/unfetch) & [node-fetch](
66
77This 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
2527As 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
3739Globally, 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```
Original file line number Diff line number Diff 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 } ) ;
You can’t perform that action at this time.
0 commit comments