@hey-api/openapi-ts 0.0.0-next-20260205083026

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 (56) hide show
  1. package/LICENSE.md +21 -0
  2. package/README.md +381 -0
  3. package/bin/run.cmd +3 -0
  4. package/bin/run.js +18 -0
  5. package/dist/clients/angular/client.ts +237 -0
  6. package/dist/clients/angular/index.ts +23 -0
  7. package/dist/clients/angular/types.ts +231 -0
  8. package/dist/clients/angular/utils.ts +408 -0
  9. package/dist/clients/axios/client.ts +154 -0
  10. package/dist/clients/axios/index.ts +21 -0
  11. package/dist/clients/axios/types.ts +158 -0
  12. package/dist/clients/axios/utils.ts +206 -0
  13. package/dist/clients/core/auth.ts +39 -0
  14. package/dist/clients/core/bodySerializer.ts +82 -0
  15. package/dist/clients/core/params.ts +167 -0
  16. package/dist/clients/core/pathSerializer.ts +169 -0
  17. package/dist/clients/core/queryKeySerializer.ts +115 -0
  18. package/dist/clients/core/serverSentEvents.ts +241 -0
  19. package/dist/clients/core/types.ts +102 -0
  20. package/dist/clients/core/utils.ts +138 -0
  21. package/dist/clients/fetch/client.ts +286 -0
  22. package/dist/clients/fetch/index.ts +23 -0
  23. package/dist/clients/fetch/types.ts +211 -0
  24. package/dist/clients/fetch/utils.ts +314 -0
  25. package/dist/clients/ky/client.ts +318 -0
  26. package/dist/clients/ky/index.ts +24 -0
  27. package/dist/clients/ky/types.ts +243 -0
  28. package/dist/clients/ky/utils.ts +312 -0
  29. package/dist/clients/next/client.ts +253 -0
  30. package/dist/clients/next/index.ts +21 -0
  31. package/dist/clients/next/types.ts +162 -0
  32. package/dist/clients/next/utils.ts +413 -0
  33. package/dist/clients/nuxt/client.ts +213 -0
  34. package/dist/clients/nuxt/index.ts +22 -0
  35. package/dist/clients/nuxt/types.ts +189 -0
  36. package/dist/clients/nuxt/utils.ts +384 -0
  37. package/dist/clients/ofetch/client.ts +259 -0
  38. package/dist/clients/ofetch/index.ts +23 -0
  39. package/dist/clients/ofetch/types.ts +275 -0
  40. package/dist/clients/ofetch/utils.ts +504 -0
  41. package/dist/index.d.mts +8634 -0
  42. package/dist/index.d.mts.map +1 -0
  43. package/dist/index.mjs +4 -0
  44. package/dist/init-DlaW5Djq.mjs +13832 -0
  45. package/dist/init-DlaW5Djq.mjs.map +1 -0
  46. package/dist/internal.d.mts +33 -0
  47. package/dist/internal.d.mts.map +1 -0
  48. package/dist/internal.mjs +4 -0
  49. package/dist/run.d.mts +1 -0
  50. package/dist/run.mjs +60 -0
  51. package/dist/run.mjs.map +1 -0
  52. package/dist/src-BYA2YioO.mjs +225 -0
  53. package/dist/src-BYA2YioO.mjs.map +1 -0
  54. package/dist/types-Ba27ofyy.d.mts +157 -0
  55. package/dist/types-Ba27ofyy.d.mts.map +1 -0
  56. package/package.json +109 -0
