From a0e1580665d30bc97720255cd3ae02e8ba3a6e17 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 28 Jun 2017 11:13:17 -0400 Subject: [PATCH 01/29] Account for ES Module bundlers in isomorphic-unfetch and respect existence of window.fetch --- packages/isomorphic-unfetch/browser.js | 2 +- packages/isomorphic-unfetch/index.js | 8 +- test/isomorphic.js | 121 +++++++++++++++++++++++++ 3 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 test/isomorphic.js diff --git a/packages/isomorphic-unfetch/browser.js b/packages/isomorphic-unfetch/browser.js index 52ef4b9..13b55c2 100644 --- a/packages/isomorphic-unfetch/browser.js +++ b/packages/isomorphic-unfetch/browser.js @@ -1 +1 @@ -module.exports = window.fetch = require('unfetch'); +module.exports = window.fetch || (window.fetch = require('unfetch').default || require('unfetch')); diff --git a/packages/isomorphic-unfetch/index.js b/packages/isomorphic-unfetch/index.js index 0ea2afb..6ae374c 100644 --- a/packages/isomorphic-unfetch/index.js +++ b/packages/isomorphic-unfetch/index.js @@ -1,3 +1,5 @@ -module.exports = global.fetch = typeof process=='undefined' ? require('unfetch') : (function(url, opts) { - return require('node-fetch')(url.replace(/^\/\//g,'https://'), opts); -}); +module.exports = global.fetch = global.fetch || ( + typeof process=='undefined' ? (require('unfetch').default || require('unfetch')) : (function(url, opts) { + return require('node-fetch')(url.replace(/^\/\//g,'https://'), opts); + }) +); diff --git a/test/isomorphic.js b/test/isomorphic.js new file mode 100644 index 0000000..0ef26a2 --- /dev/null +++ b/test/isomorphic.js @@ -0,0 +1,121 @@ +import { expect } from 'chai'; +import fs from 'fs'; +import vm from 'vm'; + +describe('isomorphic-unfetch', () => { + describe('"browser" entry', () => { + it('should resolve to fetch when window.fetch exists', () => { + function fetch() { return this } + function unfetch() {} + + let sandbox = { + process: undefined, + window: { fetch }, + fetch, + exports: {}, + require: () => unfetch + }; + sandbox.global = sandbox.window; + sandbox.module = { exports: sandbox.exports }; + let filename = require.resolve('../packages/isomorphic-unfetch/browser'); + vm.runInNewContext(fs.readFileSync(filename), sandbox, filename); + + expect(sandbox.module.exports).to.equal(fetch); + }); + + it('should resolve to unfetch when window.fetch does not exist', () => { + function unfetch() {} + + let sandbox = { + process: undefined, + window: {}, + exports: {}, + require: () => unfetch + }; + sandbox.global = sandbox.window; + sandbox.module = { exports: sandbox.exports }; + let filename = require.resolve('../packages/isomorphic-unfetch/browser'); + vm.runInNewContext(fs.readFileSync(filename), sandbox, filename); + + expect(sandbox.module.exports).to.equal(unfetch); + }); + }); + + describe('"main" entry', () => { + it('should resolve to fetch when window.fetch exists', () => { + function fetch() { return this } + function unfetch() {} + + let sandbox = { + process: undefined, + window: { fetch }, + fetch, + exports: {}, + require: () => unfetch + }; + sandbox.global = sandbox.window; + sandbox.module = { exports: sandbox.exports }; + let filename = require.resolve('../packages/isomorphic-unfetch'); + vm.runInNewContext(fs.readFileSync(filename), sandbox, filename); + + expect(sandbox.module.exports).to.equal(fetch); + }); + + it('should resolve to unfetch when window.fetch does not exist', () => { + function unfetch() {} + + let sandbox = { + process: undefined, + window: {}, + exports: {}, + require: () => unfetch + }; + sandbox.global = sandbox.window; + sandbox.module = { exports: sandbox.exports }; + let filename = require.resolve('../packages/isomorphic-unfetch'); + vm.runInNewContext(fs.readFileSync(filename), sandbox, filename); + + expect(sandbox.module.exports).to.equal(unfetch); + }); + }); + + + describe('"main" entry in NodeJS', () => { + it('should resolve to fetch when window.fetch exists', () => { + function fetch() { return this } + function unfetch() {} + + let sandbox = { + process: {}, + global: { fetch }, + exports: {}, + require: () => unfetch + }; + sandbox.module = { exports: sandbox.exports }; + let filename = require.resolve('../packages/isomorphic-unfetch'); + vm.runInNewContext(fs.readFileSync(filename), sandbox, filename); + + expect(sandbox.module.exports).to.equal(fetch); + }); + + it('should resolve to unfetch when window.fetch does not exist', () => { + let modules = { + unfetch() {}, + 'node-fetch': function nodeFetch() { return 'I AM NODE-FETCH'; } + }; + + let sandbox = { + process: {}, + global: {}, + exports: {}, + require: (module) => modules[module] + }; + sandbox.global.process = sandbox.process; + sandbox.module = { exports: sandbox.exports }; + let filename = require.resolve('../packages/isomorphic-unfetch'); + vm.runInNewContext(fs.readFileSync(filename), sandbox, filename); + + expect(sandbox.module.exports('/')).to.equal(modules['node-fetch']('/')); + }); + }); +}); From 5055eb37b1bd3e69f624cf6585ea4478a5f6921a Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Wed, 28 Jun 2017 11:15:10 -0400 Subject: [PATCH 02/29] isomorphic-unfetch@2.0.0 --- packages/isomorphic-unfetch/package.json | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index ba0aec0..3b68549 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -1,10 +1,11 @@ { "name": "isomorphic-unfetch", + "version": "2.0.0", "description": "Switches between unfetch & node-fetch for client & server.", "browser": "browser.js", "main": "index.js", "dependencies": { - "node-fetch": "^1.6.3", - "unfetch": "^2.1.0" + "node-fetch": "^1.7.1", + "unfetch": "^3.0.0" } } From ca691cad3b5c746525cb1b510e47a4474b9f61c7 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Sat, 9 Sep 2017 21:42:44 +0200 Subject: [PATCH 03/29] Add readme for isomorphic-unfetch --- packages/isomorphic-unfetch/readme.md | 53 +++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 packages/isomorphic-unfetch/readme.md diff --git a/packages/isomorphic-unfetch/readme.md b/packages/isomorphic-unfetch/readme.md new file mode 100644 index 0000000..9545479 --- /dev/null +++ b/packages/isomorphic-unfetch/readme.md @@ -0,0 +1,53 @@ +# Isomorphic Unfetch + +Switches between [unfetch](https://github.com/developit/unfetch) & [node-fetch](https://github.com/bitinn/node-fetch) for client & server. + +## Install + +This project uses [node](http://nodejs.org) and [npm](https://npmjs.com). Go check them out if you don't have them locally installed. + +```sh +$ npm install --save isomorphic-unfetch +``` + +Then with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.js.org/), use as you would anything else: + +```javascript +// using ES6 modules +import fetch from 'isomorphic-unfetch' + +// using CommonJS modules +var fetch = require('isomorphic-unfetch') +``` + +## Usage + +As a [**ponyfill**](https://ponyfill.com): + +```js +import fetch from 'isomorphic-unfetch'; + +fetch('/foo.json') + .then( r => r.json() ) + .then( data => { + console.log(data); + }); +``` + +Globally, as a [**polyfill**](https://ponyfill.com/#polyfill): + +```js +import 'isomorphic-unfetch'; + +// "fetch" is now installed globally if it wasn't already available + +fetch('/foo.json') + .then( r => r.json() ) + .then( data => { + console.log(data); + }); +``` + +## License + +[MIT License](LICENSE.md) © [Jason Miller](https://jasonformat.com/) From 7bba4f473af2693ad0b8bedf983a901afe1aedb8 Mon Sep 17 00:00:00 2001 From: johnl Date: Sat, 23 Sep 2017 17:38:04 +0200 Subject: [PATCH 04/29] fix small typo (#51) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a27b5f8..44e29ac 100644 --- a/README.md +++ b/README.md @@ -115,7 +115,7 @@ fetch('/bear', { ``` ## API -While one of Unfetch's goals is to provide a familiar interface, it's API may differ from other `fetch` polyfills/ponyfills. +While one of Unfetch's goals is to provide a familiar interface, its API may differ from other `fetch` polyfills/ponyfills. One of the key differences is that Unfetch focuses on implementing the [`fetch()` API](https://fetch.spec.whatwg.org/#fetch-api), while offering minimal (yet functional) support to the other sections of the [Fetch spec](https://fetch.spec.whatwg.org/), like the [Headers class](https://fetch.spec.whatwg.org/#headers-class) or the [Response class](https://fetch.spec.whatwg.org/#response-class). Unfetch's API is organized as follows: From 68fef152dfc90c1e90f05a18511c47374a16d848 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Sun, 24 Sep 2017 22:13:45 -0500 Subject: [PATCH 05/29] New: add TypeScript definitions Alias unfetch to ES6's fetch definition: declare function fetch(input: RequestInfo, init?: RequestInit): Promise; This may not be correct. --- package.json | 5 ++++- src/index.d.ts | 5 +++++ typings.json | 6 ++++++ 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 src/index.d.ts create mode 100644 typings.json diff --git a/package.json b/package.json index 14c0280..cba2496 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "umd:main": "dist/unfetch.umd.js", "scripts": { "test": "eslint src test && mocha --compilers js:babel-register test/**/*.js", - "build": "npm-run-all --silent clean -p rollup:* -p minify:* -s size", + "build": "npm-run-all --silent clean -p rollup:* -p minify:* -s size -p copy-typescript-definition", "clean": "rimraf dist && mkdirp dist", "rollup:cjs": "cross-env FORMAT=cjs rollup -c rollup.config.js -f cjs -n $npm_package_name -o $npm_package_main", "rollup:umd": "cross-env FORMAT=umd rollup -c rollup.config.js -f umd -n $npm_package_name -o $npm_package_umd_main", @@ -16,6 +16,7 @@ "minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map", "minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map", "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size)\"", + "copy-typescript-definition": "copyfiles -f src/index.d.ts dist", "prepublish": "npm run -s build", "release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, @@ -31,6 +32,7 @@ "Jason Miller " ], "license": "MIT", + "typings": "src/index.d.ts", "files": [ "src", "dist", @@ -56,6 +58,7 @@ "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.9.0", "chai": "^3.5.0", + "copyfiles": "^1.2.0", "cross-env": "^3.1.4", "eslint": "^3.13.1", "gzip-size-cli": "^1.0.0", diff --git a/src/index.d.ts b/src/index.d.ts new file mode 100644 index 0000000..7dd0676 --- /dev/null +++ b/src/index.d.ts @@ -0,0 +1,5 @@ +declare const unfetch: typeof fetch; + +declare module "isomorphic-unfetch" { + export = unfetch; +} diff --git a/typings.json b/typings.json new file mode 100644 index 0000000..dae3c9d --- /dev/null +++ b/typings.json @@ -0,0 +1,6 @@ + +{ + "name": "isomorphic-unfetch", + "main": "src/index.d.ts", + "version": false +} From db1866e29fa521a5c904f15a3bf020b4f02f1755 Mon Sep 17 00:00:00 2001 From: nikoladev Date: Fri, 29 Sep 2017 14:13:35 +0200 Subject: [PATCH 06/29] fix response.ok to equal true only for status codes in the 200 range --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index a5d2e06..dd6a2a0 100644 --- a/src/index.js +++ b/src/index.js @@ -33,7 +33,7 @@ export default typeof fetch=='function' ? fetch.bind() : function(url, options) }); return { - ok: (request.status/200|0) == 1, // 200-299 + ok: (request.status/100|0) == 2, // 200-299 status: request.status, statusText: request.statusText, url: request.responseURL, From eb059643bbfc7c8321814cafe8c9d59d329d22c6 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Sat, 14 Oct 2017 13:13:49 -0400 Subject: [PATCH 07/29] codesponsor.io --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 44e29ac..a5c7980 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,10 @@ * * * + + Sponsor + + ## Table of Contents - [Install](#install) From 66d149b686eb954879880057e5efcd000097f9a4 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Sun, 22 Oct 2017 16:19:17 -0500 Subject: [PATCH 08/29] Fix: rename type definitions to just "unfetch" Rename the TSDs from "isomorphic-unfetch" to "unfetch". --- src/index.d.ts | 2 +- typings.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/index.d.ts b/src/index.d.ts index 7dd0676..7c9c278 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,5 +1,5 @@ declare const unfetch: typeof fetch; -declare module "isomorphic-unfetch" { +declare module "unfetch" { export = unfetch; } diff --git a/typings.json b/typings.json index dae3c9d..1ba4ace 100644 --- a/typings.json +++ b/typings.json @@ -1,6 +1,6 @@ { - "name": "isomorphic-unfetch", + "name": "unfetch", "main": "src/index.d.ts", "version": false } From 59ef2b4dbb563724ab459f71aeeac6ba6cd64dc1 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Sun, 22 Oct 2017 17:24:19 -0500 Subject: [PATCH 09/29] Fix: TypeScript definitions for isomorphic-unfetch The unfetch package has a subpackage, isomorphic-unfetch, which requires its own typings. --- packages/isomorphic-unfetch/index.d.ts | 5 +++++ packages/isomorphic-unfetch/package.json | 1 + packages/isomorphic-unfetch/typings.json | 6 ++++++ 3 files changed, 12 insertions(+) create mode 100644 packages/isomorphic-unfetch/index.d.ts create mode 100644 packages/isomorphic-unfetch/typings.json diff --git a/packages/isomorphic-unfetch/index.d.ts b/packages/isomorphic-unfetch/index.d.ts new file mode 100644 index 0000000..7dd0676 --- /dev/null +++ b/packages/isomorphic-unfetch/index.d.ts @@ -0,0 +1,5 @@ +declare const unfetch: typeof fetch; + +declare module "isomorphic-unfetch" { + export = unfetch; +} diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index 3b68549..6a9be02 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -4,6 +4,7 @@ "description": "Switches between unfetch & node-fetch for client & server.", "browser": "browser.js", "main": "index.js", + "typings": "index.d.ts", "dependencies": { "node-fetch": "^1.7.1", "unfetch": "^3.0.0" diff --git a/packages/isomorphic-unfetch/typings.json b/packages/isomorphic-unfetch/typings.json new file mode 100644 index 0000000..5413816 --- /dev/null +++ b/packages/isomorphic-unfetch/typings.json @@ -0,0 +1,6 @@ + +{ + "name": "isomorphic-unfetch", + "main": "index.d.ts", + "version": false +} From 12b15b281d75f19ab121cef6e070fa5ed2d84722 Mon Sep 17 00:00:00 2001 From: Daniel Ruf Date: Sun, 3 Dec 2017 07:48:02 +0100 Subject: [PATCH 10/29] remove CodeSponsor (#56) see https://codesponsor.io/shutdown-notice/ --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index a5c7980..44e29ac 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,6 @@ * * * - - Sponsor - - ## Table of Contents - [Install](#install) From ea17f1714c9bf55b8f4a326491f4dbc4ba3740c0 Mon Sep 17 00:00:00 2001 From: Arthur Stolyar Date: Mon, 11 Dec 2017 01:13:05 +0300 Subject: [PATCH 11/29] Fix xhr request being sync by default --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index dd6a2a0..cdfc703 100644 --- a/src/index.js +++ b/src/index.js @@ -3,7 +3,7 @@ export default typeof fetch=='function' ? fetch.bind() : function(url, options) return new Promise( (resolve, reject) => { let request = new XMLHttpRequest(); - request.open(options.method || 'get', url); + request.open(options.method || 'get', url, true); for (let i in options.headers) { request.setRequestHeader(i, options.headers[i]); From b003956b7dde271b0f7aac490ff82f13b64d9b9f Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 11 Dec 2017 09:25:40 -0500 Subject: [PATCH 12/29] Update README with link to isomorphic-unfetch --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 44e29ac..650f45f 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ > > - Uses simple Arrays instead of Iterables, since Arrays _are_ iterables > - No streaming, just Promisifies existing XMLHttpRequest response bodies +> - No server-side usage, see [isomorphic-unfetch](https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch) if you need to support Node.js * * * From 7ffded9a13b8380e00a6ba44a04e9216678e090a Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Tue, 12 Dec 2017 12:50:42 -0500 Subject: [PATCH 13/29] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 650f45f..06dd24b 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ > > - Uses simple Arrays instead of Iterables, since Arrays _are_ iterables > - No streaming, just Promisifies existing XMLHttpRequest response bodies -> - No server-side usage, see [isomorphic-unfetch](https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch) if you need to support Node.js +> - Use in Node.JS is handled by [isomorphic-unfetch](https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch) * * * From e41c85281d45a5f8a0716cd65052997029556da3 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Fri, 15 Dec 2017 17:42:30 -0600 Subject: [PATCH 14/29] Remove typings.json files These are outdated and unnecessary. --- packages/isomorphic-unfetch/typings.json | 6 ------ typings.json | 6 ------ 2 files changed, 12 deletions(-) delete mode 100644 packages/isomorphic-unfetch/typings.json delete mode 100644 typings.json diff --git a/packages/isomorphic-unfetch/typings.json b/packages/isomorphic-unfetch/typings.json deleted file mode 100644 index 5413816..0000000 --- a/packages/isomorphic-unfetch/typings.json +++ /dev/null @@ -1,6 +0,0 @@ - -{ - "name": "isomorphic-unfetch", - "main": "index.d.ts", - "version": false -} diff --git a/typings.json b/typings.json deleted file mode 100644 index 1ba4ace..0000000 --- a/typings.json +++ /dev/null @@ -1,6 +0,0 @@ - -{ - "name": "unfetch", - "main": "src/index.d.ts", - "version": false -} From 26854c8a3cdc36b9902d6494794b8dce87158795 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Fri, 15 Dec 2017 17:44:19 -0600 Subject: [PATCH 15/29] Update package TypeScript definition syntax Specify TypeScript definitions location using "types" instead of "typings", which is out of fashion. --- package.json | 2 +- packages/isomorphic-unfetch/package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index cba2496..2a9bfe2 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "Jason Miller " ], "license": "MIT", - "typings": "src/index.d.ts", + "types": "src/index.d.ts", "files": [ "src", "dist", diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index 6a9be02..24c524e 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -4,7 +4,7 @@ "description": "Switches between unfetch & node-fetch for client & server.", "browser": "browser.js", "main": "index.js", - "typings": "index.d.ts", + "types": "index.d.ts", "dependencies": { "node-fetch": "^1.7.1", "unfetch": "^3.0.0" From 8cf18efe218423c27ae65c56f738e99532e872a6 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Fri, 15 Dec 2017 17:46:33 -0600 Subject: [PATCH 16/29] Remove unnecessary TypeScript definitions file copy TSDs do not need to appear under the build products. They can be (and are) referenced directly in sources. --- package.json | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index 2a9bfe2..34ea0d3 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "umd:main": "dist/unfetch.umd.js", "scripts": { "test": "eslint src test && mocha --compilers js:babel-register test/**/*.js", - "build": "npm-run-all --silent clean -p rollup:* -p minify:* -s size -p copy-typescript-definition", + "build": "npm-run-all --silent clean -p rollup:* -p minify:* -s size", "clean": "rimraf dist && mkdirp dist", "rollup:cjs": "cross-env FORMAT=cjs rollup -c rollup.config.js -f cjs -n $npm_package_name -o $npm_package_main", "rollup:umd": "cross-env FORMAT=umd rollup -c rollup.config.js -f umd -n $npm_package_name -o $npm_package_umd_main", @@ -16,7 +16,6 @@ "minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map", "minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map", "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size)\"", - "copy-typescript-definition": "copyfiles -f src/index.d.ts dist", "prepublish": "npm run -s build", "release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" }, @@ -58,7 +57,6 @@ "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.9.0", "chai": "^3.5.0", - "copyfiles": "^1.2.0", "cross-env": "^3.1.4", "eslint": "^3.13.1", "gzip-size-cli": "^1.0.0", From 7c9f7b4e85cfb15e3233ccfb28dd3f06514e0a66 Mon Sep 17 00:00:00 2001 From: Stephen Niedzielski Date: Fri, 15 Dec 2017 17:48:14 -0600 Subject: [PATCH 17/29] Add definitions for Headers, Body, Response, and Request --- packages/isomorphic-unfetch/index.d.ts | 14 ++++++++++++++ src/index.d.ts | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/packages/isomorphic-unfetch/index.d.ts b/packages/isomorphic-unfetch/index.d.ts index 7dd0676..cd608a7 100644 --- a/packages/isomorphic-unfetch/index.d.ts +++ b/packages/isomorphic-unfetch/index.d.ts @@ -1,3 +1,17 @@ +import { + Body as NodeBody, + Headers as NodeHeaders, + Request as NodeRequest, + Response as NodeResponse +} from "node-fetch"; + +declare namespace unfetch { + export type IsomorphicHeaders = Headers | NodeHeaders; + export type IsomorphicBody = Body | NodeBody; + export type IsomorphicResponse = Response | NodeResponse; + export type IsomorphicRequest = Request | NodeRequest +} + declare const unfetch: typeof fetch; declare module "isomorphic-unfetch" { diff --git a/src/index.d.ts b/src/index.d.ts index 7c9c278..aef4e8d 100644 --- a/src/index.d.ts +++ b/src/index.d.ts @@ -1,3 +1,17 @@ +import { + Body as NodeBody, + Headers as NodeHeaders, + Request as NodeRequest, + Response as NodeResponse +} from "node-fetch"; + +declare namespace unfetch { + export type IsomorphicHeaders = Headers | NodeHeaders; + export type IsomorphicBody = Body | NodeBody; + export type IsomorphicResponse = Response | NodeResponse; + export type IsomorphicRequest = Request | NodeRequest +} + declare const unfetch: typeof fetch; declare module "unfetch" { From 55560157515dc32b4612daf2653d0300c6ddbe7c Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Sun, 24 Dec 2017 21:03:55 -0500 Subject: [PATCH 18/29] Fix parsing of 0-length header values. Fixes #60 --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index cdfc703..71c60a8 100644 --- a/src/index.js +++ b/src/index.js @@ -25,7 +25,7 @@ export default typeof fetch=='function' ? fetch.bind() : function(url, options) headers = {}, header; - request.getAllResponseHeaders().replace(/^(.*?):\s*([\s\S]*?)$/gm, (m, key, value) => { + request.getAllResponseHeaders().replace(/^(.*?):\s*?([\s\S]*?)$/gm, (m, key, value) => { keys.push(key = key.toLowerCase()); all.push([key, value]); header = headers[key]; From b7bc09db5bc165aa10ac2d771babd5e2b15f02ac Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Mon, 16 Apr 2018 13:37:39 -0700 Subject: [PATCH 19/29] fix: handle 0-length header values and trim whitespace --- src/index.js | 2 +- test/index.js | 27 +++++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 71c60a8..934c7ce 100644 --- a/src/index.js +++ b/src/index.js @@ -25,7 +25,7 @@ export default typeof fetch=='function' ? fetch.bind() : function(url, options) headers = {}, header; - request.getAllResponseHeaders().replace(/^(.*?):\s*?([\s\S]*?)$/gm, (m, key, value) => { + request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => { keys.push(key = key.toLowerCase()); all.push([key, value]); header = headers[key]; diff --git a/test/index.js b/test/index.js index 2515ebb..fff473e 100644 --- a/test/index.js +++ b/test/index.js @@ -27,8 +27,10 @@ describe('unfetch', () => { }); describe('fetch()', () => { - it('sanity test', () => { - let xhr = { + let xhr; + + beforeEach(() => { + xhr = { setRequestHeader: spy(), getAllResponseHeaders: stub().returns('X-Foo: bar\nX-Foo:baz'), open: spy(), @@ -40,7 +42,13 @@ describe('unfetch', () => { }; global.XMLHttpRequest = stub().returns(xhr); + }); + afterEach(() => { + delete global.XMLHttpRequest; + }); + + it('sanity test', () => { let p = fetch('/foo', { headers: { a: 'b' } }) .then( r => { expect(r).to.have.property('text').that.is.a('function'); @@ -60,8 +68,6 @@ describe('unfetch', () => { expect(xhr.setRequestHeader).to.have.been.calledOnce.and.calledWith('a', 'b'); expect(xhr.open).to.have.been.calledOnce.and.calledWith('get', '/foo'); expect(xhr.send).to.have.been.calledOnce.and.calledWith(); - - delete global.XMLHttpRequest; }); expect(xhr.onload).to.be.a('function'); @@ -71,5 +77,18 @@ describe('unfetch', () => { return p; }); + + it('handles empty header values', () => { + xhr.getAllResponseHeaders = stub().returns('Server: \nX-Foo:baz'); + let p = fetch('/foo') + .then(r => { + expect(r.headers.get('server')).to.equal(''); + expect(r.headers.get('X-foo')).to.equal('baz'); + }); + + xhr.onload(); + + return p; + }); }); }); From 027148d8654208d91b403ca9fee84c6cb0686893 Mon Sep 17 00:00:00 2001 From: Resi Respati Date: Mon, 30 Apr 2018 21:18:33 +0700 Subject: [PATCH 20/29] Include Typescript typings in published package This should fix Typescript projects being unable to load the included typings. --- packages/isomorphic-unfetch/package.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index 24c524e..378bec1 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -2,6 +2,11 @@ "name": "isomorphic-unfetch", "version": "2.0.0", "description": "Switches between unfetch & node-fetch for client & server.", + "files": [ + "index.js", + "index.d.ts", + "browser.js" + ], "browser": "browser.js", "main": "index.js", "types": "index.d.ts", From e8ad5026229dcb2207456e8a3ba42e5b82a05734 Mon Sep 17 00:00:00 2001 From: Daniel Ruf Date: Thu, 3 May 2018 21:49:10 +0200 Subject: [PATCH 21/29] fix: support the npm scripts on Windows --- package.json | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 34ea0d3..99ef04f 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,14 @@ "test": "eslint src test && mocha --compilers js:babel-register test/**/*.js", "build": "npm-run-all --silent clean -p rollup:* -p minify:* -s size", "clean": "rimraf dist && mkdirp dist", - "rollup:cjs": "cross-env FORMAT=cjs rollup -c rollup.config.js -f cjs -n $npm_package_name -o $npm_package_main", - "rollup:umd": "cross-env FORMAT=umd rollup -c rollup.config.js -f umd -n $npm_package_name -o $npm_package_umd_main", - "rollup:esm": "rollup -c rollup.config.js -f es -n $npm_package_name -o $npm_package_module", - "minify:cjs": "uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map ${npm_package_main}.map --source-map ${npm_package_main}.map", - "minify:umd": "uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map ${npm_package_umd_main}.map --source-map ${npm_package_umd_main}.map", - "size": "echo \"Gzipped Size: $(strip-json-comments --no-whitespace $npm_package_main | gzip-size)\"", + "rollup:cjs": "rollup -c rollup.config.js -f cjs -n unfetch -o dist/unfetch.js", + "rollup:umd": "rollup -c rollup.config.js -f umd -n unfetch -o dist/unfetch.umd.js", + "rollup:esm": "rollup -c rollup.config.js -f es -n unfetch -o dist/unfetch.es.js", + "minify:cjs": "uglifyjs dist/unfetch.js -cm toplevel -o dist/unfetch.js -p relative --in-source-map dist/unfetch.js.map --source-map dist/unfetch.js.map", + "minify:umd": "uglifyjs dist/unfetch.umd.js -cm -o dist/unfetch.umd.js -p relative --in-source-map dist/unfetch.umd.js.map --source-map dist/unfetch.umd.js.map", + "size": "echo Gzipped Size: && (strip-json-comments --no-whitespace dist/unfetch.js | gzip-size)", "prepublish": "npm run -s build", - "release": "npm run build -s && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" + "release": "npm run build -s && git commit -am 3.0.0 && git tag 3.0.0 && git push && git push --tags && npm publish" }, "repository": "developit/unfetch", "keywords": [ From 2e1b47afc1f20aef24cc1ababd7e7a8dff25ff28 Mon Sep 17 00:00:00 2001 From: Daniel Ruf Date: Thu, 3 May 2018 22:23:57 +0200 Subject: [PATCH 22/29] fix: use cross-var instead of cross-en --- package.json | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 99ef04f..5c70870 100644 --- a/package.json +++ b/package.json @@ -10,14 +10,14 @@ "test": "eslint src test && mocha --compilers js:babel-register test/**/*.js", "build": "npm-run-all --silent clean -p rollup:* -p minify:* -s size", "clean": "rimraf dist && mkdirp dist", - "rollup:cjs": "rollup -c rollup.config.js -f cjs -n unfetch -o dist/unfetch.js", - "rollup:umd": "rollup -c rollup.config.js -f umd -n unfetch -o dist/unfetch.umd.js", - "rollup:esm": "rollup -c rollup.config.js -f es -n unfetch -o dist/unfetch.es.js", - "minify:cjs": "uglifyjs dist/unfetch.js -cm toplevel -o dist/unfetch.js -p relative --in-source-map dist/unfetch.js.map --source-map dist/unfetch.js.map", - "minify:umd": "uglifyjs dist/unfetch.umd.js -cm -o dist/unfetch.umd.js -p relative --in-source-map dist/unfetch.umd.js.map --source-map dist/unfetch.umd.js.map", - "size": "echo Gzipped Size: && (strip-json-comments --no-whitespace dist/unfetch.js | gzip-size)", + "rollup:cjs": "cross-var rollup -c rollup.config.js -f cjs -n unfetch -o $npm_package_main", + "rollup:umd": "cross-var rollup -c rollup.config.js -f umd -n unfetch -o $npm_package_umd_main", + "rollup:esm": "cross-var rollup -c rollup.config.js -f es -n unfetch -o $npm_package_module", + "minify:cjs": "cross-var uglifyjs $npm_package_main -cm toplevel -o $npm_package_main -p relative --in-source-map $npm_package_main.map --source-map $npm_package_main.map", + "minify:umd": "cross-var uglifyjs $npm_package_umd_main -cm -o $npm_package_umd_main -p relative --in-source-map $npm_package_umd_main.map --source-map $npm_package_umd_main.map", + "size": "cross-var echo Gzipped Size: && (cross-var strip-json-comments --no-whitespace $npm_package_main | gzip-size)", "prepublish": "npm run -s build", - "release": "npm run build -s && git commit -am 3.0.0 && git tag 3.0.0 && git push && git push --tags && npm publish" + "release": "cross-var npm run build -s && cross-var git commit -am $npm_package_version && cross-var git tag $npm_package_version && git push && git push --tags && npm publish" }, "repository": "developit/unfetch", "keywords": [ @@ -57,7 +57,7 @@ "babel-preset-stage-0": "^6.5.0", "babel-register": "^6.9.0", "chai": "^3.5.0", - "cross-env": "^3.1.4", + "cross-var": "^1.1.0", "eslint": "^3.13.1", "gzip-size-cli": "^1.0.0", "mkdirp": "^0.5.1", From c0409bf73e3fe5fa7bb352a00152b1f9ed1797ba Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 18 May 2018 19:20:55 -0400 Subject: [PATCH 23/29] Add badges to README --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 06dd24b..9fa8cbc 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,11 @@

unfetch
- npm travis + npm + gzip size + install size + downloads + travis

# unfetch From 7ba231e638b1705105232eef417678cbeb3e2a58 Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 3 Jul 2018 11:26:54 +0200 Subject: [PATCH 24/29] Serve the right file for browsers I think it's becoming a common technique to point at latest package via `https://unpkg.com/pkg-name` for either development or blog posts, so that any script that points at that would point at the right version for the browser. In this case, serving via unpkg the CJS file makes no sense so I hope this PR will be accepted and let anyone use `` instead of the long one `https://unpkg.com/unfetch@latest/dist/unfetch.umd.js`. Thanks for considering this change. --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index 34ea0d3..1005946 100644 --- a/package.json +++ b/package.json @@ -2,6 +2,7 @@ "name": "unfetch", "version": "3.0.0", "description": "Bare minimum fetch polyfill in 500 bytes", + "browser": "dist/unfetch.umd.js", "main": "dist/unfetch.js", "module": "dist/unfetch.es.js", "jsnext:main": "dist/unfetch.es.js", From 2d8861c7bbe955a11558ea5b3b334d76476627ef Mon Sep 17 00:00:00 2001 From: Andrea Giammarchi Date: Tue, 3 Jul 2018 19:44:13 +0200 Subject: [PATCH 25/29] Update package.json Actually using the explicit `unpkg` field instead to avoid conflicts --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 1005946..9582839 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "unfetch", "version": "3.0.0", "description": "Bare minimum fetch polyfill in 500 bytes", - "browser": "dist/unfetch.umd.js", + "unpkg": "dist/unfetch.umd.js", "main": "dist/unfetch.js", "module": "dist/unfetch.es.js", "jsnext:main": "dist/unfetch.es.js", From e3474aeeb0314807ff06516d8cfb8908b24c93a2 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 20 Jul 2018 02:37:02 +0000 Subject: [PATCH 26/29] Update node-fetch. Fixes #65. --- packages/isomorphic-unfetch/package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index 378bec1..d9da74e 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -11,7 +11,7 @@ "main": "index.js", "types": "index.d.ts", "dependencies": { - "node-fetch": "^1.7.1", - "unfetch": "^3.0.0" + "node-fetch": "^2.1.2", + "unfetch": "^3.1.0" } } From cadbc1eb1af3ff9ba843ba2c253c3a50f868204c Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 20 Jul 2018 02:37:31 +0000 Subject: [PATCH 27/29] ignore package-lock --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b7de706..a81c41c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /dist node_modules npm-debug.log +package-lock.json .DS_Store From 360f242545440978613058ff89acae58e968aa84 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 20 Jul 2018 02:41:11 +0000 Subject: [PATCH 28/29] isomorphic-unfetch @ 2.1.0 --- packages/isomorphic-unfetch/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/isomorphic-unfetch/package.json b/packages/isomorphic-unfetch/package.json index d9da74e..35b8c6a 100644 --- a/packages/isomorphic-unfetch/package.json +++ b/packages/isomorphic-unfetch/package.json @@ -1,6 +1,6 @@ { "name": "isomorphic-unfetch", - "version": "2.0.0", + "version": "2.1.0", "description": "Switches between unfetch & node-fetch for client & server.", "files": [ "index.js", From bd07f24c59d393bf705d5873527a86feffb0340d Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Fri, 20 Jul 2018 02:50:19 +0000 Subject: [PATCH 29/29] 3.1.0 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 11bf440..c29fdcf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "unfetch", - "version": "3.0.0", + "version": "3.1.0", "description": "Bare minimum fetch polyfill in 500 bytes", "unpkg": "dist/unfetch.umd.js", "main": "dist/unfetch.js",