@@ -26,6 +26,7 @@ import {
2626 MaybeFunction ,
2727 Response ,
2828 RemoveIndex ,
29+ RequestMiddlware ,
2930} from './types'
3031import * as Dom from './types.dom'
3132import { resolveRequestDocument } from './resolveRequestDocument'
@@ -156,11 +157,11 @@ const post = async <V = Variables>({
156157 variables ?: V
157158 headers ?: Dom . RequestInit [ 'headers' ]
158159 operationName ?: string
159- middleware ?: ( request : Dom . RequestInit ) => Dom . RequestInit | Promise < Dom . RequestInit >
160+ middleware ?: RequestMiddlware
160161} ) => {
161162 const body = createRequestBody ( query , variables , operationName , fetchOptions . jsonSerializer )
162163
163- let options : Dom . RequestInit = {
164+ let init : Dom . RequestInit = {
164165 method : 'POST' ,
165166 headers : {
166167 ...( typeof body === 'string' ? { 'Content-Type' : 'application/json' } : { } ) ,
@@ -170,9 +171,9 @@ const post = async <V = Variables>({
170171 ...fetchOptions ,
171172 }
172173 if ( middleware ) {
173- options = await Promise . resolve ( middleware ( options ) )
174+ ; ( { url , ... init } = await Promise . resolve ( middleware ( { ... init , url } ) ) )
174175 }
175- return await fetch ( url , options )
176+ return await fetch ( url , init )
176177}
177178
178179/**
@@ -195,7 +196,7 @@ const get = async <V = Variables>({
195196 variables ?: V
196197 headers ?: HeadersInit
197198 operationName ?: string
198- middleware ?: ( request : Dom . RequestInit ) => Dom . RequestInit | Promise < Dom . RequestInit >
199+ middleware ?: RequestMiddlware
199200} ) => {
200201 const queryParams = buildGetQueryParams < V > ( {
201202 query,
@@ -204,15 +205,15 @@ const get = async <V = Variables>({
204205 jsonSerializer : fetchOptions . jsonSerializer ,
205206 } as TBuildGetQueryParams < V > )
206207
207- let options : Dom . RequestInit = {
208+ let init : Dom . RequestInit = {
208209 method : 'GET' ,
209210 headers,
210211 ...fetchOptions ,
211212 }
212213 if ( middleware ) {
213- options = await Promise . resolve ( middleware ( options ) )
214+ ; ( { url , ... init } = await Promise . resolve ( middleware ( { ... init , url } ) ) )
214215 }
215- return await fetch ( `${ url } ?${ queryParams } ` , options )
216+ return await fetch ( `${ url } ?${ queryParams } ` , init )
216217}
217218
218219/**
@@ -461,7 +462,7 @@ async function makeRequest<T = any, V = Variables>({
461462 fetch : any
462463 method : string
463464 fetchOptions : Dom . RequestInit
464- middleware ?: ( request : Dom . RequestInit ) => Dom . RequestInit | Promise < Dom . RequestInit >
465+ middleware ?: RequestMiddlware
465466} ) : Promise < Response < T > > {
466467 const fetcher = method . toUpperCase ( ) === 'POST' ? post : get
467468 const isBathchingQuery = Array . isArray ( query )
0 commit comments