@superhero/http-request 4.1.1 → 4.1.2
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 +50 -20
- package/package.json +5 -2
package/index.js
CHANGED
|
@@ -18,8 +18,9 @@ import { setTimeout as wait } from 'node:timers/promises'
|
|
|
18
18
|
/**
|
|
19
19
|
* @typedef {Object} RequestOptions
|
|
20
20
|
*
|
|
21
|
-
* @property {String}
|
|
22
|
-
* @property {String}
|
|
21
|
+
* @property {String} [method] - The HTTP method to use.
|
|
22
|
+
* @property {String} url - The URL to make the request to.
|
|
23
|
+
* @property {String} base - The base path to resolve the URL against.
|
|
23
24
|
* @property {Object.<String, String>} [headers] - The request headers.
|
|
24
25
|
* @property {Object|String} [body] - The request body.
|
|
25
26
|
* @property {Object|String} [data] - Alias for body.
|
|
@@ -35,7 +36,6 @@ import { setTimeout as wait } from 'node:timers/promises'
|
|
|
35
36
|
* @property {Boolean} [doNotThrowOnRedirectStatus] - Set to true to avoid throwing on redirect status.
|
|
36
37
|
* @property {Stream.Readable>} [upstream] - An optional upstream stream to make it possible to pipe body to the upstream directly.
|
|
37
38
|
* @property {Stream.Writable} [downstream] - An optional downstream stream to make it possible to pipe body from the downstream to.
|
|
38
|
-
* @property {String} [method] - The HTTP method to use.
|
|
39
39
|
*
|
|
40
40
|
* @see {@link https://nodejs.org/api/http.html#httprequestoptions-callback}
|
|
41
41
|
* @see {@link https://nodejs.org/api/https.html#httpsrequestoptions-callback}
|
|
@@ -159,7 +159,9 @@ export default class Request
|
|
|
159
159
|
*/
|
|
160
160
|
get(options)
|
|
161
161
|
{
|
|
162
|
-
|
|
162
|
+
options = this.#normalizeOptions(options)
|
|
163
|
+
options.method = 'GET'
|
|
164
|
+
return this.fetch(options)
|
|
163
165
|
}
|
|
164
166
|
|
|
165
167
|
/**
|
|
@@ -172,7 +174,9 @@ export default class Request
|
|
|
172
174
|
*/
|
|
173
175
|
post(options)
|
|
174
176
|
{
|
|
175
|
-
|
|
177
|
+
options = this.#normalizeOptions(options)
|
|
178
|
+
options.method = 'POST'
|
|
179
|
+
return this.fetch(options)
|
|
176
180
|
}
|
|
177
181
|
|
|
178
182
|
/**
|
|
@@ -185,7 +189,9 @@ export default class Request
|
|
|
185
189
|
*/
|
|
186
190
|
put(options)
|
|
187
191
|
{
|
|
188
|
-
|
|
192
|
+
options = this.#normalizeOptions(options)
|
|
193
|
+
options.method = 'PUT'
|
|
194
|
+
return this.fetch(options)
|
|
189
195
|
}
|
|
190
196
|
|
|
191
197
|
/**
|
|
@@ -198,7 +204,9 @@ export default class Request
|
|
|
198
204
|
*/
|
|
199
205
|
patch(options)
|
|
200
206
|
{
|
|
201
|
-
|
|
207
|
+
options = this.#normalizeOptions(options)
|
|
208
|
+
options.method = 'PATCH'
|
|
209
|
+
return this.fetch(options)
|
|
202
210
|
}
|
|
203
211
|
|
|
204
212
|
/**
|
|
@@ -210,7 +218,9 @@ export default class Request
|
|
|
210
218
|
*/
|
|
211
219
|
delete(options)
|
|
212
220
|
{
|
|
213
|
-
|
|
221
|
+
options = this.#normalizeOptions(options)
|
|
222
|
+
options.method = 'DELETE'
|
|
223
|
+
return this.fetch(options)
|
|
214
224
|
}
|
|
215
225
|
|
|
216
226
|
/**
|
|
@@ -223,7 +233,9 @@ export default class Request
|
|
|
223
233
|
*/
|
|
224
234
|
head(options)
|
|
225
235
|
{
|
|
226
|
-
|
|
236
|
+
options = this.#normalizeOptions(options)
|
|
237
|
+
options.method = 'HEAD'
|
|
238
|
+
return this.fetch(options)
|
|
227
239
|
}
|
|
228
240
|
|
|
229
241
|
/**
|
|
@@ -236,7 +248,9 @@ export default class Request
|
|
|
236
248
|
*/
|
|
237
249
|
options(options)
|
|
238
250
|
{
|
|
239
|
-
|
|
251
|
+
options = this.#normalizeOptions(options)
|
|
252
|
+
options.method = 'OPTIONS'
|
|
253
|
+
return this.fetch(options)
|
|
240
254
|
}
|
|
241
255
|
|
|
242
256
|
/**
|
|
@@ -251,17 +265,19 @@ export default class Request
|
|
|
251
265
|
*/
|
|
252
266
|
trace(options)
|
|
253
267
|
{
|
|
254
|
-
|
|
268
|
+
options = this.#normalizeOptions(options)
|
|
269
|
+
options.method = 'TRACE'
|
|
270
|
+
return this.fetch(options)
|
|
255
271
|
}
|
|
256
272
|
|
|
257
273
|
/**
|
|
258
274
|
* Generic fetch method.
|
|
259
275
|
*
|
|
260
|
-
* @param {string} method
|
|
261
276
|
* @param {RequestOptions} options
|
|
262
277
|
*
|
|
263
278
|
* @returns {RequestResponse}
|
|
264
279
|
*
|
|
280
|
+
* @throws {TypeError} E_HTTP_REQUEST_INVALID_METHOD
|
|
265
281
|
* @throws {Error} E_HTTP_REQUEST_CLIENT_ERROR
|
|
266
282
|
* @throws {Error} E_HTTP_REQUEST_CLIENT_TIMEOUT
|
|
267
283
|
* @throws {Error} E_HTTP_REQUEST_DOWNSTREAM_ERROR
|
|
@@ -272,16 +288,11 @@ export default class Request
|
|
|
272
288
|
* @throws {Error} E_HTTP_REQUEST_RETRY_HTTP2_RECONNECT
|
|
273
289
|
* @throws {Error} E_HTTP_REQUEST_RETRY_ERROR
|
|
274
290
|
*/
|
|
275
|
-
async
|
|
291
|
+
async fetch(options)
|
|
276
292
|
{
|
|
277
|
-
|
|
278
|
-
{
|
|
279
|
-
options = { url:options }
|
|
280
|
-
}
|
|
281
|
-
|
|
293
|
+
options = this.#normalizeOptions(options)
|
|
282
294
|
options = Object.assign(
|
|
283
295
|
{
|
|
284
|
-
method,
|
|
285
296
|
headers : {},
|
|
286
297
|
retry : 3,
|
|
287
298
|
retryDelay : 200,
|
|
@@ -290,6 +301,15 @@ export default class Request
|
|
|
290
301
|
retryOnStatus : []
|
|
291
302
|
}, this.config, options)
|
|
292
303
|
|
|
304
|
+
if('string' !== typeof options.method)
|
|
305
|
+
{
|
|
306
|
+
const error = new TypeError(`Method must be a string, got ${typeof options.method}`)
|
|
307
|
+
error.code = 'E_HTTP_REQUEST_INVALID_METHOD'
|
|
308
|
+
throw error
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
options.method = options.method.toUpperCase()
|
|
312
|
+
|
|
293
313
|
try
|
|
294
314
|
{
|
|
295
315
|
return options.retry
|
|
@@ -298,13 +318,23 @@ export default class Request
|
|
|
298
318
|
}
|
|
299
319
|
catch(reason)
|
|
300
320
|
{
|
|
301
|
-
const error = new Error(`Failed request ${method} ${options.url}`)
|
|
321
|
+
const error = new Error(`Failed request ${options.method} ${options.url}`)
|
|
302
322
|
error.code = 'E_HTTP_REQUEST_FAILED'
|
|
303
323
|
error.cause = reason
|
|
304
324
|
throw error
|
|
305
325
|
}
|
|
306
326
|
}
|
|
307
327
|
|
|
328
|
+
#normalizeOptions(options)
|
|
329
|
+
{
|
|
330
|
+
if(typeof options === 'string')
|
|
331
|
+
{
|
|
332
|
+
options = { url:options }
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
return options
|
|
336
|
+
}
|
|
337
|
+
|
|
308
338
|
/**
|
|
309
339
|
* Resolves the request.
|
|
310
340
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@superhero/http-request",
|
|
3
|
-
"version": "4.1.
|
|
3
|
+
"version": "4.1.2",
|
|
4
4
|
"description": "HTTP(S) request component supporting HTTP 1.1 and HTTP 2.0",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"http request",
|
|
@@ -18,7 +18,10 @@
|
|
|
18
18
|
".": "./index.js"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
|
-
"test": "node --
|
|
21
|
+
"test": "node --test --test-reporter=@superhero/audit/reporter --experimental-test-coverage"
|
|
22
|
+
},
|
|
23
|
+
"devDependencies": {
|
|
24
|
+
"@superhero/audit": "^4.0.3"
|
|
22
25
|
},
|
|
23
26
|
"author": {
|
|
24
27
|
"name": "Erik Landvall",
|