@hey-api/openapi-ts 0.91.0 → 0.92.0

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.
Files changed (44) hide show
  1. package/README.md +7 -7
  2. package/dist/clients/angular/client.ts +23 -49
  3. package/dist/clients/angular/types.ts +11 -34
  4. package/dist/clients/angular/utils.ts +6 -22
  5. package/dist/clients/axios/client.ts +16 -27
  6. package/dist/clients/axios/types.ts +17 -54
  7. package/dist/clients/axios/utils.ts +3 -8
  8. package/dist/clients/core/auth.ts +1 -2
  9. package/dist/clients/core/bodySerializer.ts +5 -21
  10. package/dist/clients/core/params.ts +3 -10
  11. package/dist/clients/core/pathSerializer.ts +4 -14
  12. package/dist/clients/core/queryKeySerializer.ts +6 -25
  13. package/dist/clients/core/serverSentEvents.ts +8 -31
  14. package/dist/clients/core/types.ts +4 -18
  15. package/dist/clients/core/utils.ts +1 -4
  16. package/dist/clients/fetch/client.ts +25 -48
  17. package/dist/clients/fetch/types.ts +12 -40
  18. package/dist/clients/fetch/utils.ts +6 -22
  19. package/dist/clients/ky/client.ts +27 -58
  20. package/dist/clients/ky/types.ts +14 -45
  21. package/dist/clients/ky/utils.ts +6 -22
  22. package/dist/clients/next/client.ts +30 -47
  23. package/dist/clients/next/types.ts +13 -47
  24. package/dist/clients/next/utils.ts +8 -31
  25. package/dist/clients/nuxt/client.ts +18 -37
  26. package/dist/clients/nuxt/types.ts +12 -31
  27. package/dist/clients/nuxt/utils.ts +5 -17
  28. package/dist/clients/ofetch/client.ts +34 -60
  29. package/dist/clients/ofetch/types.ts +13 -44
  30. package/dist/clients/ofetch/utils.ts +20 -58
  31. package/dist/index.d.mts +289 -452
  32. package/dist/index.d.mts.map +1 -1
  33. package/dist/index.mjs +2 -2
  34. package/dist/{init-kvO44gnv.mjs → init-DlaW5Djq.mjs} +355 -86
  35. package/dist/init-DlaW5Djq.mjs.map +1 -0
  36. package/dist/internal.mjs +1 -1
  37. package/dist/run.mjs +3 -3
  38. package/dist/run.mjs.map +1 -1
  39. package/dist/{src-Dmlg6WRV.mjs → src-BYA2YioO.mjs} +5 -8
  40. package/dist/src-BYA2YioO.mjs.map +1 -0
  41. package/dist/types-Ba27ofyy.d.mts.map +1 -1
  42. package/package.json +36 -37
  43. package/dist/init-kvO44gnv.mjs.map +0 -1
  44. package/dist/src-Dmlg6WRV.mjs.map +0 -1
@@ -47,19 +47,14 @@ const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
47
47
  style = 'matrix';
48
48
  }
49
49
 
50
- const value = toValue(
51
- (toValue(path) as Record<string, unknown> | undefined)?.[name],
52
- );
50
+ const value = toValue((toValue(path) as Record<string, unknown> | undefined)?.[name]);
53
51
 
54
52
  if (value === undefined || value === null) {
55
53
  continue;
56
54
  }
57
55
 
58
56
  if (Array.isArray(value)) {
59
- url = url.replace(
60
- match,
61
- serializeArrayParam({ explode, name, style, value }),
62
- );
57
+ url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
63
58
  continue;
64
59
  }
65
60
 
