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

adding isomorphic support #31

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 1 addition & 13 deletions examples/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,8 @@
<head>
<meta charset="utf-8">
<script src="../fetch.js"></script>
<script src="main.js"></script>
</head>
<body>
<script>
var result = fetch('https://github.com')

result.then(function(response) {
console.log('response', response)
console.log('header', response.headers.get('Content-Type'))
return response.text()
}).then(function(text) {
console.log('got text', text)
}).catch(function(ex) {
console.log('failed', ex)
})
</script>
</body>
</html>
15 changes: 15 additions & 0 deletions examples/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
if (typeof fetch === 'undefined') {
fetch = require('../fetch').fetch;
}

var result = fetch('http://y.ucs.ricoh.co.jp:3000');

result.then(function(response) {
console.log('response', response)
console.log('header', response.headers.get('Content-Type'))
return response.text()
}).then(function(text) {
console.log('got text', text)
}).catch(function(ex) {
console.log('failed', ex)
})
18 changes: 14 additions & 4 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
(function() {
(function(global) {
'use strict';

if (window.fetch) {
if (global.fetch) {
return
}

var Promise = global.Promise;
if (typeof Promise === 'undefined') {
Promise = require('bluebird');
Copy link
Contributor

Choose a reason for hiding this comment

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

This will mean that bluebird's code will always be added to this file when it's browserified and even delivered to browsers that already support Promises… This will make the compiled code a lot bigger — not sure you want to do it like this… I've written an alternative suggestion in the comments.

}

var XMLHttpRequest = global.XMLHttpRequest;
if (typeof XMLHttpRequest === 'undefined') {
XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
Copy link
Contributor

Choose a reason for hiding this comment

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

Again I suspect this might bloat the client side code when compiled with browserify – you might be shipping an XMLHttpRequest polyfill to browsers… which I don't think would ever be used

}

function Headers(headers) {
this.map = {}

Expand Down Expand Up @@ -189,7 +199,7 @@

Body.call(Response.prototype)

window.fetch = function (url, options) {
global.fetch = function (url, options) {
return new Request(url, options).fetch()
}
})();
})(this);
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@
"bower": "1.3.8",
"jshint": "2.5.2",
"node-qunit-phantomjs": "0.2.2"
},
"dependencies": {
"bluebird": "^2.3.10",
"xmlhttprequest": "^1.6.0"
}
}