@@ -27,7 +27,7 @@ import FormData from 'form-data'
27
27
import Joi from 'joi'
28
28
// eslint-disable-next-line no-restricted-imports
29
29
import * as qs from 'querystring'
30
- import { errors as undiciErrors } from 'undici'
30
+ import { type BodyInit , errors as undiciErrors } from 'undici'
31
31
import YAML from 'yaml'
32
32
import {
33
33
type ProbeRequestResponse ,
@@ -73,17 +73,17 @@ export async function httpRequest({
73
73
ping,
74
74
allowUnauthorized,
75
75
followRedirects,
76
+ signal,
76
77
} = requestConfig
77
- const newReq = { method, headers, timeout, body, ping }
78
+ const newReq = { method, headers, timeout, body, ping, signal }
78
79
const renderURL = Handlebars . compile ( url )
79
80
const renderedURL = renderURL ( { responses } )
80
- newReq . headers = compileHeaders ( headers , body , responses as never )
81
81
// compile body needs to modify headers if necessary
82
- const { headers : newHeaders , body : newBody } = compileBody (
83
- newReq . headers ,
82
+ const { headers : newHeaders , body : newBody } = compileBody ( {
83
+ responses ,
84
84
body,
85
- responses
86
- )
85
+ headers : compileHeaders ( { headers , responses, body } ) ,
86
+ } )
87
87
newReq . headers = newHeaders
88
88
newReq . body = newBody
89
89
@@ -149,11 +149,13 @@ export async function httpRequest({
149
149
}
150
150
}
151
151
152
- function compileHeaders (
153
- headers : object | undefined ,
154
- body : string | object ,
155
- responses : never
156
- ) {
152
+ type ChainingRequest = {
153
+ responses : Array < ProbeRequestResponse >
154
+ body ?: BodyInit
155
+ headers ?: object
156
+ }
157
+
158
+ function compileHeaders ( { headers, responses, body } : ChainingRequest ) {
157
159
// return as-is if falsy
158
160
if ( ! headers ) return headers
159
161
// Compile headers using handlebars to render URLs that uses previous responses data.
@@ -193,18 +195,18 @@ function compileHeaders(
193
195
return newHeaders
194
196
}
195
197
196
- function compileBody (
197
- headers : object | undefined ,
198
- body : object | string ,
199
- responses : ProbeRequestResponse [ ]
200
- ) : {
201
- headers : object | undefined
202
- body : object | string
203
- } {
198
+ function compileBody ( {
199
+ responses,
200
+ body,
201
+ headers,
202
+ } : ChainingRequest ) : Pick < ChainingRequest , 'body' | 'headers' > {
204
203
// return as-is if falsy
205
204
if ( ! body ) return { headers, body }
206
205
let newHeaders = headers
207
- let newBody = generateRequestChainingBody ( body , responses )
206
+ let newBody : BodyInit | undefined = generateRequestChainingBody (
207
+ body ,
208
+ responses
209
+ )
208
210
209
211
if ( newHeaders ) {
210
212
const contentTypeKey = Object . keys ( newHeaders || { } ) . find (
@@ -247,7 +249,7 @@ async function probeHttpFetch({
247
249
method : string | undefined
248
250
headers : Headers | undefined
249
251
timeout : number
250
- body : string | object
252
+ body ?: BodyInit
251
253
ping : boolean | undefined
252
254
}
253
255
} ) : Promise < ProbeRequestResponse > {
@@ -302,7 +304,7 @@ type ProbeHTTPAxiosParams = {
302
304
method : string | undefined
303
305
headers : Headers | undefined
304
306
timeout : number
305
- body : string | object
307
+ body ?: BodyInit
306
308
ping : boolean | undefined
307
309
}
308
310
}
@@ -320,10 +322,6 @@ async function probeHttpAxios({
320
322
keepalive : true ,
321
323
url : renderedURL ,
322
324
maxRedirects,
323
- body :
324
- typeof requestParams . body === 'string'
325
- ? requestParams . body
326
- : JSON . stringify ( requestParams . body ) ,
327
325
} )
328
326
329
327
const responseTime = Date . now ( ) - startTime
@@ -343,7 +341,7 @@ async function probeHttpAxios({
343
341
export function generateRequestChainingBody (
344
342
body : object | string ,
345
343
responses : ProbeRequestResponse [ ]
346
- ) : object | string {
344
+ ) : BodyInit {
347
345
const isString = typeof body === 'string'
348
346
const template = Handlebars . compile ( isString ? body : JSON . stringify ( body ) )
349
347
const renderedBody = template ( { responses } )
@@ -352,9 +350,13 @@ export function generateRequestChainingBody(
352
350
}
353
351
354
352
function transformContentByType (
355
- content : object | string ,
353
+ content ?: BodyInit ,
356
354
contentType ?: string | number | boolean
357
355
) {
356
+ if ( ! content ) {
357
+ return { content, contentType }
358
+ }
359
+
358
360
switch ( contentType ) {
359
361
case 'application/json' : {
360
362
return { content : JSON . stringify ( content ) , contentType }
@@ -369,9 +371,8 @@ function transformContentByType(
369
371
370
372
case 'multipart/form-data' : {
371
373
const form = new FormData ( )
372
-
373
374
for ( const contentKey of Object . keys ( content ) ) {
374
- form . append ( contentKey , ( content as Record < string , never > ) [ contentKey ] )
375
+ form . append ( contentKey , ( content as any ) [ contentKey ] )
375
376
}
376
377
377
378
return { content : form , contentType : form . getHeaders ( ) [ 'content-type' ] }
0 commit comments