@hey-api/openapi-ts 0.82.3 → 0.82.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -67,7 +67,7 @@ export const createClient = (config: Config = {}): Client => {
67
67
  ...options,
68
68
  headers: mergeHeaders(_config.headers, options.headers),
69
69
  httpClient: options.httpClient ?? _config.httpClient,
70
- serializedBody: options.body as any,
70
+ serializedBody: undefined,
71
71
  };
72
72
 
73
73
  if (!opts.httpClient) {
@@ -81,12 +81,12 @@ export const createClient = (config: Config = {}): Client => {
81
81
  }
82
82
  }
83
83
 
84
- if (opts.body && opts.bodySerializer) {
84
+ if (opts.body !== undefined && opts.bodySerializer) {
85
85
  opts.serializedBody = opts.bodySerializer(opts.body);
86
86
  }
87
87
 
88
88
  // remove Content-Type header if body is empty to avoid sending invalid requests
89
- if (opts.serializedBody === undefined || opts.serializedBody === '') {
89
+ if (opts.body === undefined || opts.serializedBody === '') {
90
90
  opts.headers.delete('Content-Type');
91
91
  }
92
92
 
@@ -95,7 +95,7 @@ export const createClient = (config: Config = {}): Client => {
95
95
  const req = new HttpRequest<unknown>(
96
96
  opts.method ?? 'GET',
97
97
  url,
98
- opts.serializedBody || null,
98
+ getValidRequestBody(opts),
99
99
  {
100
100
  redirect: 'follow',
101
101
  ...opts,
@@ -199,6 +199,25 @@ export const createClient = (config: Config = {}): Client => {
199
199
  }
200
200
  };
201
201
 
202
+ function getValidRequestBody(options: ResolvedRequestOptions) {
203
+ const hasBody = options.body !== undefined;
204
+ const isSerializedBody = hasBody && options.bodySerializer;
205
+
206
+ if (isSerializedBody) {
207
+ const hasSerializedBody =
208
+ options.serializedBody !== undefined && options.serializedBody !== '';
209
+
210
+ return hasSerializedBody ? options.serializedBody : null;
211
+ }
212
+
213
+ // plain/text body
214
+ if (hasBody) {
215
+ return options.body;
216
+ }
217
+
218
+ return undefined;
219
+ }
220
+
202
221
  const makeMethodFn =
203
222
  (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
204
223
  request({ ...options, method });
@@ -57,7 +57,7 @@ export const createClient = (config: Config = {}): Client => {
57
57
  await opts.requestValidator(opts);
58
58
  }
59
59
 
60
- if (opts.body && opts.bodySerializer) {
60
+ if (opts.body !== undefined && opts.bodySerializer) {
61
61
  opts.body = opts.bodySerializer(opts.body);
62
62
  }
63
63
 
@@ -78,7 +78,7 @@ export const createClient = (config: Config = {}): Client => {
78
78
  const response = await _axios({
79
79
  ...optsWithoutAuth,
80
80
  baseURL: opts.baseURL as string,
81
- data: opts.body,
81
+ data: getValidRequestBody(opts),
82
82
  headers: opts.headers as RawAxiosRequestHeaders,
83
83
  // let `paramsSerializer()` handle query params if it exists
84
84
  params: opts.paramsSerializer ? opts.query : undefined,
@@ -112,6 +112,23 @@ export const createClient = (config: Config = {}): Client => {
112
112
  }
113
113
  };
114
114
 
115
+ function getValidRequestBody(options: RequestOptions) {
116
+ const hasBody = options.body !== undefined;
117
+ const isSerializedBody = hasBody && options.bodySerializer;
118
+
119
+ if (isSerializedBody) {
120
+ return options.body !== '' ? options.body : null;
121
+ }
122
+
123
+ // plain/text body
124
+ if (hasBody) {
125
+ return options.body;
126
+ }
127
+
128
+ // no body was provided
129
+ return undefined;
130
+ }
131
+
115
132
  const makeMethodFn =
116
133
  (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
117
134
  request({ ...options, method });
@@ -58,12 +58,12 @@ export const createClient = (config: Config = {}): Client => {
58
58
  await opts.requestValidator(opts);
59
59
  }
60
60
 
61
- if (opts.body && opts.bodySerializer) {
61
+ if (opts.body !== undefined && opts.bodySerializer) {
62
62
  opts.serializedBody = opts.bodySerializer(opts.body);
63
63
  }
64
64
 
65
65
  // remove Content-Type header if body is empty to avoid sending invalid requests
66
- if (opts.serializedBody === undefined || opts.serializedBody === '') {
66
+ if (opts.body === undefined || opts.serializedBody === '') {
67
67
  opts.headers.delete('Content-Type');
68
68
  }
69
69
 
@@ -78,7 +78,7 @@ export const createClient = (config: Config = {}): Client => {
78
78
  const requestInit: ReqInit = {
79
79
  redirect: 'follow',
80
80
  ...opts,
81
- body: opts.serializedBody,
81
+ body: getValidRequestBody(opts),
82
82
  };
83
83
 
84
84
  let request = new Request(url, requestInit);
@@ -210,6 +210,26 @@ export const createClient = (config: Config = {}): Client => {
210
210
  };
211
211
  };
212
212
 
213
+ function getValidRequestBody(options: ResolvedRequestOptions) {
214
+ const hasBody = options.body !== undefined;
215
+ const isSerializedBody = hasBody && options.bodySerializer;
216
+
217
+ if (isSerializedBody) {
218
+ const hasSerializedBody =
219
+ options.serializedBody !== undefined && options.serializedBody !== '';
220
+
221
+ return hasSerializedBody ? options.serializedBody : null;
222
+ }
223
+
224
+ // plain/text body
225
+ if (hasBody) {
226
+ return options.body;
227
+ }
228
+
229
+ // no body was provided
230
+ return undefined;
231
+ }
232
+
213
233
  const makeMethodFn =
214
234
  (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
215
235
  request({ ...options, method });
@@ -57,12 +57,12 @@ export const createClient = (config: Config = {}): Client => {
57
57
  await opts.requestValidator(opts);
58
58
  }
59
59
 
60
- if (opts.body && opts.bodySerializer) {
60
+ if (opts.body !== undefined && opts.bodySerializer) {
61
61
  opts.serializedBody = opts.bodySerializer(opts.body);
62
62
  }
63
63
 
64
64
  // remove Content-Type header if body is empty to avoid sending invalid requests
65
- if (opts.serializedBody === undefined || opts.serializedBody === '') {
65
+ if (opts.body === undefined || opts.serializedBody === '') {
66
66
  opts.headers.delete('Content-Type');
67
67
  }
68
68
 
@@ -85,10 +85,12 @@ export const createClient = (config: Config = {}): Client => {
85
85
  // fetch must be assigned here, otherwise it would throw the error:
86
86
  // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
87
87
  const _fetch = opts.fetch!;
88
- let response = await _fetch(url, {
88
+ const requestInit: ReqInit = {
89
89
  ...opts,
90
- body: opts.serializedBody as ReqInit['body'],
91
- });
90
+ body: getValidRequestBody(opts),
91
+ };
92
+
93
+ let response = await _fetch(url, requestInit);
92
94
 
93
95
  for (const fn of interceptors.response._fns) {
94
96
  if (fn) {
@@ -196,6 +198,26 @@ export const createClient = (config: Config = {}): Client => {
196
198
  };
197
199
  };
198
200
 
201
+ function getValidRequestBody(options: ResolvedRequestOptions) {
202
+ const hasBody = options.body !== undefined;
203
+ const isSerializedBody = hasBody && options.bodySerializer;
204
+
205
+ if (isSerializedBody) {
206
+ const hasSerializedBody =
207
+ options.serializedBody !== undefined && options.serializedBody !== '';
208
+
209
+ return hasSerializedBody ? options.serializedBody : null;
210
+ }
211
+
212
+ // plain/text body
213
+ if (hasBody) {
214
+ return options.body;
215
+ }
216
+
217
+ // no body was provided
218
+ return undefined;
219
+ }
220
+
199
221
  const makeMethodFn =
200
222
  (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
201
223
  request({ ...options, method });
@@ -59,7 +59,7 @@ export const createClient = (config: Config = {}): Client => {
59
59
 
60
60
  const request: Client['request'] = ({
61
61
  asyncDataOptions,
62
- composable,
62
+ composable = '$fetch',
63
63
  ...options
64
64
  }) => {
65
65
  const key = options.key;
@@ -62,7 +62,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
62
62
  }
63
63
 
64
64
  export interface RequestOptions<
65
- TComposable extends Composable = Composable,
65
+ TComposable extends Composable = '$fetch',
66
66
  ResT = unknown,
67
67
  DefaultT = undefined,
68
68
  Url extends string = string,
@@ -87,7 +87,7 @@ export interface RequestOptions<
87
87
  | 'sseMaxRetryDelay'
88
88
  > {
89
89
  asyncDataOptions?: AsyncDataOptions<ResT, ResT, KeysOf<ResT>, DefaultT>;
90
- composable: TComposable;
90
+ composable?: TComposable;
91
91
  key?: string;
92
92
  /**
93
93
  * Security mechanism(s) to use for the request.
@@ -117,7 +117,7 @@ export interface ClientOptions {
117
117
  }
118
118
 
119
119
  type MethodFn = <
120
- TComposable extends Composable,
120
+ TComposable extends Composable = '$fetch',
121
121
  ResT = unknown,
122
122
  TError = unknown,
123
123
  DefaultT = undefined,
@@ -126,7 +126,7 @@ type MethodFn = <
126
126
  ) => RequestResult<TComposable, ResT, TError>;
127
127
 
128
128
  type SseFn = <
129
- TComposable extends Composable,
129
+ TComposable extends Composable = '$fetch',
130
130
  ResT = unknown,
131
131
  TError = unknown,
132
132
  DefaultT = undefined,
@@ -135,7 +135,7 @@ type SseFn = <
135
135
  ) => Promise<ServerSentEventsResult<RequestResult<TComposable, ResT, TError>>>;
136
136
 
137
137
  type RequestFn = <
138
- TComposable extends Composable,
138
+ TComposable extends Composable = '$fetch',
139
139
  ResT = unknown,
140
140
  TError = unknown,
141
141
  DefaultT = undefined,
@@ -179,7 +179,7 @@ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn, SseFn>;
179
179
  type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
180
180
 
181
181
  export type Options<
182
- TComposable extends Composable,
182
+ TComposable extends Composable = '$fetch',
183
183
  TData extends TDataShape = TDataShape,
184
184
  ResT = unknown,
185
185
  DefaultT = undefined,