@@ -47,7 +47,7 @@ export const createClient = (config: Config = {}): Client => {
47
47
ResolvedRequestOptions
48
48
> ( ) ;
49
49
50
- // Resolve final options, serialized body, network body and URL
50
+ // precompute serialized / network body
51
51
const resolveOptions = async ( options : RequestOptions ) => {
52
52
const opts = {
53
53
..._config ,
@@ -71,13 +71,13 @@ export const createClient = (config: Config = {}): Client => {
71
71
opts . serializedBody = opts . bodySerializer ( opts . body ) ;
72
72
}
73
73
74
- // remove Content-Type header if body is empty to avoid sending invalid requests
74
+ // remove Content-Type if body is empty to avoid invalid requests
75
75
if ( opts . body === undefined || opts . serializedBody === '' ) {
76
76
opts . headers . delete ( 'Content-Type' ) ;
77
77
}
78
78
79
- // If user provides a raw body (no serializer), adjust Content-Type sensibly.
80
- // Avoid overriding explicit user-defined headers; only correct the default JSON header.
79
+ // if a raw body is provided (no serializer), adjust Content-Type only when it
80
+ // equals the default JSON value to better match the concrete body type
81
81
if (
82
82
opts . body !== undefined &&
83
83
opts . bodySerializer === null &&
@@ -86,13 +86,13 @@ export const createClient = (config: Config = {}): Client => {
86
86
) {
87
87
const b : unknown = opts . body ;
88
88
if ( typeof FormData !== 'undefined' && b instanceof FormData ) {
89
- // Let the runtime set proper boundary
89
+ // let the runtime set the multipart boundary
90
90
opts . headers . delete ( 'Content-Type' ) ;
91
91
} else if (
92
92
typeof URLSearchParams !== 'undefined' &&
93
93
b instanceof URLSearchParams
94
94
) {
95
- // Set standard urlencoded content type with charset
95
+ // standard urlencoded content type (+ charset)
96
96
opts . headers . set (
97
97
'Content-Type' ,
98
98
'application/x-www-form-urlencoded;charset=UTF-8' ,
@@ -102,13 +102,13 @@ export const createClient = (config: Config = {}): Client => {
102
102
if ( t ) {
103
103
opts . headers . set ( 'Content-Type' , t ) ;
104
104
} else {
105
- // No known type for the blob : avoid sending misleading JSON header
105
+ // unknown blob type: avoid sending a misleading JSON header
106
106
opts . headers . delete ( 'Content-Type' ) ;
107
107
}
108
108
}
109
109
}
110
110
111
- // Precompute network body for retries and consistent handling
111
+ // precompute network body (stability for retries and interceptors)
112
112
const networkBody = getValidRequestBody ( opts ) as
113
113
| RequestInit [ 'body' ]
114
114
| null
@@ -119,7 +119,7 @@ export const createClient = (config: Config = {}): Client => {
119
119
return { networkBody, opts, url } ;
120
120
} ;
121
121
122
- // Apply request interceptors to a Request and reflect header/method/signal
122
+ // apply request interceptors and mirror header/method/signal back to opts
123
123
const applyRequestInterceptors = async (
124
124
request : Request ,
125
125
opts : ResolvedRequestOptions ,
@@ -129,18 +129,17 @@ export const createClient = (config: Config = {}): Client => {
129
129
request = await fn ( request , opts ) ;
130
130
}
131
131
}
132
- // Reflect any interceptor changes into opts used for network and downstream
132
+ // reflect interceptor changes into opts used by the network layer
133
133
opts . headers = request . headers ;
134
134
opts . method = request . method as Uppercase < HttpMethod > ;
135
- // Note: we intentionally ignore request.body changes from interceptors to
136
- // avoid turning serialized bodies into streams. Body is sourced solely
137
- // from getValidRequestBody(options) for consistency.
138
- // Attempt to reflect possible signal changes
135
+ // ignore request.body changes to avoid turning serialized bodies into streams
136
+ // body comes only from getValidRequestBody(options)
137
+ // reflect signal if present
139
138
opts . signal = ( request as any ) . signal as AbortSignal | undefined ;
140
139
return request ;
141
140
} ;
142
141
143
- // Build ofetch options with stable retry logic based on body repeatability
142
+ // build ofetch options with stable retry logic based on body repeatability
144
143
const buildNetworkOptions = (
145
144
opts : ResolvedRequestOptions ,
146
145
body : BodyInit | null | undefined ,
@@ -158,13 +157,13 @@ export const createClient = (config: Config = {}): Client => {
158
157
opts,
159
158
url,
160
159
} = await resolveOptions ( options as any ) ;
161
- // Compute response type mapping once
160
+ // map parseAs -> ofetch responseType once per request
162
161
const ofetchResponseType : OfetchResponseType | undefined =
163
162
mapParseAsToResponseType ( opts . parseAs , opts . responseType ) ;
164
163
165
164
const $ofetch = opts . ofetch ?? ofetch ;
166
165
167
- // Always create Request pre- network (align with client-fetch)
166
+ // create Request before network to run middleware consistently
168
167
const networkBody = initialNetworkBody ;
169
168
const requestInit : ReqInit = {
170
169
body : networkBody ,
@@ -178,7 +177,7 @@ export const createClient = (config: Config = {}): Client => {
178
177
request = await applyRequestInterceptors ( request , opts ) ;
179
178
const finalUrl = request . url ;
180
179
181
- // Build ofetch options and perform the request
180
+ // build ofetch options and perform the request (.raw keeps the Response)
182
181
const responseOptions = buildNetworkOptions (
183
182
opts as ResolvedRequestOptions ,
184
183
networkBody ,
@@ -208,7 +207,7 @@ export const createClient = (config: Config = {}): Client => {
208
207
}
209
208
}
210
209
211
- // Ensure error is never undefined after interceptors
210
+ // ensure error is never undefined after interceptors
212
211
finalError = ( finalError as any ) || ( { } as string ) ;
213
212
214
213
if ( opts . throwOnError ) {
@@ -226,7 +225,7 @@ export const createClient = (config: Config = {}): Client => {
226
225
( method : Uppercase < HttpMethod > ) => async ( options : RequestOptions ) => {
227
226
const { networkBody, opts, url } = await resolveOptions ( options ) ;
228
227
const optsForSse : any = { ...opts } ;
229
- delete optsForSse . body ;
228
+ delete optsForSse . body ; // body is provided via serializedBody below
230
229
return createSseClient ( {
231
230
...optsForSse ,
232
231
fetch : opts . fetch ,
0 commit comments