Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

require('isomorphic-fetch/safe') to not set globals #22

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"node": true,
"browser": true,
"predef": ["describe", "it", "before"]
"predef": ["describe", "it", "before", "after"]
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ fetch('//offline-news-api.herokuapp.com/stories')
});
```

If you don't want a global:

```js
var fetch = require('isomorphic-fetch/safe');
typeof global.fetch === 'undefined'
```

## License

All open source code released by FT Labs is licenced under the MIT licence. Based on [the fine work by](https://github.com/github/fetch/pull/31) **[jxck](https://github.com/Jxck)**.
13 changes: 13 additions & 0 deletions fetch-browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// the whatwg-fetch polyfill installs the fetch() function
// on the global object (window or self)
var fetchWasDefined = 'fetch' in global;
var originalGlobalFetch = global.fetch;

require('whatwg-fetch');
module.exports = fetch;

if (fetchWasDefined) {
global.fetch = originalGlobalFetch;
} else {
delete global.fetch;
}
6 changes: 6 additions & 0 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = function(url, options) {
if (/^\/\//.test(url)) {
url = 'https:' + url;
}
return require('node-fetch').call(this, url, options);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend caching this require call outside of the function and ensure it closes over it. Without this change, I believe you would be hitting the synchronous require with each fetch.

};
6 changes: 0 additions & 6 deletions npm-client.js

This file was deleted.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"name": "isomorphic-fetch",
"version": "0.0.0",
"description": "Isomorphic WHATWG Fetch API, for Node & Browserify",
"browser": "npm-client.js",
"browser": {
"./fetch.js": "./fetch-browser.js"
},
"main": "server.js",
"scripts": {
"files": "find . -name '*.js' ! -path './node_modules/*' ! -path './bower_components/*'",
Expand Down
2 changes: 2 additions & 0 deletions safe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
'use strict';
module.exports = require('./fetch');
8 changes: 1 addition & 7 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
"use strict";

var realFetch = require('node-fetch');
module.exports = function(url, options) {
if (/^\/\//.test(url)) {
url = 'https:' + url;
}
return realFetch.call(this, url, options);
};
module.exports = require('./fetch');

if (!global.fetch) {
global.fetch = module.exports;
Expand Down
25 changes: 25 additions & 0 deletions test/safe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
"use strict";
var expect = require('chai').expect;

describe('fetch/safe', function () {
var restoreGlobalFetch;
before(function () {
var globalFetch = global.fetch;
if (globalFetch) {
delete global.fetch;
restoreGlobalFetch = function () {
global.fetch = globalFetch;
};
}
});
after(function () {
if (typeof restoreGlobalFetch === 'function') {
restoreGlobalFetch();
}
});
it('should not create a global', function () {
var safeFetch = require('../safe');
expect(typeof safeFetch).to.eql('function');
expect(typeof fetch).to.eql('undefined');
});
});