@kubb/plugin-ts 5.0.0-alpha.8 → 5.0.0-beta.10

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 (43) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +26 -7
  3. package/dist/index.cjs +1526 -4
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +574 -4
  6. package/dist/index.js +1488 -2
  7. package/dist/index.js.map +1 -0
  8. package/extension.yaml +632 -0
  9. package/package.json +43 -65
  10. package/src/components/{v2/Enum.tsx → Enum.tsx} +33 -17
  11. package/src/components/Type.tsx +31 -161
  12. package/src/constants.ts +15 -5
  13. package/src/factory.ts +295 -39
  14. package/src/generators/typeGenerator.tsx +248 -420
  15. package/src/index.ts +9 -2
  16. package/src/plugin.ts +67 -205
  17. package/src/printers/functionPrinter.ts +197 -0
  18. package/src/printers/printerTs.ts +329 -0
  19. package/src/resolvers/resolverTs.ts +66 -0
  20. package/src/types.ts +238 -94
  21. package/src/utils.ts +129 -0
  22. package/dist/components-CRu8IKY3.js +0 -729
  23. package/dist/components-CRu8IKY3.js.map +0 -1
  24. package/dist/components-DeNDKlzf.cjs +0 -982
  25. package/dist/components-DeNDKlzf.cjs.map +0 -1
  26. package/dist/components.cjs +0 -3
  27. package/dist/components.d.ts +0 -36
  28. package/dist/components.js +0 -2
  29. package/dist/generators.cjs +0 -4
  30. package/dist/generators.d.ts +0 -480
  31. package/dist/generators.js +0 -2
  32. package/dist/plugin-D5NGPj0v.js +0 -1232
  33. package/dist/plugin-D5NGPj0v.js.map +0 -1
  34. package/dist/plugin-MLTxoa8p.cjs +0 -1279
  35. package/dist/plugin-MLTxoa8p.cjs.map +0 -1
  36. package/dist/types-CsvB6X5Y.d.ts +0 -167
  37. package/src/components/index.ts +0 -1
  38. package/src/components/v2/Type.tsx +0 -59
  39. package/src/generators/index.ts +0 -2
  40. package/src/generators/v2/typeGenerator.tsx +0 -171
  41. package/src/generators/v2/utils.ts +0 -140
  42. package/src/parser.ts +0 -389
  43. package/src/printer.ts +0 -368
