Skip to content

Commit e1d6b27

Browse files
brolnickijmrlubos
authored andcommitted
chore(ofetch): cleanup comments
1 parent 9cf1458 commit e1d6b27

File tree

1 file changed

+19
-20
lines changed
  • packages/openapi-ts/src/plugins/@hey-api/client-ofetch/bundle

1 file changed

+19
-20
lines changed

packages/openapi-ts/src/plugins/@hey-api/client-ofetch/bundle/client.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const createClient = (config: Config = {}): Client => {
4747
ResolvedRequestOptions
4848
>();
4949

50-
// Resolve final options, serialized body, network body and URL
50+
// precompute serialized / network body
5151
const resolveOptions = async (options: RequestOptions) => {
5252
const opts = {
5353
..._config,
@@ -71,13 +71,13 @@ export const createClient = (config: Config = {}): Client => {
7171
opts.serializedBody = opts.bodySerializer(opts.body);
7272
}
7373

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
7575
if (opts.body === undefined || opts.serializedBody === '') {
7676
opts.headers.delete('Content-Type');
7777
}
7878

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
8181
if (
8282
opts.body !== undefined &&
8383
opts.bodySerializer === null &&
@@ -86,13 +86,13 @@ export const createClient = (config: Config = {}): Client => {
8686
) {
8787
const b: unknown = opts.body;
8888
if (typeof FormData !== 'undefined' && b instanceof FormData) {
89-
// Let the runtime set proper boundary
89+
// let the runtime set the multipart boundary
9090
opts.headers.delete('Content-Type');
9191
} else if (
9292
typeof URLSearchParams !== 'undefined' &&
9393
b instanceof URLSearchParams
9494
) {
95-
// Set standard urlencoded content type with charset
95+
// standard urlencoded content type (+ charset)
9696
opts.headers.set(
9797
'Content-Type',
9898
'application/x-www-form-urlencoded;charset=UTF-8',
@@ -102,13 +102,13 @@ export const createClient = (config: Config = {}): Client => {
102102
if (t) {
103103
opts.headers.set('Content-Type', t);
104104
} else {
105-
// No known type for the blob: avoid sending misleading JSON header
105+
// unknown blob type: avoid sending a misleading JSON header
106106
opts.headers.delete('Content-Type');
107107
}
108108
}
109109
}
110110

111-
// Precompute network body for retries and consistent handling
111+
// precompute network body (stability for retries and interceptors)
112112
const networkBody = getValidRequestBody(opts) as
113113
| RequestInit['body']
114114
| null
@@ -119,7 +119,7 @@ export const createClient = (config: Config = {}): Client => {
119119
return { networkBody, opts, url };
120120
};
121121

122-
// Apply request interceptors to a Request and reflect header/method/signal
122+
// apply request interceptors and mirror header/method/signal back to opts
123123
const applyRequestInterceptors = async (
124124
request: Request,
125125
opts: ResolvedRequestOptions,
@@ -129,18 +129,17 @@ export const createClient = (config: Config = {}): Client => {
129129
request = await fn(request, opts);
130130
}
131131
}
132-
// Reflect any interceptor changes into opts used for network and downstream
132+
// reflect interceptor changes into opts used by the network layer
133133
opts.headers = request.headers;
134134
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
139138
opts.signal = (request as any).signal as AbortSignal | undefined;
140139
return request;
141140
};
142141

143-
// Build ofetch options with stable retry logic based on body repeatability
142+
// build ofetch options with stable retry logic based on body repeatability
144143
const buildNetworkOptions = (
145144
opts: ResolvedRequestOptions,
146145
body: BodyInit | null | undefined,
@@ -158,13 +157,13 @@ export const createClient = (config: Config = {}): Client => {
158157
opts,
159158
url,
160159
} = await resolveOptions(options as any);
161-
// Compute response type mapping once
160+
// map parseAs -> ofetch responseType once per request
162161
const ofetchResponseType: OfetchResponseType | undefined =
163162
mapParseAsToResponseType(opts.parseAs, opts.responseType);
164163

165164
const $ofetch = opts.ofetch ?? ofetch;
166165

167-
// Always create Request pre-network (align with client-fetch)
166+
// create Request before network to run middleware consistently
168167
const networkBody = initialNetworkBody;
169168
const requestInit: ReqInit = {
170169
body: networkBody,
@@ -178,7 +177,7 @@ export const createClient = (config: Config = {}): Client => {
178177
request = await applyRequestInterceptors(request, opts);
179178
const finalUrl = request.url;
180179

181-
// Build ofetch options and perform the request
180+
// build ofetch options and perform the request (.raw keeps the Response)
182181
const responseOptions = buildNetworkOptions(
183182
opts as ResolvedRequestOptions,
184183
networkBody,
@@ -208,7 +207,7 @@ export const createClient = (config: Config = {}): Client => {
208207
}
209208
}
210209

211-
// Ensure error is never undefined after interceptors
210+
// ensure error is never undefined after interceptors
212211
finalError = (finalError as any) || ({} as string);
213212

214213
if (opts.throwOnError) {
@@ -226,7 +225,7 @@ export const createClient = (config: Config = {}): Client => {
226225
(method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
227226
const { networkBody, opts, url } = await resolveOptions(options);
228227
const optsForSse: any = { ...opts };
229-
delete optsForSse.body;
228+
delete optsForSse.body; // body is provided via serializedBody below
230229
return createSseClient({
231230
...optsForSse,
232231
fetch: opts.fetch,

0 commit comments

Comments
 (0)