Skip to content

Commit

Permalink
Distribute as UMD
Browse files Browse the repository at this point in the history
  • Loading branch information
jayphelps committed Apr 9, 2018
1 parent fcc4e1b commit da6e028
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
37 changes: 26 additions & 11 deletions fetch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
(function(self) {
(function(root, factory) {
/* jshint strict:false */
/* globals define, module */
if (typeof define === 'function' && define.amd) {
define([], function() { return factory({}, root) })
} else if (typeof module === 'object' && module.exports) {
factory(module.exports, root)
} else {
root.WHATWGFetch = factory({}, root)
}
})(typeof self !== 'undefined' ? self : this, function(exports, self) {
'use strict';

if (self.fetch) {
return
}

var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
Expand Down Expand Up @@ -415,11 +421,11 @@
return new Response(null, {status: status, headers: {location: url}})
}

self.Headers = Headers
self.Request = Request
self.Response = Response
exports.Headers = Headers
exports.Request = Request
exports.Response = Response

self.fetch = function(input, init) {
exports.fetch = function(input, init) {
return new Promise(function(resolve, reject) {
var request = new Request(input, init)
var xhr = new XMLHttpRequest()
Expand Down Expand Up @@ -462,5 +468,14 @@
xhr.send(typeof request._bodyInit === 'undefined' ? null : request._bodyInit)
})
}
self.fetch.polyfill = true
})(typeof self !== 'undefined' ? self : this);
exports.fetch.polyfill = true

if (!self.fetch) {
self.fetch = exports.fetch
self.Headers = Headers
self.Request = Request
self.Response = Response
}

return exports
});
2 changes: 2 additions & 0 deletions script/test
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ if [ -n "$SAUCE_BROWSER" ]; then
else
./script/phantomjs
fi

./node_modules/mocha/bin/mocha test/cjs.js
13 changes: 13 additions & 0 deletions test/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/* global describe, it, require */

var isFunction = require('chai').assert.isFunction
var WHATWGFetch = require('../')

describe('Common JS exports', function() {
it('should provide all the CJS exports', function() {
isFunction(WHATWGFetch.fetch)
isFunction(WHATWGFetch.Headers)
isFunction(WHATWGFetch.Request)
isFunction(WHATWGFetch.Response)
})
})

0 comments on commit da6e028

Please sign in to comment.