@kubb/plugin-oas 3.0.0-alpha.2 → 3.0.0-alpha.21

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 (101) hide show
  1. package/README.md +13 -4
  2. package/dist/OperationGenerator-CHXolL0o.d.cts +553 -0
  3. package/dist/OperationGenerator-CHXolL0o.d.ts +553 -0
  4. package/dist/Schema-Ch0dHkM5.d.ts +22 -0
  5. package/dist/Schema-ClNodlyP.d.cts +22 -0
  6. package/dist/chunk-6UWOTOZJ.cjs +92 -0
  7. package/dist/chunk-6UWOTOZJ.cjs.map +1 -0
  8. package/dist/chunk-AZKUU24F.js +700 -0
  9. package/dist/chunk-AZKUU24F.js.map +1 -0
  10. package/dist/chunk-DFQJ57GH.js +45 -0
  11. package/dist/chunk-DFQJ57GH.js.map +1 -0
  12. package/dist/chunk-GF26SDHQ.js +28 -0
  13. package/dist/chunk-GF26SDHQ.js.map +1 -0
  14. package/dist/chunk-HP5WAFBU.cjs +48 -0
  15. package/dist/chunk-HP5WAFBU.cjs.map +1 -0
  16. package/dist/chunk-JNGUNN72.cjs +708 -0
  17. package/dist/chunk-JNGUNN72.cjs.map +1 -0
  18. package/dist/chunk-PADR76WZ.cjs +4 -0
  19. package/dist/chunk-PADR76WZ.cjs.map +1 -0
  20. package/dist/chunk-R47XMJ32.js +3 -0
  21. package/dist/chunk-R47XMJ32.js.map +1 -0
  22. package/dist/{chunk-Y4V7HHX7.js → chunk-THGCWF2O.js} +8 -13
  23. package/dist/{chunk-Y4V7HHX7.js.map → chunk-THGCWF2O.js.map} +1 -1
  24. package/dist/chunk-TRA5BTWI.js +88 -0
  25. package/dist/chunk-TRA5BTWI.js.map +1 -0
  26. package/dist/chunk-UO57DZPV.cjs +36 -0
  27. package/dist/chunk-UO57DZPV.cjs.map +1 -0
  28. package/dist/chunk-ZWHQ54JM.cjs +32 -0
  29. package/dist/chunk-ZWHQ54JM.cjs.map +1 -0
  30. package/dist/components.cjs +20 -17
  31. package/dist/components.cjs.map +1 -1
  32. package/dist/components.d.cts +12 -8
  33. package/dist/components.d.ts +12 -8
  34. package/dist/components.js +3 -17
  35. package/dist/components.js.map +1 -1
  36. package/dist/generators.cjs +14 -0
  37. package/dist/generators.cjs.map +1 -0
  38. package/dist/generators.d.cts +10 -0
  39. package/dist/generators.d.ts +10 -0
  40. package/dist/generators.js +5 -0
  41. package/dist/generators.js.map +1 -0
  42. package/dist/hooks.cjs +101 -57
  43. package/dist/hooks.cjs.map +1 -1
  44. package/dist/hooks.d.cts +40 -8
  45. package/dist/hooks.d.ts +40 -8
  46. package/dist/hooks.js +90 -53
  47. package/dist/hooks.js.map +1 -1
  48. package/dist/index.cjs +283 -261
  49. package/dist/index.cjs.map +1 -1
  50. package/dist/index.d.cts +6 -77
  51. package/dist/index.d.ts +6 -77
  52. package/dist/index.js +235 -240
  53. package/dist/index.js.map +1 -1
  54. package/dist/utils.cjs +32 -87
  55. package/dist/utils.cjs.map +1 -1
  56. package/dist/utils.d.cts +7 -41
  57. package/dist/utils.d.ts +7 -41
  58. package/dist/utils.js +8 -86
  59. package/dist/utils.js.map +1 -1
  60. package/package.json +20 -14
  61. package/src/OperationGenerator.ts +75 -43
  62. package/src/SchemaGenerator.ts +44 -18
  63. package/src/SchemaMapper.ts +1 -1
  64. package/src/components/Oas.tsx +9 -3
  65. package/src/components/Operation.tsx +1 -1
  66. package/src/components/Schema.tsx +2 -102
  67. package/src/generator.tsx +133 -0
  68. package/src/generators/index.ts +1 -0
  69. package/src/generators/jsonGenerator.ts +32 -0
  70. package/src/hooks/index.ts +2 -0
  71. package/src/hooks/useOperationManager.ts +76 -28
  72. package/src/hooks/useSchemaManager.ts +77 -0
  73. package/src/index.ts +3 -9
  74. package/src/plugin.ts +64 -47
  75. package/src/types.ts +23 -12
  76. package/src/utils/getParams.ts +2 -2
  77. package/src/utils/index.ts +2 -2
  78. package/dist/OperationGenerator-CeM_9pxW.d.cts +0 -60
  79. package/dist/OperationGenerator-DhUhvRJE.d.ts +0 -60
  80. package/dist/Schema-DefwBJMc.d.ts +0 -35
  81. package/dist/Schema-an5hOrjZ.d.cts +0 -35
  82. package/dist/SchemaMapper-CsBQ6eEx.d.cts +0 -248
  83. package/dist/SchemaMapper-CsBQ6eEx.d.ts +0 -248
  84. package/dist/chunk-CJXRFYEF.cjs +0 -698
  85. package/dist/chunk-CJXRFYEF.cjs.map +0 -1
  86. package/dist/chunk-DRLYORTT.cjs +0 -3265
  87. package/dist/chunk-DRLYORTT.cjs.map +0 -1
  88. package/dist/chunk-K6KUETAI.js +0 -698
  89. package/dist/chunk-K6KUETAI.js.map +0 -1
  90. package/dist/chunk-LEACSHKT.js +0 -3265
  91. package/dist/chunk-LEACSHKT.js.map +0 -1
  92. package/dist/chunk-P42X362U.cjs +0 -101
  93. package/dist/chunk-P42X362U.cjs.map +0 -1
  94. package/dist/chunk-QLJIL3U5.cjs +0 -35
  95. package/dist/chunk-QLJIL3U5.cjs.map +0 -1
  96. package/dist/chunk-UB552H4J.js +0 -101
  97. package/dist/chunk-UB552H4J.js.map +0 -1
  98. package/dist/types-Dte3MA6H.d.cts +0 -143
  99. package/dist/types-Dte3MA6H.d.ts +0 -143
  100. package/src/utils/getGroupedByTagFiles.ts +0 -82
  101. package/src/utils/refSorter.ts +0 -13
