@superhero/http-request 4.0.10 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -7
- package/index.js +48 -35
- package/index.test.js +6 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -95,11 +95,12 @@ console.log(response.body)
|
|
|
95
95
|
|
|
96
96
|
Errors are thrown with specific error codes for easier handling:
|
|
97
97
|
|
|
98
|
-
- `
|
|
99
|
-
- `
|
|
100
|
-
- `
|
|
101
|
-
- `
|
|
102
|
-
- `
|
|
98
|
+
- `E_HTTP_REQUEST_FAILED`
|
|
99
|
+
- `E_HTTP_REQUEST_CLIENT_ERROR`
|
|
100
|
+
- `E_HTTP_REQUEST_CLIENT_TIMEOUT`
|
|
101
|
+
- `E_HTTP_REQUEST_DOWNSTREAM_ERROR`
|
|
102
|
+
- `E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT`
|
|
103
|
+
- `E_HTTP_REQUEST_INVALID_RESPONSE_STATUS`
|
|
103
104
|
|
|
104
105
|
Example:
|
|
105
106
|
|
|
@@ -334,10 +335,10 @@ pass 42
|
|
|
334
335
|
----------------------------------------------------------------------------------------
|
|
335
336
|
file | line % | branch % | funcs % | uncovered lines
|
|
336
337
|
----------------------------------------------------------------------------------------
|
|
337
|
-
index.js |
|
|
338
|
+
index.js | 89.56 | 87.39 | 86.67 | 100-104 137-139 150-153 353-357 361-365
|
|
338
339
|
index.test.js | 100.00 | 97.67 | 100.00 |
|
|
339
340
|
----------------------------------------------------------------------------------------
|
|
340
|
-
all files |
|
|
341
|
+
all files | 93.73 | 91.71 | 94.50 |
|
|
341
342
|
----------------------------------------------------------------------------------------
|
|
342
343
|
```
|
|
343
344
|
|
package/index.js
CHANGED
|
@@ -275,7 +275,7 @@ export default class Request
|
|
|
275
275
|
* @throws {Error} E_HTTP_REQUEST_RETRY_HTTP2_RECONNECT
|
|
276
276
|
* @throws {Error} E_HTTP_REQUEST_RETRY_ERROR
|
|
277
277
|
*/
|
|
278
|
-
#fetch(method, options)
|
|
278
|
+
async #fetch(method, options)
|
|
279
279
|
{
|
|
280
280
|
if(typeof options === 'string')
|
|
281
281
|
{
|
|
@@ -300,9 +300,19 @@ export default class Request
|
|
|
300
300
|
retryOnStatus : []
|
|
301
301
|
}, this.config, options)
|
|
302
302
|
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
303
|
+
try
|
|
304
|
+
{
|
|
305
|
+
return options.retry
|
|
306
|
+
? await this.#resolveRetryLoop(options)
|
|
307
|
+
: await this.#resolve(options)
|
|
308
|
+
}
|
|
309
|
+
catch(reason)
|
|
310
|
+
{
|
|
311
|
+
const error = new Error(`Failed request ${method} ${options.url}`)
|
|
312
|
+
error.code = 'E_HTTP_REQUEST_FAILED'
|
|
313
|
+
error.cause = reason
|
|
314
|
+
throw error
|
|
315
|
+
}
|
|
306
316
|
}
|
|
307
317
|
|
|
308
318
|
/**
|
|
@@ -463,7 +473,7 @@ export default class Request
|
|
|
463
473
|
*/
|
|
464
474
|
async #resolveRetryLoop(options)
|
|
465
475
|
{
|
|
466
|
-
const
|
|
476
|
+
const reasons = []
|
|
467
477
|
|
|
468
478
|
let retry = Math.abs(Math.floor(options.retry))
|
|
469
479
|
|
|
@@ -485,30 +495,32 @@ export default class Request
|
|
|
485
495
|
return response
|
|
486
496
|
}
|
|
487
497
|
}
|
|
488
|
-
catch(
|
|
498
|
+
catch(reason)
|
|
489
499
|
{
|
|
490
|
-
if(retry
|
|
500
|
+
if(retry <= 0)
|
|
491
501
|
{
|
|
492
|
-
|
|
502
|
+
reasons.push(reason)
|
|
493
503
|
}
|
|
494
504
|
else
|
|
495
505
|
{
|
|
496
|
-
await this.#resolveRetryLoopError(options,
|
|
506
|
+
await this.#resolveRetryLoopError(options, reasons, reason)
|
|
497
507
|
await wait(options.retryDelay)
|
|
498
508
|
}
|
|
499
509
|
}
|
|
500
510
|
}
|
|
501
511
|
|
|
502
|
-
|
|
512
|
+
const
|
|
513
|
+
uniqueReasons = reasons.filter((reason, i) => [i, -1].includes(reasons.lastIndexOf(({ code }) => code === reason.code))),
|
|
514
|
+
reason = uniqueReasons.pop()
|
|
515
|
+
|
|
516
|
+
if(uniqueReasons.length === 1)
|
|
503
517
|
{
|
|
504
|
-
throw
|
|
518
|
+
throw reason
|
|
505
519
|
}
|
|
506
520
|
else
|
|
507
521
|
{
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
error.cause = errorTrace
|
|
511
|
-
throw error
|
|
522
|
+
reason.previous = uniqueReasons
|
|
523
|
+
throw reason
|
|
512
524
|
}
|
|
513
525
|
}
|
|
514
526
|
|
|
@@ -516,18 +528,18 @@ export default class Request
|
|
|
516
528
|
* Resolves the error in the retry loop.
|
|
517
529
|
*
|
|
518
530
|
* @param {RequestOptions} options
|
|
519
|
-
* @param {Error[]}
|
|
520
|
-
* @param {Error}
|
|
531
|
+
* @param {Error[]} reasons
|
|
532
|
+
* @param {Error} reason
|
|
521
533
|
*
|
|
522
534
|
* @returns {Void}
|
|
523
535
|
*/
|
|
524
|
-
async #resolveRetryLoopError(options,
|
|
536
|
+
async #resolveRetryLoopError(options, reasons, reason)
|
|
525
537
|
{
|
|
526
|
-
switch(
|
|
538
|
+
switch(reason.code)
|
|
527
539
|
{
|
|
528
540
|
case 'E_HTTP_REQUEST_CLIENT_ERROR':
|
|
529
541
|
{
|
|
530
|
-
return
|
|
542
|
+
return reasons.push(reason)
|
|
531
543
|
}
|
|
532
544
|
case 'E_HTTP_REQUEST_HTTP2_SESSION_DESTROYED':
|
|
533
545
|
case 'E_HTTP_REQUEST_HTTP2_SESSION_CLOSED':
|
|
@@ -535,67 +547,68 @@ export default class Request
|
|
|
535
547
|
try
|
|
536
548
|
{
|
|
537
549
|
await this.reconnect()
|
|
550
|
+
return reasons.push(reason)
|
|
538
551
|
}
|
|
539
552
|
catch(reason)
|
|
540
553
|
{
|
|
541
|
-
const
|
|
542
|
-
|
|
543
|
-
|
|
554
|
+
const error = new Error(`${reason.message}, retry to reconnect to the server failed`)
|
|
555
|
+
error.code = 'E_HTTP_REQUEST_RETRY_HTTP2_RECONNECT'
|
|
556
|
+
error.cause = reason
|
|
557
|
+
error.reasons = reasons
|
|
544
558
|
throw reason
|
|
545
559
|
}
|
|
546
|
-
return errorTrace.push(error)
|
|
547
560
|
}
|
|
548
561
|
case 'E_HTTP_REQUEST_CLIENT_TIMEOUT':
|
|
549
562
|
{
|
|
550
563
|
if(options.retryOnClientTimeout)
|
|
551
564
|
{
|
|
552
|
-
return
|
|
565
|
+
return reasons.push(reason)
|
|
553
566
|
}
|
|
554
567
|
else
|
|
555
568
|
{
|
|
556
|
-
throw
|
|
569
|
+
throw reason
|
|
557
570
|
}
|
|
558
571
|
}
|
|
559
572
|
case 'E_HTTP_REQUEST_DOWNSTREAM_ERROR':
|
|
560
573
|
{
|
|
561
574
|
if(options.retryOnDownstreamError)
|
|
562
575
|
{
|
|
563
|
-
return
|
|
576
|
+
return reasons.push(reason)
|
|
564
577
|
}
|
|
565
578
|
else
|
|
566
579
|
{
|
|
567
|
-
throw
|
|
580
|
+
throw reason
|
|
568
581
|
}
|
|
569
582
|
}
|
|
570
583
|
case 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT':
|
|
571
584
|
{
|
|
572
585
|
if(options.retryOnInvalidResponseBodyFormat)
|
|
573
586
|
{
|
|
574
|
-
return
|
|
587
|
+
return reasons.push(reason)
|
|
575
588
|
}
|
|
576
589
|
else
|
|
577
590
|
{
|
|
578
|
-
throw
|
|
591
|
+
throw reason
|
|
579
592
|
}
|
|
580
593
|
}
|
|
581
594
|
case 'E_HTTP_REQUEST_INVALID_RESPONSE_STATUS':
|
|
582
595
|
{
|
|
583
596
|
if(options.retryOnErrorResponseStatus)
|
|
584
597
|
{
|
|
585
|
-
return
|
|
598
|
+
return reasons.push(reason)
|
|
586
599
|
}
|
|
587
|
-
else if(options.retryOnStatus.includes(
|
|
600
|
+
else if(options.retryOnStatus.includes(reason.response.status))
|
|
588
601
|
{
|
|
589
|
-
return
|
|
602
|
+
return reasons.push(reason)
|
|
590
603
|
}
|
|
591
604
|
else
|
|
592
605
|
{
|
|
593
|
-
throw
|
|
606
|
+
throw reason
|
|
594
607
|
}
|
|
595
608
|
}
|
|
596
609
|
default:
|
|
597
610
|
{
|
|
598
|
-
throw
|
|
611
|
+
throw reason
|
|
599
612
|
}
|
|
600
613
|
}
|
|
601
614
|
}
|
package/index.test.js
CHANGED
|
@@ -199,10 +199,10 @@ suite('@superhero/http-request', () =>
|
|
|
199
199
|
}
|
|
200
200
|
await assert.rejects(
|
|
201
201
|
request.get(options),
|
|
202
|
-
(error) => error.code === 'E_HTTP_REQUEST_CLIENT_TIMEOUT',
|
|
202
|
+
(error) => error.cause.code === 'E_HTTP_REQUEST_CLIENT_TIMEOUT',
|
|
203
203
|
'Should throw a timeout error')
|
|
204
204
|
})
|
|
205
|
-
|
|
205
|
+
|
|
206
206
|
test('Rejects invalid JSON response accurately', async () =>
|
|
207
207
|
{
|
|
208
208
|
// Alter the server to respond with invalid JSON
|
|
@@ -216,7 +216,7 @@ suite('@superhero/http-request', () =>
|
|
|
216
216
|
// Make the request
|
|
217
217
|
await assert.rejects(
|
|
218
218
|
request.get({ url: '/invalid-json' }),
|
|
219
|
-
(error) => error.code === 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT',
|
|
219
|
+
(error) => error.cause.code === 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT',
|
|
220
220
|
'Should throw a parse error')
|
|
221
221
|
})
|
|
222
222
|
|
|
@@ -383,7 +383,7 @@ suite('@superhero/http-request', () =>
|
|
|
383
383
|
|
|
384
384
|
await assert.rejects(
|
|
385
385
|
request.get(options),
|
|
386
|
-
|
|
386
|
+
(error) => error.cause.code === 'E_HTTP_REQUEST_INVALID_RESPONSE_STATUS',
|
|
387
387
|
'Should throw an error after the second attempt')
|
|
388
388
|
|
|
389
389
|
assert.equal(attempt, 2, 'Should make exactly 2 attempts')
|
|
@@ -401,7 +401,7 @@ suite('@superhero/http-request', () =>
|
|
|
401
401
|
|
|
402
402
|
await assert.rejects(
|
|
403
403
|
request.get(options),
|
|
404
|
-
(error) => error.code === 'E_HTTP_REQUEST_INVALID_RESPONSE_STATUS',
|
|
404
|
+
(error) => error.cause.code === 'E_HTTP_REQUEST_INVALID_RESPONSE_STATUS',
|
|
405
405
|
'Should throw an error right away')
|
|
406
406
|
|
|
407
407
|
assert.equal(attempt, 1, 'Should make exactly 1 attempt')
|
|
@@ -547,7 +547,7 @@ suite('@superhero/http-request', () =>
|
|
|
547
547
|
|
|
548
548
|
await assert.rejects(
|
|
549
549
|
request.get({ url: '/timeout-test', timeout: 100 }),
|
|
550
|
-
(error) => error.code === 'E_HTTP_REQUEST_CLIENT_TIMEOUT',
|
|
550
|
+
(error) => error.cause.code === 'E_HTTP_REQUEST_CLIENT_TIMEOUT',
|
|
551
551
|
'Should throw a timeout error')
|
|
552
552
|
})
|
|
553
553
|
})
|