@@ -1,1279 +0,0 @@
1
- const require_components = require("./components-DeNDKlzf.cjs");
2
- let node_path = require("node:path");
3
- node_path = require_components.__toESM(node_path);
4
- let _kubb_ast = require("@kubb/ast");
5
- let _kubb_core = require("@kubb/core");
6
- let _kubb_plugin_oas = require("@kubb/plugin-oas");
7
- let _kubb_core_hooks = require("@kubb/core/hooks");
8
- let _kubb_fabric_core_parsers_typescript = require("@kubb/fabric-core/parsers/typescript");
9
- let _kubb_plugin_oas_generators = require("@kubb/plugin-oas/generators");
10
- let _kubb_plugin_oas_hooks = require("@kubb/plugin-oas/hooks");
11
- let _kubb_plugin_oas_utils = require("@kubb/plugin-oas/utils");
12
- let _kubb_react_fabric = require("@kubb/react-fabric");
13
- let typescript = require("typescript");
14
- typescript = require_components.__toESM(typescript);
15
- let _kubb_react_fabric_jsx_runtime = require("@kubb/react-fabric/jsx-runtime");
16
- //#region ../../internals/utils/src/object.ts
17
- /**
18
- * Serializes a primitive value to a JSON string literal, stripping any surrounding quote characters first.
19
- *
20
- * @example
21
- * stringify('hello') // '"hello"'
22
- * stringify('"hello"') // '"hello"'
23
- */
24
- function stringify(value) {
25
- if (value === void 0 || value === null) return "\"\"";
26
- return JSON.stringify(require_components.trimQuotes(value.toString()));
27
- }
28
- //#endregion
29
- //#region src/generators/typeGenerator.tsx
30
- function printCombinedSchema({ name, schemas, driver }) {
31
- const properties = {};
32
- if (schemas.response) properties["response"] = require_components.createUnionDeclaration({ nodes: schemas.responses.map((res) => {
33
- const identifier = driver.resolveName({
34
- name: res.name,
35
- pluginName: pluginTsName,
36
- type: "function"
37
- });
38
- return require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0);
39
- }) });
40
- if (schemas.request) {
41
- const identifier = driver.resolveName({
42
- name: schemas.request.name,
43
- pluginName: pluginTsName,
44
- type: "function"
45
- });
46
- properties["request"] = require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0);
47
- }
48
- if (schemas.pathParams) {
49
- const identifier = driver.resolveName({
50
- name: schemas.pathParams.name,
51
- pluginName: pluginTsName,
52
- type: "function"
53
- });
54
- properties["pathParams"] = require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0);
55
- }
56
- if (schemas.queryParams) {
57
- const identifier = driver.resolveName({
58
- name: schemas.queryParams.name,
59
- pluginName: pluginTsName,
60
- type: "function"
61
- });
62
- properties["queryParams"] = require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0);
63
- }
64
- if (schemas.headerParams) {
65
- const identifier = driver.resolveName({
66
- name: schemas.headerParams.name,
67
- pluginName: pluginTsName,
68
- type: "function"
69
- });
70
- properties["headerParams"] = require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0);
71
- }
72
- if (schemas.errors) properties["errors"] = require_components.createUnionDeclaration({ nodes: schemas.errors.map((error) => {
73
- const identifier = driver.resolveName({
74
- name: error.name,
75
- pluginName: pluginTsName,
76
- type: "function"
77
- });
78
- return require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0);
79
- }) });
80
- return (0, _kubb_fabric_core_parsers_typescript.safePrint)(require_components.createTypeAliasDeclaration({
81
- name,
82
- type: require_components.createTypeLiteralNode(Object.keys(properties).map((key) => {
83
- const type = properties[key];
84
- if (!type) return;
85
- return require_components.createPropertySignature({
86
- name: require_components.pascalCase(key),
87
- type
88
- });
89
- }).filter(Boolean)),
90
- modifiers: [require_components.modifiers.export]
91
- }));
92
- }
93
- function printRequestSchema({ baseName, operation, schemas, driver }) {
94
- const name = driver.resolveName({
95
- name: `${baseName} Request`,
96
- pluginName: pluginTsName,
97
- type: "type"
98
- });
99
- const results = [];
100
- const dataRequestProperties = [];
101
- if (schemas.request) {
102
- const identifier = driver.resolveName({
103
- name: schemas.request.name,
104
- pluginName: pluginTsName,
105
- type: "type"
106
- });
107
- dataRequestProperties.push(require_components.createPropertySignature({
108
- name: "data",
109
- questionToken: true,
110
- type: require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0)
111
- }));
112
- } else dataRequestProperties.push(require_components.createPropertySignature({
113
- name: "data",
114
- questionToken: true,
115
- type: require_components.keywordTypeNodes.never
116
- }));
117
- if (schemas.pathParams) {
118
- const identifier = driver.resolveName({
119
- name: schemas.pathParams.name,
120
- pluginName: pluginTsName,
121
- type: "type"
122
- });
123
- dataRequestProperties.push(require_components.createPropertySignature({
124
- name: "pathParams",
125
- type: require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0)
126
- }));
127
- } else dataRequestProperties.push(require_components.createPropertySignature({
128
- name: "pathParams",
129
- questionToken: true,
130
- type: require_components.keywordTypeNodes.never
131
- }));
132
- if (schemas.queryParams) {
133
- const identifier = driver.resolveName({
134
- name: schemas.queryParams.name,
135
- pluginName: pluginTsName,
136
- type: "type"
137
- });
138
- dataRequestProperties.push(require_components.createPropertySignature({
139
- name: "queryParams",
140
- questionToken: true,
141
- type: require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0)
142
- }));
143
- } else dataRequestProperties.push(require_components.createPropertySignature({
144
- name: "queryParams",
145
- questionToken: true,
146
- type: require_components.keywordTypeNodes.never
147
- }));
148
- if (schemas.headerParams) {
149
- const identifier = driver.resolveName({
150
- name: schemas.headerParams.name,
151
- pluginName: pluginTsName,
152
- type: "type"
153
- });
154
- dataRequestProperties.push(require_components.createPropertySignature({
155
- name: "headerParams",
156
- questionToken: true,
157
- type: require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0)
158
- }));
159
- } else dataRequestProperties.push(require_components.createPropertySignature({
160
- name: "headerParams",
161
- questionToken: true,
162
- type: require_components.keywordTypeNodes.never
163
- }));
164
- dataRequestProperties.push(require_components.createPropertySignature({
165
- name: "url",
166
- type: require_components.createUrlTemplateType(operation.path)
167
- }));
168
- const dataRequestNode = require_components.createTypeAliasDeclaration({
169
- name,
170
- type: require_components.createTypeLiteralNode(dataRequestProperties),
171
- modifiers: [require_components.modifiers.export]
172
- });
173
- results.push((0, _kubb_fabric_core_parsers_typescript.safePrint)(dataRequestNode));
174
- return results.join("\n\n");
175
- }
176
- function printResponseSchema({ baseName, schemas, driver, unknownType }) {
177
- const results = [];
178
- const name = driver.resolveName({
179
- name: `${baseName} ResponseData`,
180
- pluginName: pluginTsName,
181
- type: "type"
182
- });
183
- if (schemas.responses && schemas.responses.length > 0) {
184
- const responsesProperties = schemas.responses.map((res) => {
185
- const identifier = driver.resolveName({
186
- name: res.name,
187
- pluginName: pluginTsName,
188
- type: "type"
189
- });
190
- return require_components.createPropertySignature({
191
- name: res.statusCode?.toString() ?? "default",
192
- type: require_components.createTypeReferenceNode(require_components.createIdentifier(identifier), void 0)
193
- });
194
- });
195
- const responsesNode = require_components.createTypeAliasDeclaration({
196
- name: `${baseName}Responses`,
197
- type: require_components.createTypeLiteralNode(responsesProperties),
198
- modifiers: [require_components.modifiers.export]
199
- });
200
- results.push((0, _kubb_fabric_core_parsers_typescript.safePrint)(responsesNode));
201
- const responseNode = require_components.createTypeAliasDeclaration({
202
- name,
203
- type: require_components.createIndexedAccessTypeNode(require_components.createTypeReferenceNode(require_components.createIdentifier(`${baseName}Responses`), void 0), require_components.createTypeOperatorNode(typescript.default.SyntaxKind.KeyOfKeyword, require_components.createTypeReferenceNode(require_components.createIdentifier(`${baseName}Responses`), void 0))),
204
- modifiers: [require_components.modifiers.export]
205
- });
206
- results.push((0, _kubb_fabric_core_parsers_typescript.safePrint)(responseNode));
207
- } else {
208
- const responseNode = require_components.createTypeAliasDeclaration({
209
- name,
210
- modifiers: [require_components.modifiers.export],
211
- type: require_components.getUnknownType(unknownType)
212
- });
213
- results.push((0, _kubb_fabric_core_parsers_typescript.safePrint)(responseNode));
214
- }
215
- return results.join("\n\n");
216
- }
217
- const typeGenerator$1 = (0, _kubb_plugin_oas_generators.createReactGenerator)({
218
- name: "typescript",
219
- Operation({ operation, generator, plugin }) {
220
- const { options, options: { enumType, enumKeyCasing, syntaxType, optionalType, arrayType, unknownType, paramsCasing } } = plugin;
221
- const mode = (0, _kubb_core_hooks.useMode)();
222
- const driver = (0, _kubb_core_hooks.usePluginDriver)();
223
- const oas = (0, _kubb_plugin_oas_hooks.useOas)();
224
- const { getSchemas, getFile, getName, getGroup } = (0, _kubb_plugin_oas_hooks.useOperationManager)(generator);
225
- const schemaManager = (0, _kubb_plugin_oas_hooks.useSchemaManager)();
226
- const name = getName(operation, {
227
- type: "type",
228
- pluginName: pluginTsName
229
- });
230
- const file = getFile(operation);
231
- const schemas = getSchemas(operation);
232
- const schemaGenerator = new _kubb_plugin_oas.SchemaGenerator(options, {
233
- fabric: generator.context.fabric,
234
- oas,
235
- events: generator.context.events,
236
- plugin,
237
- driver,
238
- mode,
239
- override: options.override
240
- });
241
- const operationSchemas = [
242
- schemas.pathParams,
243
- schemas.queryParams,
244
- schemas.headerParams,
245
- schemas.statusCodes,
246
- schemas.request,
247
- schemas.response
248
- ].flat().filter(Boolean);
249
- const mapOperationSchema = ({ name, schema, description, keysToOmit, ...options }) => {
250
- const transformedSchema = paramsCasing && (0, _kubb_plugin_oas_utils.isParameterSchema)(name) ? (0, _kubb_plugin_oas_utils.applyParamsCasing)(schema, paramsCasing) : schema;
251
- const tree = schemaGenerator.parse({
252
- schema: transformedSchema,
253
- name,
254
- parentName: null
255
- });
256
- const imports = (0, _kubb_plugin_oas_utils.getImports)(tree);
257
- const group = options.operation ? getGroup(options.operation) : void 0;
258
- const type = {
259
- name: schemaManager.getName(name, { type: "type" }),
260
- typedName: schemaManager.getName(name, { type: "type" }),
261
- file: schemaManager.getFile(options.operationName || name, { group })
262
- };
263
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
264
- root: file.path,
265
- path: imp.path,
266
- name: imp.name,
267
- isTypeOnly: true
268
- }, [
269
- name,
270
- imp.name,
271
- imp.path,
272
- imp.isTypeOnly
273
- ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.Type, {
274
- name: type.name,
275
- typedName: type.typedName,
276
- description,
277
- tree,
278
- schema: transformedSchema,
279
- enumType,
280
- enumKeyCasing,
281
- optionalType,
282
- arrayType,
283
- keysToOmit,
284
- syntaxType
285
- })] });
286
- };
287
- const responseName = schemaManager.getName(schemas.response.name, { type: "type" });
288
- const combinedSchemaName = operation.method === "get" ? `${name}Query` : `${name}Mutation`;
289
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
290
- baseName: file.baseName,
291
- path: file.path,
292
- meta: file.meta,
293
- banner: (0, _kubb_plugin_oas_utils.getBanner)({
294
- oas,
295
- output: plugin.options.output,
296
- config: driver.config
297
- }),
298
- footer: (0, _kubb_plugin_oas_utils.getFooter)({
299
- oas,
300
- output: plugin.options.output
301
- }),
302
- children: [operationSchemas.map(mapOperationSchema), generator.context.UNSTABLE_NAMING ? /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
303
- name: `${name}Request`,
304
- isExportable: true,
305
- isIndexable: true,
306
- isTypeOnly: true,
307
- children: printRequestSchema({
308
- baseName: name,
309
- operation,
310
- schemas,
311
- driver
312
- })
313
- }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
314
- name: responseName,
315
- isExportable: true,
316
- isIndexable: true,
317
- isTypeOnly: true,
318
- children: printResponseSchema({
319
- baseName: name,
320
- schemas,
321
- driver,
322
- unknownType
323
- })
324
- })] }) : /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
325
- name: combinedSchemaName,
326
- isExportable: true,
327
- isIndexable: true,
328
- isTypeOnly: true,
329
- children: printCombinedSchema({
330
- name: combinedSchemaName,
331
- schemas,
332
- driver
333
- })
334
- })]
335
- });
336
- },
337
- Schema({ schema, plugin }) {
338
- const { options: { enumType, enumKeyCasing, syntaxType, optionalType, arrayType, output } } = plugin;
339
- const mode = (0, _kubb_core_hooks.useMode)();
340
- const oas = (0, _kubb_plugin_oas_hooks.useOas)();
341
- const driver = (0, _kubb_core_hooks.usePluginDriver)();
342
- const { getName, getFile } = (0, _kubb_plugin_oas_hooks.useSchemaManager)();
343
- const imports = (0, _kubb_plugin_oas_utils.getImports)(schema.tree);
344
- const schemaFromTree = schema.tree.find((item) => item.keyword === _kubb_plugin_oas.schemaKeywords.schema);
345
- let typedName = getName(schema.name, { type: "type" });
346
- if (["asConst", "asPascalConst"].includes(enumType) && schemaFromTree && (0, _kubb_plugin_oas.isKeyword)(schemaFromTree, _kubb_plugin_oas.schemaKeywords.enum)) typedName = typedName += "Key";
347
- const type = {
348
- name: getName(schema.name, { type: "function" }),
349
- typedName,
350
- file: getFile(schema.name)
351
- };
352
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
353
- baseName: type.file.baseName,
354
- path: type.file.path,
355
- meta: type.file.meta,
356
- banner: (0, _kubb_plugin_oas_utils.getBanner)({
357
- oas,
358
- output,
359
- config: driver.config
360
- }),
361
- footer: (0, _kubb_plugin_oas_utils.getFooter)({
362
- oas,
363
- output
364
- }),
365
- children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
366
- root: type.file.path,
367
- path: imp.path,
368
- name: imp.name,
369
- isTypeOnly: true
370
- }, [
371
- schema.name,
372
- imp.path,
373
- imp.isTypeOnly
374
- ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.Type, {
375
- name: type.name,
376
- typedName: type.typedName,
377
- description: schema.value.description,
378
- tree: schema.tree,
379
- schema: schema.value,
380
- enumType,
381
- enumKeyCasing,
382
- optionalType,
383
- arrayType,
384
- syntaxType
385
- })]
386
- });
387
- }
388
- });
389
- //#endregion
390
- //#region src/constants.ts
391
- /**
392
- * `optionalType` values that cause a property's type to include `| undefined`.
393
- */
394
- const OPTIONAL_ADDS_UNDEFINED = new Set(["undefined", "questionTokenAndUndefined"]);
395
- /**
396
- * `optionalType` values that render the property key with a `?` token.
397
- */
398
- const OPTIONAL_ADDS_QUESTION_TOKEN = new Set(["questionToken", "questionTokenAndUndefined"]);
399
- /**
400
- * `enumType` values that append a `Key` suffix to the generated enum type alias.
401
- */
402
- const ENUM_TYPES_WITH_KEY_SUFFIX = new Set(["asConst", "asPascalConst"]);
403
- /**
404
- * `enumType` values that require a runtime value declaration (object, enum, or literal).
405
- */
406
- const ENUM_TYPES_WITH_RUNTIME_VALUE = new Set([
407
- "enum",
408
- "asConst",
409
- "asPascalConst",
410
- "constEnum",
411
- "literal",
412
- void 0
413
- ]);
414
- /**
415
- * `enumType` values whose type declaration is type-only (no runtime value emitted for the type alias).
416
- */
417
- const ENUM_TYPES_WITH_TYPE_ONLY = new Set([
418
- "asConst",
419
- "asPascalConst",
420
- "literal",
421
- void 0
422
- ]);
423
- //#endregion
424
- //#region src/printer.ts
425
- /**
426
- * Converts a primitive const value to a TypeScript literal type node.
427
- * Handles negative numbers via a prefix unary expression.
428
- */
429
- function constToTypeNode(value, format) {
430
- if (format === "boolean") return require_components.createLiteralTypeNode(value === true ? require_components.createTrue() : require_components.createFalse());
431
- if (format === "number" && typeof value === "number") {
432
- if (value < 0) return require_components.createLiteralTypeNode(require_components.createPrefixUnaryExpression(require_components.SyntaxKind.MinusToken, require_components.createNumericLiteral(Math.abs(value))));
433
- return require_components.createLiteralTypeNode(require_components.createNumericLiteral(value));
434
- }
435
- return require_components.createLiteralTypeNode(require_components.createStringLiteral(String(value)));
436
- }
437
- /**
438
- * Returns a `Date` reference type node when `representation` is `'date'`, otherwise falls back to `string`.
439
- */
440
- function dateOrStringNode(node) {
441
- return node.representation === "date" ? require_components.createTypeReferenceNode(require_components.createIdentifier("Date")) : require_components.keywordTypeNodes.string;
442
- }
443
- /**
444
- * Maps an array of `SchemaNode`s through the printer, filtering out `null` and `undefined` results.
445
- */
446
- function buildMemberNodes(members, print) {
447
- return (members ?? []).map(print).filter(Boolean);
448
- }
449
- /**
450
- * Builds a TypeScript tuple type node from an array schema's `items`,
451
- * applying min/max slice and optional/rest element rules.
452
- */
453
- function buildTupleNode(node, print) {
454
- let items = (node.items ?? []).map(print).filter(Boolean);
455
- const restNode = node.rest ? print(node.rest) ?? void 0 : void 0;
456
- const { min, max } = node;
457
- if (max !== void 0) {
458
- items = items.slice(0, max);
459
- if (items.length < max && restNode) items = [...items, ...Array(max - items.length).fill(restNode)];
460
- }
461
- if (min !== void 0) items = items.map((item, i) => i >= min ? require_components.createOptionalTypeNode(item) : item);
462
- if (max === void 0 && restNode) items.push(require_components.createRestTypeNode(require_components.createArrayTypeNode(restNode)));
463
- return require_components.createTupleTypeNode(items);
464
- }
465
- /**
466
- * Applies `nullable` and optional/nullish `| undefined` union modifiers to a property's resolved base type.
467
- */
468
- function buildPropertyType(schema, baseType, optionalType) {
469
- const addsUndefined = OPTIONAL_ADDS_UNDEFINED.has(optionalType);
470
- let type = baseType;
471
- if (schema.nullable) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.null] });
472
- if ((schema.nullish || schema.optional) && addsUndefined) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.undefined] });
473
- return type;
474
- }
475
- /**
476
- * Collects JSDoc annotation strings (description, deprecated, min/max, pattern, default, example, type) for a schema node.
477
- */
478
- function buildPropertyJSDocComments(schema) {
479
- return [
480
- "description" in schema && schema.description ? `@description ${require_components.jsStringEscape(schema.description)}` : void 0,
481
- "deprecated" in schema && schema.deprecated ? "@deprecated" : void 0,
482
- "min" in schema && schema.min !== void 0 ? `@minLength ${schema.min}` : void 0,
483
- "max" in schema && schema.max !== void 0 ? `@maxLength ${schema.max}` : void 0,
484
- "pattern" in schema && schema.pattern ? `@pattern ${schema.pattern}` : void 0,
485
- "default" in schema && schema.default !== void 0 ? `@default ${"primitive" in schema && schema.primitive === "string" ? stringify(schema.default) : schema.default}` : void 0,
486
- "example" in schema && schema.example !== void 0 ? `@example ${schema.example}` : void 0,
487
- "primitive" in schema && schema.primitive ? [`@type ${schema.primitive || "unknown"}`, "optional" in schema && schema.optional ? " | undefined" : void 0].filter(Boolean).join("") : void 0
488
- ];
489
- }
490
- /**
491
- * Creates TypeScript index signatures for `additionalProperties` and `patternProperties` on an object schema node.
492
- */
493
- function buildIndexSignatures(node, propertyCount, print) {
494
- const elements = [];
495
- if (node.additionalProperties && node.additionalProperties !== true) {
496
- const additionalType = print(node.additionalProperties) ?? require_components.keywordTypeNodes.unknown;
497
- elements.push(require_components.createIndexSignature(propertyCount > 0 ? require_components.keywordTypeNodes.unknown : additionalType));
498
- } else if (node.additionalProperties === true) elements.push(require_components.createIndexSignature(require_components.keywordTypeNodes.unknown));
499
- if (node.patternProperties) {
500
- const first = Object.values(node.patternProperties)[0];
501
- if (first) {
502
- let patternType = print(first) ?? require_components.keywordTypeNodes.unknown;
503
- if (first.nullable) patternType = require_components.createUnionDeclaration({ nodes: [patternType, require_components.keywordTypeNodes.null] });
504
- elements.push(require_components.createIndexSignature(patternType));
505
- }
506
- }
507
- return elements;
508
- }
509
- /**
510
- * TypeScript type printer built with `definePrinter`.
511
- *
512
- * Converts a `SchemaNode` AST node into a TypeScript AST node:
513
- * - **`printer.print(node)`** — when `options.typeName` is set, returns a full
514
- * `type Name = …` or `interface Name { … }` declaration (`ts.Node`).
515
- * Without `typeName`, returns the raw `ts.TypeNode` for the schema.
516
- *
517
- * Dispatches on `node.type` to the appropriate handler in `nodes`. Options are closed
518
- * over per printer instance, so each call to `printerTs(options)` produces an independent printer.
519
- *
520
- * @example Raw type node (no `typeName`)
521
- * ```ts
522
- * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral' })
523
- * const typeNode = printer.print(schemaNode) // ts.TypeNode
524
- * ```
525
- *
526
- * @example Full declaration (with `typeName`)
527
- * ```ts
528
- * const printer = printerTs({ optionalType: 'questionToken', arrayType: 'array', enumType: 'inlineLiteral', typeName: 'MyType' })
529
- * const declaration = printer.print(schemaNode) // ts.TypeAliasDeclaration | ts.InterfaceDeclaration
530
- * ```
531
- */
532
- const printerTs = (0, _kubb_core.definePrinter)((options) => {
533
- const addsUndefined = OPTIONAL_ADDS_UNDEFINED.has(options.optionalType);
534
- return {
535
- name: "typescript",
536
- options,
537
- nodes: {
538
- any: () => require_components.keywordTypeNodes.any,
539
- unknown: () => require_components.keywordTypeNodes.unknown,
540
- void: () => require_components.keywordTypeNodes.void,
541
- never: () => require_components.keywordTypeNodes.never,
542
- boolean: () => require_components.keywordTypeNodes.boolean,
543
- null: () => require_components.keywordTypeNodes.null,
544
- blob: () => require_components.createTypeReferenceNode("Blob", []),
545
- string: () => require_components.keywordTypeNodes.string,
546
- uuid: () => require_components.keywordTypeNodes.string,
547
- email: () => require_components.keywordTypeNodes.string,
548
- url: (node) => {
549
- if (node.path) return require_components.createUrlTemplateType(node.path);
550
- return require_components.keywordTypeNodes.string;
551
- },
552
- datetime: () => require_components.keywordTypeNodes.string,
553
- number: () => require_components.keywordTypeNodes.number,
554
- integer: () => require_components.keywordTypeNodes.number,
555
- bigint: () => require_components.keywordTypeNodes.bigint,
556
- date: dateOrStringNode,
557
- time: dateOrStringNode,
558
- ref(node) {
559
- if (!node.name) return;
560
- return require_components.createTypeReferenceNode(node.name, void 0);
561
- },
562
- enum(node) {
563
- const values = node.namedEnumValues?.map((v) => v.value) ?? node.enumValues ?? [];
564
- if (this.options.enumType === "inlineLiteral" || !node.name) {
565
- const literalNodes = values.filter((v) => v !== null).map((value) => constToTypeNode(value, typeof value)).filter(Boolean);
566
- return require_components.createUnionDeclaration({
567
- withParentheses: true,
568
- nodes: literalNodes
569
- }) ?? void 0;
570
- }
571
- const resolvedName = require_components.pascalCase(node.name);
572
- const typeName = ENUM_TYPES_WITH_KEY_SUFFIX.has(this.options.enumType) ? `${resolvedName}Key` : resolvedName;
573
- return require_components.createTypeReferenceNode(typeName, void 0);
574
- },
575
- union(node) {
576
- const members = node.members ?? [];
577
- const hasStringLiteral = members.some((m) => m.type === "enum" && m.enumType === "string");
578
- const hasPlainString = members.some((m) => (0, _kubb_ast.isPlainStringType)(m));
579
- if (hasStringLiteral && hasPlainString) {
580
- const memberNodes = members.map((m) => {
581
- if ((0, _kubb_ast.isPlainStringType)(m)) return require_components.createIntersectionDeclaration({
582
- nodes: [require_components.keywordTypeNodes.string, require_components.createTypeLiteralNode([])],
583
- withParentheses: true
584
- });
585
- return this.print(m);
586
- }).filter(Boolean);
587
- return require_components.createUnionDeclaration({
588
- withParentheses: true,
589
- nodes: memberNodes
590
- }) ?? void 0;
591
- }
592
- return require_components.createUnionDeclaration({
593
- withParentheses: true,
594
- nodes: buildMemberNodes(members, this.print)
595
- }) ?? void 0;
596
- },
597
- intersection(node) {
598
- return require_components.createIntersectionDeclaration({
599
- withParentheses: true,
600
- nodes: buildMemberNodes(node.members, this.print)
601
- }) ?? void 0;
602
- },
603
- array(node) {
604
- const itemNodes = (node.items ?? []).map((item) => this.print(item)).filter(Boolean);
605
- return require_components.createArrayDeclaration({
606
- nodes: itemNodes,
607
- arrayType: this.options.arrayType
608
- }) ?? void 0;
609
- },
610
- tuple(node) {
611
- return buildTupleNode(node, this.print);
612
- },
613
- object(node) {
614
- const { print, options } = this;
615
- const addsQuestionToken = OPTIONAL_ADDS_QUESTION_TOKEN.has(options.optionalType);
616
- const propertyNodes = node.properties.map((prop) => {
617
- const baseType = print(prop.schema) ?? require_components.keywordTypeNodes.unknown;
618
- const type = buildPropertyType(prop.schema, baseType, options.optionalType);
619
- const propertyNode = require_components.createPropertySignature({
620
- questionToken: prop.schema.optional || prop.schema.nullish ? addsQuestionToken : false,
621
- name: prop.name,
622
- type,
623
- readOnly: prop.schema.readOnly
624
- });
625
- return require_components.appendJSDocToNode({
626
- node: propertyNode,
627
- comments: buildPropertyJSDocComments(prop.schema)
628
- });
629
- });
630
- const allElements = [...propertyNodes, ...buildIndexSignatures(node, propertyNodes.length, print)];
631
- if (!allElements.length) return require_components.keywordTypeNodes.object;
632
- return require_components.createTypeLiteralNode(allElements);
633
- }
634
- },
635
- print(node) {
636
- let type = this.print(node);
637
- if (!type) return;
638
- if (node.nullable) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.null] });
639
- if ((node.nullish || node.optional) && addsUndefined) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.undefined] });
640
- const { typeName, syntaxType = "type", description, keysToOmit } = this.options;
641
- if (!typeName) return type;
642
- const useTypeGeneration = syntaxType === "type" || type.kind === require_components.syntaxKind.union || !!keysToOmit?.length;
643
- return require_components.createTypeDeclaration({
644
- name: typeName,
645
- isExportable: true,
646
- type: keysToOmit?.length ? require_components.createOmitDeclaration({
647
- keys: keysToOmit,
648
- type,
649
- nonNullable: true
650
- }) : type,
651
- syntax: useTypeGeneration ? "type" : "interface",
652
- comments: [
653
- node?.title ? require_components.jsStringEscape(node.title) : void 0,
654
- description ? `@description ${require_components.jsStringEscape(description)}` : void 0,
655
- node?.deprecated ? "@deprecated" : void 0,
656
- node && "min" in node && node.min !== void 0 ? `@minLength ${node.min}` : void 0,
657
- node && "max" in node && node.max !== void 0 ? `@maxLength ${node.max}` : void 0,
658
- node && "pattern" in node && node.pattern ? `@pattern ${node.pattern}` : void 0,
659
- node?.default ? `@default ${node.default}` : void 0,
660
- node?.example ? `@example ${node.example}` : void 0
661
- ]
662
- });
663
- }
664
- };
665
- });
666
- //#endregion
667
- //#region src/components/v2/Enum.tsx
668
- /**
669
- * Resolves the runtime identifier name and the TypeScript type name for an enum schema node.
670
- *
671
- * The raw `node.name` may be a YAML key such as `"enumNames.Type"` which is not a
672
- * valid TypeScript identifier. `pascalCase` normalizes it unconditionally; for inline enum
673
- * properties the adapter already emits a PascalCase+suffix name so `pascalCase` is a no-op.
674
- */
675
- function getEnumNames(node, enumType) {
676
- const resolved = require_components.pascalCase(node.name);
677
- return {
678
- enumName: enumType === "asPascalConst" ? resolved : require_components.camelCase(node.name),
679
- typeName: ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) ? `${resolved}Key` : resolved
680
- };
681
- }
682
- /**
683
- * Renders the enum declaration(s) for a single named `EnumSchemaNode`.
684
- *
685
- * Depending on `enumType` this may emit:
686
- * - A runtime object (`asConst` / `asPascalConst`) plus a `typeof` type alias
687
- * - A `const enum` or plain `enum` declaration (`constEnum` / `enum`)
688
- * - A union literal type alias (`literal`)
689
- *
690
- * The emitted `File.Source` nodes carry the resolved names so that the barrel
691
- * index picks up the correct export identifiers.
692
- */
693
- function Enum({ node, enumType, enumKeyCasing }) {
694
- const { enumName, typeName } = getEnumNames(node, enumType);
695
- const [nameNode, typeNode] = require_components.createEnumDeclaration({
696
- name: enumName,
697
- typeName,
698
- enums: node.namedEnumValues?.map((v) => [require_components.trimQuotes(v.name.toString()), v.value]) ?? node.enumValues?.filter((v) => v !== null && v !== void 0).map((v) => [require_components.trimQuotes(v.toString()), v]) ?? [],
699
- type: enumType,
700
- enumKeyCasing
701
- });
702
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [nameNode && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
703
- name: enumName,
704
- isExportable: true,
705
- isIndexable: true,
706
- isTypeOnly: false,
707
- children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(nameNode)
708
- }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
709
- name: typeName,
710
- isIndexable: true,
711
- isExportable: ENUM_TYPES_WITH_RUNTIME_VALUE.has(enumType),
712
- isTypeOnly: ENUM_TYPES_WITH_TYPE_ONLY.has(enumType),
713
- children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(typeNode)
714
- })] });
715
- }
716
- //#endregion
717
- //#region src/components/v2/Type.tsx
718
- function Type({ name, typedName, node, keysToOmit, optionalType, arrayType, syntaxType, enumType, enumKeyCasing, description }) {
719
- const resolvedDescription = description || node?.description;
720
- const enumSchemaNodes = (0, _kubb_ast.collect)(node, { schema(n) {
721
- if (n.type === "enum" && n.name) return n;
722
- } });
723
- const typeNode = printerTs({
724
- optionalType,
725
- arrayType,
726
- enumType,
727
- typeName: name,
728
- syntaxType,
729
- description: resolvedDescription,
730
- keysToOmit
731
- }).print(node);
732
- if (!typeNode) return;
733
- const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((node) => {
734
- return {
735
- node,
736
- ...getEnumNames(node, enumType)
737
- };
738
- });
739
- const shouldExportEnums = enumType !== "inlineLiteral";
740
- const shouldExportType = enumType === "inlineLiteral" || enums.every((item) => item.typeName !== name);
741
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ node }) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(Enum, {
742
- node,
743
- enumType,
744
- enumKeyCasing
745
- })), shouldExportType && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
746
- name: typedName,
747
- isTypeOnly: true,
748
- isExportable: true,
749
- isIndexable: true,
750
- children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(typeNode)
751
- })] });
752
- }
753
- //#endregion
754
- //#region src/generators/v2/utils.ts
755
- /**
756
- * Builds an `ObjectSchemaNode` for a group of parameters (path/query/header).
757
- * Each property is a `ref` schema pointing to the individually-resolved parameter type.
758
- * The ref name includes the parameter location so generated type names follow
759
- * the `<OperationId><Location><ParamName>` convention.
760
- */
761
- function buildParamsSchema({ params, operationId, resolveName }) {
762
- return (0, _kubb_ast.createSchema)({
763
- type: "object",
764
- properties: params.map((param) => (0, _kubb_ast.createProperty)({
765
- name: param.name,
766
- schema: (0, _kubb_ast.createSchema)({
767
- type: "ref",
768
- name: resolveName({
769
- name: `${operationId} ${require_components.pascalCase(param.in)} ${param.name}`,
770
- type: "function"
771
- }),
772
- optional: !param.required
773
- })
774
- }))
775
- });
776
- }
777
- /**
778
- * Builds an `ObjectSchemaNode` representing the `<OperationId>RequestConfig` type:
779
- * - `data` → request body ref (optional) or `never`
780
- * - `pathParams` → inline object of path param refs, or `never`
781
- * - `queryParams` → inline object of query param refs (optional), or `never`
782
- * - `headerParams` → inline object of header param refs (optional), or `never`
783
- * - `url` → Express-style template literal (plugin-ts extension, handled by printer)
784
- */
785
- function buildDataSchemaNode({ node, resolveName }) {
786
- const pathParams = node.parameters.filter((p) => p.in === "path");
787
- const queryParams = node.parameters.filter((p) => p.in === "query");
788
- const headerParams = node.parameters.filter((p) => p.in === "header");
789
- return (0, _kubb_ast.createSchema)({
790
- type: "object",
791
- properties: [
792
- (0, _kubb_ast.createProperty)({
793
- name: "data",
794
- schema: node.requestBody ? (0, _kubb_ast.createSchema)({
795
- type: "ref",
796
- name: resolveName({
797
- name: `${node.operationId} Data`,
798
- type: "function"
799
- }),
800
- optional: true
801
- }) : (0, _kubb_ast.createSchema)({
802
- type: "never",
803
- optional: true
804
- })
805
- }),
806
- (0, _kubb_ast.createProperty)({
807
- name: "pathParams",
808
- schema: pathParams.length > 0 ? buildParamsSchema({
809
- params: pathParams,
810
- operationId: node.operationId,
811
- resolveName
812
- }) : (0, _kubb_ast.createSchema)({
813
- type: "never",
814
- optional: true
815
- })
816
- }),
817
- (0, _kubb_ast.createProperty)({
818
- name: "queryParams",
819
- schema: queryParams.length > 0 ? (0, _kubb_ast.createSchema)({
820
- ...buildParamsSchema({
821
- params: queryParams,
822
- operationId: node.operationId,
823
- resolveName
824
- }),
825
- optional: true
826
- }) : (0, _kubb_ast.createSchema)({
827
- type: "never",
828
- optional: true
829
- })
830
- }),
831
- (0, _kubb_ast.createProperty)({
832
- name: "headerParams",
833
- schema: headerParams.length > 0 ? (0, _kubb_ast.createSchema)({
834
- ...buildParamsSchema({
835
- params: headerParams,
836
- operationId: node.operationId,
837
- resolveName
838
- }),
839
- optional: true
840
- }) : (0, _kubb_ast.createSchema)({
841
- type: "never",
842
- optional: true
843
- })
844
- }),
845
- (0, _kubb_ast.createProperty)({
846
- name: "url",
847
- schema: (0, _kubb_ast.createSchema)({
848
- type: "url",
849
- path: node.path
850
- })
851
- })
852
- ]
853
- });
854
- }
855
- /**
856
- * Builds an `ObjectSchemaNode` representing `<OperationId>Responses` — keyed by HTTP status code.
857
- * Numeric status codes produce unquoted numeric keys (e.g. `200:`).
858
- */
859
- function buildResponsesSchemaNode({ node, resolveName }) {
860
- const responsesWithSchema = node.responses.filter((res) => res.schema);
861
- if (responsesWithSchema.length === 0) return null;
862
- return (0, _kubb_ast.createSchema)({
863
- type: "object",
864
- properties: responsesWithSchema.map((res) => (0, _kubb_ast.createProperty)({
865
- name: String(res.statusCode),
866
- schema: (0, _kubb_ast.createSchema)({
867
- type: "ref",
868
- name: resolveName({
869
- name: `${node.operationId} Status ${res.statusCode}`,
870
- type: "function"
871
- })
872
- })
873
- }))
874
- });
875
- }
876
- /**
877
- * Builds a `UnionSchemaNode` representing `<OperationId>Response` — all response types in union format.
878
- * Returns `null` when the operation has no responses with schemas.
879
- */
880
- function buildResponseUnionSchemaNode({ node, resolveName }) {
881
- const responsesWithSchema = node.responses.filter((res) => res.schema);
882
- if (responsesWithSchema.length === 0) return null;
883
- return (0, _kubb_ast.createSchema)({
884
- type: "union",
885
- members: responsesWithSchema.map((res) => (0, _kubb_ast.createSchema)({
886
- type: "ref",
887
- name: resolveName({
888
- name: `${node.operationId} Status ${res.statusCode}`,
889
- type: "function"
890
- })
891
- }))
892
- });
893
- }
894
- //#endregion
895
- //#region src/generators/v2/typeGenerator.tsx
896
- const typeGenerator = (0, _kubb_core.createGenerator)({
897
- name: "typescript",
898
- type: "react",
899
- Operation({ node, adapter, options }) {
900
- const { enumType, enumKeyCasing, optionalType, arrayType, syntaxType, paramsCasing, group } = options;
901
- const { mode, getFile, resolveName } = (0, _kubb_core_hooks.useKubb)();
902
- const file = getFile({
903
- name: node.operationId,
904
- extname: ".ts",
905
- mode,
906
- options: { group: group ? group.type === "tag" ? { tag: node.tags[0] } : { path: node.path } : void 0 }
907
- });
908
- const params = (0, _kubb_ast.applyParamsCasing)(node.parameters, paramsCasing);
909
- function renderSchemaType({ node: schemaNode, name, typedName, description }) {
910
- if (!schemaNode) return null;
911
- const imports = adapter.getImports(schemaNode, (schemaName) => ({
912
- name: resolveName({
913
- name: schemaName,
914
- type: "type"
915
- }),
916
- path: getFile({
917
- name: schemaName,
918
- extname: ".ts",
919
- mode
920
- }).path
921
- }));
922
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
923
- root: file.path,
924
- path: imp.path,
925
- name: imp.name,
926
- isTypeOnly: true
927
- }, [
928
- name,
929
- imp.path,
930
- imp.isTypeOnly
931
- ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(Type, {
932
- name,
933
- typedName,
934
- node: schemaNode,
935
- description,
936
- enumType,
937
- enumKeyCasing,
938
- optionalType,
939
- arrayType,
940
- syntaxType
941
- })] });
942
- }
943
- const paramTypes = params.map((param) => renderSchemaType({
944
- node: param.schema,
945
- name: resolveName({
946
- name: `${node.operationId} ${require_components.pascalCase(param.in)} ${param.name}`,
947
- type: "function"
948
- }),
949
- typedName: resolveName({
950
- name: `${node.operationId} ${require_components.pascalCase(param.in)} ${param.name}`,
951
- type: "type"
952
- })
953
- }));
954
- const responseTypes = node.responses.filter((res) => res.schema).map((res) => renderSchemaType({
955
- node: res.schema,
956
- name: resolveName({
957
- name: `${node.operationId} Status ${res.statusCode}`,
958
- type: "function"
959
- }),
960
- typedName: resolveName({
961
- name: `${node.operationId} Status ${res.statusCode}`,
962
- type: "type"
963
- }),
964
- description: res.description
965
- }));
966
- const requestType = node.requestBody ? renderSchemaType({
967
- node: node.requestBody,
968
- name: resolveName({
969
- name: `${node.operationId} Data`,
970
- type: "function"
971
- }),
972
- typedName: resolveName({
973
- name: `${node.operationId} Data`,
974
- type: "type"
975
- }),
976
- description: node.requestBody.description
977
- }) : null;
978
- const dataType = renderSchemaType({
979
- node: buildDataSchemaNode({
980
- node: {
981
- ...node,
982
- parameters: params
983
- },
984
- resolveName
985
- }),
986
- name: resolveName({
987
- name: `${node.operationId} RequestConfig`,
988
- type: "function"
989
- }),
990
- typedName: resolveName({
991
- name: `${node.operationId} RequestConfig`,
992
- type: "type"
993
- })
994
- });
995
- const responsesType = renderSchemaType({
996
- node: buildResponsesSchemaNode({
997
- node,
998
- resolveName
999
- }),
1000
- name: resolveName({
1001
- name: `${node.operationId} Responses`,
1002
- type: "function"
1003
- }),
1004
- typedName: resolveName({
1005
- name: `${node.operationId} Responses`,
1006
- type: "type"
1007
- })
1008
- });
1009
- const responseType = renderSchemaType({
1010
- node: buildResponseUnionSchemaNode({
1011
- node,
1012
- resolveName
1013
- }),
1014
- name: resolveName({
1015
- name: `${node.operationId} Response`,
1016
- type: "function"
1017
- }),
1018
- typedName: resolveName({
1019
- name: `${node.operationId} Response`,
1020
- type: "type"
1021
- }),
1022
- description: "Union of all possible responses"
1023
- });
1024
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
1025
- baseName: file.baseName,
1026
- path: file.path,
1027
- meta: file.meta,
1028
- children: [
1029
- paramTypes,
1030
- responseTypes,
1031
- requestType,
1032
- dataType,
1033
- responsesType,
1034
- responseType
1035
- ]
1036
- });
1037
- },
1038
- Schema({ node, adapter, options }) {
1039
- const { enumType, enumKeyCasing, syntaxType, optionalType, arrayType } = options;
1040
- const { mode, resolveName, getFile } = (0, _kubb_core_hooks.useKubb)();
1041
- if (!node.name) return;
1042
- const imports = adapter.getImports(node, (schemaName) => ({
1043
- name: resolveName({
1044
- name: schemaName,
1045
- type: "type"
1046
- }),
1047
- path: getFile({
1048
- name: schemaName,
1049
- extname: ".ts",
1050
- mode
1051
- }).path
1052
- }));
1053
- const isEnumSchema = node.type === "enum";
1054
- let typedName = resolveName({
1055
- name: node.name,
1056
- type: "type"
1057
- });
1058
- if (ENUM_TYPES_WITH_KEY_SUFFIX.has(enumType) && isEnumSchema) typedName += "Key";
1059
- const type = {
1060
- name: resolveName({
1061
- name: node.name,
1062
- type: "function"
1063
- }),
1064
- typedName,
1065
- file: getFile({
1066
- name: node.name,
1067
- extname: ".ts",
1068
- mode
1069
- })
1070
- };
1071
- return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
1072
- baseName: type.file.baseName,
1073
- path: type.file.path,
1074
- meta: type.file.meta,
1075
- children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
1076
- root: type.file.path,
1077
- path: imp.path,
1078
- name: imp.name,
1079
- isTypeOnly: true
1080
- }, [
1081
- node.name,
1082
- imp.path,
1083
- imp.isTypeOnly
1084
- ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(Type, {
1085
- name: type.name,
1086
- typedName: type.typedName,
1087
- node,
1088
- enumType,
1089
- enumKeyCasing,
1090
- optionalType,
1091
- arrayType,
1092
- syntaxType
1093
- })]
1094
- });
1095
- }
1096
- });
1097
- //#endregion
1098
- //#region src/plugin.ts
1099
- const pluginTsName = "plugin-ts";
1100
- const pluginTs = (0, _kubb_core.createPlugin)((options) => {
1101
- const { output = {
1102
- path: "types",
1103
- barrelType: "named"
1104
- }, group, exclude = [], include, override = [], enumType = "asConst", enumKeyCasing = "none", enumSuffix = "enum", dateType = "string", integerType = "number", unknownType = "any", optionalType = "questionToken", arrayType = "array", emptySchemaType = unknownType, syntaxType = "type", transformers = {}, paramsCasing, generators = [typeGenerator$1, typeGenerator].filter(Boolean), contentType, UNSTABLE_NAMING } = options;
1105
- return {
1106
- name: pluginTsName,
1107
- options: {
1108
- output,
1109
- transformers,
1110
- dateType,
1111
- integerType,
1112
- optionalType,
1113
- arrayType,
1114
- enumType,
1115
- enumKeyCasing,
1116
- enumSuffix,
1117
- unknownType,
1118
- emptySchemaType,
1119
- syntaxType,
1120
- group,
1121
- override,
1122
- paramsCasing,
1123
- usedEnumNames: {}
1124
- },
1125
- pre: [_kubb_plugin_oas.pluginOasName],
1126
- resolvePath(baseName, pathMode, options) {
1127
- const root = node_path.default.resolve(this.config.root, this.config.output.path);
1128
- if ((pathMode ?? (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path))) === "single")
1129
- /**
1130
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
1131
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
1132
- */
1133
- return node_path.default.resolve(root, output.path);
1134
- if (group && (options?.group?.path || options?.group?.tag)) {
1135
- const groupName = group?.name ? group.name : (ctx) => {
1136
- if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
1137
- return `${require_components.camelCase(ctx.group)}Controller`;
1138
- };
1139
- return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
1140
- }
1141
- return node_path.default.resolve(root, output.path, baseName);
1142
- },
1143
- resolveName(name, type) {
1144
- const resolvedName = require_components.pascalCase(name, { isFile: type === "file" });
1145
- if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
1146
- return resolvedName;
1147
- },
1148
- async install() {
1149
- const { config, fabric, plugin, adapter, rootNode, driver, openInStudio } = this;
1150
- const root = node_path.default.resolve(config.root, config.output.path);
1151
- const mode = (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path));
1152
- if (adapter) {
1153
- await openInStudio({ ast: true });
1154
- await (0, _kubb_ast.walk)(rootNode, {
1155
- async schema(schemaNode) {
1156
- const writeTasks = generators.map(async (generator) => {
1157
- if (generator.type === "react" && generator.version === "2") {
1158
- const options = (0, _kubb_core.resolveOptions)(schemaNode, {
1159
- options: plugin.options,
1160
- exclude,
1161
- include,
1162
- override
1163
- });
1164
- if (options === null) return;
1165
- await (0, _kubb_plugin_oas.buildSchema)(schemaNode, {
1166
- options,
1167
- adapter,
1168
- config,
1169
- fabric,
1170
- Component: generator.Schema,
1171
- plugin,
1172
- driver,
1173
- mode,
1174
- version: generator.version
1175
- });
1176
- }
1177
- });
1178
- await Promise.all(writeTasks);
1179
- },
1180
- async operation(operationNode) {
1181
- const writeTasks = generators.map(async (generator) => {
1182
- if (generator.type === "react" && generator.version === "2") {
1183
- const options = (0, _kubb_core.resolveOptions)(operationNode, {
1184
- options: plugin.options,
1185
- exclude,
1186
- include,
1187
- override
1188
- });
1189
- if (options === null) return;
1190
- await (0, _kubb_plugin_oas.buildOperation)(operationNode, {
1191
- options,
1192
- adapter,
1193
- config,
1194
- fabric,
1195
- Component: generator.Operation,
1196
- plugin,
1197
- driver,
1198
- mode,
1199
- version: generator.version
1200
- });
1201
- }
1202
- });
1203
- await Promise.all(writeTasks);
1204
- }
1205
- }, { depth: "shallow" });
1206
- const barrelFiles = await (0, _kubb_core.getBarrelFiles)(this.fabric.files, {
1207
- type: output.barrelType ?? "named",
1208
- root,
1209
- output,
1210
- meta: { pluginName: this.plugin.name }
1211
- });
1212
- await this.upsertFile(...barrelFiles);
1213
- return;
1214
- }
1215
- const oas = await this.getOas();
1216
- const schemaFiles = await new _kubb_plugin_oas.SchemaGenerator(this.plugin.options, {
1217
- fabric: this.fabric,
1218
- oas,
1219
- driver: this.driver,
1220
- events: this.events,
1221
- plugin: this.plugin,
1222
- contentType,
1223
- include: void 0,
1224
- override,
1225
- mode,
1226
- output: output.path
1227
- }).build(...generators);
1228
- await this.upsertFile(...schemaFiles);
1229
- const operationFiles = await new _kubb_plugin_oas.OperationGenerator(this.plugin.options, {
1230
- fabric: this.fabric,
1231
- oas,
1232
- driver: this.driver,
1233
- events: this.events,
1234
- plugin: this.plugin,
1235
- contentType,
1236
- exclude,
1237
- include,
1238
- override,
1239
- mode,
1240
- UNSTABLE_NAMING
1241
- }).build(...generators);
1242
- await this.upsertFile(...operationFiles);
1243
- const barrelFiles = await (0, _kubb_core.getBarrelFiles)(this.fabric.files, {
1244
- type: output.barrelType ?? "named",
1245
- root,
1246
- output,
1247
- meta: { pluginName: this.plugin.name }
1248
- });
1249
- await this.upsertFile(...barrelFiles);
1250
- }
1251
- };
1252
- });
1253
- //#endregion
1254
- Object.defineProperty(exports, "pluginTs", {
1255
- enumerable: true,
1256
- get: function() {
1257
- return pluginTs;
1258
- }
1259
- });
1260
- Object.defineProperty(exports, "pluginTsName", {
1261
- enumerable: true,
1262
- get: function() {
1263
- return pluginTsName;
1264
- }
1265
- });
1266
- Object.defineProperty(exports, "typeGenerator", {
1267
- enumerable: true,
1268
- get: function() {
1269
- return typeGenerator;
1270
- }
1271
- });
1272
- Object.defineProperty(exports, "typeGenerator$1", {
1273
- enumerable: true,
1274
- get: function() {
1275
- return typeGenerator$1;
1276
- }
1277
- });
1278
-
1279
- //# sourceMappingURL=plugin-MLTxoa8p.cjs.map