@superhero/http-request 4.0.4 → 4.0.6
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 +6 -6
- package/index.js +16 -15
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -331,14 +331,14 @@ tests 42
|
|
|
331
331
|
suites 7
|
|
332
332
|
pass 42
|
|
333
333
|
|
|
334
|
-
|
|
334
|
+
----------------------------------------------------------------------------------------
|
|
335
335
|
file | line % | branch % | funcs % | uncovered lines
|
|
336
|
-
|
|
337
|
-
index.js |
|
|
336
|
+
----------------------------------------------------------------------------------------
|
|
337
|
+
index.js | 94.05 | 91.74 | 92.86 | 496-505 533-535 671-677 702-705 735-755
|
|
338
338
|
index.test.js | 100.00 | 97.67 | 100.00 |
|
|
339
|
-
|
|
340
|
-
all files |
|
|
341
|
-
|
|
339
|
+
----------------------------------------------------------------------------------------
|
|
340
|
+
all files | 96.57 | 94.36 | 97.17 |
|
|
341
|
+
----------------------------------------------------------------------------------------
|
|
342
342
|
```
|
|
343
343
|
|
|
344
344
|
---
|
package/index.js
CHANGED
|
@@ -48,20 +48,21 @@ import { setTimeout as wait } from 'node:timers/promises'
|
|
|
48
48
|
*/
|
|
49
49
|
export default class Request
|
|
50
50
|
{
|
|
51
|
-
#config
|
|
52
|
-
|
|
53
51
|
/**
|
|
54
52
|
* @param {RequestOptions} config - The default/fallback request options/configurations.
|
|
55
53
|
*/
|
|
56
54
|
constructor(config)
|
|
57
55
|
{
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
config = config && JSON.parse(JSON.stringify(config))
|
|
57
|
+
config = 'string' === typeof config
|
|
58
|
+
? { base: config }
|
|
59
|
+
: config || {}
|
|
60
|
+
|
|
61
|
+
const { base, url } = config
|
|
62
|
+
config.base = base ?? url
|
|
63
|
+
delete config.url
|
|
61
64
|
|
|
62
|
-
|
|
63
|
-
this.#config.base = base ?? url
|
|
64
|
-
delete this.#config.url
|
|
65
|
+
Object.defineProperty(this, 'config', { enumerable: true, value: config })
|
|
65
66
|
}
|
|
66
67
|
|
|
67
68
|
/**
|
|
@@ -80,13 +81,13 @@ export default class Request
|
|
|
80
81
|
{
|
|
81
82
|
await this.close()
|
|
82
83
|
|
|
83
|
-
const url = new URL(authority || this
|
|
84
|
+
const url = new URL(authority || this.config.base)
|
|
84
85
|
authority = url.protocol + '//' + url.host
|
|
85
86
|
|
|
86
87
|
this.http2Session = http2.connect(authority, ...args.slice(0, 1), () =>
|
|
87
88
|
{
|
|
88
|
-
this
|
|
89
|
-
this
|
|
89
|
+
this.config.base = authority
|
|
90
|
+
this.config.url = url.pathname + url.search
|
|
90
91
|
this.http2Session.off('error', reject)
|
|
91
92
|
accept()
|
|
92
93
|
})
|
|
@@ -245,11 +246,11 @@ export default class Request
|
|
|
245
246
|
options = { url:options }
|
|
246
247
|
}
|
|
247
248
|
|
|
248
|
-
if(this
|
|
249
|
+
if(this.config.url && options.url)
|
|
249
250
|
{
|
|
250
251
|
options.url = options.url[0] === '/'
|
|
251
252
|
? options.url
|
|
252
|
-
: this
|
|
253
|
+
: this.config.url + options.url
|
|
253
254
|
}
|
|
254
255
|
|
|
255
256
|
options = Object.assign(
|
|
@@ -261,7 +262,7 @@ export default class Request
|
|
|
261
262
|
url : '',
|
|
262
263
|
timeout : 30e3,
|
|
263
264
|
retryOnStatus : []
|
|
264
|
-
}, this
|
|
265
|
+
}, this.config, options)
|
|
265
266
|
|
|
266
267
|
return options.retry
|
|
267
268
|
? this.#resolveRetryLoop(options)
|
|
@@ -731,7 +732,7 @@ export default class Request
|
|
|
731
732
|
|
|
732
733
|
#contentTypeTextEventStream(body)
|
|
733
734
|
{
|
|
734
|
-
return body.split('\n\n').map((fields) =>
|
|
735
|
+
return body?.trim().split('\n\n').map((fields) =>
|
|
735
736
|
{
|
|
736
737
|
return Object.fromEntries(fields.split('\n').map((field) =>
|
|
737
738
|
{
|