typedapi-client-helpers 0.3.0 → 0.3.2
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/CHANGELOG.md +71 -68
- package/README.md +24 -15
- package/dist/cjs/index.js +0 -4
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/utils/handleApiResponse.js +19 -7
- package/dist/cjs/utils/handleApiResponse.js.map +1 -1
- package/dist/esm/index.js +0 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/utils/handleApiResponse.js +18 -7
- package/dist/esm/utils/handleApiResponse.js.map +1 -1
- package/dist/types/index.d.ts +0 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/interfaces/ApiResult.d.ts +10 -1
- package/dist/types/interfaces/ApiResult.d.ts.map +1 -1
- package/dist/types/types/ApiMethodOptions.d.ts +2 -2
- package/dist/types/types/ApiMethodOptions.d.ts.map +1 -1
- package/dist/types/utils/handleApiResponse.d.ts +4 -2
- package/dist/types/utils/handleApiResponse.d.ts.map +1 -1
- package/package.json +2 -1
- package/scripts/generate-api-minimal.mjs +30 -1
- package/scripts/openapi-generator/index.mjs +7 -35
- package/scripts/openapi-generator/method-template.mjs +379 -397
- package/scripts/openapi-generator/openapi-utils.mjs +411 -411
- package/scripts/openapi-generator/schema.mjs +581 -735
- package/dist/cjs/types/ApiMethodArguments.js +0 -3
- package/dist/cjs/types/ApiMethodArguments.js.map +0 -1
- package/dist/cjs/types/HttpResponse.js +0 -3
- package/dist/cjs/types/HttpResponse.js.map +0 -1
- package/dist/cjs/utils/extractArgsCallbacksAndParams.js +0 -51
- package/dist/cjs/utils/extractArgsCallbacksAndParams.js.map +0 -1
- package/dist/cjs/utils/extractArgsToastsAndParams.js +0 -49
- package/dist/cjs/utils/extractArgsToastsAndParams.js.map +0 -1
- package/dist/cjs/utils/wireMapping.js +0 -123
- package/dist/cjs/utils/wireMapping.js.map +0 -1
- package/dist/esm/types/ApiMethodArguments.js +0 -2
- package/dist/esm/types/ApiMethodArguments.js.map +0 -1
- package/dist/esm/types/HttpResponse.js +0 -2
- package/dist/esm/types/HttpResponse.js.map +0 -1
- package/dist/esm/utils/extractArgsCallbacksAndParams.js +0 -48
- package/dist/esm/utils/extractArgsCallbacksAndParams.js.map +0 -1
- package/dist/esm/utils/extractArgsToastsAndParams.js +0 -46
- package/dist/esm/utils/extractArgsToastsAndParams.js.map +0 -1
- package/dist/esm/utils/wireMapping.js +0 -119
- package/dist/esm/utils/wireMapping.js.map +0 -1
- package/dist/types/types/ApiMethodArguments.d.ts +0 -6
- package/dist/types/types/ApiMethodArguments.d.ts.map +0 -1
- package/dist/types/types/HttpResponse.d.ts +0 -2
- package/dist/types/types/HttpResponse.d.ts.map +0 -1
- package/dist/types/utils/extractArgsCallbacksAndParams.d.ts +0 -22
- package/dist/types/utils/extractArgsCallbacksAndParams.d.ts.map +0 -1
- package/dist/types/utils/extractArgsToastsAndParams.d.ts +0 -20
- package/dist/types/utils/extractArgsToastsAndParams.d.ts.map +0 -1
- package/dist/types/utils/wireMapping.d.ts +0 -30
- package/dist/types/utils/wireMapping.d.ts.map +0 -1
|
@@ -1,735 +1,581 @@
|
|
|
1
|
-
import {
|
|
2
|
-
dereference,
|
|
3
|
-
getErrorResponses,
|
|
4
|
-
getParameterSchema,
|
|
5
|
-
getRequestBodyContent,
|
|
6
|
-
getResponseContent,
|
|
7
|
-
getSuccessResponse,
|
|
8
|
-
} from "./openapi-utils.mjs";
|
|
9
|
-
import {
|
|
10
|
-
operationTypeName,
|
|
11
|
-
pascalCase,
|
|
12
|
-
propertyKey,
|
|
13
|
-
refName,
|
|
14
|
-
sanitizeIdentifier,
|
|
15
|
-
typePropertyName,
|
|
16
|
-
uniqueName,
|
|
17
|
-
} from "./names.mjs";
|
|
18
|
-
|
|
19
|
-
function jsDocFromSchema(schema, indent = "") {
|
|
20
|
-
const lines = [];
|
|
21
|
-
if (schema?.description)
|
|
22
|
-
lines.push(...String(schema.description).split(/\r?\n/));
|
|
23
|
-
if (schema?.format) lines.push(`@format ${schema.format}`);
|
|
24
|
-
if (Object.prototype.hasOwnProperty.call(schema ?? {}, "default")) {
|
|
25
|
-
lines.push(`@default ${JSON.stringify(schema.default)}`);
|
|
26
|
-
}
|
|
27
|
-
if (schema?.readOnly) lines.push("@readonly");
|
|
28
|
-
if (schema?.writeOnly) lines.push("@writeOnly");
|
|
29
|
-
if (schema?.deprecated) lines.push("@deprecated");
|
|
30
|
-
if (lines.length === 0) return "";
|
|
31
|
-
if (lines.length === 1) return `${indent}/** ${lines[0]} */\n`;
|
|
32
|
-
return `${indent}/**\n${lines.map((line) => `${indent} * ${line}`).join("\n")}\n${indent} */\n`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function unionTypes(types, fallback = "unknown") {
|
|
36
|
-
const unique = [...new Set(types.filter(Boolean))];
|
|
37
|
-
return unique.length > 0 ? unique.join(" | ") : fallback;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function intersectionTypes(types, fallback = "Record<string, unknown>") {
|
|
41
|
-
const unique = [...new Set(types.filter(Boolean))];
|
|
42
|
-
return unique.length > 0 ? unique.join(" & ") : fallback;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
function hasObjectShape(schema) {
|
|
46
|
-
return Boolean(
|
|
47
|
-
schema?.properties ||
|
|
48
|
-
schema?.additionalProperties ||
|
|
49
|
-
schema?.type === "object",
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function schemaWithoutComposition(schema) {
|
|
54
|
-
const { allOf, oneOf, anyOf, nullable, ...local } = schema ?? {};
|
|
55
|
-
return local;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function assertUniqueTypePropertyNames(entries, contextName) {
|
|
59
|
-
const seen = new Map();
|
|
60
|
-
for (const rawName of entries) {
|
|
61
|
-
const localName = typePropertyName(rawName);
|
|
62
|
-
const existing = seen.get(localName);
|
|
63
|
-
if (existing !== undefined && existing !== rawName) {
|
|
64
|
-
throw new Error(
|
|
65
|
-
`OpenAPI members ${JSON.stringify(existing)} and ${JSON.stringify(rawName)} in ${contextName} ` +
|
|
66
|
-
`both become TypeScript property ${JSON.stringify(localName)} when only the first character is lowercased.`,
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
seen.set(localName, rawName);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
)
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
schema.
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
.
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
for (const
|
|
221
|
-
const
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (schema.allOf) {
|
|
270
|
-
const parts = schema.allOf.map((item, index) =>
|
|
271
|
-
this.resolve(item, `${
|
|
272
|
-
);
|
|
273
|
-
const localSchema = schemaWithoutComposition(schema);
|
|
274
|
-
if (hasObjectShape(localSchema))
|
|
275
|
-
parts.push(
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
)
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
const
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
.map(({ response }) => getResponseContent(response)?.mediaType?.schema)
|
|
583
|
-
.filter(Boolean);
|
|
584
|
-
|
|
585
|
-
const hasNonBodyInput = parameters.length > 0;
|
|
586
|
-
const paginated =
|
|
587
|
-
Boolean(operationInfo.operation["x-typedapi-pagination"]) ||
|
|
588
|
-
/(?:Api)?Pagination(?:Sort)?Response\b|Paged(?:Response|Result)?\b|Paginated(?:Response|Result)?\b/.test(
|
|
589
|
-
responseType,
|
|
590
|
-
);
|
|
591
|
-
const isPurePaginatedQuery =
|
|
592
|
-
paginated &&
|
|
593
|
-
parametersByLocation.query.length > 0 &&
|
|
594
|
-
!parametersByLocation.path.length &&
|
|
595
|
-
!parametersByLocation.header.length &&
|
|
596
|
-
!parametersByLocation.cookie.length &&
|
|
597
|
-
!bodyType;
|
|
598
|
-
const parameterTypeNames = isPurePaginatedQuery
|
|
599
|
-
? { query: parameterTypeName(operationTypeNameBase, "query") }
|
|
600
|
-
: {};
|
|
601
|
-
|
|
602
|
-
return {
|
|
603
|
-
operationId,
|
|
604
|
-
operationTypeNameBase,
|
|
605
|
-
originalOperationId: operationInfo.originalOperationId,
|
|
606
|
-
methodName: operationInfo.methodName,
|
|
607
|
-
methodNameSource: operationInfo.methodNameSource,
|
|
608
|
-
paramsTypeName:
|
|
609
|
-
hasNonBodyInput && !isPurePaginatedQuery
|
|
610
|
-
? combinedParameterTypeName(operationTypeNameBase)
|
|
611
|
-
: undefined,
|
|
612
|
-
parameterTypeNames,
|
|
613
|
-
parameters,
|
|
614
|
-
parametersByLocation,
|
|
615
|
-
allParametersOptional: parameters.every(
|
|
616
|
-
(parameter) => parameter.in !== "path" && !parameter.required,
|
|
617
|
-
),
|
|
618
|
-
bodyType,
|
|
619
|
-
bodyRequired: Boolean(requestBodyContent?.requestBody?.required),
|
|
620
|
-
bodyWireSchemaKey: requestBodySchema
|
|
621
|
-
? operationWireSchemaKey(operationId, "body")
|
|
622
|
-
: undefined,
|
|
623
|
-
responseType,
|
|
624
|
-
responseWireSchemaKey: responseSchema
|
|
625
|
-
? operationWireSchemaKey(operationId, "response")
|
|
626
|
-
: undefined,
|
|
627
|
-
errorType,
|
|
628
|
-
errorWireSchemaKey:
|
|
629
|
-
errorSchemas.length > 0
|
|
630
|
-
? operationWireSchemaKey(operationId, "error")
|
|
631
|
-
: undefined,
|
|
632
|
-
contentType: requestBodyContent?.contentType,
|
|
633
|
-
responseContentType: responseContent?.contentType,
|
|
634
|
-
hasPathParams: parametersByLocation.path.length > 0,
|
|
635
|
-
hasQueryParams: parametersByLocation.query.length > 0,
|
|
636
|
-
hasHeaderParams: parametersByLocation.header.length > 0,
|
|
637
|
-
hasCookieParams: parametersByLocation.cookie.length > 0,
|
|
638
|
-
paginationMetadata: operationInfo.operation["x-typedapi-pagination"],
|
|
639
|
-
};
|
|
640
|
-
}
|
|
641
|
-
|
|
642
|
-
export function generateDataContracts(openApi, operations, options = {}) {
|
|
643
|
-
const resolver = new TypeResolver(openApi, options);
|
|
644
|
-
const declarations = [];
|
|
645
|
-
const schemas = openApi.components?.schemas ?? {};
|
|
646
|
-
|
|
647
|
-
const entries = Object.entries(schemas).sort(([a], [b]) =>
|
|
648
|
-
a.localeCompare(b),
|
|
649
|
-
);
|
|
650
|
-
for (const [rawName, schemaOrRef] of entries) {
|
|
651
|
-
const schema = dereference(
|
|
652
|
-
openApi,
|
|
653
|
-
schemaOrRef,
|
|
654
|
-
"Component schema reference",
|
|
655
|
-
);
|
|
656
|
-
declarations.push(
|
|
657
|
-
resolver.createDeclaration(resolver.componentName(rawName), schema),
|
|
658
|
-
);
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
for (const operationInfo of operations) {
|
|
662
|
-
const operationTypes = getOperationTypes(
|
|
663
|
-
openApi,
|
|
664
|
-
operationInfo,
|
|
665
|
-
options,
|
|
666
|
-
resolver,
|
|
667
|
-
);
|
|
668
|
-
|
|
669
|
-
if (operationTypes.paramsTypeName) {
|
|
670
|
-
declarations.push(
|
|
671
|
-
createParamsInterface(
|
|
672
|
-
openApi,
|
|
673
|
-
resolver,
|
|
674
|
-
operationTypes.paramsTypeName,
|
|
675
|
-
operationTypes.parameters,
|
|
676
|
-
),
|
|
677
|
-
);
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
for (const [location, parameters] of Object.entries(
|
|
681
|
-
operationTypes.parametersByLocation,
|
|
682
|
-
)) {
|
|
683
|
-
const typeName = operationTypes.parameterTypeNames[location];
|
|
684
|
-
if (typeName)
|
|
685
|
-
declarations.push(
|
|
686
|
-
createParamsInterface(openApi, resolver, typeName, parameters),
|
|
687
|
-
);
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
const bodyContent = getRequestBodyContent(
|
|
691
|
-
openApi,
|
|
692
|
-
operationInfo.operation.requestBody,
|
|
693
|
-
);
|
|
694
|
-
const bodySchema = bodyContent?.mediaType?.schema;
|
|
695
|
-
if (
|
|
696
|
-
bodySchema &&
|
|
697
|
-
!bodySchema.$ref &&
|
|
698
|
-
(bodyContent.contentType === "multipart/form-data" ||
|
|
699
|
-
hasObjectShape(bodySchema))
|
|
700
|
-
) {
|
|
701
|
-
declarations.push(
|
|
702
|
-
resolver.createDeclaration(
|
|
703
|
-
`${operationTypes.operationTypeNameBase}Payload`,
|
|
704
|
-
bodySchema,
|
|
705
|
-
),
|
|
706
|
-
);
|
|
707
|
-
}
|
|
708
|
-
}
|
|
709
|
-
|
|
710
|
-
for (const [name, schema] of resolver.inlineTypes.entries()) {
|
|
711
|
-
if (!resolver.componentNamesHasValue(name))
|
|
712
|
-
declarations.push(resolver.createDeclaration(name, schema));
|
|
713
|
-
}
|
|
714
|
-
|
|
715
|
-
const wireSchemas = generateWireSchemaRegistry(openApi, operations, options);
|
|
716
|
-
declarations.push(
|
|
717
|
-
`export const typedApiWireSchemas = ${JSON.stringify(wireSchemas, null, 2)} as const;`,
|
|
718
|
-
);
|
|
719
|
-
|
|
720
|
-
return `${generatedFileHeader()}\n\n${declarations.join("\n\n")}\n`;
|
|
721
|
-
}
|
|
722
|
-
|
|
723
|
-
export function generatedFileHeader() {
|
|
724
|
-
return `/* eslint-disable */
|
|
725
|
-
/* tslint:disable */
|
|
726
|
-
/*
|
|
727
|
-
* -------------------------------------------------------------------------------------
|
|
728
|
-
* ## THIS FILE WAS GENERATED BY typedapi-client-helpers ##
|
|
729
|
-
* ## DO NOT EDIT THIS FILE DIRECTLY ##
|
|
730
|
-
* ## ##
|
|
731
|
-
* ## AUTHOR: Stef Buurman ##
|
|
732
|
-
* ## SOURCE: https://github.com/Stef-Buurman/TypedApi/tree/main/npm/typed-api-client ##
|
|
733
|
-
* -------------------------------------------------------------------------------------
|
|
734
|
-
*/`;
|
|
735
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
dereference,
|
|
3
|
+
getErrorResponses,
|
|
4
|
+
getParameterSchema,
|
|
5
|
+
getRequestBodyContent,
|
|
6
|
+
getResponseContent,
|
|
7
|
+
getSuccessResponse,
|
|
8
|
+
} from "./openapi-utils.mjs";
|
|
9
|
+
import {
|
|
10
|
+
operationTypeName,
|
|
11
|
+
pascalCase,
|
|
12
|
+
propertyKey,
|
|
13
|
+
refName,
|
|
14
|
+
sanitizeIdentifier,
|
|
15
|
+
typePropertyName,
|
|
16
|
+
uniqueName,
|
|
17
|
+
} from "./names.mjs";
|
|
18
|
+
|
|
19
|
+
function jsDocFromSchema(schema, indent = "") {
|
|
20
|
+
const lines = [];
|
|
21
|
+
if (schema?.description)
|
|
22
|
+
lines.push(...String(schema.description).split(/\r?\n/));
|
|
23
|
+
if (schema?.format) lines.push(`@format ${schema.format}`);
|
|
24
|
+
if (Object.prototype.hasOwnProperty.call(schema ?? {}, "default")) {
|
|
25
|
+
lines.push(`@default ${JSON.stringify(schema.default)}`);
|
|
26
|
+
}
|
|
27
|
+
if (schema?.readOnly) lines.push("@readonly");
|
|
28
|
+
if (schema?.writeOnly) lines.push("@writeOnly");
|
|
29
|
+
if (schema?.deprecated) lines.push("@deprecated");
|
|
30
|
+
if (lines.length === 0) return "";
|
|
31
|
+
if (lines.length === 1) return `${indent}/** ${lines[0]} */\n`;
|
|
32
|
+
return `${indent}/**\n${lines.map((line) => `${indent} * ${line}`).join("\n")}\n${indent} */\n`;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function unionTypes(types, fallback = "unknown") {
|
|
36
|
+
const unique = [...new Set(types.filter(Boolean))];
|
|
37
|
+
return unique.length > 0 ? unique.join(" | ") : fallback;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function intersectionTypes(types, fallback = "Record<string, unknown>") {
|
|
41
|
+
const unique = [...new Set(types.filter(Boolean))];
|
|
42
|
+
return unique.length > 0 ? unique.join(" & ") : fallback;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function hasObjectShape(schema) {
|
|
46
|
+
return Boolean(
|
|
47
|
+
schema?.properties ||
|
|
48
|
+
schema?.additionalProperties ||
|
|
49
|
+
schema?.type === "object",
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function schemaWithoutComposition(schema) {
|
|
54
|
+
const { allOf, oneOf, anyOf, nullable, ...local } = schema ?? {};
|
|
55
|
+
return local;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function assertUniqueTypePropertyNames(entries, contextName) {
|
|
59
|
+
const seen = new Map();
|
|
60
|
+
for (const rawName of entries) {
|
|
61
|
+
const localName = typePropertyName(rawName);
|
|
62
|
+
const existing = seen.get(localName);
|
|
63
|
+
if (existing !== undefined && existing !== rawName) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`OpenAPI members ${JSON.stringify(existing)} and ${JSON.stringify(rawName)} in ${contextName} ` +
|
|
66
|
+
`both become TypeScript property ${JSON.stringify(localName)} when only the first character is lowercased.`,
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
seen.set(localName, rawName);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
export class TypeResolver {
|
|
75
|
+
constructor(openApi, options = {}) {
|
|
76
|
+
this.openApi = openApi;
|
|
77
|
+
this.options = options;
|
|
78
|
+
this.inlineTypes = new Map();
|
|
79
|
+
this.componentNames = new Map();
|
|
80
|
+
const usedNames = new Set();
|
|
81
|
+
|
|
82
|
+
for (const rawName of Object.keys(openApi.components?.schemas ?? {})) {
|
|
83
|
+
const name = uniqueName(operationTypeName(rawName), usedNames);
|
|
84
|
+
this.componentNames.set(rawName, name);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
componentName(rawName) {
|
|
89
|
+
return this.componentNames.get(rawName) ?? operationTypeName(rawName);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
resolve(schemaOrRef, contextName = "Anonymous", options = {}) {
|
|
93
|
+
if (!schemaOrRef) return "unknown";
|
|
94
|
+
|
|
95
|
+
if (schemaOrRef.$ref) {
|
|
96
|
+
dereference(this.openApi, schemaOrRef, "Schema reference");
|
|
97
|
+
return this.componentName(refName(schemaOrRef.$ref));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const schema =
|
|
101
|
+
dereference(this.openApi, schemaOrRef, "Schema reference") ?? schemaOrRef;
|
|
102
|
+
const nullable =
|
|
103
|
+
schema.nullable ||
|
|
104
|
+
(Array.isArray(schema.type) && schema.type.includes("null"));
|
|
105
|
+
let type = this.resolveNonNullable(schema, contextName, options);
|
|
106
|
+
if (nullable && !/(^|\W)null(\W|$)/.test(type)) type = `${type} | null`;
|
|
107
|
+
return type;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
resolveNonNullable(schema, contextName, options = {}) {
|
|
111
|
+
if (schema.const !== undefined) return JSON.stringify(schema.const);
|
|
112
|
+
|
|
113
|
+
if (Array.isArray(schema.enum)) {
|
|
114
|
+
if (options.inlineEnumAsUnion) {
|
|
115
|
+
return unionTypes(
|
|
116
|
+
this.enumLiteralValues(schema).map((value) => JSON.stringify(value)),
|
|
117
|
+
);
|
|
118
|
+
}
|
|
119
|
+
return operationTypeName(contextName);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (schema.oneOf || schema.anyOf) {
|
|
123
|
+
const variants = schema.oneOf ?? schema.anyOf;
|
|
124
|
+
return unionTypes(
|
|
125
|
+
variants.map((item, index) =>
|
|
126
|
+
this.resolve(item, `${contextName}${index + 1}`, options),
|
|
127
|
+
),
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (schema.allOf) {
|
|
132
|
+
const parts = schema.allOf.map((item, index) =>
|
|
133
|
+
this.resolve(item, `${contextName}${index + 1}`, options),
|
|
134
|
+
);
|
|
135
|
+
const localSchema = schemaWithoutComposition(schema);
|
|
136
|
+
if (hasObjectShape(localSchema)) {
|
|
137
|
+
parts.push(
|
|
138
|
+
this.createInlineObjectType(
|
|
139
|
+
localSchema,
|
|
140
|
+
`${contextName}Own`,
|
|
141
|
+
options,
|
|
142
|
+
),
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
return intersectionTypes(parts);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const schemaType = Array.isArray(schema.type)
|
|
149
|
+
? schema.type.find((item) => item !== "null")
|
|
150
|
+
: schema.type;
|
|
151
|
+
|
|
152
|
+
switch (schemaType) {
|
|
153
|
+
case "integer":
|
|
154
|
+
case "number":
|
|
155
|
+
return "number";
|
|
156
|
+
case "boolean":
|
|
157
|
+
return "boolean";
|
|
158
|
+
case "string":
|
|
159
|
+
if (schema.format === "binary")
|
|
160
|
+
return options.binaryAsBlob ? "Blob" : "File";
|
|
161
|
+
return "string";
|
|
162
|
+
case "array": {
|
|
163
|
+
const itemType = this.resolve(
|
|
164
|
+
schema.items ?? {},
|
|
165
|
+
`${contextName}Item`,
|
|
166
|
+
options,
|
|
167
|
+
);
|
|
168
|
+
return /^[A-Za-z_$][\w$]*(?:<.*>)?$/.test(itemType) ||
|
|
169
|
+
itemType.endsWith("[]")
|
|
170
|
+
? `${itemType}[]`
|
|
171
|
+
: `(${itemType})[]`;
|
|
172
|
+
}
|
|
173
|
+
case "object":
|
|
174
|
+
default:
|
|
175
|
+
if (schema.properties)
|
|
176
|
+
return this.createInlineObjectType(schema, contextName, options);
|
|
177
|
+
if (schema.additionalProperties) {
|
|
178
|
+
const valueType =
|
|
179
|
+
schema.additionalProperties === true
|
|
180
|
+
? "unknown"
|
|
181
|
+
: this.resolve(
|
|
182
|
+
schema.additionalProperties,
|
|
183
|
+
`${contextName}Value`,
|
|
184
|
+
options,
|
|
185
|
+
);
|
|
186
|
+
return `Record<string, ${valueType}>`;
|
|
187
|
+
}
|
|
188
|
+
return schemaType === "object" ? "Record<string, unknown>" : "unknown";
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
createInlineObjectType(schema, contextName) {
|
|
193
|
+
const base = operationTypeName(contextName);
|
|
194
|
+
let name = base;
|
|
195
|
+
let counter = 2;
|
|
196
|
+
while (
|
|
197
|
+
this.componentNamesHasValue(name) ||
|
|
198
|
+
(this.inlineTypes.has(name) && this.inlineTypes.get(name) !== schema)
|
|
199
|
+
) {
|
|
200
|
+
name = `${base}${counter++}`;
|
|
201
|
+
}
|
|
202
|
+
if (!this.inlineTypes.has(name)) this.inlineTypes.set(name, schema);
|
|
203
|
+
return name;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
componentNamesHasValue(value) {
|
|
207
|
+
return [...this.componentNames.values()].includes(value);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
objectMembers(name, schema) {
|
|
211
|
+
const required = new Set(schema.required ?? []);
|
|
212
|
+
const lines = [];
|
|
213
|
+
|
|
214
|
+
const propertyEntries = Object.entries(schema.properties ?? {});
|
|
215
|
+
assertUniqueTypePropertyNames(
|
|
216
|
+
propertyEntries.map(([propertyName]) => propertyName),
|
|
217
|
+
name,
|
|
218
|
+
);
|
|
219
|
+
|
|
220
|
+
for (const [propertyName, propertySchema] of propertyEntries) {
|
|
221
|
+
const localPropertyName = typePropertyName(propertyName);
|
|
222
|
+
const optionalToken = required.has(propertyName) ? "" : "?";
|
|
223
|
+
const propertyType = this.resolve(
|
|
224
|
+
propertySchema,
|
|
225
|
+
`${name}${pascalCase(propertyName)}`,
|
|
226
|
+
{
|
|
227
|
+
inlineEnumAsUnion: true,
|
|
228
|
+
},
|
|
229
|
+
);
|
|
230
|
+
const resolvedSchema = dereference(
|
|
231
|
+
this.openApi,
|
|
232
|
+
propertySchema,
|
|
233
|
+
"Property schema reference",
|
|
234
|
+
);
|
|
235
|
+
lines.push(
|
|
236
|
+
jsDocFromSchema(resolvedSchema, " ") +
|
|
237
|
+
` ${propertyKey(localPropertyName)}${optionalToken}: ${propertyType};`,
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
if (schema.additionalProperties) {
|
|
242
|
+
const valueType =
|
|
243
|
+
schema.additionalProperties === true
|
|
244
|
+
? "unknown"
|
|
245
|
+
: this.resolve(schema.additionalProperties, `${name}Value`, {
|
|
246
|
+
inlineEnumAsUnion: true,
|
|
247
|
+
});
|
|
248
|
+
lines.push(` [key: string]: ${valueType};`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return lines;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
createInterface(name, schema) {
|
|
255
|
+
return [
|
|
256
|
+
`export interface ${name} {`,
|
|
257
|
+
...this.objectMembers(name, schema),
|
|
258
|
+
"}",
|
|
259
|
+
].join("\n");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
createObjectTypeLiteral(name, schema) {
|
|
263
|
+
return ["{", ...this.objectMembers(name, schema), "}"].join("\n");
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
createDeclaration(name, schema) {
|
|
267
|
+
if (Array.isArray(schema.enum)) return this.createEnum(name, schema);
|
|
268
|
+
|
|
269
|
+
if (schema.allOf) {
|
|
270
|
+
const parts = schema.allOf.map((item, index) =>
|
|
271
|
+
this.resolve(item, `${name}${index + 1}`, { inlineEnumAsUnion: true }),
|
|
272
|
+
);
|
|
273
|
+
const localSchema = schemaWithoutComposition(schema);
|
|
274
|
+
if (hasObjectShape(localSchema))
|
|
275
|
+
parts.push(this.createObjectTypeLiteral(`${name}Own`, localSchema));
|
|
276
|
+
return `export type ${name} = ${intersectionTypes(parts)};`;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (hasObjectShape(schema)) return this.createInterface(name, schema);
|
|
280
|
+
return `export type ${name} = ${this.resolve(schema, name, { inlineEnumAsUnion: true })};`;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
createEnum(name, schema) {
|
|
284
|
+
if (this.options.generateUnionEnums) {
|
|
285
|
+
return `export type ${name} = ${unionTypes(
|
|
286
|
+
this.enumLiteralValues(schema).map((value) => JSON.stringify(value)),
|
|
287
|
+
)};`;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const lines = [`export const ${name} = {`];
|
|
291
|
+
for (const [index, value] of (schema.enum ?? []).entries()) {
|
|
292
|
+
const memberName = this.enumMemberName(schema, value, index);
|
|
293
|
+
const enumValue = this.enumRuntimeValue(schema, value, index);
|
|
294
|
+
lines.push(` ${propertyKey(memberName)}: ${JSON.stringify(enumValue)},`);
|
|
295
|
+
}
|
|
296
|
+
lines.push("} as const;");
|
|
297
|
+
lines.push(`export type ${name} = (typeof ${name})[keyof typeof ${name}];`);
|
|
298
|
+
return lines.join("\n");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
enumMetadataNames(schema) {
|
|
302
|
+
const names =
|
|
303
|
+
schema?.["x-enumNames"] ??
|
|
304
|
+
schema?.["x-enum-varnames"] ??
|
|
305
|
+
schema?.["x-enumVarnames"];
|
|
306
|
+
return Array.isArray(names) ? names : [];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
enumLiteralValues(schema) {
|
|
310
|
+
const values = schema.enum ?? [];
|
|
311
|
+
const names = this.enumMetadataNames(schema);
|
|
312
|
+
return this.options.enumNamesAsValues && names.length > 0
|
|
313
|
+
? values.map((value, index) => names[index] ?? value)
|
|
314
|
+
: values;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
enumRuntimeValue(schema, value, index) {
|
|
318
|
+
const names = this.enumMetadataNames(schema);
|
|
319
|
+
return this.options.enumNamesAsValues && names.length > 0
|
|
320
|
+
? (names[index] ?? value)
|
|
321
|
+
: value;
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
enumMemberName(schema, value, index) {
|
|
325
|
+
const rawName = this.enumMetadataNames(schema)[index] ?? value;
|
|
326
|
+
return operationTypeName(String(rawName) || "Value");
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
function parameterTypeName(operationId, location) {
|
|
331
|
+
return `${operationId}${pascalCase(location)}Params`;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function combinedParameterTypeName(operationId) {
|
|
335
|
+
return `${operationId}Params`;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function createParamsInterface(openApi, resolver, name, parameters) {
|
|
339
|
+
const lines = [`export interface ${name} {`];
|
|
340
|
+
assertUniqueTypePropertyNames(
|
|
341
|
+
parameters.map((parameter) => parameter.name),
|
|
342
|
+
name,
|
|
343
|
+
);
|
|
344
|
+
for (const parameter of parameters) {
|
|
345
|
+
const parameterSchema = getParameterSchema(openApi, parameter);
|
|
346
|
+
const optionalToken =
|
|
347
|
+
parameter.required || parameter.in === "path" ? "" : "?";
|
|
348
|
+
const parameterType = resolver.resolve(
|
|
349
|
+
parameterSchema,
|
|
350
|
+
`${name}${pascalCase(parameter.name)}`,
|
|
351
|
+
{
|
|
352
|
+
inlineEnumAsUnion: true,
|
|
353
|
+
},
|
|
354
|
+
);
|
|
355
|
+
lines.push(
|
|
356
|
+
jsDocFromSchema(parameterSchema, " ") +
|
|
357
|
+
` ${propertyKey(typePropertyName(parameter.name))}${optionalToken}: ${parameterType};`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
lines.push("}");
|
|
361
|
+
return lines.join("\n");
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
function resolveResponseType(
|
|
365
|
+
resolver,
|
|
366
|
+
response,
|
|
367
|
+
contextName,
|
|
368
|
+
binaryAsBlob = false,
|
|
369
|
+
) {
|
|
370
|
+
const content = getResponseContent(response);
|
|
371
|
+
const schema = content?.mediaType?.schema;
|
|
372
|
+
if (!schema) return undefined;
|
|
373
|
+
return resolver.resolve(schema, contextName, {
|
|
374
|
+
binaryAsBlob:
|
|
375
|
+
binaryAsBlob || content?.contentType === "application/octet-stream",
|
|
376
|
+
inlineEnumAsUnion: true,
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
export function getOperationTypes(
|
|
381
|
+
openApi,
|
|
382
|
+
operationInfo,
|
|
383
|
+
options = {},
|
|
384
|
+
resolver = new TypeResolver(openApi, options),
|
|
385
|
+
) {
|
|
386
|
+
const operationId = operationInfo.operationId;
|
|
387
|
+
const operationTypeNameBase = operationInfo.typeName ?? operationId;
|
|
388
|
+
const parameters = operationInfo.parameters ?? [];
|
|
389
|
+
const parametersByLocation = Object.fromEntries(
|
|
390
|
+
["path", "query", "header", "cookie"].map((location) => [
|
|
391
|
+
location,
|
|
392
|
+
parameters.filter((parameter) => parameter.in === location),
|
|
393
|
+
]),
|
|
394
|
+
);
|
|
395
|
+
|
|
396
|
+
const requestBodyContent = getRequestBodyContent(
|
|
397
|
+
openApi,
|
|
398
|
+
operationInfo.operation.requestBody,
|
|
399
|
+
);
|
|
400
|
+
const requestBodySchema = requestBodyContent?.mediaType?.schema;
|
|
401
|
+
let bodyType;
|
|
402
|
+
if (requestBodySchema) {
|
|
403
|
+
if (requestBodySchema.$ref) {
|
|
404
|
+
bodyType = resolver.resolve(requestBodySchema, `${operationTypeNameBase}Payload`);
|
|
405
|
+
} else if (
|
|
406
|
+
requestBodyContent?.contentType === "multipart/form-data" ||
|
|
407
|
+
hasObjectShape(requestBodySchema)
|
|
408
|
+
) {
|
|
409
|
+
bodyType = `${operationTypeNameBase}Payload`;
|
|
410
|
+
} else {
|
|
411
|
+
bodyType = resolver.resolve(requestBodySchema, `${operationTypeNameBase}Payload`, {
|
|
412
|
+
inlineEnumAsUnion: true,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
const successResponse = getSuccessResponse(
|
|
418
|
+
openApi,
|
|
419
|
+
operationInfo.operation,
|
|
420
|
+
options.defaultResponseAsSuccess,
|
|
421
|
+
);
|
|
422
|
+
const responseContent = getResponseContent(successResponse?.response);
|
|
423
|
+
const responseType = successResponse
|
|
424
|
+
? (resolveResponseType(
|
|
425
|
+
resolver,
|
|
426
|
+
successResponse.response,
|
|
427
|
+
`${operationTypeNameBase}Response`,
|
|
428
|
+
) ?? "void")
|
|
429
|
+
: "void";
|
|
430
|
+
|
|
431
|
+
const errorResponses = getErrorResponses(openApi, operationInfo.operation);
|
|
432
|
+
const errorTypes = errorResponses
|
|
433
|
+
.map(({ statusCode, response }) =>
|
|
434
|
+
resolveResponseType(
|
|
435
|
+
resolver,
|
|
436
|
+
response,
|
|
437
|
+
`${operationTypeNameBase}Error${statusCode}`,
|
|
438
|
+
),
|
|
439
|
+
)
|
|
440
|
+
.filter(Boolean);
|
|
441
|
+
const usesHttpErrorFallback = errorTypes.length === 0;
|
|
442
|
+
const errorType = unionTypes(errorTypes, "ApiHttpError");
|
|
443
|
+
|
|
444
|
+
const hasNonBodyInput = parameters.length > 0;
|
|
445
|
+
const paginated =
|
|
446
|
+
Boolean(operationInfo.operation["x-typedapi-pagination"]) ||
|
|
447
|
+
/(?:Api)?Pagination(?:Sort)?Response\b|Paged(?:Response|Result)?\b|Paginated(?:Response|Result)?\b/.test(
|
|
448
|
+
responseType,
|
|
449
|
+
);
|
|
450
|
+
const isPurePaginatedQuery =
|
|
451
|
+
paginated &&
|
|
452
|
+
parametersByLocation.query.length > 0 &&
|
|
453
|
+
!parametersByLocation.path.length &&
|
|
454
|
+
!parametersByLocation.header.length &&
|
|
455
|
+
!parametersByLocation.cookie.length &&
|
|
456
|
+
!bodyType;
|
|
457
|
+
const parameterTypeNames = isPurePaginatedQuery
|
|
458
|
+
? { query: parameterTypeName(operationTypeNameBase, "query") }
|
|
459
|
+
: {};
|
|
460
|
+
|
|
461
|
+
return {
|
|
462
|
+
operationId,
|
|
463
|
+
operationTypeNameBase,
|
|
464
|
+
originalOperationId: operationInfo.originalOperationId,
|
|
465
|
+
methodName: operationInfo.methodName,
|
|
466
|
+
methodNameSource: operationInfo.methodNameSource,
|
|
467
|
+
paramsTypeName:
|
|
468
|
+
hasNonBodyInput && !isPurePaginatedQuery
|
|
469
|
+
? combinedParameterTypeName(operationTypeNameBase)
|
|
470
|
+
: undefined,
|
|
471
|
+
parameterTypeNames,
|
|
472
|
+
parameters,
|
|
473
|
+
parametersByLocation,
|
|
474
|
+
allParametersOptional: parameters.every(
|
|
475
|
+
(parameter) => parameter.in !== "path" && !parameter.required,
|
|
476
|
+
),
|
|
477
|
+
bodyType,
|
|
478
|
+
bodyRequired: Boolean(requestBodyContent?.requestBody?.required),
|
|
479
|
+
responseType,
|
|
480
|
+
errorType,
|
|
481
|
+
usesHttpErrorFallback,
|
|
482
|
+
contentType: requestBodyContent?.contentType,
|
|
483
|
+
responseContentType: responseContent?.contentType,
|
|
484
|
+
hasPathParams: parametersByLocation.path.length > 0,
|
|
485
|
+
hasQueryParams: parametersByLocation.query.length > 0,
|
|
486
|
+
hasHeaderParams: parametersByLocation.header.length > 0,
|
|
487
|
+
hasCookieParams: parametersByLocation.cookie.length > 0,
|
|
488
|
+
paginationMetadata: operationInfo.operation["x-typedapi-pagination"],
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
export function generateDataContracts(openApi, operations, options = {}) {
|
|
493
|
+
const resolver = new TypeResolver(openApi, options);
|
|
494
|
+
const declarations = [];
|
|
495
|
+
const schemas = openApi.components?.schemas ?? {};
|
|
496
|
+
|
|
497
|
+
const entries = Object.entries(schemas).sort(([a], [b]) =>
|
|
498
|
+
a.localeCompare(b),
|
|
499
|
+
);
|
|
500
|
+
for (const [rawName, schemaOrRef] of entries) {
|
|
501
|
+
const schema = dereference(
|
|
502
|
+
openApi,
|
|
503
|
+
schemaOrRef,
|
|
504
|
+
"Component schema reference",
|
|
505
|
+
);
|
|
506
|
+
declarations.push(
|
|
507
|
+
resolver.createDeclaration(resolver.componentName(rawName), schema),
|
|
508
|
+
);
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
for (const operationInfo of operations) {
|
|
512
|
+
const operationTypes = getOperationTypes(
|
|
513
|
+
openApi,
|
|
514
|
+
operationInfo,
|
|
515
|
+
options,
|
|
516
|
+
resolver,
|
|
517
|
+
);
|
|
518
|
+
|
|
519
|
+
if (operationTypes.paramsTypeName) {
|
|
520
|
+
declarations.push(
|
|
521
|
+
createParamsInterface(
|
|
522
|
+
openApi,
|
|
523
|
+
resolver,
|
|
524
|
+
operationTypes.paramsTypeName,
|
|
525
|
+
operationTypes.parameters,
|
|
526
|
+
),
|
|
527
|
+
);
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
for (const [location, parameters] of Object.entries(
|
|
531
|
+
operationTypes.parametersByLocation,
|
|
532
|
+
)) {
|
|
533
|
+
const typeName = operationTypes.parameterTypeNames[location];
|
|
534
|
+
if (typeName)
|
|
535
|
+
declarations.push(
|
|
536
|
+
createParamsInterface(openApi, resolver, typeName, parameters),
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
const bodyContent = getRequestBodyContent(
|
|
541
|
+
openApi,
|
|
542
|
+
operationInfo.operation.requestBody,
|
|
543
|
+
);
|
|
544
|
+
const bodySchema = bodyContent?.mediaType?.schema;
|
|
545
|
+
if (
|
|
546
|
+
bodySchema &&
|
|
547
|
+
!bodySchema.$ref &&
|
|
548
|
+
(bodyContent.contentType === "multipart/form-data" ||
|
|
549
|
+
hasObjectShape(bodySchema))
|
|
550
|
+
) {
|
|
551
|
+
declarations.push(
|
|
552
|
+
resolver.createDeclaration(
|
|
553
|
+
`${operationTypes.operationTypeNameBase}Payload`,
|
|
554
|
+
bodySchema,
|
|
555
|
+
),
|
|
556
|
+
);
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
for (const [name, schema] of resolver.inlineTypes.entries()) {
|
|
561
|
+
if (!resolver.componentNamesHasValue(name))
|
|
562
|
+
declarations.push(resolver.createDeclaration(name, schema));
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
|
|
566
|
+
return `${generatedFileHeader()}\n\n${declarations.join("\n\n")}\n`;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
export function generatedFileHeader() {
|
|
570
|
+
return `/* eslint-disable */
|
|
571
|
+
/* tslint:disable */
|
|
572
|
+
/*
|
|
573
|
+
* -------------------------------------------------------------------------------------
|
|
574
|
+
* ## THIS FILE WAS GENERATED BY typedapi-client-helpers ##
|
|
575
|
+
* ## DO NOT EDIT THIS FILE DIRECTLY ##
|
|
576
|
+
* ## ##
|
|
577
|
+
* ## AUTHOR: Stef Buurman ##
|
|
578
|
+
* ## SOURCE: https://github.com/Stef-Buurman/TypedApi/tree/main/npm/typed-api-client ##
|
|
579
|
+
* -------------------------------------------------------------------------------------
|
|
580
|
+
*/`;
|
|
581
|
+
}
|