@@ -76,8 +76,58 @@ async function createServices ({ t, wsReconnectOptions, wsTargetOptions, wsServe
7676 }
7777}
7878
79+ // Helper function to wrap fetch
80+ async function request ( url , options = { } ) {
81+ // Handle got's object format: request({ url: '...', headers: {...} })
82+ if ( typeof url === 'object' && url . url ) {
83+ options = url
84+ url = options . url
85+ }
86+
87+ const fetchOptions = {
88+ method : options . method || 'GET' ,
89+ headers : options . headers || { } ,
90+ redirect : options . followRedirect === false ? 'manual' : 'follow'
91+ }
92+
93+ if ( options . json ) {
94+ fetchOptions . headers [ 'content-type' ] = 'application/json'
95+ fetchOptions . body = JSON . stringify ( options . json )
96+ }
97+
98+ if ( options . retry !== undefined ) {
99+ // Native fetch doesn't have retry, just ignore it
100+ }
101+
102+ const response = await fetch ( url , fetchOptions )
103+
104+ // Convert fetch Response to got-like response
105+ const result = {
106+ statusCode : response . status ,
107+ statusMessage : response . statusText ,
108+ headers : Object . fromEntries ( response . headers . entries ( ) )
109+ }
110+
111+ // Handle response body based on responseType
112+ if ( options . responseType === 'json' ) {
113+ result . body = await response . json ( )
114+ } else {
115+ result . body = await response . text ( )
116+ }
117+
118+ // Throw on HTTP errors like got does
119+ if ( ! response . ok && response . status !== 302 ) {
120+ const error = new Error ( `Response code ${ response . status } (${ response . statusText } )` )
121+ error . response = result
122+ throw error
123+ }
124+
125+ return result
126+ }
127+
79128module . exports = {
80129 waitForLogMessage,
81130 createTargetServer,
82- createServices
131+ createServices,
132+ request
83133}
0 commit comments