@@ -212,6 +212,10 @@ export function createExtraParamsMiddleware(config: ExtraParamsConfig): Middlewa
212212 async onRequest ( { request} ) {
213213 let modifiedRequest = request ;
214214
215+ // HTTP methods that don't allow a request body
216+ const methodsWithoutBody = [ 'GET' , 'HEAD' ] ;
217+ const canHaveBody = ! methodsWithoutBody . includes ( modifiedRequest . method . toUpperCase ( ) ) ;
218+
215219 // Add extra headers first (before other modifications)
216220 if ( config . headers && Object . keys ( config . headers ) . length > 0 ) {
217221 const newHeaders = new Headers ( modifiedRequest . headers ) ;
@@ -222,28 +226,26 @@ export function createExtraParamsMiddleware(config: ExtraParamsConfig): Middlewa
222226 modifiedRequest = new Request ( modifiedRequest . url , {
223227 method : modifiedRequest . method ,
224228 headers : newHeaders ,
225- body : modifiedRequest . body ,
226- duplex : modifiedRequest . body ? 'half' : undefined ,
229+ ...( canHaveBody && modifiedRequest . body ? { body : modifiedRequest . body , duplex : 'half' } : { } ) ,
227230 } as RequestInit ) ;
228231 }
229232
230233 // Add extra query parameters
231234 if ( config . query && Object . keys ( config . query ) . length > 0 ) {
232- const url = new URL ( request . url ) ;
235+ const url = new URL ( modifiedRequest . url ) ;
233236 for ( const [ key , value ] of Object . entries ( config . query ) ) {
234237 if ( value !== undefined ) {
235238 url . searchParams . set ( key , String ( value ) ) ;
236239 }
237240 }
238241 logger . trace (
239- { extraQuery : config . query , originalUrl : request . url , newUrl : url . toString ( ) } ,
242+ { extraQuery : config . query , originalUrl : modifiedRequest . url , newUrl : url . toString ( ) } ,
240243 '[ExtraParams] Adding extra query params to URL' ,
241244 ) ;
242245 modifiedRequest = new Request ( url . toString ( ) , {
243- method : request . method ,
244- headers : request . headers ,
245- body : request . body ,
246- duplex : request . body ? 'half' : undefined ,
246+ method : modifiedRequest . method ,
247+ headers : modifiedRequest . headers ,
248+ ...( canHaveBody && modifiedRequest . body ? { body : modifiedRequest . body , duplex : 'half' } : { } ) ,
247249 } as RequestInit ) ;
248250 }
249251
@@ -268,8 +270,8 @@ export function createExtraParamsMiddleware(config: ExtraParamsConfig): Middlewa
268270 } catch {
269271 logger . warn ( '[ExtraParams] Could not parse request body as JSON, skipping body merge' ) ;
270272 }
271- } else if ( ! modifiedRequest . body ) {
272- // No existing body, create one with extra fields
273+ } else if ( ! modifiedRequest . body && canHaveBody ) {
274+ // No existing body, create one with extra fields (only for methods that allow a body)
273275 logger . trace ( { body : config . body } , '[ExtraParams] Creating new body with extra fields' ) ;
274276 const headers = new Headers ( modifiedRequest . headers ) ;
275277 headers . set ( 'content-type' , 'application/json' ) ;
0 commit comments