@hey-api/openapi-ts 0.87.1 → 0.87.3
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 -0
- package/dist/clients/fetch/client.ts +34 -1
- package/dist/clients/ky/client.ts +339 -0
- package/dist/clients/ky/index.ts +24 -0
- package/dist/clients/ky/types.ts +271 -0
- package/dist/clients/ky/utils.ts +328 -0
- package/dist/{config-PWeoedFF.d.cts → config-BI0uP8YS.d.cts} +1010 -27
- package/dist/{config-CNi83ZTD.d.ts → config-BpulPCv9.d.ts} +1010 -27
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +325 -325
- package/dist/index.d.ts +325 -325
- package/dist/index.js +1 -1
- package/dist/internal.cjs +1 -1
- package/dist/internal.d.cts +1 -1
- package/dist/internal.d.ts +1 -1
- package/dist/internal.js +1 -1
- package/dist/openApi--TCWOigv.js +19 -0
- package/dist/openApi--TCWOigv.js.map +1 -0
- package/dist/openApi-hVsWZkpA.cjs +19 -0
- package/dist/openApi-hVsWZkpA.cjs.map +1 -0
- package/dist/run.cjs +1 -1
- package/dist/run.cjs.map +1 -1
- package/dist/run.js +1 -1
- package/dist/run.js.map +1 -1
- package/dist/src-CF9c-t93.js +11 -0
- package/dist/{src-CZCpIWy7.js.map → src-CF9c-t93.js.map} +1 -1
- package/dist/src-CVDS5cbw.cjs +19 -0
- package/dist/{src-Cvd6zAsc.cjs.map → src-CVDS5cbw.cjs.map} +1 -1
- package/package.json +5 -4
- package/dist/openApi-CCTdD3hW.cjs +0 -19
- package/dist/openApi-CCTdD3hW.cjs.map +0 -1
- package/dist/openApi-D7xrG_wR.js +0 -19
- package/dist/openApi-D7xrG_wR.js.map +0 -1
- package/dist/src-CZCpIWy7.js +0 -11
- package/dist/src-Cvd6zAsc.cjs +0 -19
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
import { getAuthToken } from '../core/auth';
|
|
2
|
+
import type { QuerySerializerOptions } from '../core/bodySerializer';
|
|
3
|
+
import { jsonBodySerializer } from '../core/bodySerializer';
|
|
4
|
+
import {
|
|
5
|
+
serializeArrayParam,
|
|
6
|
+
serializeObjectParam,
|
|
7
|
+
serializePrimitiveParam,
|
|
8
|
+
} from '../core/pathSerializer';
|
|
9
|
+
import { getUrl } from '../core/utils';
|
|
10
|
+
import type { Client, ClientOptions, Config, RequestOptions } from './types';
|
|
11
|
+
|
|
12
|
+
export const createQuerySerializer = <T = unknown>({
|
|
13
|
+
parameters = {},
|
|
14
|
+
...args
|
|
15
|
+
}: QuerySerializerOptions = {}) => {
|
|
16
|
+
const querySerializer = (queryParams: T) => {
|
|
17
|
+
const search: string[] = [];
|
|
18
|
+
if (queryParams && typeof queryParams === 'object') {
|
|
19
|
+
for (const name in queryParams) {
|
|
20
|
+
const value = queryParams[name];
|
|
21
|
+
|
|
22
|
+
if (value === undefined || value === null) {
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const options = parameters[name] || args;
|
|
27
|
+
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
const serializedArray = serializeArrayParam({
|
|
30
|
+
allowReserved: options.allowReserved,
|
|
31
|
+
explode: true,
|
|
32
|
+
name,
|
|
33
|
+
style: 'form',
|
|
34
|
+
value,
|
|
35
|
+
...options.array,
|
|
36
|
+
});
|
|
37
|
+
if (serializedArray) search.push(serializedArray);
|
|
38
|
+
} else if (typeof value === 'object') {
|
|
39
|
+
const serializedObject = serializeObjectParam({
|
|
40
|
+
allowReserved: options.allowReserved,
|
|
41
|
+
explode: true,
|
|
42
|
+
name,
|
|
43
|
+
style: 'deepObject',
|
|
44
|
+
value: value as Record<string, unknown>,
|
|
45
|
+
...options.object,
|
|
46
|
+
});
|
|
47
|
+
if (serializedObject) search.push(serializedObject);
|
|
48
|
+
} else {
|
|
49
|
+
const serializedPrimitive = serializePrimitiveParam({
|
|
50
|
+
allowReserved: options.allowReserved,
|
|
51
|
+
name,
|
|
52
|
+
value: value as string,
|
|
53
|
+
});
|
|
54
|
+
if (serializedPrimitive) search.push(serializedPrimitive);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return search.join('&');
|
|
59
|
+
};
|
|
60
|
+
return querySerializer;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Infers parseAs value from provided Content-Type header.
|
|
65
|
+
*/
|
|
66
|
+
export const getParseAs = (
|
|
67
|
+
contentType: string | null,
|
|
68
|
+
): Exclude<Config['parseAs'], 'auto'> => {
|
|
69
|
+
if (!contentType) {
|
|
70
|
+
return 'stream';
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const cleanContent = contentType.split(';')[0]?.trim();
|
|
74
|
+
|
|
75
|
+
if (!cleanContent) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (
|
|
80
|
+
cleanContent.startsWith('application/json') ||
|
|
81
|
+
cleanContent.endsWith('+json')
|
|
82
|
+
) {
|
|
83
|
+
return 'json';
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (cleanContent === 'multipart/form-data') {
|
|
87
|
+
return 'formData';
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (
|
|
91
|
+
['application/', 'audio/', 'image/', 'video/'].some((type) =>
|
|
92
|
+
cleanContent.startsWith(type),
|
|
93
|
+
)
|
|
94
|
+
) {
|
|
95
|
+
return 'blob';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (cleanContent.startsWith('text/')) {
|
|
99
|
+
return 'text';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
const checkForExistence = (
|
|
106
|
+
options: Pick<RequestOptions, 'auth' | 'query'> & {
|
|
107
|
+
headers: Headers;
|
|
108
|
+
},
|
|
109
|
+
name?: string,
|
|
110
|
+
): boolean => {
|
|
111
|
+
if (!name) {
|
|
112
|
+
return false;
|
|
113
|
+
}
|
|
114
|
+
if (
|
|
115
|
+
options.headers.has(name) ||
|
|
116
|
+
options.query?.[name] ||
|
|
117
|
+
options.headers.get('Cookie')?.includes(`${name}=`)
|
|
118
|
+
) {
|
|
119
|
+
return true;
|
|
120
|
+
}
|
|
121
|
+
return false;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const setAuthParams = async ({
|
|
125
|
+
security,
|
|
126
|
+
...options
|
|
127
|
+
}: Pick<Required<RequestOptions>, 'security'> &
|
|
128
|
+
Pick<RequestOptions, 'auth' | 'query'> & {
|
|
129
|
+
headers: Headers;
|
|
130
|
+
}) => {
|
|
131
|
+
for (const auth of security) {
|
|
132
|
+
if (checkForExistence(options, auth.name)) {
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const token = await getAuthToken(auth, options.auth);
|
|
137
|
+
|
|
138
|
+
if (!token) {
|
|
139
|
+
continue;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const name = auth.name ?? 'Authorization';
|
|
143
|
+
|
|
144
|
+
switch (auth.in) {
|
|
145
|
+
case 'query':
|
|
146
|
+
if (!options.query) {
|
|
147
|
+
options.query = {};
|
|
148
|
+
}
|
|
149
|
+
options.query[name] = token;
|
|
150
|
+
break;
|
|
151
|
+
case 'cookie':
|
|
152
|
+
options.headers.append('Cookie', `${name}=${token}`);
|
|
153
|
+
break;
|
|
154
|
+
case 'header':
|
|
155
|
+
default:
|
|
156
|
+
options.headers.set(name, token);
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
export const buildUrl: Client['buildUrl'] = (options) =>
|
|
163
|
+
getUrl({
|
|
164
|
+
baseUrl: options.baseUrl as string,
|
|
165
|
+
path: options.path,
|
|
166
|
+
query: options.query,
|
|
167
|
+
querySerializer:
|
|
168
|
+
typeof options.querySerializer === 'function'
|
|
169
|
+
? options.querySerializer
|
|
170
|
+
: createQuerySerializer(options.querySerializer),
|
|
171
|
+
url: options.url,
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
export const mergeConfigs = (a: Config, b: Config): Config => {
|
|
175
|
+
const config = { ...a, ...b };
|
|
176
|
+
if (config.baseUrl?.endsWith('/')) {
|
|
177
|
+
config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1);
|
|
178
|
+
}
|
|
179
|
+
config.headers = mergeHeaders(a.headers, b.headers);
|
|
180
|
+
return config;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const headersEntries = (headers: Headers): Array<[string, string]> => {
|
|
184
|
+
const entries: Array<[string, string]> = [];
|
|
185
|
+
headers.forEach((value, key) => {
|
|
186
|
+
entries.push([key, value]);
|
|
187
|
+
});
|
|
188
|
+
return entries;
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export const mergeHeaders = (
|
|
192
|
+
...headers: Array<Required<Config>['headers'] | undefined>
|
|
193
|
+
): Headers => {
|
|
194
|
+
const mergedHeaders = new Headers();
|
|
195
|
+
for (const header of headers) {
|
|
196
|
+
if (!header) {
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
const iterator =
|
|
201
|
+
header instanceof Headers
|
|
202
|
+
? headersEntries(header)
|
|
203
|
+
: Object.entries(header);
|
|
204
|
+
|
|
205
|
+
for (const [key, value] of iterator) {
|
|
206
|
+
if (value === null) {
|
|
207
|
+
mergedHeaders.delete(key);
|
|
208
|
+
} else if (Array.isArray(value)) {
|
|
209
|
+
for (const v of value) {
|
|
210
|
+
mergedHeaders.append(key, v as string);
|
|
211
|
+
}
|
|
212
|
+
} else if (value !== undefined) {
|
|
213
|
+
mergedHeaders.set(
|
|
214
|
+
key,
|
|
215
|
+
typeof value === 'object' ? JSON.stringify(value) : (value as string),
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return mergedHeaders;
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
type ErrInterceptor<Err, Res, Req, Options> = (
|
|
224
|
+
error: Err,
|
|
225
|
+
response: Res,
|
|
226
|
+
request: Req,
|
|
227
|
+
options: Options,
|
|
228
|
+
) => Err | Promise<Err>;
|
|
229
|
+
|
|
230
|
+
type ReqInterceptor<Req, Options> = (
|
|
231
|
+
request: Req,
|
|
232
|
+
options: Options,
|
|
233
|
+
) => Req | Promise<Req>;
|
|
234
|
+
|
|
235
|
+
type ResInterceptor<Res, Req, Options> = (
|
|
236
|
+
response: Res,
|
|
237
|
+
request: Req,
|
|
238
|
+
options: Options,
|
|
239
|
+
) => Res | Promise<Res>;
|
|
240
|
+
|
|
241
|
+
class Interceptors<Interceptor> {
|
|
242
|
+
fns: Array<Interceptor | null> = [];
|
|
243
|
+
|
|
244
|
+
clear(): void {
|
|
245
|
+
this.fns = [];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
eject(id: number | Interceptor): void {
|
|
249
|
+
const index = this.getInterceptorIndex(id);
|
|
250
|
+
if (this.fns[index]) {
|
|
251
|
+
this.fns[index] = null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
exists(id: number | Interceptor): boolean {
|
|
256
|
+
const index = this.getInterceptorIndex(id);
|
|
257
|
+
return Boolean(this.fns[index]);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
getInterceptorIndex(id: number | Interceptor): number {
|
|
261
|
+
if (typeof id === 'number') {
|
|
262
|
+
return this.fns[id] ? id : -1;
|
|
263
|
+
}
|
|
264
|
+
return this.fns.indexOf(id);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
update(
|
|
268
|
+
id: number | Interceptor,
|
|
269
|
+
fn: Interceptor,
|
|
270
|
+
): number | Interceptor | false {
|
|
271
|
+
const index = this.getInterceptorIndex(id);
|
|
272
|
+
if (this.fns[index]) {
|
|
273
|
+
this.fns[index] = fn;
|
|
274
|
+
return id;
|
|
275
|
+
}
|
|
276
|
+
return false;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
use(fn: Interceptor): number {
|
|
280
|
+
this.fns.push(fn);
|
|
281
|
+
return this.fns.length - 1;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
export interface Middleware<Req, Res, Err, Options> {
|
|
286
|
+
error: Interceptors<ErrInterceptor<Err, Res, Req, Options>>;
|
|
287
|
+
request: Interceptors<ReqInterceptor<Req, Options>>;
|
|
288
|
+
response: Interceptors<ResInterceptor<Res, Req, Options>>;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
export const createInterceptors = <Req, Res, Err, Options>(): Middleware<
|
|
292
|
+
Req,
|
|
293
|
+
Res,
|
|
294
|
+
Err,
|
|
295
|
+
Options
|
|
296
|
+
> => ({
|
|
297
|
+
error: new Interceptors<ErrInterceptor<Err, Res, Req, Options>>(),
|
|
298
|
+
request: new Interceptors<ReqInterceptor<Req, Options>>(),
|
|
299
|
+
response: new Interceptors<ResInterceptor<Res, Req, Options>>(),
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const defaultQuerySerializer = createQuerySerializer({
|
|
303
|
+
allowReserved: false,
|
|
304
|
+
array: {
|
|
305
|
+
explode: true,
|
|
306
|
+
style: 'form',
|
|
307
|
+
},
|
|
308
|
+
object: {
|
|
309
|
+
explode: true,
|
|
310
|
+
style: 'deepObject',
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
const defaultHeaders = {
|
|
315
|
+
'Content-Type': 'application/json',
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
export const createConfig = <T extends ClientOptions = ClientOptions>(
|
|
319
|
+
override: Config<Omit<ClientOptions, keyof T> & T> = {},
|
|
320
|
+
): Config<Omit<ClientOptions, keyof T> & T> => ({
|
|
321
|
+
...jsonBodySerializer,
|
|
322
|
+
headers: defaultHeaders,
|
|
323
|
+
parseAs: 'auto',
|
|
324
|
+
querySerializer: defaultQuerySerializer,
|
|
325
|
+
throwOnError: false,
|
|
326
|
+
timeout: 10000,
|
|
327
|
+
...override,
|
|
328
|
+
});
|