@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,318 @@
1
+ import type { HTTPError, Options as KyOptions } from 'ky';
2
+ import ky from 'ky';
3
+
4
+ import { createSseClient } from '../core/serverSentEvents';
5
+ import type { HttpMethod } from '../core/types';
6
+ import { getValidRequestBody } from '../core/utils';
7
+ import type { Client, Config, RequestOptions, ResolvedRequestOptions, RetryOptions } from './types';
8
+ import type { Middleware } from './utils';
9
+ import {
10
+ buildUrl,
11
+ createConfig,
12
+ createInterceptors,
13
+ getParseAs,
14
+ mergeConfigs,
15
+ mergeHeaders,
16
+ setAuthParams,
17
+ } from './utils';
18
+
19
+ export const createClient = (config: Config = {}): Client => {
20
+ let _config = mergeConfigs(createConfig(), config);
21
+
22
+ const getConfig = (): Config => ({ ..._config });
23
+
24
+ const setConfig = (config: Config): Config => {
25
+ _config = mergeConfigs(_config, config);
26
+ return getConfig();
27
+ };
28
+
29
+ const interceptors = createInterceptors<Request, Response, unknown, ResolvedRequestOptions>();
30
+
31
+ const beforeRequest = async (options: RequestOptions) => {
32
+ const opts = {
33
+ ..._config,
34
+ ...options,
35
+ headers: mergeHeaders(_config.headers, options.headers),
36
+ ky: options.ky ?? _config.ky ?? ky,
37
+ serializedBody: undefined,
38
+ };
39
+
40
+ if (opts.security) {
41
+ await setAuthParams({
42
+ ...opts,
43
+ security: opts.security,
44
+ });
45
+ }
46
+
47
+ if (opts.requestValidator) {
48
+ await opts.requestValidator(opts);
49
+ }
50
+
51
+ if (opts.body !== undefined && opts.bodySerializer) {
52
+ opts.serializedBody = opts.bodySerializer(opts.body);
53
+ }
54
+
55
+ if (opts.body === undefined || opts.serializedBody === '') {
56
+ opts.headers.delete('Content-Type');
57
+ }
58
+
59
+ const url = buildUrl(opts);
60
+
61
+ return { opts, url };
62
+ };
63
+
64
+ const parseErrorResponse = async (
65
+ response: Response,
66
+ request: Request,
67
+ opts: ResolvedRequestOptions,
68
+ interceptorsMiddleware: Middleware<Request, Response, unknown, ResolvedRequestOptions>,
69
+ ) => {
70
+ const result = {
71
+ request,
72
+ response,
73
+ };
74
+
75
+ const textError = await response.text();
76
+ let jsonError: unknown;
77
+
78
+ try {
79
+ jsonError = JSON.parse(textError);
80
+ } catch {
81
+ jsonError = undefined;
82
+ }
83
+
84
+ const error = jsonError ?? textError;
85
+ let finalError = error;
86
+
87
+ for (const fn of interceptorsMiddleware.error.fns) {
88
+ if (fn) {
89
+ finalError = (await fn(error, response, request, opts)) as string;
90
+ }
91
+ }
92
+
93
+ finalError = finalError || ({} as string);
94
+
95
+ if (opts.throwOnError) {
96
+ throw finalError;
97
+ }
98
+
99
+ return opts.responseStyle === 'data'
100
+ ? undefined
101
+ : {
102
+ error: finalError,
103
+ ...result,
104
+ };
105
+ };
106
+
107
+ const request: Client['request'] = async (options) => {
108
+ // @ts-expect-error
109
+ const { opts, url } = await beforeRequest(options);
110
+
111
+ const kyInstance = opts.ky!;
112
+
113
+ const validBody = getValidRequestBody(opts);
114
+
115
+ const kyOptions: KyOptions = {
116
+ body: validBody as BodyInit,
117
+ cache: opts.cache,
118
+ credentials: opts.credentials,
119
+ headers: opts.headers,
120
+ integrity: opts.integrity,
121
+ keepalive: opts.keepalive,
122
+ method: opts.method as KyOptions['method'],
123
+ mode: opts.mode,
124
+ redirect: 'follow',
125
+ referrer: opts.referrer,
126
+ referrerPolicy: opts.referrerPolicy,
127
+ signal: opts.signal,
128
+ throwHttpErrors: opts.throwOnError ?? false,
129
+ timeout: opts.timeout,
130
+ ...opts.kyOptions,
131
+ };
132
+
133
+ if (opts.retry && typeof opts.retry === 'object') {
134
+ const retryOpts = opts.retry as RetryOptions;
135
+ kyOptions.retry = {
136
+ limit: retryOpts.limit ?? 2,
137
+ methods: retryOpts.methods as Array<
138
+ 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options' | 'trace'
139
+ >,
140
+ statusCodes: retryOpts.statusCodes,
141
+ };
142
+ }
143
+
144
+ let request = new Request(url, {
145
+ body: kyOptions.body as BodyInit,
146
+ headers: kyOptions.headers as HeadersInit,
147
+ method: kyOptions.method,
148
+ });
149
+
150
+ for (const fn of interceptors.request.fns) {
151
+ if (fn) {
152
+ request = await fn(request, opts);
153
+ }
154
+ }
155
+
156
+ let response: Response;
157
+
158
+ try {
159
+ response = await kyInstance(request, kyOptions);
160
+ } catch (error) {
161
+ if (error && typeof error === 'object' && 'response' in error) {
162
+ const httpError = error as HTTPError;
163
+ response = httpError.response;
164
+
165
+ for (const fn of interceptors.response.fns) {
166
+ if (fn) {
167
+ response = await fn(response, request, opts);
168
+ }
169
+ }
170
+
171
+ return parseErrorResponse(response, request, opts, interceptors);
172
+ }
173
+
174
+ throw error;
175
+ }
176
+
177
+ for (const fn of interceptors.response.fns) {
178
+ if (fn) {
179
+ response = await fn(response, request, opts);
180
+ }
181
+ }
182
+
183
+ const result = {
184
+ request,
185
+ response,
186
+ };
187
+
188
+ if (response.ok) {
189
+ const parseAs =
190
+ (opts.parseAs === 'auto'
191
+ ? getParseAs(response.headers.get('Content-Type'))
192
+ : opts.parseAs) ?? 'json';
193
+
194
+ if (response.status === 204 || response.headers.get('Content-Length') === '0') {
195
+ let emptyData: any;
196
+ switch (parseAs) {
197
+ case 'arrayBuffer':
198
+ case 'blob':
199
+ case 'text':
200
+ emptyData = await response[parseAs]();
201
+ break;
202
+ case 'formData':
203
+ emptyData = new FormData();
204
+ break;
205
+ case 'stream':
206
+ emptyData = response.body;
207
+ break;
208
+ case 'json':
209
+ default:
210
+ emptyData = {};
211
+ break;
212
+ }
213
+ return opts.responseStyle === 'data'
214
+ ? emptyData
215
+ : {
216
+ data: emptyData,
217
+ ...result,
218
+ };
219
+ }
220
+
221
+ let data: any;
222
+ switch (parseAs) {
223
+ case 'arrayBuffer':
224
+ case 'blob':
225
+ case 'formData':
226
+ case 'text':
227
+ data = await response[parseAs]();
228
+ break;
229
+ case 'json': {
230
+ // Some servers return 200 with no Content-Length and empty body.
231
+ // response.json() would throw; read as text and parse if non-empty.
232
+ const text = await response.text();
233
+ data = text ? JSON.parse(text) : {};
234
+ break;
235
+ }
236
+ case 'stream':
237
+ return opts.responseStyle === 'data'
238
+ ? response.body
239
+ : {
240
+ data: response.body,
241
+ ...result,
242
+ };
243
+ }
244
+
245
+ if (parseAs === 'json') {
246
+ if (opts.responseValidator) {
247
+ await opts.responseValidator(data);
248
+ }
249
+
250
+ if (opts.responseTransformer) {
251
+ data = await opts.responseTransformer(data);
252
+ }
253
+ }
254
+
255
+ return opts.responseStyle === 'data'
256
+ ? data
257
+ : {
258
+ data,
259
+ ...result,
260
+ };
261
+ }
262
+
263
+ return parseErrorResponse(response, request, opts, interceptors);
264
+ };
265
+
266
+ const makeMethodFn = (method: Uppercase<HttpMethod>) => (options: RequestOptions) =>
267
+ request({ ...options, method });
268
+
269
+ const makeSseFn = (method: Uppercase<HttpMethod>) => async (options: RequestOptions) => {
270
+ const { opts, url } = await beforeRequest(options);
271
+ return createSseClient({
272
+ ...opts,
273
+ body: opts.body as BodyInit | null | undefined,
274
+ fetch: globalThis.fetch,
275
+ headers: opts.headers as unknown as Record<string, string>,
276
+ method,
277
+ onRequest: async (url, init) => {
278
+ let request = new Request(url, init);
279
+ for (const fn of interceptors.request.fns) {
280
+ if (fn) {
281
+ request = await fn(request, opts);
282
+ }
283
+ }
284
+ return request;
285
+ },
286
+ serializedBody: getValidRequestBody(opts) as BodyInit | null | undefined,
287
+ url,
288
+ });
289
+ };
290
+
291
+ return {
292
+ buildUrl,
293
+ connect: makeMethodFn('CONNECT'),
294
+ delete: makeMethodFn('DELETE'),
295
+ get: makeMethodFn('GET'),
296
+ getConfig,
297
+ head: makeMethodFn('HEAD'),
298
+ interceptors,
299
+ options: makeMethodFn('OPTIONS'),
300
+ patch: makeMethodFn('PATCH'),
301
+ post: makeMethodFn('POST'),
302
+ put: makeMethodFn('PUT'),
303
+ request,
304
+ setConfig,
305
+ sse: {
306
+ connect: makeSseFn('CONNECT'),
307
+ delete: makeSseFn('DELETE'),
308
+ get: makeSseFn('GET'),
309
+ head: makeSseFn('HEAD'),
310
+ options: makeSseFn('OPTIONS'),
311
+ patch: makeSseFn('PATCH'),
312
+ post: makeSseFn('POST'),
313
+ put: makeSseFn('PUT'),
314
+ trace: makeSseFn('TRACE'),
315
+ },
316
+ trace: makeMethodFn('TRACE'),
317
+ } as Client;
318
+ };
@@ -0,0 +1,24 @@
1
+ export type { Auth } from '../core/auth';
2
+ export type { QuerySerializerOptions } from '../core/bodySerializer';
3
+ export {
4
+ formDataBodySerializer,
5
+ jsonBodySerializer,
6
+ urlSearchParamsBodySerializer,
7
+ } from '../core/bodySerializer';
8
+ export { buildClientParams } from '../core/params';
9
+ export { serializeQueryKeyValue } from '../core/queryKeySerializer';
10
+ export { createClient } from './client';
11
+ export type {
12
+ Client,
13
+ ClientOptions,
14
+ Config,
15
+ CreateClientConfig,
16
+ Options,
17
+ RequestOptions,
18
+ RequestResult,
19
+ ResolvedRequestOptions,
20
+ ResponseStyle,
21
+ RetryOptions,
22
+ TDataShape,
23
+ } from './types';
24
+ export { createConfig, mergeHeaders } from './utils';
@@ -0,0 +1,243 @@
1
+ import type { Options as KyOptions } from 'ky';
2
+ import type ky from 'ky';
3
+
4
+ import type { Auth } from '../core/auth';
5
+ import type {
6
+ ServerSentEventsOptions,
7
+ ServerSentEventsResult,
8
+ } from '../core/serverSentEvents';
9
+ import type { Client as CoreClient, Config as CoreConfig } from '../core/types';
10
+ import type { Middleware } from './utils';
11
+
12
+ export type ResponseStyle = 'data' | 'fields';
13
+
14
+ export interface RetryOptions {
15
+ /**
16
+ * Maximum number of retry attempts
17
+ *
18
+ * @default 2
19
+ */
20
+ limit?: number;
21
+ /**
22
+ * HTTP methods to retry
23
+ *
24
+ * @default ['get', 'put', 'head', 'delete', 'options', 'trace']
25
+ */
26
+ methods?: Array<'get' | 'post' | 'put' | 'delete' | 'patch' | 'head' | 'options' | 'trace'>;
27
+ /**
28
+ * HTTP status codes to retry
29
+ *
30
+ * @default [408, 413, 429, 500, 502, 503, 504]
31
+ */
32
+ statusCodes?: number[];
33
+ }
34
+
35
+ export interface Config<T extends ClientOptions = ClientOptions>
36
+ extends
37
+ Omit<KyOptions, 'body' | 'headers' | 'method' | 'prefixUrl' | 'retry' | 'throwHttpErrors'>,
38
+ CoreConfig {
39
+ /**
40
+ * Base URL for all requests made by this client.
41
+ */
42
+ baseUrl?: T['baseUrl'];
43
+ /**
44
+ * Ky instance to use. You can use this option to provide a custom
45
+ * ky instance.
46
+ */
47
+ ky?: typeof ky;
48
+ /**
49
+ * Additional ky-specific options that will be passed directly to ky.
50
+ * This allows you to use any ky option not explicitly exposed in the config.
51
+ */
52
+ kyOptions?: Omit<KyOptions, 'method' | 'prefixUrl'>;
53
+ /**
54
+ * Return the response data parsed in a specified format. By default, `auto`
55
+ * will infer the appropriate method from the `Content-Type` response header.
56
+ * You can override this behavior with any of the {@link Body} methods.
57
+ * Select `stream` if you don't want to parse response data at all.
58
+ *
59
+ * @default 'auto'
60
+ */
61
+ parseAs?: 'arrayBuffer' | 'auto' | 'blob' | 'formData' | 'json' | 'stream' | 'text';
62
+ /**
63
+ * Should we return only data or multiple fields (data, error, response, etc.)?
64
+ *
65
+ * @default 'fields'
66
+ */
67
+ responseStyle?: ResponseStyle;
68
+ /**
69
+ * Retry configuration
70
+ */
71
+ retry?: RetryOptions;
72
+ /**
73
+ * Throw an error instead of returning it in the response?
74
+ *
75
+ * @default false
76
+ */
77
+ throwOnError?: T['throwOnError'];
78
+ /**
79
+ * Request timeout in milliseconds
80
+ *
81
+ * @default 10000
82
+ */
83
+ timeout?: number;
84
+ }
85
+
86
+ export interface RequestOptions<
87
+ TData = unknown,
88
+ TResponseStyle extends ResponseStyle = 'fields',
89
+ ThrowOnError extends boolean = boolean,
90
+ Url extends string = string,
91
+ >
92
+ extends
93
+ Config<{
94
+ responseStyle: TResponseStyle;
95
+ throwOnError: ThrowOnError;
96
+ }>,
97
+ Pick<
98
+ ServerSentEventsOptions<TData>,
99
+ | 'onSseError'
100
+ | 'onSseEvent'
101
+ | 'sseDefaultRetryDelay'
102
+ | 'sseMaxRetryAttempts'
103
+ | 'sseMaxRetryDelay'
104
+ > {
105
+ /**
106
+ * Any body that you want to add to your request.
107
+ *
108
+ * {@link https://developer.mozilla.org/docs/Web/API/fetch#body}
109
+ */
110
+ body?: unknown;
111
+ path?: Record<string, unknown>;
112
+ query?: Record<string, unknown>;
113
+ /**
114
+ * Security mechanism(s) to use for the request.
115
+ */
116
+ security?: ReadonlyArray<Auth>;
117
+ url: Url;
118
+ }
119
+
120
+ export interface ResolvedRequestOptions<
121
+ TResponseStyle extends ResponseStyle = 'fields',
122
+ ThrowOnError extends boolean = boolean,
123
+ Url extends string = string,
124
+ > extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> {
125
+ serializedBody?: string;
126
+ }
127
+
128
+ export type RequestResult<
129
+ TData = unknown,
130
+ TError = unknown,
131
+ ThrowOnError extends boolean = boolean,
132
+ TResponseStyle extends ResponseStyle = 'fields',
133
+ > = ThrowOnError extends true
134
+ ? Promise<
135
+ TResponseStyle extends 'data'
136
+ ? TData extends Record<string, unknown>
137
+ ? TData[keyof TData]
138
+ : TData
139
+ : {
140
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
141
+ request: Request;
142
+ response: Response;
143
+ }
144
+ >
145
+ : Promise<
146
+ TResponseStyle extends 'data'
147
+ ? (TData extends Record<string, unknown> ? TData[keyof TData] : TData) | undefined
148
+ : (
149
+ | {
150
+ data: TData extends Record<string, unknown> ? TData[keyof TData] : TData;
151
+ error: undefined;
152
+ }
153
+ | {
154
+ data: undefined;
155
+ error: TError extends Record<string, unknown> ? TError[keyof TError] : TError;
156
+ }
157
+ ) & {
158
+ request: Request;
159
+ response: Response;
160
+ }
161
+ >;
162
+
163
+ export interface ClientOptions {
164
+ baseUrl?: string;
165
+ responseStyle?: ResponseStyle;
166
+ throwOnError?: boolean;
167
+ }
168
+
169
+ type MethodFn = <
170
+ TData = unknown,
171
+ TError = unknown,
172
+ ThrowOnError extends boolean = false,
173
+ TResponseStyle extends ResponseStyle = 'fields',
174
+ >(
175
+ options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
176
+ ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
177
+
178
+ type SseFn = <
179
+ TData = unknown,
180
+ TError = unknown,
181
+ ThrowOnError extends boolean = false,
182
+ TResponseStyle extends ResponseStyle = 'fields',
183
+ >(
184
+ options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
185
+ ) => Promise<ServerSentEventsResult<TData, TError>>;
186
+
187
+ type RequestFn = <
188
+ TData = unknown,
189
+ TError = unknown,
190
+ ThrowOnError extends boolean = false,
191
+ TResponseStyle extends ResponseStyle = 'fields',
192
+ >(
193
+ options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'> &
194
+ Pick<Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>, 'method'>,
195
+ ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
196
+
197
+ type BuildUrlFn = <
198
+ TData extends {
199
+ body?: unknown;
200
+ path?: Record<string, unknown>;
201
+ query?: Record<string, unknown>;
202
+ url: string;
203
+ },
204
+ >(
205
+ options: TData & Options<TData>,
206
+ ) => string;
207
+
208
+ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn, SseFn> & {
209
+ interceptors: Middleware<Request, Response, unknown, ResolvedRequestOptions>;
210
+ };
211
+
212
+ /**
213
+ * The `createClientConfig()` function will be called on client initialization
214
+ * and the returned object will become the client's initial configuration.
215
+ *
216
+ * You may want to initialize your client this way instead of calling
217
+ * `setConfig()`. This is useful for example if you're using Next.js
218
+ * to ensure your client always has the correct values.
219
+ */
220
+ export type CreateClientConfig<T extends ClientOptions = ClientOptions> = (
221
+ override?: Config<ClientOptions & T>,
222
+ ) => Config<Required<ClientOptions> & T>;
223
+
224
+ export interface TDataShape {
225
+ body?: unknown;
226
+ headers?: unknown;
227
+ path?: unknown;
228
+ query?: unknown;
229
+ url: string;
230
+ }
231
+
232
+ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
233
+
234
+ export type Options<
235
+ TData extends TDataShape = TDataShape,
236
+ ThrowOnError extends boolean = boolean,
237
+ TResponse = unknown,
238
+ TResponseStyle extends ResponseStyle = 'fields',
239
+ > = OmitKeys<
240
+ RequestOptions<TResponse, TResponseStyle, ThrowOnError>,
241
+ 'body' | 'path' | 'query' | 'url'
242
+ > &
243
+ ([TData] extends [never] ? unknown : Omit<TData, 'url'>);