@kubb/plugin-oas 3.16.1 → 3.16.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.
Files changed (111) hide show
  1. package/dist/Oas-CuqAnIw-.js +60 -0
  2. package/dist/Oas-CuqAnIw-.js.map +1 -0
  3. package/dist/Oas-Cv_pyXRM.cjs +78 -0
  4. package/dist/Oas-Cv_pyXRM.cjs.map +1 -0
  5. package/dist/OperationGenerator-CgbXErhW.d.cts +900 -0
  6. package/dist/OperationGenerator-OtLcGk2y.d.ts +900 -0
  7. package/dist/Schema-DJshqHto.d.cts +38 -0
  8. package/dist/Schema-DOXGUW1m.d.ts +38 -0
  9. package/dist/SchemaGenerator-8205BXCp.cjs +942 -0
  10. package/dist/SchemaGenerator-8205BXCp.cjs.map +1 -0
  11. package/dist/SchemaGenerator-B8Io6kky.js +930 -0
  12. package/dist/SchemaGenerator-B8Io6kky.js.map +1 -0
  13. package/dist/SchemaMapper-BaZQKrQB.js +54 -0
  14. package/dist/SchemaMapper-BaZQKrQB.js.map +1 -0
  15. package/dist/SchemaMapper-BiagBQN4.d.cts +388 -0
  16. package/dist/SchemaMapper-C2J2d3o4.d.ts +388 -0
  17. package/dist/SchemaMapper-D8J0V9Pj.cjs +66 -0
  18. package/dist/SchemaMapper-D8J0V9Pj.cjs.map +1 -0
  19. package/dist/chunk-CUT6urMc.cjs +30 -0
  20. package/dist/components.cjs +4 -19
  21. package/dist/components.d.cts +42 -35
  22. package/dist/components.d.ts +42 -35
  23. package/dist/components.js +3 -3
  24. package/dist/generators-BhLMlRNk.js +137 -0
  25. package/dist/generators-BhLMlRNk.js.map +1 -0
  26. package/dist/generators-DLH2kvlh.cjs +155 -0
  27. package/dist/generators-DLH2kvlh.cjs.map +1 -0
  28. package/dist/generators.cjs +4 -13
  29. package/dist/generators.d.cts +5 -7
  30. package/dist/generators.d.ts +5 -7
  31. package/dist/generators.js +5 -5
  32. package/dist/getFooter-BChY2kC1.cjs +43 -0
  33. package/dist/getFooter-BChY2kC1.cjs.map +1 -0
  34. package/dist/getFooter-T7_pZ6f8.js +31 -0
  35. package/dist/getFooter-T7_pZ6f8.js.map +1 -0
  36. package/dist/getSchemas-D8mP129c.cjs +67 -0
  37. package/dist/getSchemas-D8mP129c.cjs.map +1 -0
  38. package/dist/getSchemas-WoSBIxG8.js +55 -0
  39. package/dist/getSchemas-WoSBIxG8.js.map +1 -0
  40. package/dist/hooks.cjs +213 -195
  41. package/dist/hooks.cjs.map +1 -1
  42. package/dist/hooks.d.cts +91 -86
  43. package/dist/hooks.d.ts +91 -86
  44. package/dist/hooks.js +212 -193
  45. package/dist/hooks.js.map +1 -1
  46. package/dist/index.cjs +280 -344
  47. package/dist/index.cjs.map +1 -1
  48. package/dist/index.d.cts +7 -10
  49. package/dist/index.d.ts +7 -10
  50. package/dist/index.js +274 -318
  51. package/dist/index.js.map +1 -1
  52. package/dist/mocks.cjs +802 -737
  53. package/dist/mocks.cjs.map +1 -1
  54. package/dist/mocks.d.cts +12 -11
  55. package/dist/mocks.d.ts +12 -11
  56. package/dist/mocks.js +802 -735
  57. package/dist/mocks.js.map +1 -1
  58. package/dist/parseFromConfig-CMNGsef2.cjs +33 -0
  59. package/dist/parseFromConfig-CMNGsef2.cjs.map +1 -0
  60. package/dist/parseFromConfig-yAiFK03V.js +27 -0
  61. package/dist/parseFromConfig-yAiFK03V.js.map +1 -0
  62. package/dist/utils.cjs +58 -74
  63. package/dist/utils.cjs.map +1 -1
  64. package/dist/utils.d.cts +96 -42
  65. package/dist/utils.d.ts +96 -42
  66. package/dist/utils.js +47 -46
  67. package/dist/utils.js.map +1 -1
  68. package/package.json +28 -36
  69. package/src/SchemaGenerator.ts +6 -1
  70. package/src/components/Operation.tsx +1 -2
  71. package/src/mocks/schemas.ts +39 -0
  72. package/src/utils/getBanner.ts +5 -5
  73. package/src/utils/getFooter.ts +4 -4
  74. package/dist/OperationGenerator-C7NPZtOM.d.ts +0 -288
  75. package/dist/OperationGenerator-DJE2XQ83.d.cts +0 -288
  76. package/dist/Schema-AjebpeVY.d.ts +0 -32
  77. package/dist/Schema-M6983FL1.d.cts +0 -32
  78. package/dist/SchemaMapper-B21ZmWag.d.cts +0 -275
  79. package/dist/SchemaMapper-B21ZmWag.d.ts +0 -275
  80. package/dist/chunk-6S7YFY6F.js +0 -33
  81. package/dist/chunk-6S7YFY6F.js.map +0 -1
  82. package/dist/chunk-76E35IZ2.js +0 -56
  83. package/dist/chunk-76E35IZ2.js.map +0 -1
  84. package/dist/chunk-7RFNM43R.cjs +0 -41
  85. package/dist/chunk-7RFNM43R.cjs.map +0 -1
  86. package/dist/chunk-DTD4ZUDA.cjs +0 -36
  87. package/dist/chunk-DTD4ZUDA.cjs.map +0 -1
  88. package/dist/chunk-G3FDK7YW.js +0 -35
  89. package/dist/chunk-G3FDK7YW.js.map +0 -1
  90. package/dist/chunk-JNN4JPWK.js +0 -28
  91. package/dist/chunk-JNN4JPWK.js.map +0 -1
  92. package/dist/chunk-MBX66JAT.js +0 -93
  93. package/dist/chunk-MBX66JAT.js.map +0 -1
  94. package/dist/chunk-NFLZLRQS.cjs +0 -869
  95. package/dist/chunk-NFLZLRQS.cjs.map +0 -1
  96. package/dist/chunk-P2VXWGXZ.js +0 -862
  97. package/dist/chunk-P2VXWGXZ.js.map +0 -1
  98. package/dist/chunk-PORSNYI5.js +0 -47
  99. package/dist/chunk-PORSNYI5.js.map +0 -1
  100. package/dist/chunk-QJMOOF2A.cjs +0 -97
  101. package/dist/chunk-QJMOOF2A.cjs.map +0 -1
  102. package/dist/chunk-YWMMI3MO.cjs +0 -59
  103. package/dist/chunk-YWMMI3MO.cjs.map +0 -1
  104. package/dist/chunk-Z2NREI4X.cjs +0 -32
  105. package/dist/chunk-Z2NREI4X.cjs.map +0 -1
  106. package/dist/chunk-ZVFL3NXX.cjs +0 -50
  107. package/dist/chunk-ZVFL3NXX.cjs.map +0 -1
  108. package/dist/components.cjs.map +0 -1
  109. package/dist/components.js.map +0 -1
  110. package/dist/generators.cjs.map +0 -1
  111. package/dist/generators.js.map +0 -1