@@ -0,0 +1,162 @@
1
+ import type { Auth } from '../core/auth';
2
+ import type {
3
+ ServerSentEventsOptions,
4
+ ServerSentEventsResult,
5
+ } from '../core/serverSentEvents';
6
+ import type { Client as CoreClient, Config as CoreConfig } from '../core/types';
7
+ import type { Middleware } from './utils';
8
+
9
+ export interface Config<T extends ClientOptions = ClientOptions>
10
+ extends Omit<RequestInit, 'body' | 'headers' | 'method'>, CoreConfig {
11
+ /**
12
+ * Base URL for all requests made by this client.
13
+ */
14
+ baseUrl?: T['baseUrl'];
15
+ /**
16
+ * Fetch API implementation. You can use this option to provide a custom
17
+ * fetch instance.
18
+ *
19
+ * @default globalThis.fetch
20
+ */
21
+ fetch?: typeof fetch;
22
+ /**
23
+ * Return the response data parsed in a specified format. By default, `auto`
24
+ * will infer the appropriate method from the `Content-Type` response header.
25
+ * You can override this behavior with any of the {@link Body} methods.
26
+ * Select `stream` if you don't want to parse response data at all.
27
+ *
28
+ * @default 'auto'
29
+ */
30
+ parseAs?: 'arrayBuffer' | 'auto' | 'blob' | 'formData' | 'json' | 'stream' | 'text';
31
+ /**
32
+ * Throw an error instead of returning it in the response?
33
+ *
34
+ * @default false
35
+ */
36
+ throwOnError?: T['throwOnError'];
37
+ }
38
+
39
+ export interface RequestOptions<
40
+ TData = unknown,
41
+ ThrowOnError extends boolean = boolean,
42
+ Url extends string = string,
43
+ >
44
+ extends
45
+ Config<{
46
+ throwOnError: ThrowOnError;
47
+ }>,
48
+ Pick<
49
+ ServerSentEventsOptions<TData>,
50
+ | 'onSseError'
51
+ | 'onSseEvent'
52
+ | 'sseDefaultRetryDelay'
53
+ | 'sseMaxRetryAttempts'
54
+ | 'sseMaxRetryDelay'
55
+ > {
56
+ /**
57
+ * Any body that you want to add to your request.
58
+ *
59
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
60
+ */
61
+ body?: unknown;
62
+ path?: Record<string, unknown>;
63
+ query?: Record<string, unknown>;
64
+ /**
65
+ * Security mechanism(s) to use for the request.
66
+ */
67
+ security?: ReadonlyArray<Auth>;
68
+ url: Url;
69
+ }
70
+
71
+ export interface ResolvedRequestOptions<
72
+ ThrowOnError extends boolean = boolean,
73
+ Url extends string = string,
74
+ > extends RequestOptions<unknown, ThrowOnError, Url> {
75
+ serializedBody?: string;
76
+ }
77
+
78
+ export type RequestResult<
79
+ TData = unknown,
80
+ TError = unknown,
81
+ ThrowOnError extends boolean = boolean,
82
+ > = ThrowOnError extends true
83
+ ? Promise<{
84
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
85
+ response: Response;
86
+ }>
87
+ : Promise<
88
+ (
89
+ | {
90
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
91
+ error: undefined;
92
+ }
93
+ | {
94
+ data: undefined;
95
+ error: TError extends Record<string, unknown> ? TError[keyof TError] : TError;
96
+ }
97
+ ) & {
98
+ response: Response;
99
+ }
100
+ >;
101
+
102
+ export interface ClientOptions {
103
+ baseUrl?: string;
104
+ throwOnError?: boolean;
105
+ }
106
+
107
+ type MethodFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(
108
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
109
+ ) => RequestResult<TData, TError, ThrowOnError>;
110
+
111
+ type SseFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(
112
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
113
+ ) => Promise<ServerSentEventsResult<TData, TError>>;
114
+
115
+ type RequestFn = <TData = unknown, TError = unknown, ThrowOnError extends boolean = false>(
116
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'> &
117
+ Pick<Required<RequestOptions<TData, ThrowOnError>>, 'method'>,
118
+ ) => RequestResult<TData, TError, ThrowOnError>;
119
+
120
+ type BuildUrlFn = <
121
+ TData extends {
122
+ body?: unknown;
123
+ path?: Record<string, unknown>;
124
+ query?: Record<string, unknown>;
125
+ url: string;
126
+ },
127
+ >(
128
+ options: TData & Options<TData>,
129
+ ) => string;
130
+
131
+ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn, SseFn> & {
132
+ interceptors: Middleware<Response, unknown, ResolvedRequestOptions>;
133
+ };
134
+
135
+ /**
136
+ * The `createClientConfig()` function will be called on client initialization
137
+ * and the returned object will become the client's initial configuration.
138
+ *
139
+ * You may want to initialize your client this way instead of calling
140
+ * `setConfig()`. This is useful for example if you're using Next.js
141
+ * to ensure your client always has the correct values.
142
+ */
143
+ export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
144
+ override?: Config<ClientOptions & T>,
145
+ ) => Config<Required<ClientOptions> & T>;
146
+
147
+ export interface TDataShape {
148
+ body?: unknown;
149
+ headers?: unknown;
150
+ path?: unknown;
151
+ query?: unknown;
152
+ url: string;
153
+ }
154
+
155
+ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
156
+
157
+ export type Options<
158
+ TData extends TDataShape = TDataShape,
159
+ ThrowOnError extends boolean = boolean,
160
+ TResponse = unknown,
161
+ > = OmitKeys<RequestOptions<TResponse, ThrowOnError>, 'body' | 'path' | 'query' | 'url'> &
162
+ ([TData] extends [never] ? unknown : Omit<TData, 'url'>);
@@ -0,0 +1,413 @@
1
+ import { getAuthToken } from '../core/auth';
2
+ import type {
3
+ QuerySerializer,
4
+ QuerySerializerOptions,
5
+ } from '../core/bodySerializer';
6
+ import { jsonBodySerializer } from '../core/bodySerializer';
7
+ import {
8
+ serializeArrayParam,
9
+ serializeObjectParam,
10
+ serializePrimitiveParam,
11
+ } from '../core/pathSerializer';
12
+ import type { Client, ClientOptions, Config, RequestOptions } from './types';
13
+
14
+ interface PathSerializer {
15
+ path: Record<string, unknown>;
16
+ url: string;
17
+ }
18
+
19
+ const PATH_PARAM_RE = /\{[^{}]+\}/g;
20
+
21
+ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited';
22
+ type MatrixStyle = 'label' | 'matrix' | 'simple';
23
+ type ArraySeparatorStyle = ArrayStyle | MatrixStyle;
24
+
25
+ const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
26
+ let url = _url;
27
+ const matches = _url.match(PATH_PARAM_RE);
28
+ if (matches) {
29
+ for (const match of matches) {
30
+ let explode = false;
31
+ let name = match.substring(1, match.length - 1);
32
+ let style: ArraySeparatorStyle = 'simple';
33
+
34
+ if (name.endsWith('*')) {
35
+ explode = true;
36
+ name = name.substring(0, name.length - 1);
37
+ }
38
+
39
+ if (name.startsWith('.')) {
40
+ name = name.substring(1);
41
+ style = 'label';
42
+ } else if (name.startsWith(';')) {
43
+ name = name.substring(1);
44
+ style = 'matrix';
45
+ }
46
+
47
+ const value = path[name];
48
+
49
+ if (value === undefined || value === null) {
50
+ continue;
51
+ }
52
+
53
+ if (Array.isArray(value)) {
54
+ url = url.replace(match, serializeArrayParam({ explode, name, style, value }));
55
+ continue;
56
+ }
57
+
58
+ if (typeof value === 'object') {
59
+ url = url.replace(
60
+ match,
61
+ serializeObjectParam({
62
+ explode,
63
+ name,
64
+ style,
65
+ value: value as Record<string, unknown>,
66
+ valueOnly: true,
67
+ }),
68
+ );
69
+ continue;
70
+ }
71
+
72
+ if (style === 'matrix') {
73
+ url = url.replace(
74
+ match,
75
+ `;${serializePrimitiveParam({
76
+ name,
77
+ value: value as string,
78
+ })}`,
79
+ );
80
+ continue;
81
+ }
82
+
83
+ const replaceValue = encodeURIComponent(
84
+ style === 'label' ? `.${value as string}` : (value as string),
85
+ );
86
+ url = url.replace(match, replaceValue);
87
+ }
88
+ }
89
+ return url;
90
+ };
91
+
92
+ export const createQuerySerializer = <T = unknown>({
93
+ parameters = {},
94
+ ...args
95
+ }: QuerySerializerOptions = {}) => {
96
+ const querySerializer = (queryParams: T) => {
97
+ const search: string[] = [];
98
+ if (queryParams && typeof queryParams === 'object') {
99
+ for (const name in queryParams) {
100
+ const value = queryParams[name];
101
+
102
+ if (value === undefined || value === null) {
103
+ continue;
104
+ }
105
+
106
+ const options = parameters[name] || args;
107
+
108
+ if (Array.isArray(value)) {
109
+ const serializedArray = serializeArrayParam({
110
+ allowReserved: options.allowReserved,
111
+ explode: true,
112
+ name,
113
+ style: 'form',
114
+ value,
115
+ ...options.array,
116
+ });
117
+ if (serializedArray) search.push(serializedArray);
118
+ } else if (typeof value === 'object') {
119
+ const serializedObject = serializeObjectParam({
120
+ allowReserved: options.allowReserved,
121
+ explode: true,
122
+ name,
123
+ style: 'deepObject',
124
+ value: value as Record<string, unknown>,
125
+ ...options.object,
126
+ });
127
+ if (serializedObject) search.push(serializedObject);
128
+ } else {
129
+ const serializedPrimitive = serializePrimitiveParam({
130
+ allowReserved: options.allowReserved,
131
+ name,
132
+ value: value as string,
133
+ });
134
+ if (serializedPrimitive) search.push(serializedPrimitive);
135
+ }
136
+ }
137
+ }
138
+ return search.join('&');
139
+ };
140
+ return querySerializer;
141
+ };
142
+
143
+ /**
144
+ * Infers parseAs value from provided Content-Type header.
145
+ */
146
+ export const getParseAs = (contentType: string | null): Exclude<Config['parseAs'], 'auto'> => {
147
+ if (!contentType) {
148
+ // If no Content-Type header is provided, the best we can do is return the raw response body,
149
+ // which is effectively the same as the 'stream' option.
150
+ return 'stream';
151
+ }
152
+
153
+ const cleanContent = contentType.split(';')[0]?.trim();
154
+
155
+ if (!cleanContent) {
156
+ return;
157
+ }
158
+
159
+ if (cleanContent.startsWith('application/json') || cleanContent.endsWith('+json')) {
160
+ return 'json';
161
+ }
162
+
163
+ if (cleanContent === 'multipart/form-data') {
164
+ return 'formData';
165
+ }
166
+
167
+ if (
168
+ ['application/', 'audio/', 'image/', 'video/'].some((type) => cleanContent.startsWith(type))
169
+ ) {
170
+ return 'blob';
171
+ }
172
+
173
+ if (cleanContent.startsWith('text/')) {
174
+ return 'text';
175
+ }
176
+
177
+ return;
178
+ };
179
+
180
+ const checkForExistence = (
181
+ options: Pick<RequestOptions, 'auth' | 'query'> & {
182
+ headers: Headers;
183
+ },
184
+ name?: string,
185
+ ): boolean => {
186
+ if (!name) {
187
+ return false;
188
+ }
189
+ if (
190
+ options.headers.has(name) ||
191
+ options.query?.[name] ||
192
+ options.headers.get('Cookie')?.includes(`${name}=`)
193
+ ) {
194
+ return true;
195
+ }
196
+ return false;
197
+ };
198
+
199
+ export const setAuthParams = async ({
200
+ security,
201
+ ...options
202
+ }: Pick<Required<RequestOptions>, 'security'> &
203
+ Pick<RequestOptions, 'auth' | 'query'> & {
204
+ headers: Headers;
205
+ }) => {
206
+ for (const auth of security) {
207
+ if (checkForExistence(options, auth.name)) {
208
+ continue;
209
+ }
210
+ const token = await getAuthToken(auth, options.auth);
211
+
212
+ if (!token) {
213
+ continue;
214
+ }
215
+
216
+ const name = auth.name ?? 'Authorization';
217
+
218
+ switch (auth.in) {
219
+ case 'query':
220
+ if (!options.query) {
221
+ options.query = {};
222
+ }
223
+ options.query[name] = token;
224
+ break;
225
+ case 'cookie':
226
+ options.headers.append('Cookie', `${name}=${token}`);
227
+ break;
228
+ case 'header':
229
+ default:
230
+ options.headers.set(name, token);
231
+ break;
232
+ }
233
+ }
234
+ };
235
+
236
+ export const buildUrl: Client['buildUrl'] = (options) => {
237
+ const url = getUrl({
238
+ baseUrl: options.baseUrl as string,
239
+ path: options.path,
240
+ query: options.query,
241
+ querySerializer:
242
+ typeof options.querySerializer === 'function'
243
+ ? options.querySerializer
244
+ : createQuerySerializer(options.querySerializer),
245
+ url: options.url,
246
+ });
247
+ return url;
248
+ };
249
+
250
+ export const getUrl = ({
251
+ baseUrl,
252
+ path,
253
+ query,
254
+ querySerializer,
255
+ url: _url,
256
+ }: {
257
+ baseUrl?: string;
258
+ path?: Record<string, unknown>;
259
+ query?: Record<string, unknown>;
260
+ querySerializer: QuerySerializer;
261
+ url: string;
262
+ }) => {
263
+ const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
264
+ let url = (baseUrl ?? '') + pathUrl;
265
+ if (path) {
266
+ url = defaultPathSerializer({ path, url });
267
+ }
268
+ let search = query ? querySerializer(query) : '';
269
+ if (search.startsWith('?')) {
270
+ search = search.substring(1);
271
+ }
272
+ if (search) {
273
+ url += `?${search}`;
274
+ }
275
+ return url;
276
+ };
277
+
278
+ export const mergeConfigs = (a: Config, b: Config): Config => {
279
+ const config = { ...a, ...b };
280
+ if (config.baseUrl?.endsWith('/')) {
281
+ config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
282
+ }
283
+ config.headers = mergeHeaders(a.headers, b.headers);
284
+ return config;
285
+ };
286
+
287
+ const headersEntries = (headers: Headers): Array<[string, string]> => {
288
+ const entries: Array<[string, string]> = [];
289
+ headers.forEach((value, key) => {
290
+ entries.push([key, value]);
291
+ });
292
+ return entries;
293
+ };
294
+
295
+ export const mergeHeaders = (
296
+ ...headers: Array<Required<Config>['headers'] | undefined>
297
+ ): Headers => {
298
+ const mergedHeaders = new Headers();
299
+ for (const header of headers) {
300
+ if (!header || typeof header !== 'object') {
301
+ continue;
302
+ }
303
+
304
+ const iterator = header instanceof Headers ? headersEntries(header) : Object.entries(header);
305
+
306
+ for (const [key, value] of iterator) {
307
+ if (value === null) {
308
+ mergedHeaders.delete(key);
309
+ } else if (Array.isArray(value)) {
310
+ for (const v of value) {
311
+ mergedHeaders.append(key, v as string);
312
+ }
313
+ } else if (value !== undefined) {
314
+ // assume object headers are meant to be JSON stringified, i.e. their
315
+ // content value in OpenAPI specification is 'application/json'
316
+ mergedHeaders.set(
317
+ key,
318
+ typeof value === 'object' ? JSON.stringify(value) : (value as string),
319
+ );
320
+ }
321
+ }
322
+ }
323
+ return mergedHeaders;
324
+ };
325
+
326
+ type ErrInterceptor<Err, Res, Options> = (
327
+ error: Err,
328
+ response: Res,
329
+ options: Options,
330
+ ) => Err | Promise<Err>;
331
+
332
+ type ReqInterceptor<Options> = (options: Options) => void | Promise<void>;
333
+
334
+ type ResInterceptor<Res, Options> = (response: Res, options: Options) => Res | Promise<Res>;
335
+
336
+ class Interceptors<Interceptor> {
337
+ fns: Array<Interceptor | null> = [];
338
+
339
+ clear(): void {
340
+ this.fns = [];
341
+ }
342
+
343
+ eject(id: number | Interceptor): void {
344
+ const index = this.getInterceptorIndex(id);
345
+ if (this.fns[index]) {
346
+ this.fns[index] = null;
347
+ }
348
+ }
349
+
350
+ exists(id: number | Interceptor): boolean {
351
+ const index = this.getInterceptorIndex(id);
352
+ return Boolean(this.fns[index]);
353
+ }
354
+
355
+ getInterceptorIndex(id: number | Interceptor): number {
356
+ if (typeof id === 'number') {
357
+ return this.fns[id] ? id : -1;
358
+ }
359
+ return this.fns.indexOf(id);
360
+ }
361
+
362
+ update(id: number | Interceptor, fn: Interceptor): number | Interceptor | false {
363
+ const index = this.getInterceptorIndex(id);
364
+ if (this.fns[index]) {
365
+ this.fns[index] = fn;
366
+ return id;
367
+ }
368
+ return false;
369
+ }
370
+
371
+ use(fn: Interceptor): number {
372
+ this.fns.push(fn);
373
+ return this.fns.length - 1;
374
+ }
375
+ }
376
+
377
+ export interface Middleware<Res, Err, Options> {
378
+ error: Interceptors<ErrInterceptor<Err, Res, Options>>;
379
+ request: Interceptors<ReqInterceptor<Options>>;
380
+ response: Interceptors<ResInterceptor<Res, Options>>;
381
+ }
382
+
383
+ export const createInterceptors = <Res, Err, Options>(): Middleware<Res, Err, Options> => ({
384
+ error: new Interceptors<ErrInterceptor<Err, Res, Options>>(),
385
+ request: new Interceptors<ReqInterceptor<Options>>(),
386
+ response: new Interceptors<ResInterceptor<Res, Options>>(),
387
+ });
388
+
389
+ const defaultQuerySerializer = createQuerySerializer({
390
+ allowReserved: false,
391
+ array: {
392
+ explode: true,
393
+ style: 'form',
394
+ },
395
+ object: {
396
+ explode: true,
397
+ style: 'deepObject',
398
+ },
399
+ });
400
+
401
+ const defaultHeaders = {
402
+ 'Content-Type': 'application/json',
403
+ };
404
+
405
+ export const createConfig = <T extends ClientOptions = ClientOptions>(
406
+ override: Config<Omit<ClientOptions, keyof T> & T> = {},
407
+ ): Config<Omit<ClientOptions, keyof T> & T> => ({
408
+ ...jsonBodySerializer,
409
+ headers: defaultHeaders,
410
+ parseAs: 'auto',
411
+ querySerializer: defaultQuerySerializer,
412
+ ...override,
413
+ });