@hey-api/openapi-ts 0.82.3 → 0.82.5

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.
@@ -16,6 +16,7 @@ import { filter } from 'rxjs/operators';
16
16
 
17
17
  import { createSseClient } from '../core/serverSentEvents';
18
18
  import type { HttpMethod } from '../core/types';
19
+ import { getValidRequestBody } from '../core/utils';
19
20
  import type {
20
21
  Client,
21
22
  Config,
@@ -67,7 +68,7 @@ export const createClient = (config: Config = {}): Client => {
67
68
  ...options,
68
69
  headers: mergeHeaders(_config.headers, options.headers),
69
70
  httpClient: options.httpClient ?? _config.httpClient,
70
- serializedBody: options.body as any,
71
+ serializedBody: undefined,
71
72
  };
72
73
 
73
74
  if (!opts.httpClient) {
@@ -81,12 +82,12 @@ export const createClient = (config: Config = {}): Client => {
81
82
  }
82
83
  }
83
84
 
84
- if (opts.body && opts.bodySerializer) {
85
+ if (opts.body !== undefined && opts.bodySerializer) {
85
86
  opts.serializedBody = opts.bodySerializer(opts.body);
86
87
  }
87
88
 
88
89
  // remove Content-Type header if body is empty to avoid sending invalid requests
89
- if (opts.serializedBody === undefined || opts.serializedBody === '') {
90
+ if (opts.body === undefined || opts.serializedBody === '') {
90
91
  opts.headers.delete('Content-Type');
91
92
  }
92
93
 
@@ -95,7 +96,7 @@ export const createClient = (config: Config = {}): Client => {
95
96
  const req = new HttpRequest<unknown>(
96
97
  opts.method ?? 'GET',
97
98
  url,
98
- opts.serializedBody || null,
99
+ getValidRequestBody(opts),
99
100
  {
100
101
  redirect: 'follow',
101
102
  ...opts,
@@ -3,6 +3,7 @@ import axios from 'axios';
3
3
 
4
4
  import { createSseClient } from '../core/serverSentEvents';
5
5
  import type { HttpMethod } from '../core/types';
6
+ import { getValidRequestBody } from '../core/utils';
6
7
  import type { Client, Config, RequestOptions } from './types';
7
8
  import {
8
9
  buildUrl,
@@ -57,7 +58,7 @@ export const createClient = (config: Config = {}): Client => {
57
58
  await opts.requestValidator(opts);
58
59
  }
59
60
 
60
- if (opts.body && opts.bodySerializer) {
61
+ if (opts.body !== undefined && opts.bodySerializer) {
61
62
  opts.body = opts.bodySerializer(opts.body);
62
63
  }
63
64
 
@@ -78,7 +79,7 @@ export const createClient = (config: Config = {}): Client => {
78
79
  const response = await _axios({
79
80
  ...optsWithoutAuth,
80
81
  baseURL: opts.baseURL as string,
81
- data: opts.body,
82
+ data: getValidRequestBody(opts),
82
83
  headers: opts.headers as RawAxiosRequestHeaders,
83
84
  // let `paramsSerializer()` handle query params if it exists
84
85
  params: opts.paramsSerializer ? opts.query : undefined,
@@ -1,4 +1,4 @@
1
- import type { QuerySerializer } from './bodySerializer';
1
+ import type { BodySerializer, QuerySerializer } from './bodySerializer';
2
2
  import {
3
3
  type ArraySeparatorStyle,
4
4
  serializeArrayParam,
@@ -110,3 +110,32 @@ export const getUrl = ({
110
110
  }
111
111
  return url;
112
112
  };
113
+
114
+ export function getValidRequestBody(options: {
115
+ body?: unknown;
116
+ bodySerializer?: BodySerializer | null;
117
+ serializedBody?: unknown;
118
+ }) {
119
+ const hasBody = options.body !== undefined;
120
+ const isSerializedBody = hasBody && options.bodySerializer;
121
+
122
+ if (isSerializedBody) {
123
+ if ('serializedBody' in options) {
124
+ const hasSerializedBody =
125
+ options.serializedBody !== undefined && options.serializedBody !== '';
126
+
127
+ return hasSerializedBody ? options.serializedBody : null;
128
+ }
129
+
130
+ // not all clients implement a serializedBody property (i.e. client-axios)
131
+ return options.body !== '' ? options.body : null;
132
+ }
133
+
134
+ // plain/text body
135
+ if (hasBody) {
136
+ return options.body;
137
+ }
138
+
139
+ // no body was provided
140
+ return undefined;
141
+ }
@@ -1,5 +1,6 @@
1
1
  import { createSseClient } from '../core/serverSentEvents';
2
2
  import type { HttpMethod } from '../core/types';
3
+ import { getValidRequestBody } from '../core/utils';
3
4
  import type {
4
5
  Client,
5
6
  Config,
@@ -58,12 +59,12 @@ export const createClient = (config: Config = {}): Client => {
58
59
  await opts.requestValidator(opts);
59
60
  }
60
61
 
61
- if (opts.body && opts.bodySerializer) {
62
+ if (opts.body !== undefined && opts.bodySerializer) {
62
63
  opts.serializedBody = opts.bodySerializer(opts.body);
63
64
  }
64
65
 
65
66
  // remove Content-Type header if body is empty to avoid sending invalid requests
66
- if (opts.serializedBody === undefined || opts.serializedBody === '') {
67
+ if (opts.body === undefined || opts.serializedBody === '') {
67
68
  opts.headers.delete('Content-Type');
68
69
  }
69
70
 
@@ -78,7 +79,7 @@ export const createClient = (config: Config = {}): Client => {
78
79
  const requestInit: ReqInit = {
79
80
  redirect: 'follow',
80
81
  ...opts,
81
- body: opts.serializedBody,
82
+ body: getValidRequestBody(opts),
82
83
  };
83
84
 
84
85
  let request = new Request(url, requestInit);
@@ -181,17 +181,27 @@ export const mergeConfigs = (a: Config, b: Config): Config => {
181
181
  return config;
182
182
  };
183
183
 
184
+ const headersEntries = (headers: Headers): Array<[string, string]> => {
185
+ const entries: Array<[string, string]> = [];
186
+ headers.forEach((value, key) => {
187
+ entries.push([key, value]);
188
+ });
189
+ return entries;
190
+ };
191
+
184
192
  export const mergeHeaders = (
185
193
  ...headers: Array<Required<Config>['headers'] | undefined>
186
194
  ): Headers => {
187
195
  const mergedHeaders = new Headers();
188
196
  for (const header of headers) {
189
- if (!header || typeof header !== 'object') {
197
+ if (!header) {
190
198
  continue;
191
199
  }
192
200
 
193
201
  const iterator =
194
- header instanceof Headers ? header.entries() : Object.entries(header);
202
+ header instanceof Headers
203
+ ? headersEntries(header)
204
+ : Object.entries(header);
195
205
 
196
206
  for (const [key, value] of iterator) {
197
207
  if (value === null) {
@@ -1,5 +1,6 @@
1
1
  import { createSseClient } from '../core/serverSentEvents';
2
2
  import type { HttpMethod } from '../core/types';
3
+ import { getValidRequestBody } from '../core/utils';
3
4
  import type {
4
5
  Client,
5
6
  Config,
@@ -57,12 +58,12 @@ export const createClient = (config: Config = {}): Client => {
57
58
  await opts.requestValidator(opts);
58
59
  }
59
60
 
60
- if (opts.body && opts.bodySerializer) {
61
+ if (opts.body !== undefined && opts.bodySerializer) {
61
62
  opts.serializedBody = opts.bodySerializer(opts.body);
62
63
  }
63
64
 
64
65
  // remove Content-Type header if body is empty to avoid sending invalid requests
65
- if (opts.serializedBody === undefined || opts.serializedBody === '') {
66
+ if (opts.body === undefined || opts.serializedBody === '') {
66
67
  opts.headers.delete('Content-Type');
67
68
  }
68
69
 
@@ -85,10 +86,12 @@ export const createClient = (config: Config = {}): Client => {
85
86
  // fetch must be assigned here, otherwise it would throw the error:
86
87
  // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
87
88
  const _fetch = opts.fetch!;
88
- let response = await _fetch(url, {
89
+ const requestInit: ReqInit = {
89
90
  ...opts,
90
- body: opts.serializedBody as ReqInit['body'],
91
- });
91
+ body: getValidRequestBody(opts),
92
+ };
93
+
94
+ let response = await _fetch(url, requestInit);
92
95
 
93
96
  for (const fn of interceptors.response._fns) {
94
97
  if (fn) {
@@ -293,6 +293,14 @@ export const mergeConfigs = (a: Config, b: Config): Config => {
293
293
  return config;
294
294
  };
295
295
 
296
+ const headersEntries = (headers: Headers): Array<[string, string]> => {
297
+ const entries: Array<[string, string]> = [];
298
+ headers.forEach((value, key) => {
299
+ entries.push([key, value]);
300
+ });
301
+ return entries;
302
+ };
303
+
296
304
  export const mergeHeaders = (
297
305
  ...headers: Array<Required<Config>['headers'] | undefined>
298
306
  ): Headers => {
@@ -303,7 +311,9 @@ export const mergeHeaders = (
303
311
  }
304
312
 
305
313
  const iterator =
306
- header instanceof Headers ? header.entries() : Object.entries(header);
314
+ header instanceof Headers
315
+ ? headersEntries(header)
316
+ : Object.entries(header);
307
317
 
308
318
  for (const [key, value] of iterator) {
309
319
  if (value === null) {
@@ -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,
@@ -250,6 +250,14 @@ export const mergeConfigs = (a: Config, b: Config): Config => {
250
250
  return config;
251
251
  };
252
252
 
253
+ const headersEntries = (headers: Headers): Array<[string, string]> => {
254
+ const entries: Array<[string, string]> = [];
255
+ headers.forEach((value, key) => {
256
+ entries.push([key, value]);
257
+ });
258
+ return entries;
259
+ };
260
+
253
261
  export const mergeHeaders = (
254
262
  ...headers: Array<Required<Config>['headers'] | undefined>
255
263
  ): Headers => {
@@ -266,7 +274,7 @@ export const mergeHeaders = (
266
274
 
267
275
  const iterator =
268
276
  h instanceof Headers
269
- ? h.entries()
277
+ ? headersEntries(h)
270
278
  : Object.entries(h as Record<string, unknown>);
271
279
 
272
280
  for (const [key, value] of iterator) {