@hey-api/openapi-ts 0.80.6 → 0.80.8
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.
- package/README.md +2 -2
- package/dist/chunk-F4DKDLSY.js +42 -0
- package/dist/chunk-F4DKDLSY.js.map +1 -0
- package/dist/clients/angular/client.ts +162 -0
- package/dist/clients/angular/index.ts +23 -0
- package/dist/clients/angular/types.ts +209 -0
- package/dist/clients/angular/utils.ts +417 -0
- package/dist/index.cjs +67 -64
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +12 -11
- package/dist/internal.cjs.map +1 -1
- package/dist/internal.d.cts +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +1 -1
- package/dist/{types.d-Cpp7XW_3.d.cts → types.d-5eps4CIa.d.cts} +121 -66
- package/dist/{types.d-Cpp7XW_3.d.ts → types.d-5eps4CIa.d.ts} +121 -66
- package/package.json +10 -1
- package/dist/chunk-CCWQBTYN.js +0 -39
- package/dist/chunk-CCWQBTYN.js.map +0 -1
|
@@ -0,0 +1,417 @@
|
|
|
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(
|
|
55
|
+
match,
|
|
56
|
+
serializeArrayParam({ explode, name, style, value }),
|
|
57
|
+
);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (typeof value === 'object') {
|
|
62
|
+
url = url.replace(
|
|
63
|
+
match,
|
|
64
|
+
serializeObjectParam({
|
|
65
|
+
explode,
|
|
66
|
+
name,
|
|
67
|
+
style,
|
|
68
|
+
value: value as Record<string, unknown>,
|
|
69
|
+
valueOnly: true,
|
|
70
|
+
}),
|
|
71
|
+
);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (style === 'matrix') {
|
|
76
|
+
url = url.replace(
|
|
77
|
+
match,
|
|
78
|
+
`;${serializePrimitiveParam({
|
|
79
|
+
name,
|
|
80
|
+
value: value as string,
|
|
81
|
+
})}`,
|
|
82
|
+
);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const replaceValue = encodeURIComponent(
|
|
87
|
+
style === 'label' ? `.${value as string}` : (value as string),
|
|
88
|
+
);
|
|
89
|
+
url = url.replace(match, replaceValue);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return url;
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const createQuerySerializer = <T = unknown>({
|
|
96
|
+
allowReserved,
|
|
97
|
+
array,
|
|
98
|
+
object,
|
|
99
|
+
}: QuerySerializerOptions = {}) => {
|
|
100
|
+
const querySerializer = (queryParams: T) => {
|
|
101
|
+
const search: string[] = [];
|
|
102
|
+
if (queryParams && typeof queryParams === 'object') {
|
|
103
|
+
for (const name in queryParams) {
|
|
104
|
+
const value = queryParams[name];
|
|
105
|
+
|
|
106
|
+
if (value === undefined || value === null) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (Array.isArray(value)) {
|
|
111
|
+
const serializedArray = serializeArrayParam({
|
|
112
|
+
allowReserved,
|
|
113
|
+
explode: true,
|
|
114
|
+
name,
|
|
115
|
+
style: 'form',
|
|
116
|
+
value,
|
|
117
|
+
...array,
|
|
118
|
+
});
|
|
119
|
+
if (serializedArray) search.push(serializedArray);
|
|
120
|
+
} else if (typeof value === 'object') {
|
|
121
|
+
const serializedObject = serializeObjectParam({
|
|
122
|
+
allowReserved,
|
|
123
|
+
explode: true,
|
|
124
|
+
name,
|
|
125
|
+
style: 'deepObject',
|
|
126
|
+
value: value as Record<string, unknown>,
|
|
127
|
+
...object,
|
|
128
|
+
});
|
|
129
|
+
if (serializedObject) search.push(serializedObject);
|
|
130
|
+
} else {
|
|
131
|
+
const serializedPrimitive = serializePrimitiveParam({
|
|
132
|
+
allowReserved,
|
|
133
|
+
name,
|
|
134
|
+
value: value as string,
|
|
135
|
+
});
|
|
136
|
+
if (serializedPrimitive) search.push(serializedPrimitive);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return search.join('&');
|
|
141
|
+
};
|
|
142
|
+
return querySerializer;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Infers parseAs value from provided Content-Type header.
|
|
147
|
+
*/
|
|
148
|
+
export const getParseAs = (
|
|
149
|
+
contentType: string | null,
|
|
150
|
+
): 'blob' | 'formData' | 'json' | 'stream' | 'text' | undefined => {
|
|
151
|
+
if (!contentType) {
|
|
152
|
+
// If no Content-Type header is provided, the best we can do is return the raw response body,
|
|
153
|
+
// which is effectively the same as the 'stream' option.
|
|
154
|
+
return 'stream';
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const cleanContent = contentType.split(';')[0]?.trim();
|
|
158
|
+
|
|
159
|
+
if (!cleanContent) {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (
|
|
164
|
+
cleanContent.startsWith('application/json') ||
|
|
165
|
+
cleanContent.endsWith('+json')
|
|
166
|
+
) {
|
|
167
|
+
return 'json';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (cleanContent === 'multipart/form-data') {
|
|
171
|
+
return 'formData';
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (
|
|
175
|
+
['application/', 'audio/', 'image/', 'video/'].some((type) =>
|
|
176
|
+
cleanContent.startsWith(type),
|
|
177
|
+
)
|
|
178
|
+
) {
|
|
179
|
+
return 'blob';
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (cleanContent.startsWith('text/')) {
|
|
183
|
+
return 'text';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return;
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
export const setAuthParams = async ({
|
|
190
|
+
security,
|
|
191
|
+
...options
|
|
192
|
+
}: Pick<Required<RequestOptions>, 'security'> &
|
|
193
|
+
Pick<RequestOptions, 'auth' | 'query'> & {
|
|
194
|
+
headers: Headers;
|
|
195
|
+
}) => {
|
|
196
|
+
for (const auth of security) {
|
|
197
|
+
const token = await getAuthToken(auth, options.auth);
|
|
198
|
+
|
|
199
|
+
if (!token) {
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
const name = auth.name ?? 'Authorization';
|
|
204
|
+
|
|
205
|
+
switch (auth.in) {
|
|
206
|
+
case 'query':
|
|
207
|
+
if (!options.query) {
|
|
208
|
+
options.query = {};
|
|
209
|
+
}
|
|
210
|
+
options.query[name] = token;
|
|
211
|
+
break;
|
|
212
|
+
case 'cookie':
|
|
213
|
+
options.headers.append('Cookie', `${name}=${token}`);
|
|
214
|
+
break;
|
|
215
|
+
case 'header':
|
|
216
|
+
default:
|
|
217
|
+
options.headers.set(name, token);
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
export const buildUrl: Client['buildUrl'] = (options) => {
|
|
226
|
+
const url = getUrl({
|
|
227
|
+
baseUrl: options.baseUrl as string,
|
|
228
|
+
path: options.path,
|
|
229
|
+
query: options.query,
|
|
230
|
+
querySerializer:
|
|
231
|
+
typeof options.querySerializer === 'function'
|
|
232
|
+
? options.querySerializer
|
|
233
|
+
: createQuerySerializer(options.querySerializer),
|
|
234
|
+
url: options.url,
|
|
235
|
+
});
|
|
236
|
+
return url;
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const getUrl = ({
|
|
240
|
+
baseUrl,
|
|
241
|
+
path,
|
|
242
|
+
query,
|
|
243
|
+
querySerializer,
|
|
244
|
+
url: _url,
|
|
245
|
+
}: {
|
|
246
|
+
baseUrl?: string;
|
|
247
|
+
path?: Record<string, unknown>;
|
|
248
|
+
query?: Record<string, unknown>;
|
|
249
|
+
querySerializer: QuerySerializer;
|
|
250
|
+
url: string;
|
|
251
|
+
}) => {
|
|
252
|
+
const pathUrl = _url.startsWith('/') ? _url : `/${_url}`;
|
|
253
|
+
let url = (baseUrl ?? '') + pathUrl;
|
|
254
|
+
if (path) {
|
|
255
|
+
url = defaultPathSerializer({ path, url });
|
|
256
|
+
}
|
|
257
|
+
let search = query ? querySerializer(query) : '';
|
|
258
|
+
if (search.startsWith('?')) {
|
|
259
|
+
search = search.substring(1);
|
|
260
|
+
}
|
|
261
|
+
if (search) {
|
|
262
|
+
url += `?${search}`;
|
|
263
|
+
}
|
|
264
|
+
return url;
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
export const mergeConfigs = (a: Config, b: Config): Config => {
|
|
268
|
+
const config = { ...a, ...b };
|
|
269
|
+
if (config.baseUrl?.endsWith('/')) {
|
|
270
|
+
config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
|
|
271
|
+
}
|
|
272
|
+
config.headers = mergeHeaders(a.headers, b.headers);
|
|
273
|
+
return config;
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
export const mergeHeaders = (
|
|
277
|
+
...headers: Array<Required<Config>['headers'] | undefined>
|
|
278
|
+
): Headers => {
|
|
279
|
+
const mergedHeaders = new Headers();
|
|
280
|
+
for (const header of headers) {
|
|
281
|
+
if (!header || typeof header !== 'object') {
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const iterator =
|
|
286
|
+
header instanceof Headers ? header.entries() : Object.entries(header);
|
|
287
|
+
|
|
288
|
+
for (const [key, value] of iterator) {
|
|
289
|
+
if (value === null) {
|
|
290
|
+
mergedHeaders.delete(key);
|
|
291
|
+
} else if (Array.isArray(value)) {
|
|
292
|
+
for (const v of value) {
|
|
293
|
+
mergedHeaders.append(key, v as string);
|
|
294
|
+
}
|
|
295
|
+
} else if (value !== undefined) {
|
|
296
|
+
// assume object headers are meant to be JSON stringified, i.e. their
|
|
297
|
+
// content value in OpenAPI specification is 'application/json'
|
|
298
|
+
mergedHeaders.set(
|
|
299
|
+
key,
|
|
300
|
+
typeof value === 'object' ? JSON.stringify(value) : (value as string),
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
return mergedHeaders;
|
|
306
|
+
};
|
|
307
|
+
|
|
308
|
+
type ErrInterceptor<Err, Res, Req, Options> = (
|
|
309
|
+
error: Err,
|
|
310
|
+
response: Res,
|
|
311
|
+
request: Req,
|
|
312
|
+
options: Options,
|
|
313
|
+
) => Err | Promise<Err>;
|
|
314
|
+
|
|
315
|
+
type ReqInterceptor<Req, Options> = (
|
|
316
|
+
request: Req,
|
|
317
|
+
options: Options,
|
|
318
|
+
) => Req | Promise<Req>;
|
|
319
|
+
|
|
320
|
+
type ResInterceptor<Res, Req, Options> = (
|
|
321
|
+
response: Res,
|
|
322
|
+
request: Req,
|
|
323
|
+
options: Options,
|
|
324
|
+
) => Res | Promise<Res>;
|
|
325
|
+
|
|
326
|
+
class Interceptors<Interceptor> {
|
|
327
|
+
_fns: (Interceptor | null)[];
|
|
328
|
+
|
|
329
|
+
constructor() {
|
|
330
|
+
this._fns = [];
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
clear() {
|
|
334
|
+
this._fns = [];
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
getInterceptorIndex(id: number | Interceptor): number {
|
|
338
|
+
if (typeof id === 'number') {
|
|
339
|
+
return this._fns[id] ? id : -1;
|
|
340
|
+
} else {
|
|
341
|
+
return this._fns.indexOf(id);
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
exists(id: number | Interceptor) {
|
|
345
|
+
const index = this.getInterceptorIndex(id);
|
|
346
|
+
return !!this._fns[index];
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
eject(id: number | Interceptor) {
|
|
350
|
+
const index = this.getInterceptorIndex(id);
|
|
351
|
+
if (this._fns[index]) {
|
|
352
|
+
this._fns[index] = null;
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
update(id: number | Interceptor, fn: Interceptor) {
|
|
357
|
+
const index = this.getInterceptorIndex(id);
|
|
358
|
+
if (this._fns[index]) {
|
|
359
|
+
this._fns[index] = fn;
|
|
360
|
+
return id;
|
|
361
|
+
} else {
|
|
362
|
+
return false;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
use(fn: Interceptor) {
|
|
367
|
+
this._fns = [...this._fns, fn];
|
|
368
|
+
return this._fns.length - 1;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
// `createInterceptors()` response, meant for external use as it does not
|
|
373
|
+
// expose internals
|
|
374
|
+
export interface Middleware<Req, Res, Err, Options> {
|
|
375
|
+
error: Pick<
|
|
376
|
+
Interceptors<ErrInterceptor<Err, Res, Req, Options>>,
|
|
377
|
+
'eject' | 'use'
|
|
378
|
+
>;
|
|
379
|
+
request: Pick<Interceptors<ReqInterceptor<Req, Options>>, 'eject' | 'use'>;
|
|
380
|
+
response: Pick<
|
|
381
|
+
Interceptors<ResInterceptor<Res, Req, Options>>,
|
|
382
|
+
'eject' | 'use'
|
|
383
|
+
>;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// do not add `Middleware` as return type so we can use _fns internally
|
|
387
|
+
export const createInterceptors = <Req, Res, Err, Options>() => ({
|
|
388
|
+
error: new Interceptors<ErrInterceptor<Err, Res, Req, Options>>(),
|
|
389
|
+
request: new Interceptors<ReqInterceptor<Req, Options>>(),
|
|
390
|
+
response: new Interceptors<ResInterceptor<Res, Req, Options>>(),
|
|
391
|
+
});
|
|
392
|
+
|
|
393
|
+
const defaultQuerySerializer = createQuerySerializer({
|
|
394
|
+
allowReserved: false,
|
|
395
|
+
array: {
|
|
396
|
+
explode: true,
|
|
397
|
+
style: 'form',
|
|
398
|
+
},
|
|
399
|
+
object: {
|
|
400
|
+
explode: true,
|
|
401
|
+
style: 'deepObject',
|
|
402
|
+
},
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const defaultHeaders = {
|
|
406
|
+
'Content-Type': 'application/json',
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
export const createConfig = <T extends ClientOptions = ClientOptions>(
|
|
410
|
+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
|
|
411
|
+
): Config<Omit<ClientOptions, keyof T> & T> => ({
|
|
412
|
+
...jsonBodySerializer,
|
|
413
|
+
headers: defaultHeaders,
|
|
414
|
+
// parseAs: 'auto',
|
|
415
|
+
querySerializer: defaultQuerySerializer,
|
|
416
|
+
...override,
|
|
417
|
+
});
|