@hey-api/openapi-ts 0.72.2 → 0.73.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 (40) hide show
  1. package/README.md +5 -12
  2. package/dist/chunk-QUDCWAFW.js +39 -0
  3. package/dist/chunk-QUDCWAFW.js.map +1 -0
  4. package/dist/clients/axios/client.ts +111 -0
  5. package/dist/clients/axios/index.ts +21 -0
  6. package/dist/clients/axios/types.ts +178 -0
  7. package/dist/clients/axios/utils.ts +286 -0
  8. package/dist/clients/core/auth.ts +40 -0
  9. package/dist/clients/core/bodySerializer.ts +84 -0
  10. package/dist/clients/core/params.ts +141 -0
  11. package/dist/clients/core/pathSerializer.ts +179 -0
  12. package/dist/clients/core/types.ts +98 -0
  13. package/dist/clients/fetch/client.ts +181 -0
  14. package/dist/clients/fetch/index.ts +22 -0
  15. package/dist/clients/fetch/types.ts +215 -0
  16. package/dist/clients/fetch/utils.ts +415 -0
  17. package/dist/clients/next/client.ts +163 -0
  18. package/dist/clients/next/index.ts +21 -0
  19. package/dist/clients/next/types.ts +166 -0
  20. package/dist/clients/next/utils.ts +404 -0
  21. package/dist/clients/nuxt/client.ts +145 -0
  22. package/dist/clients/nuxt/index.ts +22 -0
  23. package/dist/clients/nuxt/types.ts +192 -0
  24. package/dist/clients/nuxt/utils.ts +358 -0
  25. package/dist/index.cjs +63 -63
  26. package/dist/index.cjs.map +1 -1
  27. package/dist/index.d.cts +6 -4
  28. package/dist/index.d.ts +6 -4
  29. package/dist/index.js +9 -9
  30. package/dist/index.js.map +1 -1
  31. package/dist/internal.cjs +10 -10
  32. package/dist/internal.cjs.map +1 -1
  33. package/dist/internal.d.cts +2 -2
  34. package/dist/internal.d.ts +2 -2
  35. package/dist/internal.js +1 -1
  36. package/dist/{types.d-DtkupL5A.d.cts → types.d-CAqtwzfv.d.cts} +16 -25
  37. package/dist/{types.d-DtkupL5A.d.ts → types.d-CAqtwzfv.d.ts} +16 -25
  38. package/package.json +4 -3
  39. package/dist/chunk-LC3ZVK3Y.js +0 -39
  40. package/dist/chunk-LC3ZVK3Y.js.map +0 -1
