Skip to content

Commit d995bda

Browse files
authored
Merge pull request #1618 from hakobpogh/master
2 parents 04f4f1f + 58ffbc4 commit d995bda

File tree

3 files changed

+77
-9
lines changed

3 files changed

+77
-9
lines changed

packages/sdk-middleware-http/src/http.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,15 @@ export default function createHttpMiddleware({
6868
} = {},
6969
fetch: fetcher,
7070
abortController: _abortController,
71+
getAbortController
7172
}: HttpMiddlewareOptions): Middleware {
7273
if (!fetcher && typeof fetch === 'undefined')
7374
throw new Error(
7475
'`fetch` is not available. Please pass in `fetch` as an option or have it globally available.'
7576
)
76-
if (timeout && !_abortController && typeof AbortController === 'undefined')
77+
if (timeout && !getAbortController && !_abortController && typeof AbortController === 'undefined')
7778
throw new Error(
78-
'`AbortController` is not available. Please pass in `AbortController` as an option or have it globally available when using timeout.'
79+
'`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.'
7980
)
8081
let fetchFunction: typeof fetch
8182
if (fetcher) {
@@ -87,15 +88,15 @@ export default function createHttpMiddleware({
8788
fetchFunction = fetch
8889
}
8990

90-
let abortController
91-
if (timeout || _abortController)
92-
// eslint-disable-next-line
93-
abortController = _abortController || new AbortController()
94-
9591
return (next: Next): Next => (
9692
request: MiddlewareRequest,
9793
response: MiddlewareResponse
9894
) => {
95+
let abortController
96+
if (timeout || getAbortController || _abortController)
97+
// eslint-disable-next-line
98+
abortController = (getAbortController ? getAbortController(): null) || _abortController || new AbortController()
99+
99100
const url = host.replace(/\/$/, '') + request.uri
100101
const body =
101102
typeof request.body === 'string' || Buffer.isBuffer(request.body)

packages/sdk-middleware-http/test/http.spec.js

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Http', () => {
3636
createHttpMiddleware({ host: testHost, timeout: 100, fetch })
3737
}).toThrow(
3838
new Error(
39-
'`AbortController` is not available. Please pass in `AbortController` as an option or have it globally available when using timeout.'
39+
'`AbortController` is not available. Please pass in `getAbortController` as an option or have AbortController globally available when using timeout.'
4040
)
4141
)
4242
})
@@ -102,6 +102,39 @@ describe('Http', () => {
102102
httpMiddleware(next)(request, response)
103103
}))
104104

105+
test('execute a get request with getAbortController timeout (success)', () => {
106+
expect.assertions(1)
107+
new Promise((resolve, reject) => {
108+
const request = createTestRequest({
109+
uri: '/foo/bar',
110+
})
111+
const response = { resolve, reject }
112+
const next = (req, res) => {
113+
expect(res).toEqual({
114+
...response,
115+
body: { foo: 'bar' },
116+
statusCode: 200,
117+
})
118+
resolve()
119+
}
120+
// Use default options
121+
const httpMiddleware = createHttpMiddleware({
122+
host: testHost,
123+
timeout: 1000, // time out after 1s
124+
fetch,
125+
getAbortController: () => new AbortController(),
126+
})
127+
nock(testHost)
128+
.defaultReplyHeaders({
129+
'Content-Type': 'application/json',
130+
})
131+
.get('/foo/bar')
132+
.delay(10) // delay response with 10ms
133+
.reply(200, { foo: 'bar' })
134+
135+
httpMiddleware(next)(request, response)
136+
})})
137+
105138
test('execute a get request with short timeout (fail)', () =>
106139
new Promise((resolve, reject) => {
107140
const request = createTestRequest({
@@ -134,6 +167,39 @@ describe('Http', () => {
134167
httpMiddleware(next)(request, response)
135168
}))
136169

170+
test('execute a get request with getAbortController short timeout (fail)', () => {
171+
expect.assertions(1)
172+
return new Promise((resolve, reject) => {
173+
const request = createTestRequest({
174+
uri: '/foo/bar',
175+
})
176+
const response = { resolve, reject }
177+
const next = (req, res) => {
178+
expect(res).toEqual({
179+
...response,
180+
error: expect.any(Error),
181+
statusCode: 0,
182+
})
183+
resolve()
184+
}
185+
// Use default options
186+
const httpMiddleware = createHttpMiddleware({
187+
host: testHost,
188+
timeout: 10, // time out after 10ms
189+
fetch,
190+
getAbortController: () => new AbortController(),
191+
})
192+
nock(testHost)
193+
.defaultReplyHeaders({
194+
'Content-Type': 'application/json',
195+
})
196+
.get('/foo/bar')
197+
.delay(100) // delay response with 100ms
198+
.reply(200, { foo: 'bar' })
199+
200+
httpMiddleware(next)(request, response)
201+
})})
202+
137203
test('execute a request with timeout and client re-use', () =>
138204
new Promise((resolve, reject) => {
139205
const request = createTestRequest({

types/sdk.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,8 @@ export type HttpMiddlewareOptions = {
243243
maxDelay?: number,
244244
},
245245
fetch?: typeof fetch,
246-
abortController?: AbortController,
246+
abortController?: AbortController, // deprecated
247+
getAbortController: () => AbortController
247248
}
248249
export type QueueMiddlewareOptions = {
249250
concurrency: number,

0 commit comments

Comments
 (0)