@@ -192,9 +187,7 @@ export const setAuthParams = async ({
192
187
  if (!options.query) {
193
188
  options.query = {};
194
189
  }
195
- const queryValue = toValue(options.query) as
196
- | Record<string, unknown>
197
- | undefined;
190
+ const queryValue = toValue(options.query) as Record<string, unknown> | undefined;
198
191
  if (queryValue) {
199
192
  queryValue[name] = token;
200
193
  }
@@ -282,9 +275,7 @@ export const mergeHeaders = (
282
275
  }
283
276
 
284
277
  const iterator =
285
- h instanceof Headers
286
- ? headersEntries(h)
287
- : Object.entries(h as Record<string, unknown>);
278
+ h instanceof Headers ? headersEntries(h) : Object.entries(h as Record<string, unknown>);
288
279
 
289
280
  for (const [key, value] of iterator) {
290
281
  if (value === null) {
@@ -297,10 +288,7 @@ export const mergeHeaders = (
297
288
  const v = unwrapRefs(value);
298
289
  // assume object headers are meant to be JSON stringified, i.e. their
299
290
  // content value in OpenAPI specification is 'application/json'
300
- mergedHeaders.set(
301
- key,
302
- typeof v === 'object' ? JSON.stringify(v) : (v as string),
303
- );
291
+ mergedHeaders.set(key, typeof v === 'object' ? JSON.stringify(v) : (v as string));
304
292
  }
305
293
  }
306
294
  }
@@ -3,12 +3,7 @@ import { ofetch, type ResponseType as OfetchResponseType } from 'ofetch';
3
3
  import { createSseClient } from '../core/serverSentEvents';
4
4
  import type { HttpMethod } from '../core/types';
5
5
  import { getValidRequestBody } from '../core/utils';
6
- import type {
7
- Client,
8
- Config,
9
- RequestOptions,
10
- ResolvedRequestOptions,
11
- } from './types';
6
+ import type { Client, Config, RequestOptions, ResolvedRequestOptions } from './types';
12
7
  import {
13
8
  buildOfetchOptions,
14
9
  buildUrl,
@@ -40,12 +35,7 @@ export const createClient = (config: Config = {}): Client => {
40
35
  return getConfig();
41
36
  };
42
37
 
43
- const interceptors = createInterceptors<
44
- Request,
45
- Response,
46
- unknown,
47
- ResolvedRequestOptions
48
- >();
38
+ const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>();
49
39
 
50
40
  // precompute serialized / network body
51
41
  const resolveOptions = async (options: RequestOptions) => {
@@ -81,22 +71,15 @@ export const createClient = (config: Config = {}): Client => {
81
71
  if (
82
72
  opts.body !== undefined &&
83
73
  opts.bodySerializer === null &&
84
- (opts.headers.get('Content-Type') || '').toLowerCase() ===
85
- 'application/json'
74
+ (opts.headers.get('Content-Type') || '').toLowerCase() === 'application/json'
86
75
  ) {
87
76
  const b: unknown = opts.body;
88
77
  if (typeof FormData !== 'undefined' && b instanceof FormData) {
89
78
  // let the runtime set the multipart boundary
90
79
  opts.headers.delete('Content-Type');
91
- } else if (
92
- typeof URLSearchParams !== 'undefined' &&
93
- b instanceof URLSearchParams
94
- ) {
80
+ } else if (typeof URLSearchParams !== 'undefined' && b instanceof URLSearchParams) {
95
81
  // standard urlencoded content type (+ charset)
96
- opts.headers.set(
97
- 'Content-Type',
98
- 'application/x-www-form-urlencoded;charset=UTF-8',
99
- );
82
+ opts.headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
100
83
  } else if (typeof Blob !== 'undefined' && b instanceof Blob) {
101
84
  const t = b.type?.trim();
102
85
  if (t) {
@@ -109,10 +92,7 @@ export const createClient = (config: Config = {}): Client => {
109
92
  }
110
93
 
111
94
  // precompute network body (stability for retries and interceptors)
112
- const networkBody = getValidRequestBody(opts) as
113
- | RequestInit['body']
114
- | null
115
- | undefined;
95
+ const networkBody = getValidRequestBody(opts) as RequestInit['body'] | null | undefined;
116
96
 
117
97
  const url = buildUrl(opts);
118
98
 
@@ -158,21 +138,17 @@ export const createClient = (config: Config = {}): Client => {
158
138
  body: BodyInit | null | undefined,
159
139
  responseType: OfetchResponseType | undefined,
160
140
  ) => {
161
- const effectiveRetry = isRepeatableBody(body)
162
- ? (opts.retry as any)
163
- : (0 as any);
141
+ const effectiveRetry = isRepeatableBody(body) ? (opts.retry as any) : (0 as any);
164
142
  return buildOfetchOptions(opts, body, responseType, effectiveRetry);
165
143
  };
166
144
 
167
145
  const request: Client['request'] = async (options) => {
168
- const {
169
- networkBody: initialNetworkBody,
170
- opts,
171
- url,
172
- } = await resolveOptions(options as any);
146
+ const { networkBody: initialNetworkBody, opts, url } = await resolveOptions(options as any);
173
147
  // map parseAs -> ofetch responseType once per request
174
- const ofetchResponseType: OfetchResponseType | undefined =
175
- mapParseAsToResponseType(opts.parseAs, opts.responseType);
148
+ const ofetchResponseType: OfetchResponseType | undefined = mapParseAsToResponseType(
149
+ opts.parseAs,
150
+ opts.responseType,
151
+ );
176
152
 
177
153
  const $ofetch = opts.ofetch ?? ofetch;
178
154
 
@@ -230,30 +206,28 @@ export const createClient = (config: Config = {}): Client => {
230
206
  return wrapErrorReturn(finalError, result, opts.responseStyle) as any;
231
207
  };
232
208
 
233
- const makeMethodFn =
234
- (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
235
- request({ ...options, method } as any);
236
-
237
- const makeSseFn =
238
- (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
239
- const { networkBody, opts, url } = await resolveOptions(options);
240
- const optsForSse: any = { ...opts };
241
- delete optsForSse.body; // body is provided via serializedBody below
242
- return createSseClient({
243
- ...optsForSse,
244
- fetch: opts.fetch,
245
- headers: opts.headers as Headers,
246
- method,
247
- onRequest: async (url, init) => {
248
- let request = new Request(url, init);
249
- request = await applyRequestInterceptors(request, opts, networkBody);
250
- return request;
251
- },
252
- serializedBody: networkBody as BodyInit | null | undefined,
253
- signal: opts.signal,
254
- url,
255
- });
256
- };
209
+ const makeMethodFn = (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
210
+ request({ ...options, method } as any);
211
+
212
+ const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
213
+ const { networkBody, opts, url } = await resolveOptions(options);
214
+ const optsForSse: any = { ...opts };
215
+ delete optsForSse.body; // body is provided via serializedBody below
216
+ return createSseClient({
217
+ ...optsForSse,
218
+ fetch: opts.fetch,
219
+ headers: opts.headers as Headers,
220
+ method,
221
+ onRequest: async (url, init) => {
222
+ let request = new Request(url, init);
223
+ request = await applyRequestInterceptors(request, opts, networkBody);
224
+ return request;
225
+ },
226
+ serializedBody: networkBody as BodyInit | null | undefined,
227
+ signal: opts.signal,
228
+ url,
229
+ });
230
+ };
257
231
 
258
232
  return {
259
233
  buildUrl,
@@ -1,7 +1,4 @@
1
- import type {
2
- FetchOptions as OfetchOptions,
3
- ResponseType as OfetchResponseType,
4
- } from 'ofetch';
1
+ import type { FetchOptions as OfetchOptions, ResponseType as OfetchResponseType } from 'ofetch';
5
2
  import type { ofetch } from 'ofetch';
6
3
 
7
4
  import type { Auth } from '../core/auth';
@@ -9,17 +6,13 @@ import type {
9
6
  ServerSentEventsOptions,
10
7
  ServerSentEventsResult,
11
8
  } from '../core/serverSentEvents';
12
- import type {
13
- Client as CoreClient,
14
- Config as CoreConfig,
15
- } from '../core/types';
9
+ import type { Client as CoreClient, Config as CoreConfig } from '../core/types';
16
10
  import type { Middleware } from './utils';
17
11
 
18
12
  export type ResponseStyle = 'data' | 'fields';
19
13
 
20
14
  export interface Config<T extends ClientOptions = ClientOptions>
21
- extends Omit<RequestInit, 'body' | 'headers' | 'method'>,
22
- CoreConfig {
15
+ extends Omit<RequestInit, 'body' | 'headers' | 'method'>, CoreConfig {
23
16
  /**
24
17
  * HTTP(S) agent configuration (Node.js only). Passed through to ofetch.
25
18
  */
@@ -84,14 +77,7 @@ export interface Config<T extends ClientOptions = ClientOptions>
84
77
  *
85
78
  * @default 'auto'
86
79
  */
87
- parseAs?:
88
- | 'arrayBuffer'
89
- | 'auto'
90
- | 'blob'
91
- | 'formData'
92
- | 'json'
93
- | 'stream'
94
- | 'text';
80
+ parseAs?: 'arrayBuffer' | 'auto' | 'blob' | 'formData' | 'json' | 'stream' | 'text';
95
81
  /** Custom response parser (ofetch). */
96
82
  parseResponse?: OfetchOptions['parseResponse'];
97
83
  /**
@@ -134,7 +120,9 @@ export interface RequestOptions<
134
120
  TResponseStyle extends ResponseStyle = 'fields',
135
121
  ThrowOnError extends boolean = boolean,
136
122
  Url extends string = string,
137
- > extends Config<{
123
+ >
124
+ extends
125
+ Config<{
138
126
  responseStyle: TResponseStyle;
139
127
  throwOnError: ThrowOnError;
140
128
  }>,
@@ -181,32 +169,22 @@ export type RequestResult<
181
169
  ? TData[keyof TData]
182
170
  : TData
183
171
  : {
184
- data: TData extends Record<string, unknown>
185
- ? TData[keyof TData]
186
- : TData;
172
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
187
173
  request: Request;
188
174
  response: Response;
189
175
  }
190
176
  >
191
177
  : Promise<
192
178
  TResponseStyle extends 'data'
193
- ?
194
- | (TData extends Record<string, unknown>
195
- ? TData[keyof TData]
196
- : TData)
197
- | undefined
179
+ ? (TData extends Record<string, unknown> ? TData[keyof TData] : TData) | undefined
198
180
  : (
199
181
  | {
200
- data: TData extends Record<string, unknown>
201
- ? TData[keyof TData]
202
- : TData;
182
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
203
183
  error: undefined;
204
184
  }
205
185
  | {
206
186
  data: undefined;
207
- error: TError extends Record<string, unknown>
208
- ? TError[keyof TError]
209
- : TError;
187
+ error: TError extends Record<string, unknown> ? TError[keyof TError] : TError;
210
188
  }
211
189
  ) & {
212
190
  request: Request;
@@ -245,10 +223,7 @@ type RequestFn = <
245
223
  TResponseStyle extends ResponseStyle = 'fields',
246
224
  >(
247
225
  options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'> &
248
- Pick<
249
- Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>,
250
- 'method'
251
- >,
226
+ Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'>,
252
227
  ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
253
228
 
254
229
  type BuildUrlFn = <
@@ -262,13 +237,7 @@ type BuildUrlFn = <
262
237
  options: TData & Options<TData>,
263
238
  ) => string;
264
239
 
265
- export type Client = CoreClient<
266
- RequestFn,
267
- Config,
268
- MethodFn,
269
- BuildUrlFn,
270
- SseFn
271
- > & {
240
+ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn, SseFn> & {
272
241
  interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
273
242
  };
274
243
 
@@ -1,7 +1,4 @@
1
- import type {
2
- FetchOptions as OfetchOptions,
3
- ResponseType as OfetchResponseType,
4
- } from 'ofetch';
1
+ import type { FetchOptions as OfetchOptions, ResponseType as OfetchResponseType } from 'ofetch';
5
2
 
6
3
  import { getAuthToken } from '../core/auth';
7
4
  import type { QuerySerializerOptions } from '../core/bodySerializer';
@@ -75,9 +72,7 @@ export const createQuerySerializer = <T = unknown>({
75
72
  /**
76
73
  * Infers parseAs value from provided Content-Type header.
77
74
  */
78
- export const getParseAs = (
79
- contentType: string | null,
80
- ): Exclude<Config['parseAs'], 'auto'> => {
75
+ export const getParseAs = (contentType: string | null): Exclude<Config['parseAs'], 'auto'> => {
81
76
  if (!contentType) {
82
77
  // If no Content-Type header is provided, the best we can do is return the raw response body,
83
78
  // which is effectively the same as the 'stream' option.
@@ -90,10 +85,7 @@ export const getParseAs = (
90
85
  return;
91
86
  }
92
87
 
93
- if (
94
- cleanContent.startsWith('application/json') ||
95
- cleanContent.endsWith('+json')
96
- ) {
88
+ if (cleanContent.startsWith('application/json') || cleanContent.endsWith('+json')) {
97
89
  return 'json';
98
90
  }
99
91
 
@@ -102,9 +94,7 @@ export const getParseAs = (
102
94
  }
103
95
 
104
96
  if (
105
- ['application/', 'audio/', 'image/', 'video/'].some((type) =>
106
- cleanContent.startsWith(type),
107
- )
97
+ ['application/', 'audio/', 'image/', 'video/'].some((type) => cleanContent.startsWith(type))
108
98
  ) {
109
99
  return 'blob';
110
100
  }
@@ -233,10 +223,7 @@ export const mergeHeaders = (
233
223
  continue;
234
224
  }
235
225
 
236
- const iterator =
237
- header instanceof Headers
238
- ? headersEntries(header)
239
- : Object.entries(header);
226
+ const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
240
227
 
241
228
  for (const [key, value] of iterator) {
242
229
  if (value === null) {
@@ -264,17 +251,13 @@ export const mergeHeaders = (
264
251
  export const isRepeatableBody = (body: unknown): boolean => {
265
252
  if (body == null) return true; // undefined/null treated as no-body
266
253
  if (typeof body === 'string') return true;
267
- if (typeof URLSearchParams !== 'undefined' && body instanceof URLSearchParams)
268
- return true;
269
- if (typeof Uint8Array !== 'undefined' && body instanceof Uint8Array)
270
- return true;
271
- if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer)
272
- return true;
254
+ if (typeof URLSearchParams !== 'undefined' && body instanceof URLSearchParams) return true;
255
+ if (typeof Uint8Array !== 'undefined' && body instanceof Uint8Array) return true;
256
+ if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) return true;
273
257
  if (typeof Blob !== 'undefined' && body instanceof Blob) return true;
274
258
  if (typeof FormData !== 'undefined' && body instanceof FormData) return true;
275
259
  // Streams are not repeatable
276
- if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream)
277
- return false;
260
+ if (typeof ReadableStream !== 'undefined' && body instanceof ReadableStream) return false;
278
261
  // Default: assume non-repeatable for unknown structured bodies
279
262
  return false;
280
263
  };
@@ -286,13 +269,8 @@ export const wrapDataReturn = <T>(
286
269
  data: T,
287
270
  result: { request: Request; response: Response },
288
271
  responseStyle: ResponseStyle | undefined,
289
- ):
290
- | T
291
- | ((T extends Record<string, unknown> ? { data: T } : { data: T }) &
292
- typeof result) =>
293
- (responseStyle ?? 'fields') === 'data'
294
- ? (data as any)
295
- : ({ data, ...result } as any);
272
+ ): T | ((T extends Record<string, unknown> ? { data: T } : { data: T }) & typeof result) =>
273
+ (responseStyle ?? 'fields') === 'data' ? (data as any) : ({ data, ...result } as any);
296
274
 
297
275
  /**
298
276
  * Small helper to unify error vs fields return style.
@@ -303,11 +281,8 @@ export const wrapErrorReturn = <E>(
303
281
  responseStyle: ResponseStyle | undefined,
304
282
  ):
305
283
  | undefined
306
- | ((E extends Record<string, unknown> ? { error: E } : { error: E }) &
307
- typeof result) =>
308
- (responseStyle ?? 'fields') === 'data'
309
- ? undefined
310
- : ({ error, ...result } as any);
284
+ | ((E extends Record<string, unknown> ? { error: E } : { error: E }) & typeof result) =>
285
+ (responseStyle ?? 'fields') === 'data' ? undefined : ({ error, ...result } as any);
311
286
 
312
287
  /**
313
288
  * Build options for $ofetch.raw from our resolved opts and body.
@@ -324,9 +299,7 @@ export const buildOfetchOptions = (
324
299
  credentials: opts.credentials as OfetchOptions['credentials'],
325
300
  dispatcher: opts.dispatcher as OfetchOptions['dispatcher'],
326
301
  headers: opts.headers as Headers,
327
- ignoreResponseError:
328
- (opts.ignoreResponseError as OfetchOptions['ignoreResponseError']) ??
329
- true,
302
+ ignoreResponseError: (opts.ignoreResponseError as OfetchOptions['ignoreResponseError']) ?? true,
330
303
  method: opts.method,
331
304
  onRequest: opts.onRequest as OfetchOptions['onRequest'],
332
305
  onRequestError: opts.onRequestError as OfetchOptions['onRequestError'],
@@ -338,8 +311,7 @@ export const buildOfetchOptions = (
338
311
  responseType,
339
312
  retry: retryOverride ?? (opts.retry as OfetchOptions['retry']),
340
313
  retryDelay: opts.retryDelay as OfetchOptions['retryDelay'],
341
- retryStatusCodes:
342
- opts.retryStatusCodes as OfetchOptions['retryStatusCodes'],
314
+ retryStatusCodes: opts.retryStatusCodes as OfetchOptions['retryStatusCodes'],
343
315
  signal: opts.signal,
344
316
  timeout: opts.timeout as number | undefined,
345
317
  }) as OfetchOptions;
@@ -358,15 +330,11 @@ export const parseSuccess = async (
358
330
  }
359
331
 
360
332
  const inferredParseAs =
361
- (opts.parseAs === 'auto'
362
- ? getParseAs(response.headers.get('Content-Type'))
363
- : opts.parseAs) ?? 'json';
333
+ (opts.parseAs === 'auto' ? getParseAs(response.headers.get('Content-Type')) : opts.parseAs) ??
334
+ 'json';
364
335
 
365
336
  // Handle empty responses
366
- if (
367
- response.status === 204 ||
368
- response.headers.get('Content-Length') === '0'
369
- ) {
337
+ if (response.status === 204 || response.headers.get('Content-Length') === '0') {
370
338
  switch (inferredParseAs) {
371
339
  case 'arrayBuffer':
372
340
  case 'blob':
@@ -442,10 +410,7 @@ type ErrInterceptor<Err, Res, Req, Options> = (
442
410
  options: Options,
443
411
  ) => Err | Promise<Err>;
444
412
 
445
- type ReqInterceptor<Req, Options> = (
446
- request: Req,
447
- options: Options,
448
- ) => Req | Promise<Req>;
413
+ type ReqInterceptor<Req, Options> = (request: Req, options: Options) => Req | Promise<Req>;
449
414
 
450
415
  type ResInterceptor<Res, Req, Options> = (
451
416
  response: Res,
@@ -479,10 +444,7 @@ class Interceptors<Interceptor> {
479
444
  return this.fns.indexOf(id);
480
445
  }
481
446
 
482
- update(
483
- id: number | Interceptor,
484
- fn: Interceptor,
485
- ): number | Interceptor | false {
447
+ update(id: number | Interceptor, fn: Interceptor): number | Interceptor | false {
486
448
  const index = this.getInterceptorIndex(id);
487
449
  if (this.fns[index]) {
488
450
  this.fns[index] = fn;