@@ -0,0 +1,192 @@
1
+ import type {
2
+ AsyncDataOptions,
3
+ useAsyncData,
4
+ useFetch,
5
+ UseFetchOptions,
6
+ useLazyAsyncData,
7
+ useLazyFetch,
8
+ } from 'nuxt/app';
9
+ import type { Ref } from 'vue';
10
+
11
+ import type { Auth } from '../core/auth';
12
+ import type { QuerySerializerOptions } from '../core/bodySerializer';
13
+ import type {
14
+ Client as CoreClient,
15
+ Config as CoreConfig,
16
+ } from '../core/types';
17
+
18
+ export type ArraySeparatorStyle = ArrayStyle | MatrixStyle;
19
+ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited';
20
+ type MatrixStyle = 'label' | 'matrix' | 'simple';
21
+ export type ObjectSeparatorStyle = ObjectStyle | MatrixStyle;
22
+ type ObjectStyle = 'form' | 'deepObject';
23
+
24
+ export type QuerySerializer = (
25
+ query: Parameters<Client['buildUrl']>[0]['query'],
26
+ ) => string;
27
+
28
+ type WithRefs<TData> = {
29
+ [K in keyof TData]: NonNullable<TData[K]> extends object
30
+ ? WithRefs<NonNullable<TData[K]>> | Ref<NonNullable<TData[K]>>
31
+ : NonNullable<TData[K]> | Ref<NonNullable<TData[K]>>;
32
+ };
33
+
34
+ // copied from Nuxt
35
+ export type KeysOf<T> = Array<
36
+ T extends T ? (keyof T extends string ? keyof T : never) : never
37
+ >;
38
+
39
+ export interface Config<T extends ClientOptions = ClientOptions>
40
+ extends Omit<
41
+ FetchOptions<unknown>,
42
+ 'baseURL' | 'body' | 'headers' | 'method' | 'query'
43
+ >,
44
+ WithRefs<Pick<FetchOptions<unknown>, 'query'>>,
45
+ Omit<CoreConfig, 'querySerializer'> {
46
+ /**
47
+ * Base URL for all requests made by this client.
48
+ */
49
+ baseURL?: T['baseURL'];
50
+ /**
51
+ * A function for serializing request query parameters. By default, arrays
52
+ * will be exploded in form style, objects will be exploded in deepObject
53
+ * style, and reserved characters are percent-encoded.
54
+ *
55
+ * {@link https://swagger.io/docs/specification/serialization/#query View examples}
56
+ */
57
+ querySerializer?: QuerySerializer | QuerySerializerOptions;
58
+ }
59
+
60
+ export interface RequestOptions<
61
+ TComposable extends Composable = Composable,
62
+ ResT = unknown,
63
+ DefaultT = undefined,
64
+ Url extends string = string,
65
+ > extends Config,
66
+ WithRefs<{
67
+ /**
68
+ * Any body that you want to add to your request.
69
+ *
70
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
71
+ */
72
+ body?: unknown;
73
+ path?: FetchOptions<unknown>['query'];
74
+ query?: FetchOptions<unknown>['query'];
75
+ }> {
76
+ asyncDataOptions?: AsyncDataOptions<ResT, ResT, KeysOf<ResT>, DefaultT>;
77
+ composable: TComposable;
78
+ key?: string;
79
+ /**
80
+ * Security mechanism(s) to use for the request.
81
+ */
82
+ security?: ReadonlyArray<Auth>;
83
+ url: Url;
84
+ }
85
+
86
+ export type RequestResult<
87
+ TComposable extends Composable,
88
+ ResT,
89
+ TError,
90
+ > = TComposable extends '$fetch'
91
+ ? ReturnType<typeof $fetch<ResT>>
92
+ : TComposable extends 'useAsyncData'
93
+ ? ReturnType<typeof useAsyncData<ResT | null, TError>>
94
+ : TComposable extends 'useFetch'
95
+ ? ReturnType<typeof useFetch<ResT | null, TError>>
96
+ : TComposable extends 'useLazyAsyncData'
97
+ ? ReturnType<typeof useLazyAsyncData<ResT | null, TError>>
98
+ : TComposable extends 'useLazyFetch'
99
+ ? ReturnType<typeof useLazyFetch<ResT | null, TError>>
100
+ : never;
101
+
102
+ export interface ClientOptions {
103
+ baseURL?: string;
104
+ }
105
+
106
+ type MethodFn = <
107
+ TComposable extends Composable,
108
+ ResT = unknown,
109
+ TError = unknown,
110
+ DefaultT = undefined,
111
+ >(
112
+ options: Omit<RequestOptions<TComposable, ResT, DefaultT>, 'method'>,
113
+ ) => RequestResult<TComposable, ResT, TError>;
114
+
115
+ type RequestFn = <
116
+ TComposable extends Composable,
117
+ ResT = unknown,
118
+ TError = unknown,
119
+ DefaultT = undefined,
120
+ >(
121
+ options: Omit<RequestOptions<TComposable, ResT, DefaultT>, 'method'> &
122
+ Pick<Required<RequestOptions<TComposable, ResT, DefaultT>>, 'method'>,
123
+ ) => RequestResult<TComposable, ResT, TError>;
124
+
125
+ /**
126
+ * The `createClientConfig()` function will be called on client initialization
127
+ * and the returned object will become the client's initial configuration.
128
+ *
129
+ * You may want to initialize your client this way instead of calling
130
+ * `setConfig()`. This is useful for example if you're using Next.js
131
+ * to ensure your client always has the correct values.
132
+ */
133
+ export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
134
+ override?: Config<ClientOptions & T>,
135
+ ) => Config<Required<ClientOptions> & T>;
136
+
137
+ export interface TDataShape {
138
+ body?: unknown;
139
+ headers?: unknown;
140
+ path?: FetchOptions<unknown>['query'];
141
+ query?: FetchOptions<unknown>['query'];
142
+ url: string;
143
+ }
144
+
145
+ export type BuildUrlOptions<
146
+ TData extends Omit<TDataShape, 'headers'> = Omit<TDataShape, 'headers'>,
147
+ > = Pick<WithRefs<TData>, 'path' | 'query'> &
148
+ Pick<TData, 'url'> &
149
+ Pick<Options<'$fetch', TData>, 'baseURL' | 'querySerializer'>;
150
+
151
+ type BuildUrlFn = <TData extends Omit<TDataShape, 'headers'>>(
152
+ options: BuildUrlOptions<TData>,
153
+ ) => string;
154
+
155
+ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn>;
156
+
157
+ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
158
+
159
+ export type Options<
160
+ TComposable extends Composable,
161
+ TData extends TDataShape = TDataShape,
162
+ ResT = unknown,
163
+ DefaultT = undefined,
164
+ > = OmitKeys<
165
+ RequestOptions<TComposable, ResT, DefaultT>,
166
+ 'body' | 'path' | 'query' | 'url'
167
+ > &
168
+ WithRefs<Omit<TData, 'url'>>;
169
+
170
+ export type OptionsLegacyParser<TData = unknown> = TData extends { body?: any }
171
+ ? TData extends { headers?: any }
172
+ ? OmitKeys<RequestOptions, 'body' | 'headers' | 'url'> & TData
173
+ : OmitKeys<RequestOptions, 'body' | 'url'> &
174
+ TData &
175
+ Pick<RequestOptions, 'headers'>
176
+ : TData extends { headers?: any }
177
+ ? OmitKeys<RequestOptions, 'headers' | 'url'> &
178
+ TData &
179
+ Pick<RequestOptions, 'body'>
180
+ : OmitKeys<RequestOptions, 'url'> & TData;
181
+
182
+ type FetchOptions<TData> = Omit<
183
+ UseFetchOptions<TData, TData>,
184
+ keyof AsyncDataOptions<TData>
185
+ >;
186
+
187
+ export type Composable =
188
+ | '$fetch'
189
+ | 'useAsyncData'
190
+ | 'useFetch'
191
+ | 'useLazyAsyncData'
192
+ | 'useLazyFetch';
@@ -0,0 +1,358 @@
1
+ import type { ComputedRef, Ref } from 'vue';
2
+ import { isRef, toValue, unref } from 'vue';
3
+
4
+ import { getAuthToken } from '../core/auth';
5
+ import type { QuerySerializerOptions } from '../core/bodySerializer';
6
+ import { jsonBodySerializer } from '../core/bodySerializer';
7
+ import {
8
+ serializeArrayParam,
9
+ serializeObjectParam,
10
+ serializePrimitiveParam,
11
+ } from '../core/pathSerializer';
12
+ import type {
13
+ ArraySeparatorStyle,
14
+ BuildUrlOptions,
15
+ Client,
16
+ ClientOptions,
17
+ Config,
18
+ QuerySerializer,
19
+ RequestOptions,
20
+ } from './types';
21
+
22
+ type PathSerializer = Pick<Required<BuildUrlOptions>, 'path' | 'url'>;
23
+
24
+ const PATH_PARAM_RE = /\{[^{}]+\}/g;
25
+
26
+ type MaybeArray<T> = T | T[];
27
+
28
+ const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
29
+ let url = _url;
30
+ const matches = _url.match(PATH_PARAM_RE);
31
+ if (matches) {
32
+ for (const match of matches) {
33
+ let explode = false;
34
+ let name = match.substring(1, match.length - 1);
35
+ let style: ArraySeparatorStyle = 'simple';
36
+
37
+ if (name.endsWith('*')) {
38
+ explode = true;
39
+ name = name.substring(0, name.length - 1);
40
+ }
41
+
42
+ if (name.startsWith('.')) {
43
+ name = name.substring(1);
44
+ style = 'label';
45
+ } else if (name.startsWith(';')) {
46
+ name = name.substring(1);
47
+ style = 'matrix';
48
+ }
49
+
50
+ const value = toValue(toValue(path)[name]);
51
+
52
+ if (value === undefined || value === null) {
53
+ continue;
54
+ }
55
+
56
+ if (Array.isArray(value)) {
57
+ url = url.replace(
58
+ match,
59
+ serializeArrayParam({ explode, name, style, value }),
60
+ );
61
+ continue;
62
+ }
63
+
64
+ if (typeof value === 'object') {
65
+ url = url.replace(
66
+ match,
67
+ serializeObjectParam({
68
+ explode,
69
+ name,
70
+ style,
71
+ value: value as Record<string, unknown>,
72
+ valueOnly: true,
73
+ }),
74
+ );
75
+ continue;
76
+ }
77
+
78
+ if (style === 'matrix') {
79
+ url = url.replace(
80
+ match,
81
+ `;${serializePrimitiveParam({
82
+ name,
83
+ value: value as string,
84
+ })}`,
85
+ );
86
+ continue;
87
+ }
88
+
89
+ const replaceValue = encodeURIComponent(
90
+ style === 'label' ? `.${value as string}` : (value as string),
91
+ );
92
+ url = url.replace(match, replaceValue);
93
+ }
94
+ }
95
+ return url;
96
+ };
97
+
98
+ export const createQuerySerializer = <T = unknown>({
99
+ allowReserved,
100
+ array,
101
+ object,
102
+ }: QuerySerializerOptions = {}) => {
103
+ const querySerializer = (queryParams: T) => {
104
+ const search: string[] = [];
105
+ const qParams = toValue(queryParams);
106
+ if (qParams && typeof qParams === 'object') {
107
+ for (const name in qParams) {
108
+ const value = toValue(qParams[name]);
109
+
110
+ if (value === undefined || value === null) {
111
+ continue;
112
+ }
113
+
114
+ if (Array.isArray(value)) {
115
+ const serializedArray = serializeArrayParam({
116
+ allowReserved,
117
+ explode: true,
118
+ name,
119
+ style: 'form',
120
+ value,
121
+ ...array,
122
+ });
123
+ if (serializedArray) search.push(serializedArray);
124
+ } else if (typeof value === 'object') {
125
+ const serializedObject = serializeObjectParam({
126
+ allowReserved,
127
+ explode: true,
128
+ name,
129
+ style: 'deepObject',
130
+ value: value as Record<string, unknown>,
131
+ ...object,
132
+ });
133
+ if (serializedObject) search.push(serializedObject);
134
+ } else {
135
+ const serializedPrimitive = serializePrimitiveParam({
136
+ allowReserved,
137
+ name,
138
+ value: value as string,
139
+ });
140
+ if (serializedPrimitive) search.push(serializedPrimitive);
141
+ }
142
+ }
143
+ }
144
+ return search.join('&');
145
+ };
146
+ return querySerializer;
147
+ };
148
+
149
+ export const setAuthParams = async ({
150
+ security,
151
+ ...options
152
+ }: Pick<Required<RequestOptions>, 'security'> &
153
+ Pick<RequestOptions, 'auth' | 'query'> & {
154
+ headers: Headers;
155
+ }) => {
156
+ for (const auth of security) {
157
+ const token = await getAuthToken(auth, options.auth);
158
+
159
+ if (!token) {
160
+ continue;
161
+ }
162
+
163
+ const name = auth.name ?? 'Authorization';
164
+
165
+ switch (auth.in) {
166
+ case 'query':
167
+ if (!options.query) {
168
+ options.query = {};
169
+ }
170
+ toValue(options.query)[name] = token;
171
+ break;
172
+ case 'cookie':
173
+ options.headers.append('Cookie', `${name}=${token}`);
174
+ break;
175
+ case 'header':
176
+ default:
177
+ options.headers.set(name, token);
178
+ break;
179
+ }
180
+
181
+ return;
182
+ }
183
+ };
184
+
185
+ export const buildUrl: Client['buildUrl'] = (options) => {
186
+ const url = getUrl({
187
+ baseUrl: options.baseURL as string,
188
+ path: options.path,
189
+ query: options.query,
190
+ querySerializer:
191
+ typeof options.querySerializer === 'function'
192
+ ? options.querySerializer
193
+ : createQuerySerializer(options.querySerializer),
194
+ url: options.url,
195
+ });
196
+ return url;
197
+ };
198
+
199
+ export const getUrl = ({
200
+ baseUrl,
201
+ path,
202
+ query,
203
+ querySerializer,
204
+ url: _url,
205
+ }: Pick<BuildUrlOptions, 'path' | 'query' | 'url'> & {
206
+ baseUrl?: string;
207
+ querySerializer: QuerySerializer;
208
+ }) => {
209
+ const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
210
+ let url = (baseUrl ?? '') + pathUrl;
211
+ if (path) {
212
+ url = defaultPathSerializer({ path, url });
213
+ }
214
+ let search = query ? querySerializer(query) : '';
215
+ if (search.startsWith('?')) {
216
+ search = search.substring(1);
217
+ }
218
+ if (search) {
219
+ url += `?${search}`;
220
+ }
221
+ return url;
222
+ };
223
+
224
+ export const mergeConfigs = (a: Config, b: Config): Config => {
225
+ const config = { ...a, ...b };
226
+ if (config.baseURL?.endsWith('/')) {
227
+ config.baseURL = config.baseURL.substring(0, config.baseURL.length - 1);
228
+ }
229
+ config.headers = mergeHeaders(a.headers, b.headers);
230
+ return config;
231
+ };
232
+
233
+ export const mergeHeaders = (
234
+ ...headers: Array<Required<Config>['headers'] | undefined>
235
+ ): Headers => {
236
+ const mergedHeaders = new Headers();
237
+ for (const header of headers) {
238
+ if (!header || typeof header !== 'object') {
239
+ continue;
240
+ }
241
+
242
+ let h: unknown = header;
243
+ if (isRef(h)) {
244
+ h = unref(h);
245
+ }
246
+
247
+ const iterator =
248
+ h instanceof Headers
249
+ ? h.entries()
250
+ : Object.entries(h as Record<string, unknown>);
251
+
252
+ for (const [key, value] of iterator) {
253
+ if (value === null) {
254
+ mergedHeaders.delete(key);
255
+ } else if (Array.isArray(value)) {
256
+ for (const v of value) {
257
+ mergedHeaders.append(key, unwrapRefs(v) as string);
258
+ }
259
+ } else if (value !== undefined) {
260
+ const v = unwrapRefs(value);
261
+ // assume object headers are meant to be JSON stringified, i.e. their
262
+ // content value in OpenAPI specification is 'application/json'
263
+ mergedHeaders.set(
264
+ key,
265
+ typeof v === 'object' ? JSON.stringify(v) : (v as string),
266
+ );
267
+ }
268
+ }
269
+ }
270
+ return mergedHeaders;
271
+ };
272
+
273
+ export const mergeInterceptors = <T>(...args: Array<MaybeArray<T>>): Array<T> =>
274
+ args.reduce<Array<T>>((acc, item) => {
275
+ if (typeof item === 'function') {
276
+ acc.push(item);
277
+ } else if (Array.isArray(item)) {
278
+ return acc.concat(item);
279
+ }
280
+ return acc;
281
+ }, []);
282
+
283
+ const defaultQuerySerializer = createQuerySerializer({
284
+ allowReserved: false,
285
+ array: {
286
+ explode: true,
287
+ style: 'form',
288
+ },
289
+ object: {
290
+ explode: true,
291
+ style: 'deepObject',
292
+ },
293
+ });
294
+
295
+ const defaultHeaders = {
296
+ 'Content-Type': 'application/json',
297
+ };
298
+
299
+ export const createConfig = <T extends ClientOptions = ClientOptions>(
300
+ override: Config<Omit<ClientOptions, keyof T> & T> = {},
301
+ ): Config<Omit<ClientOptions, keyof T> & T> => ({
302
+ ...jsonBodySerializer,
303
+ headers: defaultHeaders,
304
+ querySerializer: defaultQuerySerializer,
305
+ ...override,
306
+ });
307
+
308
+ type UnwrapRefs<T> =
309
+ T extends Ref<infer V>
310
+ ? V
311
+ : T extends ComputedRef<infer V>
312
+ ? V
313
+ : T extends Record<string, unknown> // this doesn't handle functions well
314
+ ? { [K in keyof T]: UnwrapRefs<T[K]> }
315
+ : T;
316
+
317
+ const unwrapRefs = <T>(value: T): UnwrapRefs<T> => {
318
+ if (value === null || typeof value !== 'object' || value instanceof Headers) {
319
+ return (isRef(value) ? unref(value) : value) as UnwrapRefs<T>;
320
+ }
321
+
322
+ if (Array.isArray(value)) {
323
+ return value.map((item) => unwrapRefs(item)) as UnwrapRefs<T>;
324
+ }
325
+
326
+ if (isRef(value)) {
327
+ return unwrapRefs(unref(value) as T);
328
+ }
329
+
330
+ // unwrap into new object to avoid modifying the source
331
+ const result: Record<string, unknown> = {};
332
+ for (const key in value) {
333
+ result[key] = unwrapRefs(value[key] as T);
334
+ }
335
+ return result as UnwrapRefs<T>;
336
+ };
337
+
338
+ export const serializeBody = (
339
+ opts: Pick<Parameters<Client['request']>[0], 'body' | 'bodySerializer'>,
340
+ ) => {
341
+ if (opts.body && opts.bodySerializer) {
342
+ return opts.bodySerializer(opts.body);
343
+ }
344
+ return opts.body;
345
+ };
346
+
347
+ export const executeFetchFn = (
348
+ opts: Omit<Parameters<Client['request']>[0], 'composable'>,
349
+ fetchFn: Required<Config>['$fetch'],
350
+ ) => {
351
+ const unwrappedOpts = unwrapRefs(opts);
352
+ unwrappedOpts.body = serializeBody(unwrappedOpts);
353
+ return fetchFn(
354
+ buildUrl(opts),
355
+ // @ts-expect-error
356
+ unwrappedOpts,
357
+ );
358
+ };