@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.
@@ -14,6 +14,7 @@ import {
14
14
  import { firstValueFrom } from 'rxjs';
15
15
  import { filter } from 'rxjs/operators';
16
16
 
17
+ import { createSseClient } from '../core/serverSentEvents';
17
18
  import type {
18
19
  Client,
19
20
  Config,
@@ -58,7 +59,7 @@ export const createClient = (config: Config = {}): Client => {
58
59
  ThrowOnError extends boolean = false,
59
60
  TResponseStyle extends ResponseStyle = 'fields',
60
61
  >(
61
- options: RequestOptions<TResponseStyle, ThrowOnError>,
62
+ options: RequestOptions<unknown, TResponseStyle, ThrowOnError>,
62
63
  ) => {
63
64
  const opts = {
64
65
  ..._config,
@@ -100,11 +101,11 @@ export const createClient = (config: Config = {}): Client => {
100
101
  },
101
102
  );
102
103
 
103
- return { opts, req };
104
+ return { opts, req, url };
104
105
  };
105
106
 
106
- const request: Client['request'] = async (options) => {
107
- const { opts, req: initialReq } = requestOptions(options);
107
+ const beforeRequest = async (options: RequestOptions) => {
108
+ const { opts, req, url } = requestOptions(options);
108
109
 
109
110
  if (opts.security) {
110
111
  await setAuthParams({
@@ -117,6 +118,13 @@ export const createClient = (config: Config = {}): Client => {
117
118
  await opts.requestValidator(opts);
118
119
  }
119
120
 
121
+ return { opts, req, url };
122
+ };
123
+
124
+ const request: Client['request'] = async (options) => {
125
+ // @ts-expect-error
126
+ const { opts, req: initialReq } = await beforeRequest(options);
127
+
120
128
  let req = initialReq;
121
129
 
122
130
  for (const fn of interceptors.request._fns) {
@@ -190,18 +198,33 @@ export const createClient = (config: Config = {}): Client => {
190
198
  }
191
199
  };
192
200
 
201
+ const makeMethod = (method: Required<Config>['method']) => {
202
+ const fn = (options: RequestOptions) => request({ ...options, method });
203
+ fn.sse = async (options: RequestOptions) => {
204
+ const { opts, url } = await beforeRequest(options);
205
+ return createSseClient({
206
+ ...opts,
207
+ body: opts.body as BodyInit | null | undefined,
208
+ headers: opts.headers as unknown as Record<string, string>,
209
+ method,
210
+ url,
211
+ });
212
+ };
213
+ return fn;
214
+ };
215
+
193
216
  return {
194
217
  buildUrl,
195
- connect: (options) => request({ ...options, method: 'CONNECT' }),
196
- delete: (options) => request({ ...options, method: 'DELETE' }),
197
- get: (options) => request({ ...options, method: 'GET' }),
218
+ connect: makeMethod('CONNECT'),
219
+ delete: makeMethod('DELETE'),
220
+ get: makeMethod('GET'),
198
221
  getConfig,
199
- head: (options) => request({ ...options, method: 'HEAD' }),
222
+ head: makeMethod('HEAD'),
200
223
  interceptors,
201
- options: (options) => request({ ...options, method: 'OPTIONS' }),
202
- patch: (options) => request({ ...options, method: 'PATCH' }),
203
- post: (options) => request({ ...options, method: 'POST' }),
204
- put: (options) => request({ ...options, method: 'PUT' }),
224
+ options: makeMethod('OPTIONS'),
225
+ patch: makeMethod('PATCH'),
226
+ post: makeMethod('POST'),
227
+ put: makeMethod('PUT'),
205
228
  request,
206
229
  requestOptions: (options) => {
207
230
  if (options.security) {
@@ -217,6 +240,6 @@ export const createClient = (config: Config = {}): Client => {
217
240
  return requestOptions(options).req;
218
241
  },
219
242
  setConfig,
220
- trace: (options) => request({ ...options, method: 'TRACE' }),
221
- };
243
+ trace: makeMethod('TRACE'),
244
+ } as Client;
222
245
  };
@@ -8,6 +8,10 @@ import type {
8
8
  import type { Injector } from '@angular/core';
9
9
 
10
10
  import type { Auth } from '../core/auth';
11
+ import type {
12
+ ServerSentEventsOptions,
13
+ ServerSentEventsResult,
14
+ } from '../core/serverSentEvents';
11
15
  import type {
12
16
  Client as CoreClient,
13
17
  Config as CoreConfig,
@@ -61,13 +65,22 @@ export interface Config<T extends ClientOptions = ClientOptions>
61
65
  }
62
66
 
63
67
  export interface RequestOptions<
68
+ TData = unknown,
64
69
  TResponseStyle extends ResponseStyle = 'fields',
65
70
  ThrowOnError extends boolean = boolean,
66
71
  Url extends string = string,
67
72
  > extends Config<{
68
- responseStyle: TResponseStyle;
69
- throwOnError: ThrowOnError;
70
- }> {
73
+ responseStyle: TResponseStyle;
74
+ throwOnError: ThrowOnError;
75
+ }>,
76
+ Pick<
77
+ ServerSentEventsOptions<TData>,
78
+ | 'onSseError'
79
+ | 'onSseEvent'
80
+ | 'sseDefaultRetryDelay'
81
+ | 'sseMaxRetryAttempts'
82
+ | 'sseMaxRetryDelay'
83
+ > {
71
84
  /**
72
85
  * Any body that you want to add to your request.
73
86
  *
@@ -91,7 +104,7 @@ export interface ResolvedRequestOptions<
91
104
  TResponseStyle extends ResponseStyle = 'fields',
92
105
  ThrowOnError extends boolean = boolean,
93
106
  Url extends string = string,
94
- > extends RequestOptions<TResponseStyle, ThrowOnError, Url> {
107
+ > extends RequestOptions<unknown, TResponseStyle, ThrowOnError, Url> {
95
108
  serializedBody?: string;
96
109
  }
97
110
 
@@ -142,30 +155,46 @@ export interface ClientOptions {
142
155
  throwOnError?: boolean;
143
156
  }
144
157
 
145
- type MethodFn = <
158
+ type MethodFnBase = <
146
159
  TData = unknown,
147
160
  TError = unknown,
148
161
  ThrowOnError extends boolean = false,
149
162
  TResponseStyle extends ResponseStyle = 'fields',
150
163
  >(
151
- options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'>,
164
+ options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
152
165
  ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
153
166
 
167
+ type MethodFnServerSentEvents = <
168
+ TData = unknown,
169
+ TError = unknown,
170
+ ThrowOnError extends boolean = false,
171
+ TResponseStyle extends ResponseStyle = 'fields',
172
+ >(
173
+ options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'>,
174
+ ) => Promise<ServerSentEventsResult<TData, TError>>;
175
+
176
+ type MethodFn = MethodFnBase & {
177
+ sse: MethodFnServerSentEvents;
178
+ };
179
+
154
180
  type RequestFn = <
155
181
  TData = unknown,
156
182
  TError = unknown,
157
183
  ThrowOnError extends boolean = false,
158
184
  TResponseStyle extends ResponseStyle = 'fields',
159
185
  >(
160
- options: Omit<RequestOptions<TResponseStyle, ThrowOnError>, 'method'> &
161
- Pick<Required<RequestOptions<TResponseStyle, ThrowOnError>>, 'method'>,
186
+ options: Omit<RequestOptions<TData, TResponseStyle, ThrowOnError>, 'method'> &
187
+ Pick<
188
+ Required<RequestOptions<TData, TResponseStyle, ThrowOnError>>,
189
+ 'method'
190
+ >,
162
191
  ) => RequestResult<TData, TError, ThrowOnError, TResponseStyle>;
163
192
 
164
193
  type RequestOptionsFn = <
165
194
  ThrowOnError extends boolean = false,
166
195
  TResponseStyle extends ResponseStyle = 'fields',
167
196
  >(
168
- options: RequestOptions<TResponseStyle, ThrowOnError>,
197
+ options: RequestOptions<unknown, TResponseStyle, ThrowOnError>,
169
198
  ) => HttpRequest<unknown>;
170
199
 
171
200
  type BuildUrlFn = <
@@ -186,7 +215,6 @@ export type Client = CoreClient<RequestFn, Config, MethodFn, BuildUrlFn> & {
186
215
  unknown,
187
216
  ResolvedRequestOptions
188
217
  >;
189
-
190
218
  requestOptions: RequestOptionsFn;
191
219
  };
192
220
 
@@ -215,9 +243,10 @@ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
215
243
  export type Options<
216
244
  TData extends TDataShape = TDataShape,
217
245
  ThrowOnError extends boolean = boolean,
246
+ TResponse = unknown,
218
247
  TResponseStyle extends ResponseStyle = 'fields',
219
248
  > = OmitKeys<
220
- RequestOptions<TResponseStyle, ThrowOnError>,
249
+ RequestOptions<TResponse, TResponseStyle, ThrowOnError>,
221
250
  'body' | 'path' | 'query' | 'url'
222
251
  > &
223
252
  Omit<TData, 'url'>;
@@ -229,18 +258,22 @@ export type OptionsLegacyParser<
229
258
  > = TData extends { body?: any }
230
259
  ? TData extends { headers?: any }
231
260
  ? OmitKeys<
232
- RequestOptions<TResponseStyle, ThrowOnError>,
261
+ RequestOptions<unknown, TResponseStyle, ThrowOnError>,
233
262
  'body' | 'headers' | 'url'
234
263
  > &
235
264
  TData
236
- : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'body' | 'url'> &
265
+ : OmitKeys<
266
+ RequestOptions<unknown, TResponseStyle, ThrowOnError>,
267
+ 'body' | 'url'
268
+ > &
237
269
  TData &
238
- Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'headers'>
270
+ Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, 'headers'>
239
271
  : TData extends { headers?: any }
240
272
  ? OmitKeys<
241
- RequestOptions<TResponseStyle, ThrowOnError>,
273
+ RequestOptions<unknown, TResponseStyle, ThrowOnError>,
242
274
  'headers' | 'url'
243
275
  > &
244
276
  TData &
245
- Pick<RequestOptions<TResponseStyle, ThrowOnError>, 'body'>
246
- : OmitKeys<RequestOptions<TResponseStyle, ThrowOnError>, 'url'> & TData;
277
+ Pick<RequestOptions<unknown, TResponseStyle, ThrowOnError>, 'body'>
278
+ : OmitKeys<RequestOptions<unknown, TResponseStyle, ThrowOnError>, 'url'> &
279
+ TData;
@@ -1,7 +1,8 @@
1
1
  import type { AxiosError, AxiosInstance, RawAxiosRequestHeaders } from 'axios';
2
2
  import axios from 'axios';
3
3
 
4
- import type { Client, Config } from './types';
4
+ import { createSseClient } from '../core/serverSentEvents';
5
+ import type { Client, Config, RequestOptions } from './types';
5
6
  import {
6
7
  buildUrl,
7
8
  createConfig,
@@ -36,8 +37,7 @@ export const createClient = (config: Config = {}): Client => {
36
37
  return getConfig();
37
38
  };
38
39
 
39
- // @ts-expect-error
40
- const request: Client['request'] = async (options) => {
40
+ const beforeRequest = async (options: RequestOptions) => {
41
41
  const opts = {
42
42
  ..._config,
43
43
  ...options,
@@ -62,6 +62,13 @@ export const createClient = (config: Config = {}): Client => {
62
62
 
63
63
  const url = buildUrl(opts);
64
64
 
65
+ return { opts, url };
66
+ };
67
+
68
+ // @ts-expect-error
69
+ const request: Client['request'] = async (options) => {
70
+ // @ts-expect-error
71
+ const { opts, url } = await beforeRequest(options);
65
72
  try {
66
73
  // assign Axios here for consistency with fetch
67
74
  const _axios = opts.axios!;
@@ -104,18 +111,37 @@ export const createClient = (config: Config = {}): Client => {
104
111
  }
105
112
  };
106
113
 
114
+ const makeMethod = (method: Required<Config>['method']) => {
115
+ const fn = (options: RequestOptions) => request({ ...options, method });
116
+ fn.sse = async (options: RequestOptions) => {
117
+ const { opts, url } = await beforeRequest(options);
118
+ return createSseClient({
119
+ ...opts,
120
+ body: opts.body as BodyInit | null | undefined,
121
+ headers: opts.headers as Record<string, string>,
122
+ method,
123
+ // @ts-expect-error
124
+ signal: opts.signal,
125
+ url,
126
+ });
127
+ };
128
+ return fn;
129
+ };
130
+
107
131
  return {
108
132
  buildUrl,
109
- delete: (options) => request({ ...options, method: 'DELETE' }),
110
- get: (options) => request({ ...options, method: 'GET' }),
133
+ connect: makeMethod('CONNECT'),
134
+ delete: makeMethod('DELETE'),
135
+ get: makeMethod('GET'),
111
136
  getConfig,
112
- head: (options) => request({ ...options, method: 'HEAD' }),
137
+ head: makeMethod('HEAD'),
113
138
  instance,
114
- options: (options) => request({ ...options, method: 'OPTIONS' }),
115
- patch: (options) => request({ ...options, method: 'PATCH' }),
116
- post: (options) => request({ ...options, method: 'POST' }),
117
- put: (options) => request({ ...options, method: 'PUT' }),
139
+ options: makeMethod('OPTIONS'),
140
+ patch: makeMethod('PATCH'),
141
+ post: makeMethod('POST'),
142
+ put: makeMethod('PUT'),
118
143
  request,
119
144
  setConfig,
145
+ trace: makeMethod('TRACE'),
120
146
  } as Client;
121
147
  };
@@ -8,6 +8,10 @@ import type {
8
8
  } from 'axios';
9
9
 
10
10
  import type { Auth } from '../core/auth';
11
+ import type {
12
+ ServerSentEventsOptions,
13
+ ServerSentEventsResult,
14
+ } from '../core/serverSentEvents';
11
15
  import type {
12
16
  Client as CoreClient,
13
17
  Config as CoreConfig,
@@ -54,11 +58,20 @@ export interface Config<T extends ClientOptions = ClientOptions>
54
58
  }
55
59
 
56
60
  export interface RequestOptions<
61
+ TData = unknown,
57
62
  ThrowOnError extends boolean = boolean,
58
63
  Url extends string = string,
59
64
  > extends Config<{
60
- throwOnError: ThrowOnError;
61
- }> {
65
+ throwOnError: ThrowOnError;
66
+ }>,
67
+ Pick<
68
+ ServerSentEventsOptions<TData>,
69
+ | 'onSseError'
70
+ | 'onSseEvent'
71
+ | 'sseDefaultRetryDelay'
72
+ | 'sseMaxRetryAttempts'
73
+ | 'sseMaxRetryDelay'
74
+ > {
62
75
  /**
63
76
  * Any body that you want to add to your request.
64
77
  *
@@ -74,6 +87,11 @@ export interface RequestOptions<
74
87
  url: Url;
75
88
  }
76
89
 
90
+ export interface ClientOptions {
91
+ baseURL?: string;
92
+ throwOnError?: boolean;
93
+ }
94
+
77
95
  export type RequestResult<
78
96
  TData = unknown,
79
97
  TError = unknown,
@@ -98,26 +116,33 @@ export type RequestResult<
98
116
  })
99
117
  >;
100
118
 
101
- export interface ClientOptions {
102
- baseURL?: string;
103
- throwOnError?: boolean;
104
- }
105
-
106
- type MethodFn = <
119
+ type MethodFnBase = <
107
120
  TData = unknown,
108
121
  TError = unknown,
109
122
  ThrowOnError extends boolean = false,
110
123
  >(
111
- options: Omit<RequestOptions<ThrowOnError>, 'method'>,
124
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
112
125
  ) => RequestResult<TData, TError, ThrowOnError>;
113
126
 
127
+ type MethodFnServerSentEvents = <
128
+ TData = unknown,
129
+ TError = unknown,
130
+ ThrowOnError extends boolean = false,
131
+ >(
132
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'>,
133
+ ) => Promise<ServerSentEventsResult<TData, TError>>;
134
+
135
+ type MethodFn = MethodFnBase & {
136
+ sse: MethodFnServerSentEvents;
137
+ };
138
+
114
139
  type RequestFn = <
115
140
  TData = unknown,
116
141
  TError = unknown,
117
142
  ThrowOnError extends boolean = false,
118
143
  >(
119
- options: Omit<RequestOptions<ThrowOnError>, 'method'> &
120
- Pick<Required<RequestOptions<ThrowOnError>>, 'method'>,
144
+ options: Omit<RequestOptions<TData, ThrowOnError>, 'method'> &
145
+ Pick<Required<RequestOptions<TData, ThrowOnError>>, 'method'>,
121
146
  ) => RequestResult<TData, TError, ThrowOnError>;
122
147
 
123
148
  type BuildUrlFn = <
@@ -160,7 +185,11 @@ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>;
160
185
  export type Options<
161
186
  TData extends TDataShape = TDataShape,
162
187
  ThrowOnError extends boolean = boolean,
163
- > = OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'path' | 'query' | 'url'> &
188
+ TResponse = unknown,
189
+ > = OmitKeys<
190
+ RequestOptions<TResponse, ThrowOnError>,
191
+ 'body' | 'path' | 'query' | 'url'
192
+ > &
164
193
  Omit<TData, 'url'>;
165
194
 
166
195
  export type OptionsLegacyParser<
@@ -168,12 +197,16 @@ export type OptionsLegacyParser<
168
197
  ThrowOnError extends boolean = boolean,
169
198
  > = TData extends { body?: any }
170
199
  ? TData extends { headers?: any }
171
- ? OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'headers' | 'url'> & TData
172
- : OmitKeys<RequestOptions<ThrowOnError>, 'body' | 'url'> &
200
+ ? OmitKeys<
201
+ RequestOptions<unknown, ThrowOnError>,
202
+ 'body' | 'headers' | 'url'
203
+ > &
204
+ TData
205
+ : OmitKeys<RequestOptions<unknown, ThrowOnError>, 'body' | 'url'> &
173
206
  TData &
174
- Pick<RequestOptions<ThrowOnError>, 'headers'>
207
+ Pick<RequestOptions<unknown, ThrowOnError>, 'headers'>
175
208
  : TData extends { headers?: any }
176
- ? OmitKeys<RequestOptions<ThrowOnError>, 'headers' | 'url'> &
209
+ ? OmitKeys<RequestOptions<unknown, ThrowOnError>, 'headers' | 'url'> &
177
210
  TData &
178
- Pick<RequestOptions<ThrowOnError>, 'body'>
179
- : OmitKeys<RequestOptions<ThrowOnError>, 'url'> & TData;
211
+ Pick<RequestOptions<unknown, ThrowOnError>, 'body'>
212
+ : OmitKeys<RequestOptions<unknown, ThrowOnError>, 'url'> & TData;
@@ -1,93 +1,13 @@
1
1
  import { getAuthToken } from '../core/auth';
2
- import type {
3
- QuerySerializer,
4
- QuerySerializerOptions,
5
- } from '../core/bodySerializer';
6
- import type { ArraySeparatorStyle } from '../core/pathSerializer';
2
+ import type { QuerySerializerOptions } from '../core/bodySerializer';
7
3
  import {
8
4
  serializeArrayParam,
9
5
  serializeObjectParam,
10
6
  serializePrimitiveParam,
11
7
  } from '../core/pathSerializer';
8
+ import { getUrl } from '../core/utils';
12
9
  import type { Client, ClientOptions, Config, RequestOptions } from './types';
13
10
 
14
- interface PathSerializer {
15
- path: Record<string, unknown>;
16
- url: string;
17
- }
18
-
19
- const PATH_PARAM_RE = /\{[^{}]+\}/g;
20
-
21
- const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => {
22
- let url = _url;
23
- const matches = _url.match(PATH_PARAM_RE);
24
- if (matches) {
25
- for (const match of matches) {
26
- let explode = false;
27
- let name = match.substring(1, match.length - 1);
28
- let style: ArraySeparatorStyle = 'simple';
29
-
30
- if (name.endsWith('*')) {
31
- explode = true;
32
- name = name.substring(0, name.length - 1);
33
- }
34
-
35
- if (name.startsWith('.')) {
36
- name = name.substring(1);
37
- style = 'label';
38
- } else if (name.startsWith(';')) {
39
- name = name.substring(1);
40
- style = 'matrix';
41
- }
42
-
43
- const value = path[name];
44
-
45
- if (value === undefined || value === null) {
46
- continue;
47
- }
48
-
49
- if (Array.isArray(value)) {
50
- url = url.replace(
51
- match,
52
- serializeArrayParam({ explode, name, style, value }),
53
- );
54
- continue;
55
- }
56
-
57
- if (typeof value === 'object') {
58
- url = url.replace(
59
- match,
60
- serializeObjectParam({
61
- explode,
62
- name,
63
- style,
64
- value: value as Record<string, unknown>,
65
- valueOnly: true,
66
- }),
67
- );
68
- continue;
69
- }
70
-
71
- if (style === 'matrix') {
72
- url = url.replace(
73
- match,
74
- `;${serializePrimitiveParam({
75
- name,
76
- value: value as string,
77
- })}`,
78
- );
79
- continue;
80
- }
81
-
82
- const replaceValue = encodeURIComponent(
83
- style === 'label' ? `.${value as string}` : (value as string),
84
- );
85
- url = url.replace(match, replaceValue);
86
- }
87
- }
88
- return url;
89
- };
90
-
91
11
  export const createQuerySerializer = <T = unknown>({
92
12
  allowReserved,
93
13
  array,
@@ -203,8 +123,9 @@ export const setAuthParams = async ({
203
123
  }
204
124
  };
205
125
 
206
- export const buildUrl: Client['buildUrl'] = (options) => {
207
- const url = getUrl({
126
+ export const buildUrl: Client['buildUrl'] = (options) =>
127
+ getUrl({
128
+ baseUrl: options.baseURL as string,
208
129
  path: options.path,
209
130
  // let `paramsSerializer()` handle query params if it exists
210
131
  query: !options.paramsSerializer ? options.query : undefined,
@@ -214,34 +135,6 @@ export const buildUrl: Client['buildUrl'] = (options) => {
214
135
  : createQuerySerializer(options.querySerializer),
215
136
  url: options.url,
216
137
  });
217
- return url;
218
- };
219
-
220
- export const getUrl = ({
221
- path,
222
- query,
223
- querySerializer,
224
- url: _url,
225
- }: {
226
- path?: Record<string, unknown>;
227
- query?: Record<string, unknown>;
228
- querySerializer: QuerySerializer;
229
- url: string;
230
- }) => {
231
- const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
232
- let url = pathUrl;
233
- if (path) {
234
- url = defaultPathSerializer({ path, url });
235
- }
236
- let search = query ? querySerializer(query) : '';
237
- if (search.startsWith('?')) {
238
- search = search.substring(1);
239
- }
240
- if (search) {
241
- url += `?${search}`;
242
- }
243
- return url;
244
- };
245
138
 
246
139
  export const mergeConfigs = (a: Config, b: Config): Config => {
247
140
  const config = { ...a, ...b };