@hey-api/openapi-ts 0.80.18 → 0.81.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.
@@ -1,4 +1,10 @@
1
- import type { Client, Config, ResolvedRequestOptions } from './types';
1
+ import { createSseClient } from '../core/serverSentEvents';
2
+ import type {
3
+ Client,
4
+ Config,
5
+ RequestOptions,
6
+ ResolvedRequestOptions,
7
+ } from './types';
2
8
  import {
3
9
  buildUrl,
4
10
  createConfig,
@@ -30,8 +36,7 @@ export const createClient = (config: Config = {}): Client => {
30
36
  ResolvedRequestOptions
31
37
  >();
32
38
 
33
- // @ts-expect-error
34
- const request: Client['request'] = async (options) => {
39
+ const beforeRequest = async (options: RequestOptions) => {
35
40
  const opts = {
36
41
  ..._config,
37
42
  ...options,
@@ -60,13 +65,22 @@ export const createClient = (config: Config = {}): Client => {
60
65
  opts.headers.delete('Content-Type');
61
66
  }
62
67
 
68
+ const url = buildUrl(opts);
69
+
70
+ return { opts, url };
71
+ };
72
+
73
+ // @ts-expect-error
74
+ const request: Client['request'] = async (options) => {
75
+ // @ts-expect-error
76
+ const { opts, url } = await beforeRequest(options);
77
+
63
78
  for (const fn of interceptors.request._fns) {
64
79
  if (fn) {
65
80
  await fn(opts);
66
81
  }
67
82
  }
68
83
 
69
- const url = buildUrl(opts);
70
84
  // fetch must be assigned here, otherwise it would throw the error:
71
85
  // TypeError: Failed to execute 'fetch' on 'Window': Illegal invocation
72
86
  const _fetch = opts.fetch!;
@@ -163,20 +177,35 @@ export const createClient = (config: Config = {}): Client => {
163
177
  };
164
178
  };
165
179
 
180
+ const makeMethod = (method: Required<Config>['method']) => {
181
+ const fn = (options: RequestOptions) => request({ ...options, method });
182
+ fn.sse = async (options: RequestOptions) => {
183
+ const { opts, url } = await beforeRequest(options);
184
+ return createSseClient({
185
+ ...opts,
186
+ body: opts.body as BodyInit | null | undefined,
187
+ headers: opts.headers as unknown as Record<string, string>,
188
+ method,
189
+ url,
190
+ });
191
+ };
192
+ return fn;
193
+ };
194
+
166
195
  return {
167
196
  buildUrl,
168
- connect: (options) => request({ ...options, method: 'CONNECT' }),
169
- delete: (options) => request({ ...options, method: 'DELETE' }),
170
- get: (options) => request({ ...options, method: 'GET' }),
197
+ connect: makeMethod('CONNECT'),
198
+ delete: makeMethod('DELETE'),
199
+ get: makeMethod('GET'),
171
200
  getConfig,
172
- head: (options) => request({ ...options, method: 'HEAD' }),
201
+ head: makeMethod('HEAD'),
173
202
  interceptors,
174
- options: (options) => request({ ...options, method: 'OPTIONS' }),
175
- patch: (options) => request({ ...options, method: 'PATCH' }),
176
- post: (options) => request({ ...options, method: 'POST' }),
177
- put: (options) => request({ ...options, method: 'PUT' }),
203
+ options: makeMethod('OPTIONS'),
204
+ patch: makeMethod('PATCH'),
205
+ post: makeMethod('POST'),
206
+ put: makeMethod('PUT'),
178
207
  request,
179
208
  setConfig,
180
- trace: (options) => request({ ...options, method: 'TRACE' }),
181
- };
209
+ trace: makeMethod('TRACE'),
210
+ } as Client;
182
211
  };
@@ -1,4 +1,8 @@
1
1
  import type { Auth } from '../core/auth';
2
+ import type {
3
+ ServerSentEventsOptions,
4
+ ServerSentEventsResult,
5
+ } from '../core/serverSentEvents';
2
6
  import type {
3
7
  Client as CoreClient,
4
8
  Config as CoreConfig,
@@ -44,11 +48,20 @@ export interface Config<T extends ClientOptions = ClientOptions>
44
48
  }
45
49
 
46
50
  export interface RequestOptions<
51
+ TData = unknown,
47
52
  ThrowOnError extends boolean = boolean,
48
53
  Url extends string = string,
49
54
  > extends Config<{
50
- throwOnError: ThrowOnError;
51
- }> {
55
+ throwOnError: ThrowOnError;
56
+ }>,
57
+ Pick<
58
+ ServerSentEventsOptions<TData>,
59
+ | 'onSseError'
60
+ | 'onSseEvent'
61
+ | 'sseDefaultRetryDelay'
62
+ | 'sseMaxRetryAttempts'
63
+ | 'sseMaxRetryDelay'
64
+ > {
52
65
  /**
53
66
  * Any body that you want to add to your request.
54
67
  *
@@ -67,7 +80,7 @@ export interface RequestOptions<
67
80
  export interface ResolvedRequestOptions<
68
81
  ThrowOnError extends boolean = boolean,
69
82
  Url extends string = string,
70
- > extends RequestOptions<ThrowOnError, Url> {
83
+ > extends RequestOptions<unknown, ThrowOnError, Url> {
71
84
  serializedBody?: string;
72
85
  }
73
86
 
@@ -104,21 +117,33 @@ export interface ClientOptions {
104
117
  throwOnError?: boolean;
105
118
  }
106
119
 
107
- type MethodFn = <
120
+ type MethodFnBase = <
108
121
  TData = unknown,
109
122
  TError = unknown,
110
123
  ThrowOnError extends boolean = false,
111
124
  >(
112
- options: Omit<RequestOptions<ThrowOnError>, 'method'>,
125
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
113
126
  ) => RequestResult<TData, TError, ThrowOnError>;
114
127
 
128
+ type MethodFnServerSentEvents = <
129
+ TData = unknown,
130
+ TError = unknown,
131
+ ThrowOnError extends boolean = false,
132
+ >(
133
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
134
+ ) => Promise<ServerSentEventsResult<TData, TError>>;
135
+
136
+ type MethodFn = MethodFnBase & {
137
+ sse: MethodFnServerSentEvents;
138
+ };
139
+
115
140
  type RequestFn = <
116
141
  TData = unknown,
117
142
  TError = unknown,
118
143
  ThrowOnError extends boolean = false,
119
144
  >(
120
- options: Omit<RequestOptions<ThrowOnError>, 'method'> &
121
- Pick<Required<RequestOptions<ThrowOnError>>, 'method'>,
145
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'> &
146
+ Pick<Required<RequestOptions<TData, ThrowOnError>>, 'method'>,
122
147
  ) => RequestResult<TData, TError, ThrowOnError>;
123
148
 
124
149
  type BuildUrlFn = <
@@ -161,7 +186,11 @@ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
161
186
  export type Options<
162
187
  TData extends TDataShape = TDataShape,
163
188
  ThrowOnError extends boolean = boolean,
164
- > = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> &
189
+ TResponse = unknown,
190
+ > = OmitKeys<
191
+ RequestOptions<TResponse, ThrowOnError>,
192
+ 'body' | 'path' | 'query' | 'url'
193
+ > &
165
194
  Omit<TData, 'url'>;
166
195
 
167
196
  export type OptionsLegacyParser<
@@ -169,12 +198,16 @@ export type OptionsLegacyParser<
169
198
  ThrowOnError extends boolean = boolean,
170
199
  > = TData extends { body?: any }
171
200
  ? TData extends { headers?: any }
172
- ? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & TData
173
- : OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> &
201
+ ? OmitKeys<
202
+ RequestOptions<unknown, ThrowOnError>,
203
+ 'body' | 'headers' | 'url'
204
+ > &
205
+ TData
206
+ : OmitKeys<RequestOptions<unknown, ThrowOnError>, 'body' | 'url'> &
174
207
  TData &
175
- Pick<RequestOptions<ThrowOnError>, 'headers'>
208
+ Pick<RequestOptions<unknown, ThrowOnError>, 'headers'>
176
209
  : TData extends { headers?: any }
177
- ? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> &
210
+ ? OmitKeys<RequestOptions<unknown, ThrowOnError>, 'headers' | 'url'> &
178
211
  TData &
179
- Pick<RequestOptions<ThrowOnError>, 'body'>
180
- : OmitKeys<RequestOptions<ThrowOnError>, 'url'> & TData;
212
+ Pick<RequestOptions<unknown, ThrowOnError>, 'body'>
213
+ : OmitKeys<RequestOptions<unknown, ThrowOnError>, 'url'> & TData;
@@ -6,7 +6,8 @@ import {
6
6
  } from 'nuxt/app';
7
7
  import { reactive, ref, watch } from 'vue';
8
8
 
9
- import type { Client, Config } from './types';
9
+ import { createSseClient } from '../core/serverSentEvents';
10
+ import type { Client, Config, RequestOptions } from './types';
10
11
  import {
11
12
  buildUrl,
12
13
  createConfig,
@@ -16,6 +17,7 @@ import {
16
17
  mergeInterceptors,
17
18
  serializeBody,
18
19
  setAuthParams,
20
+ unwrapRefs,
19
21
  } from './utils';
20
22
 
21
23
  export const createClient = (config: Config = {}): Client => {
@@ -28,6 +30,32 @@ export const createClient = (config: Config = {}): Client => {
28
30
  return getConfig();
29
31
  };
30
32
 
33
+ const beforeRequest = async (options: RequestOptions) => {
34
+ const opts = {
35
+ ..._config,
36
+ ...options,
37
+ $fetch: options.$fetch ?? _config.$fetch ?? $fetch,
38
+ headers: mergeHeaders(_config.headers, options.headers),
39
+ onRequest: mergeInterceptors(_config.onRequest, options.onRequest),
40
+ onResponse: mergeInterceptors(_config.onResponse, options.onResponse),
41
+ };
42
+
43
+ if (opts.security) {
44
+ await setAuthParams({
45
+ ...opts,
46
+ security: opts.security,
47
+ });
48
+ }
49
+
50
+ if (opts.requestValidator) {
51
+ await opts.requestValidator(opts);
52
+ }
53
+
54
+ const url = buildUrl(opts);
55
+
56
+ return { opts, url };
57
+ };
58
+
31
59
  const request: Client['request'] = ({
32
60
  asyncDataOptions,
33
61
  composable,
@@ -107,7 +135,11 @@ export const createClient = (config: Config = {}): Client => {
107
135
  const fetchFn = opts.$fetch;
108
136
 
109
137
  if (composable === '$fetch') {
110
- return executeFetchFn(opts, fetchFn);
138
+ return executeFetchFn(
139
+ // @ts-expect-error
140
+ opts,
141
+ fetchFn,
142
+ );
111
143
  }
112
144
 
113
145
  if (composable === 'useFetch' || composable === 'useLazyFetch') {
@@ -126,7 +158,12 @@ export const createClient = (config: Config = {}): Client => {
126
158
  : useFetch(() => buildUrl(opts), opts);
127
159
  }
128
160
 
129
- const handler: any = () => executeFetchFn(opts, fetchFn);
161
+ const handler: any = () =>
162
+ executeFetchFn(
163
+ // @ts-expect-error
164
+ opts,
165
+ fetchFn,
166
+ );
130
167
 
131
168
  if (composable === 'useAsyncData') {
132
169
  return key
@@ -143,19 +180,34 @@ export const createClient = (config: Config = {}): Client => {
143
180
  return undefined as any;
144
181
  };
145
182
 
183
+ const makeMethod = (method: Required<Config>['method']) => {
184
+ const fn = (options: RequestOptions) => request({ ...options, method });
185
+ fn.sse = async (options: RequestOptions) => {
186
+ const { opts, url } = await beforeRequest(options);
187
+ return createSseClient({
188
+ ...unwrapRefs(opts),
189
+ body: opts.body as BodyInit | null | undefined,
190
+ method,
191
+ signal: unwrapRefs(opts.signal) as AbortSignal,
192
+ url,
193
+ });
194
+ };
195
+ return fn;
196
+ };
197
+
146
198
  return {
147
199
  buildUrl,
148
- connect: (options) => request({ ...options, method: 'CONNECT' }),
149
- delete: (options) => request({ ...options, method: 'DELETE' }),
150
- get: (options) => request({ ...options, method: 'GET' }),
200
+ connect: makeMethod('CONNECT'),
201
+ delete: makeMethod('DELETE'),
202
+ get: makeMethod('GET'),
151
203
  getConfig,
152
- head: (options) => request({ ...options, method: 'HEAD' }),
153
- options: (options) => request({ ...options, method: 'OPTIONS' }),
154
- patch: (options) => request({ ...options, method: 'PATCH' }),
155
- post: (options) => request({ ...options, method: 'POST' }),
156
- put: (options) => request({ ...options, method: 'PUT' }),
204
+ head: makeMethod('HEAD'),
205
+ options: makeMethod('OPTIONS'),
206
+ patch: makeMethod('PATCH'),
207
+ post: makeMethod('POST'),
208
+ put: makeMethod('PUT'),
157
209
  request,
158
210
  setConfig,
159
- trace: (options) => request({ ...options, method: 'TRACE' }),
160
- };
211
+ trace: makeMethod('TRACE'),
212
+ } as Client;
161
213
  };
@@ -10,6 +10,10 @@ import type { Ref } from 'vue';
10
10
 
11
11
  import type { Auth } from '../core/auth';
12
12
  import type { QuerySerializerOptions } from '../core/bodySerializer';
13
+ import type {
14
+ ServerSentEventsOptions,
15
+ ServerSentEventsResult,
16
+ } from '../core/serverSentEvents';
13
17
  import type {
14
18
  Client as CoreClient,
15
19
  Config as CoreConfig,
@@ -73,7 +77,15 @@ export interface RequestOptions<
73
77
  path?: FetchOptions<unknown>['query'];
74
78
  query?: FetchOptions<unknown>['query'];
75
79
  rawBody?: unknown;
76
- }> {
80
+ }>,
81
+ Pick<
82
+ ServerSentEventsOptions<ResT>,
83
+ | 'onSseError'
84
+ | 'onSseEvent'
85
+ | 'sseDefaultRetryDelay'
86
+ | 'sseMaxRetryAttempts'
87
+ | 'sseMaxRetryDelay'
88
+ > {
77
89
  asyncDataOptions?: AsyncDataOptions<ResT, ResT, KeysOf<ResT>, DefaultT>;
78
90
  composable: TComposable;
79
91
  key?: string;
@@ -104,7 +116,7 @@ export interface ClientOptions {
104
116
  baseURL?: string;
105
117
  }
106
118
 
107
- type MethodFn = <
119
+ type MethodFnBase = <
108
120
  TComposable extends Composable,
109
121
  ResT = unknown,
110
122
  TError = unknown,
@@ -113,6 +125,19 @@ type MethodFn = <
113
125
  options: Omit<RequestOptions<TComposable, ResT, DefaultT>, 'method'>,
114
126
  ) => RequestResult<TComposable, ResT, TError>;
115
127
 
128
+ type MethodFnServerSentEvents = <
129
+ TComposable extends Composable,
130
+ ResT = unknown,
131
+ TError = unknown,
132
+ DefaultT = undefined,
133
+ >(
134
+ options: Omit<RequestOptions<TComposable, ResT, DefaultT>, 'method'>,
135
+ ) => Promise<ServerSentEventsResult<RequestResult<TComposable, ResT, TError>>>;
136
+
137
+ type MethodFn = MethodFnBase & {
138
+ sse: MethodFnServerSentEvents;
139
+ };
140
+
116
141
  type RequestFn = <
117
142
  TComposable extends Composable,
118
143
  ResT = unknown,
@@ -334,7 +334,7 @@ type UnwrapRefs<T> =
334
334
  ? { [K in keyof T]: UnwrapRefs<T[K]> }
335
335
  : T;
336
336
 
337
- const unwrapRefs = <T>(value: T): UnwrapRefs<T> => {
337
+ export const unwrapRefs = <T>(value: T): UnwrapRefs<T> => {
338
338
  if (value === null || typeof value !== 'object' || value instanceof Headers) {
339
339
  return (isRef(value) ? unref(value) : value) as UnwrapRefs<T>;
340
340
  }