-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add abort request polyfill and update types (#148)
* feat: add AbortController polyfill * doc: update cases; update types
- Loading branch information
Showing
6 changed files
with
128 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,22 @@ | ||
import AbortController from 'abort-controller'; | ||
import { AbortController as AcAbortController, AbortSignal as AcAbortSignal } from 'abort-controller'; | ||
|
||
let AbortController = undefined; | ||
let AbortSignal = undefined; | ||
|
||
const g = | ||
typeof self !== 'undefined' | ||
? self | ||
: typeof window !== 'undefined' | ||
? window | ||
: typeof global !== 'undefined' | ||
? global | ||
: /* otherwise */ undefined; | ||
|
||
if (g) { | ||
AbortController = typeof g.AbortController !== 'undefined' ? g.AbortController : AcAbortController; | ||
AbortSignal = typeof g.AbortSignal !== 'undefined' ? g.AbortSignal : AcAbortSignal; | ||
} | ||
|
||
export default AbortController; | ||
|
||
export { AbortController, AbortSignal }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import request, { extend, fetch } from './request'; | ||
import Onion from './onion'; | ||
import { RequestError, ResponseError } from './utils'; | ||
import AbortController from './cancel/abortControllerCancel'; | ||
import { AbortController, AbortSignal } from './cancel/abortControllerCancel'; | ||
|
||
export { extend, RequestError, ResponseError, Onion, fetch, AbortController, AbortSignal }; | ||
|
||
export { extend, RequestError, ResponseError, Onion, fetch, AbortController }; | ||
export default request; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import createTestServer from 'create-test-server'; | ||
import request, { AbortController } from '../../src/index'; | ||
|
||
const writeData = (data, res) => { | ||
res.setHeader('access-control-allow-origin', '*'); | ||
res.send(data); | ||
}; | ||
|
||
describe('test abortController', () => { | ||
let server; | ||
|
||
beforeAll(async () => { | ||
server = await createTestServer(); | ||
}); | ||
|
||
afterAll(() => { | ||
server.close(); | ||
}); | ||
|
||
const prefix = api => `${server.url}${api}`; | ||
|
||
jest.useFakeTimers(); | ||
|
||
it('test request abort', () => { | ||
expect.assertions(2); | ||
server.get('/test/abort1', (req, res) => { | ||
setTimeout(() => { | ||
writeData(req.query, res); | ||
}, 2000); | ||
}); | ||
|
||
const controller = new AbortController(); | ||
const { signal } = controller; | ||
setTimeout(() => { | ||
controller.abort(); | ||
}, 500); | ||
expect(signal.aborted).toBe(false); | ||
jest.runAllTimers(); | ||
expect(signal.aborted).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters