From 7ab75e1ac56fa0591f62db7b40dc5eee3b0db957 Mon Sep 17 00:00:00 2001 From: Jxck Date: Fri, 7 Nov 2014 14:05:43 +0900 Subject: [PATCH 1/3] add isomorphic support --- fetch.js | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/fetch.js b/fetch.js index 186dc585..4bf2ae25 100644 --- a/fetch.js +++ b/fetch.js @@ -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'); + } + + var XMLHttpRequest = global.XMLHttpRequest; + if (typeof XMLHttpRequest === 'undefined') { + XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; + } + function Headers(headers) { this.map = {} @@ -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); From 7002315c974c7a21ffa45b2ff562ee9c554ca01b Mon Sep 17 00:00:00 2001 From: Jxck Date: Fri, 7 Nov 2014 14:06:07 +0900 Subject: [PATCH 2/3] export js to main.js in example --- examples/index.html | 14 +------------- examples/main.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 examples/main.js diff --git a/examples/index.html b/examples/index.html index 399b5053..920787d5 100644 --- a/examples/index.html +++ b/examples/index.html @@ -3,20 +3,8 @@ + - diff --git a/examples/main.js b/examples/main.js new file mode 100644 index 00000000..1f202f4b --- /dev/null +++ b/examples/main.js @@ -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) +}) From 0d3a2d00e61e086776c231b8364b78216ec48e6f Mon Sep 17 00:00:00 2001 From: Jxck Date: Fri, 7 Nov 2014 14:07:45 +0900 Subject: [PATCH 3/3] add xhr, promise dependencies to package.json --- package.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/package.json b/package.json index f6eb5d3b..342bd3b3 100644 --- a/package.json +++ b/package.json @@ -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" } }