@@ -1,862 +0,0 @@
1
- import { isKeyword, schemaKeywords } from './chunk-76E35IZ2.js';
2
- import { getSchemaFactory, getSchemas } from './chunk-PORSNYI5.js';
3
- import { BaseGenerator } from '@kubb/core';
4
- import transformers, { pascalCase } from '@kubb/core/transformers';
5
- import { getUniqueName } from '@kubb/core/utils';
6
- import { isDiscriminator, isNullable, isReference } from '@kubb/oas';
7
- import { uniqueWith, isDeepEqual, isNumber } from 'remeda';
8
- import pLimit from 'p-limit';
9
-
10
- var SchemaGenerator = class _SchemaGenerator extends BaseGenerator {
11
- // Collect the types of all referenced schemas, so we can export them later
12
- refs = {};
13
- // Keep track of already used type aliases
14
- #usedAliasNames = {};
15
- /**
16
- * Creates a type node from a given schema.
17
- * Delegates to getBaseTypeFromSchema internally and
18
- * optionally adds a union with null.
19
- */
20
- parse(props) {
21
- const options = this.#getOptions(props);
22
- const defaultSchemas = this.#parseSchemaObject(props);
23
- const schemas = options.transformers?.schema?.(props, defaultSchemas) || defaultSchemas || [];
24
- return uniqueWith(schemas, isDeepEqual);
25
- }
26
- deepSearch(tree, keyword) {
27
- return _SchemaGenerator.deepSearch(tree, keyword);
28
- }
29
- find(tree, keyword) {
30
- return _SchemaGenerator.find(tree, keyword);
31
- }
32
- static deepSearch(tree, keyword) {
33
- const foundItems = [];
34
- tree?.forEach((schema) => {
35
- if (schema.keyword === keyword) {
36
- foundItems.push(schema);
37
- }
38
- if (isKeyword(schema, schemaKeywords.object)) {
39
- Object.values(schema.args?.properties || {}).forEach((entrySchema) => {
40
- foundItems.push(..._SchemaGenerator.deepSearch(entrySchema, keyword));
41
- });
42
- Object.values(schema.args?.additionalProperties || {}).forEach((entrySchema) => {
43
- foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
44
- });
45
- }
46
- if (isKeyword(schema, schemaKeywords.array)) {
47
- schema.args.items.forEach((entrySchema) => {
48
- foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
49
- });
50
- }
51
- if (isKeyword(schema, schemaKeywords.and)) {
52
- schema.args.forEach((entrySchema) => {
53
- foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
54
- });
55
- }
56
- if (isKeyword(schema, schemaKeywords.tuple)) {
57
- schema.args.items.forEach((entrySchema) => {
58
- foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
59
- });
60
- }
61
- if (isKeyword(schema, schemaKeywords.union)) {
62
- schema.args.forEach((entrySchema) => {
63
- foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
64
- });
65
- }
66
- });
67
- return foundItems;
68
- }
69
- static findInObject(tree, keyword) {
70
- let foundItem;
71
- tree?.forEach((schema) => {
72
- if (!foundItem && schema.keyword === keyword) {
73
- foundItem = schema;
74
- }
75
- if (isKeyword(schema, schemaKeywords.object)) {
76
- Object.values(schema.args?.properties || {}).forEach((entrySchema) => {
77
- if (!foundItem) {
78
- foundItem = _SchemaGenerator.find(entrySchema, keyword);
79
- }
80
- });
81
- Object.values(schema.args?.additionalProperties || {}).forEach((entrySchema) => {
82
- if (!foundItem) {
83
- foundItem = _SchemaGenerator.find([entrySchema], keyword);
84
- }
85
- });
86
- }
87
- });
88
- return foundItem;
89
- }
90
- static find(tree, keyword) {
91
- let foundItem;
92
- tree?.forEach((schema) => {
93
- if (!foundItem && schema.keyword === keyword) {
94
- foundItem = schema;
95
- }
96
- if (isKeyword(schema, schemaKeywords.array)) {
97
- schema.args.items.forEach((entrySchema) => {
98
- if (!foundItem) {
99
- foundItem = _SchemaGenerator.find([entrySchema], keyword);
100
- }
101
- });
102
- }
103
- if (isKeyword(schema, schemaKeywords.and)) {
104
- schema.args.forEach((entrySchema) => {
105
- if (!foundItem) {
106
- foundItem = _SchemaGenerator.find([entrySchema], keyword);
107
- }
108
- });
109
- }
110
- if (isKeyword(schema, schemaKeywords.tuple)) {
111
- schema.args.items.forEach((entrySchema) => {
112
- if (!foundItem) {
113
- foundItem = _SchemaGenerator.find([entrySchema], keyword);
114
- }
115
- });
116
- }
117
- if (isKeyword(schema, schemaKeywords.union)) {
118
- schema.args.forEach((entrySchema) => {
119
- if (!foundItem) {
120
- foundItem = _SchemaGenerator.find([entrySchema], keyword);
121
- }
122
- });
123
- }
124
- });
125
- return foundItem;
126
- }
127
- static combineObjects(tree) {
128
- if (!tree) {
129
- return [];
130
- }
131
- return tree.map((schema) => {
132
- if (!isKeyword(schema, schemaKeywords.and)) {
133
- return schema;
134
- }
135
- let mergedProperties = null;
136
- let mergedAdditionalProps = [];
137
- const newArgs = [];
138
- for (const subSchema of schema.args) {
139
- if (isKeyword(subSchema, schemaKeywords.object)) {
140
- const { properties = {}, additionalProperties = [] } = subSchema.args ?? {};
141
- if (!mergedProperties) {
142
- mergedProperties = {};
143
- }
144
- for (const [key, value] of Object.entries(properties)) {
145
- mergedProperties[key] = value;
146
- }
147
- if (additionalProperties.length > 0) {
148
- mergedAdditionalProps = additionalProperties;
149
- }
150
- } else {
151
- newArgs.push(subSchema);
152
- }
153
- }
154
- if (mergedProperties) {
155
- newArgs.push({
156
- keyword: schemaKeywords.object,
157
- args: {
158
- properties: mergedProperties,
159
- additionalProperties: mergedAdditionalProps
160
- }
161
- });
162
- }
163
- return {
164
- keyword: schemaKeywords.and,
165
- args: newArgs
166
- };
167
- });
168
- }
169
- #getUsedEnumNames(props) {
170
- const options = this.#getOptions(props);
171
- return options.usedEnumNames || {};
172
- }
173
- #getOptions({ name }) {
174
- const { override = [] } = this.context;
175
- return {
176
- ...this.options,
177
- ...override.find(({ pattern, type }) => {
178
- if (name && type === "schemaName") {
179
- return !!name.match(pattern);
180
- }
181
- return false;
182
- })?.options || {}
183
- };
184
- }
185
- #getUnknownType(props) {
186
- const options = this.#getOptions(props);
187
- if (options.unknownType === "any") {
188
- return schemaKeywords.any;
189
- }
190
- if (options.unknownType === "void") {
191
- return schemaKeywords.void;
192
- }
193
- return schemaKeywords.unknown;
194
- }
195
- #getEmptyType(props) {
196
- const options = this.#getOptions(props);
197
- if (options.emptySchemaType === "any") {
198
- return schemaKeywords.any;
199
- }
200
- if (options.emptySchemaType === "void") {
201
- return schemaKeywords.void;
202
- }
203
- return schemaKeywords.unknown;
204
- }
205
- /**
206
- * Recursively creates a type literal with the given props.
207
- */
208
- #parseProperties({ schemaObject, name }) {
209
- const properties = schemaObject?.properties || {};
210
- const additionalProperties = schemaObject?.additionalProperties;
211
- const required = schemaObject?.required;
212
- const propertiesSchemas = Object.keys(properties).map((propertyName) => {
213
- const validationFunctions = [];
214
- const propertySchema = properties[propertyName];
215
- const isRequired = Array.isArray(required) ? required?.includes(propertyName) : !!required;
216
- const nullable = propertySchema.nullable ?? propertySchema["x-nullable"] ?? false;
217
- validationFunctions.push(...this.parse({ schemaObject: propertySchema, name: propertyName, parentName: name }));
218
- validationFunctions.push({
219
- keyword: schemaKeywords.name,
220
- args: propertyName
221
- });
222
- if (!isRequired && nullable) {
223
- validationFunctions.push({ keyword: schemaKeywords.nullish });
224
- } else if (!isRequired) {
225
- validationFunctions.push({ keyword: schemaKeywords.optional });
226
- }
227
- return {
228
- [propertyName]: validationFunctions
229
- };
230
- }).reduce((acc, curr) => ({ ...acc, ...curr }), {});
231
- let additionalPropertiesSchemas = [];
232
- if (additionalProperties) {
233
- additionalPropertiesSchemas = additionalProperties === true || !Object.keys(additionalProperties).length ? [{ keyword: this.#getUnknownType({ schemaObject, name }) }] : this.parse({ schemaObject: additionalProperties, parentName: name });
234
- }
235
- return [
236
- {
237
- keyword: schemaKeywords.object,
238
- args: {
239
- properties: propertiesSchemas,
240
- additionalProperties: additionalPropertiesSchemas
241
- }
242
- }
243
- ];
244
- }
245
- /**
246
- * Create a type alias for the schema referenced by the given ReferenceObject
247
- */
248
- #getRefAlias(schemaObject, name) {
249
- const { $ref } = schemaObject;
250
- const ref = this.refs[$ref];
251
- if (ref) {
252
- const dereferencedSchema = this.context.oas.dereferenceWithRef(schemaObject);
253
- if (dereferencedSchema && isDiscriminator(dereferencedSchema)) {
254
- const [key] = Object.entries(dereferencedSchema.discriminator.mapping || {}).find(([_key, value]) => value.replace(/.+\//, "") === name) || [];
255
- if (key) {
256
- return [
257
- {
258
- keyword: schemaKeywords.and,
259
- args: [
260
- {
261
- keyword: schemaKeywords.ref,
262
- args: { name: ref.propertyName, $ref, path: ref.path, isImportable: !!this.context.oas.get($ref) }
263
- },
264
- {
265
- keyword: schemaKeywords.object,
266
- args: {
267
- properties: {
268
- [dereferencedSchema.discriminator.propertyName]: [
269
- {
270
- keyword: schemaKeywords.const,
271
- args: {
272
- name: key,
273
- format: "string",
274
- value: key
275
- }
276
- }
277
- ]
278
- }
279
- }
280
- }
281
- ]
282
- }
283
- ];
284
- }
285
- }
286
- return [
287
- {
288
- keyword: schemaKeywords.ref,
289
- args: { name: ref.propertyName, $ref, path: ref.path, isImportable: !!this.context.oas.get($ref) }
290
- }
291
- ];
292
- }
293
- const originalName = getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
294
- const propertyName = this.context.pluginManager.resolveName({
295
- name: originalName,
296
- pluginKey: this.context.plugin.key,
297
- type: "function"
298
- });
299
- const fileName = this.context.pluginManager.resolveName({
300
- name: originalName,
301
- pluginKey: this.context.plugin.key,
302
- type: "file"
303
- });
304
- const file = this.context.pluginManager.getFile({
305
- name: fileName,
306
- pluginKey: this.context.plugin.key,
307
- extname: ".ts"
308
- });
309
- this.refs[$ref] = {
310
- propertyName,
311
- originalName,
312
- path: file.path
313
- };
314
- return this.#getRefAlias(schemaObject, name);
315
- }
316
- #getParsedSchemaObject(schema) {
317
- return getSchemaFactory(this.context.oas)(schema);
318
- }
319
- #addDiscriminatorToSchema({
320
- schema,
321
- schemaObject,
322
- discriminator
323
- }) {
324
- if (!isKeyword(schema, schemaKeywords.union)) {
325
- return schema;
326
- }
327
- const objectPropertySchema = _SchemaGenerator.find(this.parse({ schemaObject }), schemaKeywords.object);
328
- return {
329
- ...schema,
330
- args: Object.entries(discriminator.mapping || {}).map(([key, value]) => {
331
- const arg = schema.args.find((item) => isKeyword(item, schemaKeywords.ref) && item.args.$ref === value);
332
- return {
333
- keyword: schemaKeywords.and,
334
- args: [
335
- arg,
336
- {
337
- keyword: schemaKeywords.object,
338
- args: {
339
- properties: {
340
- ...objectPropertySchema?.args?.properties || {},
341
- [discriminator.propertyName]: [
342
- {
343
- keyword: schemaKeywords.const,
344
- args: {
345
- name: key,
346
- format: "string",
347
- value: key
348
- }
349
- },
350
- //enum and literal will conflict
351
- ...objectPropertySchema?.args?.properties[discriminator.propertyName] || []
352
- ].filter((item) => !isKeyword(item, schemaKeywords.enum))
353
- }
354
- }
355
- }
356
- ]
357
- };
358
- })
359
- };
360
- }
361
- /**
362
- * This is the very core of the OpenAPI to TS conversion - it takes a
363
- * schema and returns the appropriate type.
364
- */
365
- #parseSchemaObject({ schemaObject: _schemaObject, name, parentName }) {
366
- const { schemaObject, version } = this.#getParsedSchemaObject(_schemaObject);
367
- const options = this.#getOptions({ schemaObject, name });
368
- const emptyType = this.#getEmptyType({ schemaObject, name });
369
- if (!schemaObject) {
370
- return [{ keyword: emptyType }];
371
- }
372
- const baseItems = [
373
- {
374
- keyword: schemaKeywords.schema,
375
- args: {
376
- type: schemaObject.type,
377
- format: schemaObject.format
378
- }
379
- }
380
- ];
381
- const min = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0;
382
- const max = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0;
383
- const nullable = isNullable(schemaObject);
384
- const defaultNullAndNullable = schemaObject.default === null && nullable;
385
- if (schemaObject.default !== void 0 && !defaultNullAndNullable && !Array.isArray(schemaObject.default)) {
386
- if (typeof schemaObject.default === "string") {
387
- baseItems.push({
388
- keyword: schemaKeywords.default,
389
- args: transformers.stringify(schemaObject.default)
390
- });
391
- } else if (typeof schemaObject.default === "boolean") {
392
- baseItems.push({
393
- keyword: schemaKeywords.default,
394
- args: schemaObject.default ?? false
395
- });
396
- } else {
397
- baseItems.push({
398
- keyword: schemaKeywords.default,
399
- args: schemaObject.default
400
- });
401
- }
402
- }
403
- if (schemaObject.deprecated) {
404
- baseItems.push({
405
- keyword: schemaKeywords.deprecated
406
- });
407
- }
408
- if (schemaObject.description) {
409
- baseItems.push({
410
- keyword: schemaKeywords.describe,
411
- args: schemaObject.description
412
- });
413
- }
414
- if (max !== void 0) {
415
- baseItems.unshift({ keyword: schemaKeywords.max, args: max });
416
- }
417
- if (min !== void 0) {
418
- baseItems.unshift({ keyword: schemaKeywords.min, args: min });
419
- }
420
- if (nullable) {
421
- baseItems.push({ keyword: schemaKeywords.nullable });
422
- }
423
- if (schemaObject.type && Array.isArray(schemaObject.type)) {
424
- const items = schemaObject.type.filter((value) => value !== "null");
425
- const hasNull = schemaObject.type.includes("null");
426
- if (hasNull && !nullable) {
427
- baseItems.push({ keyword: schemaKeywords.nullable });
428
- }
429
- if (items.length > 1) {
430
- const parsedItems = [
431
- {
432
- keyword: schemaKeywords.union,
433
- args: items.map(
434
- (item) => this.parse({
435
- schemaObject: { ...schemaObject, type: item },
436
- name,
437
- parentName
438
- })[0]
439
- ).filter(Boolean).filter((item) => !isKeyword(item, schemaKeywords.unknown)).map((item) => isKeyword(item, schemaKeywords.object) ? { ...item, args: { ...item.args, strict: true } } : item)
440
- }
441
- ];
442
- return [...parsedItems, ...baseItems].filter(Boolean);
443
- }
444
- }
445
- if (schemaObject.readOnly) {
446
- baseItems.push({ keyword: schemaKeywords.readOnly });
447
- }
448
- if (schemaObject.writeOnly) {
449
- baseItems.push({ keyword: schemaKeywords.writeOnly });
450
- }
451
- if (isReference(schemaObject)) {
452
- return [
453
- ...this.#getRefAlias(schemaObject, name),
454
- schemaObject.description && {
455
- keyword: schemaKeywords.describe,
456
- args: schemaObject.description
457
- },
458
- nullable && { keyword: schemaKeywords.nullable },
459
- schemaObject.readOnly && { keyword: schemaKeywords.readOnly },
460
- schemaObject.writeOnly && { keyword: schemaKeywords.writeOnly },
461
- {
462
- keyword: schemaKeywords.schema,
463
- args: {
464
- type: schemaObject.type,
465
- format: schemaObject.format
466
- }
467
- }
468
- ].filter(Boolean);
469
- }
470
- if (schemaObject.oneOf || schemaObject.anyOf) {
471
- const schemaWithoutOneOf = { ...schemaObject, oneOf: void 0, anyOf: void 0 };
472
- const discriminator = this.context.oas.getDiscriminator(schemaObject);
473
- const union = {
474
- keyword: schemaKeywords.union,
475
- args: (schemaObject.oneOf || schemaObject.anyOf).map((item) => {
476
- return item && this.parse({ schemaObject: item, name, parentName })[0];
477
- }).filter(Boolean).filter((item) => !isKeyword(item, schemaKeywords.unknown))
478
- };
479
- if (discriminator) {
480
- if (this.context) return [this.#addDiscriminatorToSchema({ schemaObject: schemaWithoutOneOf, schema: union, discriminator }), ...baseItems];
481
- }
482
- if (schemaWithoutOneOf.properties) {
483
- const propertySchemas = this.parse({ schemaObject: schemaWithoutOneOf, name, parentName });
484
- union.args = [
485
- ...union.args.map((arg) => {
486
- return {
487
- keyword: schemaKeywords.and,
488
- args: [arg, ...propertySchemas]
489
- };
490
- })
491
- ];
492
- return [union, ...baseItems];
493
- }
494
- return [union, ...baseItems];
495
- }
496
- if (schemaObject.allOf) {
497
- const schemaWithoutAllOf = { ...schemaObject, allOf: void 0 };
498
- const and = {
499
- keyword: schemaKeywords.and,
500
- args: schemaObject.allOf.map((item) => {
501
- return item && this.parse({ schemaObject: item, name, parentName })[0];
502
- }).filter(Boolean).filter((item) => !isKeyword(item, schemaKeywords.unknown))
503
- };
504
- if (schemaWithoutAllOf.required?.length) {
505
- const allOfItems = schemaObject.allOf;
506
- const resolvedSchemas = [];
507
- for (const item of allOfItems) {
508
- const resolved = isReference(item) ? this.context.oas.get(item.$ref) : item;
509
- if (resolved) {
510
- resolvedSchemas.push(resolved);
511
- }
512
- }
513
- const existingKeys = schemaWithoutAllOf.properties ? new Set(Object.keys(schemaWithoutAllOf.properties)) : null;
514
- const parsedItems = [];
515
- for (const key of schemaWithoutAllOf.required) {
516
- if (existingKeys?.has(key)) {
517
- continue;
518
- }
519
- for (const schema of resolvedSchemas) {
520
- if (schema.properties?.[key]) {
521
- parsedItems.push({
522
- properties: {
523
- [key]: schema.properties[key]
524
- },
525
- required: [key]
526
- });
527
- break;
528
- }
529
- }
530
- }
531
- for (const item of parsedItems) {
532
- const parsed = this.parse({ schemaObject: item, name, parentName });
533
- if (Array.isArray(parsed)) {
534
- and.args = and.args ? and.args.concat(parsed) : parsed;
535
- }
536
- }
537
- }
538
- if (schemaWithoutAllOf.properties) {
539
- and.args = [...and.args || [], ...this.parse({ schemaObject: schemaWithoutAllOf, name, parentName })];
540
- }
541
- return _SchemaGenerator.combineObjects([and, ...baseItems]);
542
- }
543
- if (schemaObject.enum) {
544
- if (options.enumSuffix === "") {
545
- this.context.pluginManager.logger.emit("info", "EnumSuffix set to an empty string does not work");
546
- }
547
- const enumName = getUniqueName(pascalCase([parentName, name, options.enumSuffix].join(" ")), this.#getUsedEnumNames({ schemaObject, name }));
548
- const typeName = this.context.pluginManager.resolveName({
549
- name: enumName,
550
- pluginKey: this.context.plugin.key,
551
- type: "type"
552
- });
553
- const nullableEnum = schemaObject.enum.includes(null);
554
- if (nullableEnum) {
555
- baseItems.push({ keyword: schemaKeywords.nullable });
556
- }
557
- const filteredValues = schemaObject.enum.filter((value) => value !== null);
558
- const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schemaObject).map((extensionKey) => {
559
- return [
560
- {
561
- keyword: schemaKeywords.enum,
562
- args: {
563
- name,
564
- typeName,
565
- asConst: false,
566
- items: [...new Set(schemaObject[extensionKey])].map((name2, index) => ({
567
- name: transformers.stringify(name2),
568
- value: schemaObject.enum?.[index],
569
- format: isNumber(schemaObject.enum?.[index]) ? "number" : "string"
570
- }))
571
- }
572
- },
573
- ...baseItems.filter(
574
- (item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches
575
- )
576
- ];
577
- });
578
- if (schemaObject.type === "number" || schemaObject.type === "integer") {
579
- const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
580
- return [
581
- {
582
- keyword: schemaKeywords.enum,
583
- args: {
584
- name: enumName,
585
- typeName,
586
- asConst: true,
587
- items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({
588
- name: name2,
589
- value,
590
- format: "number"
591
- })) : [...new Set(filteredValues)].map((value) => {
592
- return {
593
- name: value,
594
- value,
595
- format: "number"
596
- };
597
- })
598
- }
599
- },
600
- ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
601
- ];
602
- }
603
- if (schemaObject.type === "boolean") {
604
- const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
605
- return [
606
- {
607
- keyword: schemaKeywords.enum,
608
- args: {
609
- name: enumName,
610
- typeName,
611
- asConst: true,
612
- items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({
613
- name: name2,
614
- value,
615
- format: "boolean"
616
- })) : [...new Set(filteredValues)].map((value) => {
617
- return {
618
- name: value,
619
- value,
620
- format: "boolean"
621
- };
622
- })
623
- }
624
- },
625
- ...baseItems.filter((item) => item.keyword !== schemaKeywords.matches)
626
- ];
627
- }
628
- if (extensionEnums.length > 0 && extensionEnums[0]) {
629
- return extensionEnums[0];
630
- }
631
- return [
632
- {
633
- keyword: schemaKeywords.enum,
634
- args: {
635
- name: enumName,
636
- typeName,
637
- asConst: false,
638
- items: [...new Set(filteredValues)].map((value) => ({
639
- name: transformers.stringify(value),
640
- value,
641
- format: isNumber(value) ? "number" : "string"
642
- }))
643
- }
644
- },
645
- ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
646
- ];
647
- }
648
- if ("prefixItems" in schemaObject) {
649
- const prefixItems = schemaObject.prefixItems;
650
- const items = "items" in schemaObject ? schemaObject.items : [];
651
- const min2 = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0;
652
- const max2 = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0;
653
- return [
654
- {
655
- keyword: schemaKeywords.tuple,
656
- args: {
657
- min: min2,
658
- max: max2,
659
- items: prefixItems.map((item) => {
660
- return this.parse({ schemaObject: item, name, parentName })[0];
661
- }).filter(Boolean),
662
- rest: this.parse({
663
- schemaObject: items,
664
- name,
665
- parentName
666
- })[0]
667
- }
668
- },
669
- ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
670
- ];
671
- }
672
- if (version === "3.1" && "const" in schemaObject) {
673
- if (schemaObject["const"] === null) {
674
- return [{ keyword: schemaKeywords.null }];
675
- }
676
- if (schemaObject["const"] === void 0) {
677
- return [{ keyword: schemaKeywords.undefined }];
678
- }
679
- let format = typeof schemaObject["const"];
680
- if (format !== "number" && format !== "boolean") {
681
- format = "string";
682
- }
683
- return [
684
- {
685
- keyword: schemaKeywords.const,
686
- args: {
687
- name: schemaObject["const"],
688
- format,
689
- value: schemaObject["const"]
690
- }
691
- },
692
- ...baseItems
693
- ];
694
- }
695
- if (schemaObject.format) {
696
- if (schemaObject.type === "integer" && (schemaObject.format === "int32" || schemaObject.format === "int64")) {
697
- baseItems.unshift({ keyword: schemaKeywords.integer });
698
- return baseItems;
699
- }
700
- if (schemaObject.type === "number" && (schemaObject.format === "float" || schemaObject.format === "double")) {
701
- baseItems.unshift({ keyword: schemaKeywords.number });
702
- return baseItems;
703
- }
704
- switch (schemaObject.format) {
705
- case "binary":
706
- baseItems.push({ keyword: schemaKeywords.blob });
707
- return baseItems;
708
- case "date-time":
709
- if (options.dateType) {
710
- if (options.dateType === "date") {
711
- baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
712
- return baseItems;
713
- }
714
- if (options.dateType === "stringOffset") {
715
- baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: true } });
716
- return baseItems;
717
- }
718
- if (options.dateType === "stringLocal") {
719
- baseItems.unshift({ keyword: schemaKeywords.datetime, args: { local: true } });
720
- return baseItems;
721
- }
722
- baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: false } });
723
- return baseItems;
724
- }
725
- break;
726
- case "date":
727
- if (options.dateType) {
728
- if (options.dateType === "date") {
729
- baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
730
- return baseItems;
731
- }
732
- baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "string" } });
733
- return baseItems;
734
- }
735
- break;
736
- case "time":
737
- if (options.dateType) {
738
- if (options.dateType === "date") {
739
- baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "date" } });
740
- return baseItems;
741
- }
742
- baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "string" } });
743
- return baseItems;
744
- }
745
- break;
746
- case "uuid":
747
- baseItems.unshift({ keyword: schemaKeywords.uuid });
748
- return baseItems;
749
- case "email":
750
- case "idn-email":
751
- baseItems.unshift({ keyword: schemaKeywords.email });
752
- return baseItems;
753
- case "uri":
754
- case "ipv4":
755
- case "ipv6":
756
- case "uri-reference":
757
- case "hostname":
758
- case "idn-hostname":
759
- baseItems.unshift({ keyword: schemaKeywords.url });
760
- return baseItems;
761
- }
762
- }
763
- if (schemaObject.pattern && schemaObject.type === "string") {
764
- baseItems.unshift({
765
- keyword: schemaKeywords.matches,
766
- args: schemaObject.pattern
767
- });
768
- return baseItems;
769
- }
770
- if ("items" in schemaObject || schemaObject.type === "array") {
771
- const min2 = schemaObject.minimum ?? schemaObject.minLength ?? schemaObject.minItems ?? void 0;
772
- const max2 = schemaObject.maximum ?? schemaObject.maxLength ?? schemaObject.maxItems ?? void 0;
773
- const items = this.parse({ schemaObject: "items" in schemaObject ? schemaObject.items : [], name, parentName });
774
- const unique = !!schemaObject.uniqueItems;
775
- return [
776
- {
777
- keyword: schemaKeywords.array,
778
- args: {
779
- items,
780
- min: min2,
781
- max: max2,
782
- unique
783
- }
784
- },
785
- ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
786
- ];
787
- }
788
- if (schemaObject.properties || schemaObject.additionalProperties) {
789
- if (isDiscriminator(schemaObject)) {
790
- const schemaObjectOverriden = Object.keys(schemaObject.properties || {}).reduce((acc, propertyName) => {
791
- if (acc.properties?.[propertyName] && propertyName === schemaObject.discriminator.propertyName) {
792
- return {
793
- ...acc,
794
- properties: {
795
- ...acc.properties,
796
- [propertyName]: {
797
- ...acc.properties[propertyName] || {},
798
- enum: schemaObject.discriminator.mapping ? Object.keys(schemaObject.discriminator.mapping) : void 0
799
- }
800
- }
801
- };
802
- }
803
- return acc;
804
- }, schemaObject || {});
805
- return [
806
- ...this.#parseProperties({
807
- schemaObject: schemaObjectOverriden,
808
- name
809
- }),
810
- ...baseItems
811
- ];
812
- }
813
- return [...this.#parseProperties({ schemaObject, name }), ...baseItems];
814
- }
815
- if (schemaObject.type) {
816
- const type = Array.isArray(schemaObject.type) ? schemaObject.type.filter((item) => item !== "null")[0] : schemaObject.type;
817
- if (!["boolean", "object", "number", "string", "integer", "null"].includes(type)) {
818
- this.context.pluginManager.logger.emit("warning", `Schema type '${schemaObject.type}' is not valid for schema ${parentName}.${name}`);
819
- }
820
- return [{ keyword: type }, ...baseItems];
821
- }
822
- return [{ keyword: emptyType }];
823
- }
824
- async build(...generators) {
825
- const { oas, contentType, include } = this.context;
826
- const schemas = getSchemas({ oas, contentType, includes: include });
827
- const schemaEntries = Object.entries(schemas);
828
- const generatorLimit = pLimit(1);
829
- const schemaLimit = pLimit(10);
830
- const writeTasks = generators.map(
831
- (generator) => generatorLimit(async () => {
832
- const schemaTasks = schemaEntries.map(
833
- ([name, schemaObject]) => schemaLimit(async () => {
834
- const options = this.#getOptions({ name });
835
- const tree = this.parse({ name, schemaObject });
836
- const result = await generator.schema?.({
837
- instance: this,
838
- schema: {
839
- name,
840
- value: schemaObject,
841
- tree
842
- },
843
- options: {
844
- ...this.options,
845
- ...options
846
- }
847
- });
848
- return result ?? [];
849
- })
850
- );
851
- const schemaResults = await Promise.all(schemaTasks);
852
- return schemaResults.flat();
853
- })
854
- );
855
- const nestedResults = await Promise.all(writeTasks);
856
- return nestedResults.flat();
857
- }
858
- };
859
-
860
- export { SchemaGenerator };
861
- //# sourceMappingURL=chunk-P2VXWGXZ.js.map
862
- //# sourceMappingURL=chunk-P2VXWGXZ.js.map