Skip to content

Commit 79265be

Browse files
committed
AbortController ponyfill
1 parent 1670b30 commit 79265be

File tree

5 files changed

+58
-2
lines changed

5 files changed

+58
-2
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"test": "eslint src test && jest",
1212
"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",
1313
"prepare": "npm run -s build",
14-
"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"
14+
"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",
15+
"format": "eslint {src,test} --fix"
1516
},
1617
"repository": "developit/unfetch",
1718
"keywords": [

src/AbortController.mjs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function() {
2+
this.signal = {onabort: () => {}}
3+
this.abort = () => {
4+
this.signal.onabort()
5+
}
6+
}

src/index.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export default function(url, options) {
1717

1818
request.onerror = reject;
1919

20+
if (options.signal) options.signal.onabort = () => { request.abort(); }
21+
request.onabort = () => reject(new DOMException('The user aborted a request.'));
22+
2023
request.send(options.body || null);
2124

2225
function response() {

test/AbortController.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import fetch from '../src/index.mjs';
2+
import AbortController from '../src/AbortController.mjs';
3+
4+
describe('AbortController', () => {
5+
it('should be a function', () => {
6+
expect(AbortController).toEqual(expect.any(Function));
7+
});
8+
9+
describe('AbortController()', () => {
10+
let xhr;
11+
12+
beforeEach(() => {
13+
xhr = {
14+
setRequestHeader: jest.fn(),
15+
getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'),
16+
open: jest.fn(),
17+
send: jest.fn(),
18+
abort: jest.fn(() => xhr.onabort({ type: 'abort' })) ,
19+
status: 200,
20+
statusText: 'OK',
21+
responseText: '{"a":"b"}',
22+
responseURL: '/foo?redirect'
23+
};
24+
25+
global.XMLHttpRequest = jest.fn(() => xhr);
26+
});
27+
28+
afterEach(() => {
29+
delete global.XMLHttpRequest;
30+
});
31+
32+
it('handles abort', () => {
33+
let controller = new AbortController();
34+
let signal = controller.signal;
35+
let p = fetch('/foo', { signal })
36+
.then(() => {})
37+
.catch((e) => {
38+
expect(e.message).toEqual('The user aborted a request.');
39+
});
40+
41+
controller.abort();
42+
43+
return p;
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)