@superhero/http-request 4.0.3 → 4.0.4
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/index.js +56 -13
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -388,7 +388,7 @@ export default class Request
|
|
|
388
388
|
if((response.status >= 400 && false === !!options.doNotThrowOnErrorStatus)
|
|
389
389
|
|| (response.status >= 300 && false === !!options.doNotThrowOnRedirectStatus && response.status < 400))
|
|
390
390
|
{
|
|
391
|
-
const error = new Error(`Invalid HTTP status ${response.status} ${method}
|
|
391
|
+
const error = new Error(`Invalid HTTP status ${response.status} ${method} ${url}`)
|
|
392
392
|
error.code = 'E_HTTP_REQUEST_INVALID_RESPONSE_STATUS'
|
|
393
393
|
error.response = response
|
|
394
394
|
|
|
@@ -667,7 +667,7 @@ export default class Request
|
|
|
667
667
|
*/
|
|
668
668
|
#onStreamError(method, url, response, reason)
|
|
669
669
|
{
|
|
670
|
-
const error = new Error(`Downstream error [${reason.code}] ${method}
|
|
670
|
+
const error = new Error(`Downstream error [${reason.code}] ${method} ${url}`)
|
|
671
671
|
error.code = 'E_HTTP_REQUEST_DOWNSTREAM_ERROR'
|
|
672
672
|
error.cause = reason
|
|
673
673
|
error.response = response
|
|
@@ -689,23 +689,66 @@ export default class Request
|
|
|
689
689
|
*/
|
|
690
690
|
#onStreamEnd(method, url, response, accept, reject)
|
|
691
691
|
{
|
|
692
|
-
|
|
692
|
+
try
|
|
693
693
|
{
|
|
694
|
-
|
|
694
|
+
const contentType = response.headers['content-type']
|
|
695
|
+
|
|
696
|
+
if(contentType?.startsWith('application/json'))
|
|
695
697
|
{
|
|
696
|
-
response.body =
|
|
698
|
+
response.body = this.#contentTypeApplicationJson(response.body)
|
|
697
699
|
}
|
|
698
|
-
|
|
700
|
+
else if(contentType?.startsWith('text/event-stream'))
|
|
699
701
|
{
|
|
700
|
-
|
|
701
|
-
error.code = 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT'
|
|
702
|
-
error.cause = reason
|
|
703
|
-
error.response = response
|
|
704
|
-
reject(error)
|
|
705
|
-
return
|
|
702
|
+
response.body = this.#contentTypeTextEventStream(response.body)
|
|
706
703
|
}
|
|
707
704
|
}
|
|
708
|
-
|
|
705
|
+
catch(reason)
|
|
706
|
+
{
|
|
707
|
+
const error = new TypeError(`Invalid response body format ${method} ${url}`)
|
|
708
|
+
error.code = 'E_HTTP_REQUEST_INVALID_RESPONSE_BODY_FORMAT'
|
|
709
|
+
error.cause = reason
|
|
710
|
+
error.response = response
|
|
711
|
+
reject(error)
|
|
712
|
+
return
|
|
713
|
+
}
|
|
714
|
+
|
|
709
715
|
accept(response)
|
|
710
716
|
}
|
|
717
|
+
|
|
718
|
+
#contentTypeApplicationJson(body)
|
|
719
|
+
{
|
|
720
|
+
try
|
|
721
|
+
{
|
|
722
|
+
return JSON.parse(body || '{}')
|
|
723
|
+
}
|
|
724
|
+
catch(reason)
|
|
725
|
+
{
|
|
726
|
+
const error = new TypeError(`Invalid JSON format`)
|
|
727
|
+
error.cause = reason
|
|
728
|
+
throw error
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
|
|
732
|
+
#contentTypeTextEventStream(body)
|
|
733
|
+
{
|
|
734
|
+
return body.split('\n\n').map((fields) =>
|
|
735
|
+
{
|
|
736
|
+
return Object.fromEntries(fields.split('\n').map((field) =>
|
|
737
|
+
{
|
|
738
|
+
const
|
|
739
|
+
separator = field.indexOf(':'),
|
|
740
|
+
param = field.slice(0, separator),
|
|
741
|
+
value = field.slice(separator + 1).trim()
|
|
742
|
+
|
|
743
|
+
try
|
|
744
|
+
{
|
|
745
|
+
return [ param, JSON.parse(value)]
|
|
746
|
+
}
|
|
747
|
+
catch(reason)
|
|
748
|
+
{
|
|
749
|
+
return [ param, value ]
|
|
750
|
+
}
|
|
751
|
+
}))
|
|
752
|
+
})
|
|
753
|
+
}
|
|
711
754
|
}
|