@@ -0,0 +1,700 @@
1
+ import { getSchemaFactory, getSchemas } from './chunk-DFQJ57GH.js';
2
+ import { BaseGenerator } from '@kubb/core';
3
+ import transformers, { pascalCase } from '@kubb/core/transformers';
4
+ import { getUniqueName } from '@kubb/core/utils';
5
+ import { isReference } from '@kubb/oas';
6
+ import { uniqueWith, isDeepEqual, isNumber } from 'remeda';
7
+
8
+ // src/SchemaMapper.ts
9
+ var schemaKeywords = {
10
+ any: "any",
11
+ strict: "strict",
12
+ unknown: "unknown",
13
+ number: "number",
14
+ integer: "integer",
15
+ string: "string",
16
+ boolean: "boolean",
17
+ undefined: "undefined",
18
+ nullable: "nullable",
19
+ null: "null",
20
+ nullish: "nullish",
21
+ array: "array",
22
+ tuple: "tuple",
23
+ enum: "enum",
24
+ union: "union",
25
+ datetime: "datetime",
26
+ date: "date",
27
+ email: "email",
28
+ uuid: "uuid",
29
+ url: "url",
30
+ /* intersection */
31
+ default: "default",
32
+ const: "const",
33
+ and: "and",
34
+ describe: "describe",
35
+ min: "min",
36
+ max: "max",
37
+ optional: "optional",
38
+ readOnly: "readOnly",
39
+ // custom ones
40
+ object: "object",
41
+ ref: "ref",
42
+ matches: "matches",
43
+ firstName: "firstName",
44
+ lastName: "lastName",
45
+ password: "password",
46
+ phone: "phone",
47
+ blob: "blob",
48
+ deprecated: "deprecated",
49
+ example: "example",
50
+ schema: "schema",
51
+ catchall: "catchall",
52
+ time: "time",
53
+ name: "name"
54
+ };
55
+ function isKeyword(meta, keyword) {
56
+ return meta.keyword === keyword;
57
+ }
58
+ var SchemaGenerator = class _SchemaGenerator extends BaseGenerator {
59
+ // Collect the types of all referenced schemas, so we can export them later
60
+ refs = {};
61
+ // Keep track of already used type aliases
62
+ #usedAliasNames = {};
63
+ /**
64
+ * Creates a type node from a given schema.
65
+ * Delegates to getBaseTypeFromSchema internally and
66
+ * optionally adds a union with null.
67
+ */
68
+ parse(props) {
69
+ const options = this.#getOptions(props);
70
+ const defaultSchemas = this.#parseSchemaObject(props);
71
+ const schemas = options.transformers?.schema?.(props, defaultSchemas) || defaultSchemas || [];
72
+ return uniqueWith(schemas, isDeepEqual);
73
+ }
74
+ deepSearch(tree, keyword) {
75
+ return _SchemaGenerator.deepSearch(tree, keyword);
76
+ }
77
+ find(tree, keyword) {
78
+ return _SchemaGenerator.find(tree, keyword);
79
+ }
80
+ static deepSearch(tree, keyword) {
81
+ const foundItems = [];
82
+ tree?.forEach((schema) => {
83
+ if (schema.keyword === keyword) {
84
+ foundItems.push(schema);
85
+ }
86
+ if (schema.keyword === schemaKeywords.object) {
87
+ const subItem = schema;
88
+ Object.values(subItem.args?.properties || {}).forEach((entrySchema) => {
89
+ foundItems.push(..._SchemaGenerator.deepSearch(entrySchema, keyword));
90
+ });
91
+ Object.values(subItem.args?.additionalProperties || {}).forEach((entrySchema) => {
92
+ foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
93
+ });
94
+ }
95
+ if (schema.keyword === schemaKeywords.array) {
96
+ const subItem = schema;
97
+ subItem.args.items.forEach((entrySchema) => {
98
+ foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
99
+ });
100
+ }
101
+ if (schema.keyword === schemaKeywords.and) {
102
+ const subItem = schema;
103
+ subItem.args.forEach((entrySchema) => {
104
+ foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
105
+ });
106
+ }
107
+ if (schema.keyword === schemaKeywords.tuple) {
108
+ const subItem = schema;
109
+ subItem.args.items.forEach((entrySchema) => {
110
+ foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
111
+ });
112
+ }
113
+ if (schema.keyword === schemaKeywords.union) {
114
+ const subItem = schema;
115
+ subItem.args.forEach((entrySchema) => {
116
+ foundItems.push(..._SchemaGenerator.deepSearch([entrySchema], keyword));
117
+ });
118
+ }
119
+ });
120
+ return foundItems;
121
+ }
122
+ static findInObject(tree, keyword) {
123
+ let foundItem = void 0;
124
+ tree?.forEach((schema) => {
125
+ if (!foundItem && schema.keyword === keyword) {
126
+ foundItem = schema;
127
+ }
128
+ if (schema.keyword === schemaKeywords.object) {
129
+ const subItem = schema;
130
+ Object.values(subItem.args?.properties || {}).forEach((entrySchema) => {
131
+ if (!foundItem) {
132
+ foundItem = _SchemaGenerator.find(entrySchema, keyword);
133
+ }
134
+ });
135
+ Object.values(subItem.args?.additionalProperties || {}).forEach((entrySchema) => {
136
+ if (!foundItem) {
137
+ foundItem = _SchemaGenerator.find([entrySchema], keyword);
138
+ }
139
+ });
140
+ }
141
+ });
142
+ return foundItem;
143
+ }
144
+ static find(tree, keyword) {
145
+ let foundItem = void 0;
146
+ tree?.forEach((schema) => {
147
+ if (!foundItem && schema.keyword === keyword) {
148
+ foundItem = schema;
149
+ }
150
+ if (schema.keyword === schemaKeywords.array) {
151
+ const subItem = schema;
152
+ subItem.args.items.forEach((entrySchema) => {
153
+ if (!foundItem) {
154
+ foundItem = _SchemaGenerator.find([entrySchema], keyword);
155
+ }
156
+ });
157
+ }
158
+ if (schema.keyword === schemaKeywords.and) {
159
+ const subItem = schema;
160
+ subItem.args.forEach((entrySchema) => {
161
+ if (!foundItem) {
162
+ foundItem = _SchemaGenerator.find([entrySchema], keyword);
163
+ }
164
+ });
165
+ }
166
+ if (schema.keyword === schemaKeywords.tuple) {
167
+ const subItem = schema;
168
+ subItem.args.items.forEach((entrySchema) => {
169
+ if (!foundItem) {
170
+ foundItem = _SchemaGenerator.find([entrySchema], keyword);
171
+ }
172
+ });
173
+ }
174
+ if (schema.keyword === schemaKeywords.union) {
175
+ const subItem = schema;
176
+ subItem.args.forEach((entrySchema) => {
177
+ if (!foundItem) {
178
+ foundItem = _SchemaGenerator.find([entrySchema], keyword);
179
+ }
180
+ });
181
+ }
182
+ });
183
+ return foundItem;
184
+ }
185
+ #getUsedEnumNames(props) {
186
+ const options = this.#getOptions(props);
187
+ return options.usedEnumNames || {};
188
+ }
189
+ #getOptions({ name }) {
190
+ const { override = [] } = this.context;
191
+ return {
192
+ ...this.options,
193
+ ...override.find(({ pattern, type }) => {
194
+ if (name && type === "schemaName") {
195
+ return !!name.match(pattern);
196
+ }
197
+ return false;
198
+ })?.options || {}
199
+ };
200
+ }
201
+ #getUnknownReturn(props) {
202
+ const options = this.#getOptions(props);
203
+ if (options.unknownType === "any") {
204
+ return schemaKeywords.any;
205
+ }
206
+ return schemaKeywords.unknown;
207
+ }
208
+ /**
209
+ * Recursively creates a type literal with the given props.
210
+ */
211
+ #parseProperties({ schema, name }) {
212
+ const properties = schema?.properties || {};
213
+ const additionalProperties = schema?.additionalProperties;
214
+ const required = schema?.required;
215
+ const propertiesSchemas = Object.keys(properties).map((propertyName) => {
216
+ const validationFunctions = [];
217
+ const propertySchema = properties[propertyName];
218
+ const isRequired = Array.isArray(required) ? required?.includes(propertyName) : !!required;
219
+ const nullable = propertySchema.nullable ?? propertySchema["x-nullable"] ?? false;
220
+ validationFunctions.push(...this.parse({ schema: propertySchema, name: propertyName, parentName: name }));
221
+ validationFunctions.push({
222
+ keyword: schemaKeywords.name,
223
+ args: propertyName
224
+ });
225
+ if (!isRequired && nullable) {
226
+ validationFunctions.push({ keyword: schemaKeywords.nullish });
227
+ } else if (!isRequired) {
228
+ validationFunctions.push({ keyword: schemaKeywords.optional });
229
+ }
230
+ return {
231
+ [propertyName]: validationFunctions
232
+ };
233
+ }).reduce((acc, curr) => ({ ...acc, ...curr }), {});
234
+ let additionalPropertiesSchemas = [];
235
+ if (additionalProperties) {
236
+ additionalPropertiesSchemas = additionalProperties === true ? [{ keyword: this.#getUnknownReturn({ schema, name }) }] : this.parse({ schema: additionalProperties, parentName: name });
237
+ }
238
+ return [
239
+ {
240
+ keyword: schemaKeywords.object,
241
+ args: {
242
+ properties: propertiesSchemas,
243
+ additionalProperties: additionalPropertiesSchemas
244
+ }
245
+ }
246
+ ];
247
+ }
248
+ /**
249
+ * Create a type alias for the schema referenced by the given ReferenceObject
250
+ */
251
+ #getRefAlias(obj) {
252
+ const { $ref } = obj;
253
+ let ref = this.refs[$ref];
254
+ const originalName = getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
255
+ const propertyName = this.context.pluginManager.resolveName({
256
+ name: originalName,
257
+ pluginKey: this.context.plugin.key,
258
+ type: "function"
259
+ });
260
+ if (ref) {
261
+ return [
262
+ {
263
+ keyword: schemaKeywords.ref,
264
+ args: { name: ref.propertyName, path: ref.path }
265
+ }
266
+ ];
267
+ }
268
+ const fileName = this.context.pluginManager.resolveName({
269
+ name: originalName,
270
+ pluginKey: this.context.plugin.key,
271
+ type: "file"
272
+ });
273
+ const file = this.context.pluginManager.getFile({
274
+ name: fileName,
275
+ pluginKey: this.context.plugin.key,
276
+ extName: ".ts"
277
+ });
278
+ ref = this.refs[$ref] = {
279
+ propertyName,
280
+ originalName,
281
+ path: file.path
282
+ };
283
+ return [
284
+ {
285
+ keyword: schemaKeywords.ref,
286
+ args: { name: ref.propertyName, path: ref?.path }
287
+ }
288
+ ];
289
+ }
290
+ #getParsedSchemaObject(schema) {
291
+ const parsedSchema = getSchemaFactory(this.context.oas)(schema);
292
+ return parsedSchema;
293
+ }
294
+ /**
295
+ * This is the very core of the OpenAPI to TS conversion - it takes a
296
+ * schema and returns the appropriate type.
297
+ */
298
+ #parseSchemaObject({ schema: _schema, name, parentName }) {
299
+ const options = this.#getOptions({ schema: _schema, name });
300
+ const unknownReturn = this.#getUnknownReturn({ schema: _schema, name });
301
+ const { schema, version } = this.#getParsedSchemaObject(_schema);
302
+ if (!schema) {
303
+ return [{ keyword: unknownReturn }];
304
+ }
305
+ const baseItems = [
306
+ {
307
+ keyword: schemaKeywords.schema,
308
+ args: {
309
+ type: schema.type,
310
+ format: schema.format
311
+ }
312
+ }
313
+ ];
314
+ const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
315
+ const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
316
+ const nullable = schema.nullable ?? schema["x-nullable"] ?? false;
317
+ if (schema.default !== void 0 && !Array.isArray(schema.default)) {
318
+ if (typeof schema.default === "string") {
319
+ baseItems.push({
320
+ keyword: schemaKeywords.default,
321
+ args: transformers.stringify(schema.default)
322
+ });
323
+ } else if (typeof schema.default === "boolean") {
324
+ baseItems.push({
325
+ keyword: schemaKeywords.default,
326
+ args: schema.default ?? false
327
+ });
328
+ } else {
329
+ baseItems.push({
330
+ keyword: schemaKeywords.default,
331
+ args: schema.default
332
+ });
333
+ }
334
+ }
335
+ if (schema.description) {
336
+ baseItems.push({
337
+ keyword: schemaKeywords.describe,
338
+ args: schema.description
339
+ });
340
+ }
341
+ if (schema.pattern) {
342
+ baseItems.unshift({
343
+ keyword: schemaKeywords.matches,
344
+ args: schema.pattern
345
+ });
346
+ }
347
+ if (max !== void 0) {
348
+ baseItems.unshift({ keyword: schemaKeywords.max, args: max });
349
+ }
350
+ if (min !== void 0) {
351
+ baseItems.unshift({ keyword: schemaKeywords.min, args: min });
352
+ }
353
+ if (nullable) {
354
+ baseItems.push({ keyword: schemaKeywords.nullable });
355
+ }
356
+ if (schema.type && Array.isArray(schema.type)) {
357
+ const [_schema2, nullable2] = schema.type;
358
+ if (nullable2 === "null") {
359
+ baseItems.push({ keyword: schemaKeywords.nullable });
360
+ }
361
+ }
362
+ if (schema.readOnly) {
363
+ baseItems.push({ keyword: schemaKeywords.readOnly });
364
+ }
365
+ if (isReference(schema)) {
366
+ return [
367
+ ...this.#getRefAlias(schema),
368
+ nullable && { keyword: schemaKeywords.nullable },
369
+ {
370
+ keyword: schemaKeywords.schema,
371
+ args: {
372
+ type: schema.type,
373
+ format: schema.format
374
+ }
375
+ }
376
+ ].filter(Boolean);
377
+ }
378
+ if (schema.oneOf) {
379
+ const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
380
+ const union = {
381
+ keyword: schemaKeywords.union,
382
+ args: schema.oneOf.map((item) => {
383
+ return item && this.parse({ schema: item, name, parentName })[0];
384
+ }).filter(Boolean).filter((item) => {
385
+ return item && item.keyword !== unknownReturn;
386
+ })
387
+ };
388
+ if (schemaWithoutOneOf.properties) {
389
+ return [...this.parse({ schema: schemaWithoutOneOf, name, parentName }), union, ...baseItems];
390
+ }
391
+ return [union, ...baseItems];
392
+ }
393
+ if (schema.anyOf) {
394
+ const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
395
+ const union = {
396
+ keyword: schemaKeywords.union,
397
+ args: schema.anyOf.map((item) => {
398
+ return item && this.parse({ schema: item, name, parentName })[0];
399
+ }).filter(Boolean).filter((item) => {
400
+ return item && item.keyword !== unknownReturn;
401
+ }).map((item) => {
402
+ if (isKeyword(item, schemaKeywords.object)) {
403
+ return {
404
+ ...item,
405
+ args: {
406
+ ...item.args,
407
+ strict: true
408
+ }
409
+ };
410
+ }
411
+ return item;
412
+ })
413
+ };
414
+ if (schemaWithoutAnyOf.properties) {
415
+ return [...this.parse({ schema: schemaWithoutAnyOf, name, parentName }), union, ...baseItems];
416
+ }
417
+ return [union, ...baseItems];
418
+ }
419
+ if (schema.allOf) {
420
+ const schemaWithoutAllOf = { ...schema, allOf: void 0 };
421
+ const and = {
422
+ keyword: schemaKeywords.and,
423
+ args: schema.allOf.map((item) => {
424
+ return item && this.parse({ schema: item, name, parentName })[0];
425
+ }).filter(Boolean).filter((item) => {
426
+ return item && item.keyword !== unknownReturn;
427
+ })
428
+ };
429
+ if (schemaWithoutAllOf.properties) {
430
+ return [
431
+ {
432
+ ...and,
433
+ args: [...and.args || [], ...this.parse({ schema: schemaWithoutAllOf, name, parentName })]
434
+ },
435
+ ...baseItems
436
+ ];
437
+ }
438
+ return [and, ...baseItems];
439
+ }
440
+ if (schema.enum) {
441
+ const enumName = getUniqueName(pascalCase([parentName, name, options.enumSuffix].join(" ")), this.#getUsedEnumNames({ schema, name }));
442
+ const typeName = this.context.pluginManager.resolveName({
443
+ name: enumName,
444
+ pluginKey: this.context.plugin.key,
445
+ type: "type"
446
+ });
447
+ const nullableEnum = schema.enum.includes(null);
448
+ if (nullableEnum) {
449
+ baseItems.push({ keyword: schemaKeywords.nullable });
450
+ }
451
+ const filteredValues = schema.enum.filter((value) => value !== null);
452
+ const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schema).map((extensionKey) => {
453
+ return [
454
+ {
455
+ keyword: schemaKeywords.enum,
456
+ args: {
457
+ name,
458
+ typeName,
459
+ asConst: false,
460
+ items: [...new Set(schema[extensionKey])].map((name2, index) => ({
461
+ name: transformers.stringify(name2),
462
+ value: schema.enum?.[index],
463
+ format: isNumber(schema.enum?.[index]) ? "number" : "string"
464
+ }))
465
+ }
466
+ },
467
+ ...baseItems.filter(
468
+ (item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches
469
+ )
470
+ ];
471
+ });
472
+ if (schema.type === "number" || schema.type === "integer") {
473
+ const enumNames = extensionEnums[0]?.find((item) => isKeyword(item, schemaKeywords.enum));
474
+ return [
475
+ {
476
+ keyword: schemaKeywords.enum,
477
+ args: {
478
+ name: enumName,
479
+ typeName,
480
+ asConst: true,
481
+ items: enumNames?.args?.items ? [...new Set(enumNames.args.items)].map(({ name: name2, value }) => ({
482
+ name: name2,
483
+ value,
484
+ format: "number"
485
+ })) : [...new Set(filteredValues)].map((value) => {
486
+ return {
487
+ name: value,
488
+ value,
489
+ format: "number"
490
+ };
491
+ })
492
+ }
493
+ },
494
+ ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
495
+ ];
496
+ }
497
+ if (extensionEnums.length > 0 && extensionEnums[0]) {
498
+ return extensionEnums[0];
499
+ }
500
+ return [
501
+ {
502
+ keyword: schemaKeywords.enum,
503
+ args: {
504
+ name: enumName,
505
+ typeName,
506
+ asConst: false,
507
+ items: [...new Set(filteredValues)].map((value) => ({
508
+ name: transformers.stringify(value),
509
+ value,
510
+ format: isNumber(value) ? "number" : "string"
511
+ }))
512
+ }
513
+ },
514
+ ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max && item.keyword !== schemaKeywords.matches)
515
+ ];
516
+ }
517
+ if ("prefixItems" in schema) {
518
+ const prefixItems = schema.prefixItems;
519
+ const min2 = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
520
+ const max2 = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
521
+ return [
522
+ {
523
+ keyword: schemaKeywords.tuple,
524
+ args: {
525
+ min: min2,
526
+ max: max2,
527
+ items: prefixItems.map((item) => {
528
+ return this.parse({ schema: item, name, parentName })[0];
529
+ }).filter(Boolean)
530
+ }
531
+ },
532
+ ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
533
+ ];
534
+ }
535
+ if (version === "3.1" && "const" in schema) {
536
+ if (schema["const"]) {
537
+ return [
538
+ {
539
+ keyword: schemaKeywords.const,
540
+ args: {
541
+ name: schema["const"],
542
+ format: typeof schema["const"] === "number" ? "number" : "string",
543
+ value: schema["const"]
544
+ }
545
+ },
546
+ ...baseItems
547
+ ];
548
+ }
549
+ return [{ keyword: schemaKeywords.null }];
550
+ }
551
+ if (schema.format) {
552
+ switch (schema.format) {
553
+ case "binary":
554
+ baseItems.push({ keyword: schemaKeywords.blob });
555
+ return baseItems;
556
+ case "date-time":
557
+ if (options.dateType) {
558
+ if (options.dateType === "date") {
559
+ baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
560
+ return baseItems;
561
+ }
562
+ if (options.dateType === "stringOffset") {
563
+ baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: true } });
564
+ return baseItems;
565
+ }
566
+ if (options.dateType === "stringLocal") {
567
+ baseItems.unshift({ keyword: schemaKeywords.datetime, args: { local: true } });
568
+ return baseItems;
569
+ }
570
+ baseItems.unshift({ keyword: schemaKeywords.datetime, args: { offset: false } });
571
+ return baseItems;
572
+ }
573
+ break;
574
+ case "date":
575
+ if (options.dateType) {
576
+ if (options.dateType === "date") {
577
+ baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "date" } });
578
+ return baseItems;
579
+ }
580
+ baseItems.unshift({ keyword: schemaKeywords.date, args: { type: "string" } });
581
+ return baseItems;
582
+ }
583
+ break;
584
+ case "time":
585
+ if (options.dateType) {
586
+ if (options.dateType === "date") {
587
+ baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "date" } });
588
+ return baseItems;
589
+ }
590
+ baseItems.unshift({ keyword: schemaKeywords.time, args: { type: "string" } });
591
+ return baseItems;
592
+ }
593
+ break;
594
+ case "uuid":
595
+ baseItems.unshift({ keyword: schemaKeywords.uuid });
596
+ break;
597
+ case "email":
598
+ case "idn-email":
599
+ baseItems.unshift({ keyword: schemaKeywords.email });
600
+ break;
601
+ case "uri":
602
+ case "ipv4":
603
+ case "ipv6":
604
+ case "uri-reference":
605
+ case "hostname":
606
+ case "idn-hostname":
607
+ baseItems.unshift({ keyword: schemaKeywords.url });
608
+ break;
609
+ }
610
+ }
611
+ if ("items" in schema || schema.type === "array") {
612
+ const min2 = schema.minimum ?? schema.minLength ?? schema.minItems ?? void 0;
613
+ const max2 = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? void 0;
614
+ const items = this.parse({ schema: "items" in schema ? schema.items : [], name, parentName });
615
+ return [
616
+ {
617
+ keyword: schemaKeywords.array,
618
+ args: {
619
+ items,
620
+ min: min2,
621
+ max: max2
622
+ }
623
+ },
624
+ ...baseItems.filter((item) => item.keyword !== schemaKeywords.min && item.keyword !== schemaKeywords.max)
625
+ ];
626
+ }
627
+ if (schema.properties || schema.additionalProperties) {
628
+ return [...this.#parseProperties({ schema, name }), ...baseItems];
629
+ }
630
+ if (schema.type) {
631
+ if (Array.isArray(schema.type)) {
632
+ const [type] = schema.type;
633
+ return [
634
+ ...this.parse({
635
+ schema: {
636
+ ...schema,
637
+ type
638
+ },
639
+ name,
640
+ parentName
641
+ }),
642
+ ...baseItems
643
+ ].filter(Boolean);
644
+ }
645
+ if (!["boolean", "object", "number", "string", "integer", "null"].includes(schema.type)) {
646
+ this.context.pluginManager.logger.emit("warning", `Schema type '${schema.type}' is not valid for schema ${parentName}.${name}`);
647
+ }
648
+ return [{ keyword: schema.type }, ...baseItems];
649
+ }
650
+ return [{ keyword: unknownReturn }];
651
+ }
652
+ async build(...generators) {
653
+ const { oas, contentType, include } = this.context;
654
+ const schemas = getSchemas({ oas, contentType, includes: include });
655
+ const promises = Object.entries(schemas).reduce((acc, [name, value]) => {
656
+ if (!value) {
657
+ return acc;
658
+ }
659
+ const options = this.#getOptions({ name });
660
+ const promiseOperation = this.schema.call(this, name, value, {
661
+ ...this.options,
662
+ ...options
663
+ });
664
+ if (promiseOperation) {
665
+ acc.push(promiseOperation);
666
+ }
667
+ generators?.forEach((generator) => {
668
+ const tree = this.parse({ schema: value, name });
669
+ const promise = generator.schema?.({
670
+ instance: this,
671
+ schema: {
672
+ name,
673
+ value,
674
+ tree
675
+ },
676
+ options: {
677
+ ...this.options,
678
+ ...options
679
+ }
680
+ });
681
+ if (promise) {
682
+ acc.push(promise);
683
+ }
684
+ });
685
+ return acc;
686
+ }, []);
687
+ const files = await Promise.all(promises);
688
+ return files.flat().filter(Boolean);
689
+ }
690
+ /**
691
+ * Schema
692
+ */
693
+ async schema(name, object, options) {
694
+ return [];
695
+ }
696
+ };
697
+
698
+ export { SchemaGenerator, isKeyword, schemaKeywords };
699
+ //# sourceMappingURL=chunk-AZKUU24F.js.map
700
+ //# sourceMappingURL=chunk-AZKUU24F.js.map