diff --git a/package-lock.json b/package-lock.json index fcbcb23..2f75d7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "unfetch", - "version": "4.0.0", + "version": "4.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index d8c11ca..b35459b 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,8 @@ "test": "eslint src test && jest", "build": "microbundle src/index.mjs && microbundle -f cjs polyfill/polyfill.mjs -o polyfill/index.js --no-sourcemap && cp dist/unfetch.mjs dist/unfetch.es.js", "prepare": "npm run -s build", - "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" + "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", + "format": "eslint {src,test} --fix" }, "repository": "developit/unfetch", "keywords": [ diff --git a/src/AbortController.mjs b/src/AbortController.mjs new file mode 100644 index 0000000..8d23e76 --- /dev/null +++ b/src/AbortController.mjs @@ -0,0 +1,6 @@ +export default function() { + this.signal = { onabort: () => {} }; + this.abort = () => { + this.signal.onabort(); + }; +} diff --git a/src/index.mjs b/src/index.mjs index d4beeec..04b3ecb 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -17,6 +17,9 @@ export default function(url, options) { request.onerror = reject; + if (options.signal) options.signal.onabort = () => { request.abort(); } + request.onabort = () => reject(new DOMException('The user aborted a request.')); + request.send(options.body || null); function response() { diff --git a/test/AbortController.js b/test/AbortController.js new file mode 100644 index 0000000..515445e --- /dev/null +++ b/test/AbortController.js @@ -0,0 +1,46 @@ +import fetch from '../src/index.mjs'; +import AbortController from '../src/AbortController.mjs'; + +describe('AbortController', () => { + it('should be a function', () => { + expect(AbortController).toEqual(expect.any(Function)); + }); + + describe('AbortController()', () => { + let xhr; + + beforeEach(() => { + xhr = { + setRequestHeader: jest.fn(), + getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'), + open: jest.fn(), + send: jest.fn(), + abort: jest.fn(() => xhr.onabort({ type: 'abort' })) , + status: 200, + statusText: 'OK', + responseText: '{"a":"b"}', + responseURL: '/foo?redirect' + }; + + global.XMLHttpRequest = jest.fn(() => xhr); + }); + + afterEach(() => { + delete global.XMLHttpRequest; + }); + + it('handles abort', () => { + let controller = new AbortController(); + let signal = controller.signal; + let p = fetch('/foo', { signal }) + .then(() => {}) + .catch((e) => { + expect(e.message).toEqual('The user aborted a request.'); + }); + + controller.abort(); + + return p; + }); + }); +});