@kubb/plugin-ts 5.0.0-alpha.2 → 5.0.0-alpha.4

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.
@@ -0,0 +1,1013 @@
1
+ const require_components = require("./components-DI0aTIBg.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, pluginManager }) {
31
+ const properties = {};
32
+ if (schemas.response) properties["response"] = require_components.createUnionDeclaration({ nodes: schemas.responses.map((res) => {
33
+ const identifier = pluginManager.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 = pluginManager.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 = pluginManager.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 = pluginManager.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 = pluginManager.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 = pluginManager.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, pluginManager }) {
94
+ const name = pluginManager.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 = pluginManager.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 = pluginManager.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 = pluginManager.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 = pluginManager.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, pluginManager, unknownType }) {
177
+ const results = [];
178
+ const name = pluginManager.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 = pluginManager.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: { mapper, enumType, enumKeyCasing, syntaxType, optionalType, arrayType, unknownType, paramsCasing } } = plugin;
221
+ const mode = (0, _kubb_core_hooks.useMode)();
222
+ const pluginManager = (0, _kubb_core_hooks.usePluginManager)();
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
+ pluginManager,
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
+ mapper,
280
+ enumType,
281
+ enumKeyCasing,
282
+ optionalType,
283
+ arrayType,
284
+ keysToOmit,
285
+ syntaxType
286
+ })] });
287
+ };
288
+ const responseName = schemaManager.getName(schemas.response.name, { type: "type" });
289
+ const combinedSchemaName = operation.method === "get" ? `${name}Query` : `${name}Mutation`;
290
+ return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
291
+ baseName: file.baseName,
292
+ path: file.path,
293
+ meta: file.meta,
294
+ banner: (0, _kubb_plugin_oas_utils.getBanner)({
295
+ oas,
296
+ output: plugin.options.output,
297
+ config: pluginManager.config
298
+ }),
299
+ footer: (0, _kubb_plugin_oas_utils.getFooter)({
300
+ oas,
301
+ output: plugin.options.output
302
+ }),
303
+ 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, {
304
+ name: `${name}Request`,
305
+ isExportable: true,
306
+ isIndexable: true,
307
+ isTypeOnly: true,
308
+ children: printRequestSchema({
309
+ baseName: name,
310
+ operation,
311
+ schemas,
312
+ pluginManager
313
+ })
314
+ }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
315
+ name: responseName,
316
+ isExportable: true,
317
+ isIndexable: true,
318
+ isTypeOnly: true,
319
+ children: printResponseSchema({
320
+ baseName: name,
321
+ schemas,
322
+ pluginManager,
323
+ unknownType
324
+ })
325
+ })] }) : /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
326
+ name: combinedSchemaName,
327
+ isExportable: true,
328
+ isIndexable: true,
329
+ isTypeOnly: true,
330
+ children: printCombinedSchema({
331
+ name: combinedSchemaName,
332
+ schemas,
333
+ pluginManager
334
+ })
335
+ })]
336
+ });
337
+ },
338
+ Schema({ schema, plugin }) {
339
+ const { options: { mapper, enumType, enumKeyCasing, syntaxType, optionalType, arrayType, output } } = plugin;
340
+ const mode = (0, _kubb_core_hooks.useMode)();
341
+ const oas = (0, _kubb_plugin_oas_hooks.useOas)();
342
+ const pluginManager = (0, _kubb_core_hooks.usePluginManager)();
343
+ const { getName, getFile } = (0, _kubb_plugin_oas_hooks.useSchemaManager)();
344
+ const imports = (0, _kubb_plugin_oas_utils.getImports)(schema.tree);
345
+ const schemaFromTree = schema.tree.find((item) => item.keyword === _kubb_plugin_oas.schemaKeywords.schema);
346
+ let typedName = getName(schema.name, { type: "type" });
347
+ if (["asConst", "asPascalConst"].includes(enumType) && schemaFromTree && (0, _kubb_plugin_oas.isKeyword)(schemaFromTree, _kubb_plugin_oas.schemaKeywords.enum)) typedName = typedName += "Key";
348
+ const type = {
349
+ name: getName(schema.name, { type: "function" }),
350
+ typedName,
351
+ file: getFile(schema.name)
352
+ };
353
+ return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
354
+ baseName: type.file.baseName,
355
+ path: type.file.path,
356
+ meta: type.file.meta,
357
+ banner: (0, _kubb_plugin_oas_utils.getBanner)({
358
+ oas,
359
+ output,
360
+ config: pluginManager.config
361
+ }),
362
+ footer: (0, _kubb_plugin_oas_utils.getFooter)({
363
+ oas,
364
+ output
365
+ }),
366
+ children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
367
+ root: type.file.path,
368
+ path: imp.path,
369
+ name: imp.name,
370
+ isTypeOnly: true
371
+ }, [
372
+ schema.name,
373
+ imp.path,
374
+ imp.isTypeOnly
375
+ ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(require_components.Type, {
376
+ name: type.name,
377
+ typedName: type.typedName,
378
+ description: schema.value.description,
379
+ tree: schema.tree,
380
+ schema: schema.value,
381
+ mapper,
382
+ enumType,
383
+ enumKeyCasing,
384
+ optionalType,
385
+ arrayType,
386
+ syntaxType
387
+ })]
388
+ });
389
+ }
390
+ });
391
+ //#endregion
392
+ //#region src/printer.ts
393
+ function constToTypeNode(value, format) {
394
+ if (format === "boolean") return require_components.createLiteralTypeNode(value === true ? require_components.createTrue() : require_components.createFalse());
395
+ if (format === "number" && typeof value === "number") {
396
+ if (value < 0) return require_components.createLiteralTypeNode(require_components.createPrefixUnaryExpression(require_components.SyntaxKind.MinusToken, require_components.createNumericLiteral(Math.abs(value))));
397
+ return require_components.createLiteralTypeNode(require_components.createNumericLiteral(value));
398
+ }
399
+ return require_components.createLiteralTypeNode(require_components.createStringLiteral(String(value)));
400
+ }
401
+ function dateOrStringNode(node) {
402
+ return node.representation === "date" ? require_components.createTypeReferenceNode(require_components.createIdentifier("Date")) : require_components.keywordTypeNodes.string;
403
+ }
404
+ function buildMemberNodes(members, print) {
405
+ return (members ?? []).map(print).filter(Boolean);
406
+ }
407
+ function buildTupleNode(node, print) {
408
+ let items = (node.items ?? []).map(print).filter(Boolean);
409
+ const restNode = node.rest ? print(node.rest) ?? void 0 : void 0;
410
+ const { min, max } = node;
411
+ if (max !== void 0) {
412
+ items = items.slice(0, max);
413
+ if (items.length < max && restNode) items = [...items, ...Array(max - items.length).fill(restNode)];
414
+ }
415
+ if (min !== void 0) items = items.map((item, i) => i >= min ? require_components.createOptionalTypeNode(item) : item);
416
+ if (max === void 0 && restNode) items.push(require_components.createRestTypeNode(require_components.createArrayTypeNode(restNode)));
417
+ return require_components.createTupleTypeNode(items);
418
+ }
419
+ function buildPropertyType(schema, baseType, optionalType) {
420
+ const addsUndefined = ["undefined", "questionTokenAndUndefined"].includes(optionalType);
421
+ let type = baseType;
422
+ if (schema.nullable) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.null] });
423
+ if ((schema.nullish || schema.optional) && addsUndefined) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.undefined] });
424
+ return type;
425
+ }
426
+ function buildPropertyJSDocComments(schema) {
427
+ return [
428
+ "description" in schema && schema.description ? `@description ${require_components.jsStringEscape(schema.description)}` : void 0,
429
+ "deprecated" in schema && schema.deprecated ? "@deprecated" : void 0,
430
+ "min" in schema && schema.min !== void 0 ? `@minLength ${schema.min}` : void 0,
431
+ "max" in schema && schema.max !== void 0 ? `@maxLength ${schema.max}` : void 0,
432
+ "pattern" in schema && schema.pattern ? `@pattern ${schema.pattern}` : void 0,
433
+ "default" in schema && schema.default !== void 0 ? `@default ${"primitive" in schema && schema.primitive === "string" ? stringify(schema.default) : schema.default}` : void 0,
434
+ "example" in schema && schema.example !== void 0 ? `@example ${schema.example}` : void 0,
435
+ "primitive" in schema && schema.primitive ? [`@type ${schema.primitive || "unknown"}`, "optional" in schema && schema.optional ? " | undefined" : void 0].filter(Boolean).join("") : void 0
436
+ ];
437
+ }
438
+ function buildIndexSignatures(node, propertyCount, print) {
439
+ const elements = [];
440
+ if (node.additionalProperties && node.additionalProperties !== true) {
441
+ const additionalType = print(node.additionalProperties) ?? require_components.keywordTypeNodes.unknown;
442
+ elements.push(require_components.createIndexSignature(propertyCount > 0 ? require_components.keywordTypeNodes.unknown : additionalType));
443
+ } else if (node.additionalProperties === true) elements.push(require_components.createIndexSignature(require_components.keywordTypeNodes.unknown));
444
+ if (node.patternProperties) {
445
+ const first = Object.values(node.patternProperties)[0];
446
+ if (first) {
447
+ let patternType = print(first) ?? require_components.keywordTypeNodes.unknown;
448
+ if (first.nullable) patternType = require_components.createUnionDeclaration({ nodes: [patternType, require_components.keywordTypeNodes.null] });
449
+ elements.push(require_components.createIndexSignature(patternType));
450
+ }
451
+ }
452
+ return elements;
453
+ }
454
+ /**
455
+ * Converts a `SchemaNode` AST node into a TypeScript `ts.TypeNode`.
456
+ *
457
+ * Built on `definePrinter` — dispatches on `node.type`, with options closed over
458
+ * per printer instance. Produces the same `ts.TypeNode` output as the keyword-based
459
+ * `parse` in `parser.ts`.
460
+ */
461
+ const printerTs = (0, _kubb_core.definePrinter)((options) => ({
462
+ name: "typescript",
463
+ options,
464
+ nodes: {
465
+ any: () => require_components.keywordTypeNodes.any,
466
+ unknown: () => require_components.keywordTypeNodes.unknown,
467
+ void: () => require_components.keywordTypeNodes.void,
468
+ boolean: () => require_components.keywordTypeNodes.boolean,
469
+ null: () => require_components.keywordTypeNodes.null,
470
+ blob: () => require_components.createTypeReferenceNode("Blob", []),
471
+ string: () => require_components.keywordTypeNodes.string,
472
+ uuid: () => require_components.keywordTypeNodes.string,
473
+ email: () => require_components.keywordTypeNodes.string,
474
+ url: () => require_components.keywordTypeNodes.string,
475
+ datetime: () => require_components.keywordTypeNodes.string,
476
+ number: () => require_components.keywordTypeNodes.number,
477
+ integer: () => require_components.keywordTypeNodes.number,
478
+ bigint: () => require_components.keywordTypeNodes.bigint,
479
+ date: (node) => dateOrStringNode(node),
480
+ time: (node) => dateOrStringNode(node),
481
+ ref(node) {
482
+ if (!node.name) return;
483
+ return require_components.createTypeReferenceNode(node.name, void 0);
484
+ },
485
+ enum(node) {
486
+ const values = node.namedEnumValues?.map((v) => v.value) ?? node.enumValues ?? [];
487
+ if (this.options.enumType === "inlineLiteral" || !node.name) {
488
+ const literalNodes = values.filter((v) => v !== null).map((value) => constToTypeNode(value, typeof value === "number" ? "number" : typeof value === "boolean" ? "boolean" : "string")).filter(Boolean);
489
+ return require_components.createUnionDeclaration({
490
+ withParentheses: true,
491
+ nodes: literalNodes
492
+ }) ?? void 0;
493
+ }
494
+ const typeName = ["asConst", "asPascalConst"].includes(this.options.enumType) ? `${node.name}Key` : node.name;
495
+ return require_components.createTypeReferenceNode(typeName, void 0);
496
+ },
497
+ union(node) {
498
+ const members = node.members ?? [];
499
+ const hasStringLiteral = members.some((m) => m.type === "enum" && m.enumType === "string");
500
+ const hasPlainString = members.some((m) => (0, _kubb_ast.isPlainStringType)(m));
501
+ if (hasStringLiteral && hasPlainString) {
502
+ const nodes = members.map((m) => {
503
+ if ((0, _kubb_ast.isPlainStringType)(m)) return require_components.createIntersectionDeclaration({
504
+ nodes: [require_components.keywordTypeNodes.string, require_components.createTypeLiteralNode([])],
505
+ withParentheses: true
506
+ });
507
+ return this.print(m);
508
+ }).filter(Boolean);
509
+ return require_components.createUnionDeclaration({
510
+ withParentheses: true,
511
+ nodes
512
+ }) ?? void 0;
513
+ }
514
+ return require_components.createUnionDeclaration({
515
+ withParentheses: true,
516
+ nodes: buildMemberNodes(members, this.print)
517
+ }) ?? void 0;
518
+ },
519
+ intersection(node) {
520
+ return require_components.createIntersectionDeclaration({
521
+ withParentheses: true,
522
+ nodes: buildMemberNodes(node.members, this.print)
523
+ }) ?? void 0;
524
+ },
525
+ array(node) {
526
+ const itemNodes = (node.items ?? []).map((item) => this.print(item)).filter(Boolean);
527
+ return require_components.createArrayDeclaration({
528
+ nodes: itemNodes,
529
+ arrayType: this.options.arrayType
530
+ }) ?? void 0;
531
+ },
532
+ tuple(node) {
533
+ return buildTupleNode(node, this.print);
534
+ },
535
+ object(node) {
536
+ const addsQuestionToken = ["questionToken", "questionTokenAndUndefined"].includes(this.options.optionalType);
537
+ const { print } = this;
538
+ const propertyNodes = node.properties.map((prop) => {
539
+ const baseType = print(prop.schema) ?? require_components.keywordTypeNodes.unknown;
540
+ const type = buildPropertyType(prop.schema, baseType, this.options.optionalType);
541
+ const propertyNode = require_components.createPropertySignature({
542
+ questionToken: prop.schema.optional || prop.schema.nullish ? addsQuestionToken : false,
543
+ name: prop.name,
544
+ type,
545
+ readOnly: prop.schema.readOnly
546
+ });
547
+ return require_components.appendJSDocToNode({
548
+ node: propertyNode,
549
+ comments: buildPropertyJSDocComments(prop.schema)
550
+ });
551
+ });
552
+ const allElements = [...propertyNodes, ...buildIndexSignatures(node, propertyNodes.length, print)];
553
+ if (!allElements.length) return require_components.keywordTypeNodes.object;
554
+ return require_components.createTypeLiteralNode(allElements);
555
+ }
556
+ }
557
+ }));
558
+ //#endregion
559
+ //#region src/components/v2/Type.tsx
560
+ function Type({ name, typedName, node, keysToOmit, optionalType, arrayType, syntaxType, enumType, enumKeyCasing, ...rest }) {
561
+ const typeNodes = [];
562
+ const description = rest.description || node?.description;
563
+ const enumSchemaNodes = (0, _kubb_ast.collect)(node, { schema(n) {
564
+ if (n.type === "enum" && n.name) return n;
565
+ } });
566
+ let type = printerTs({
567
+ optionalType,
568
+ arrayType,
569
+ enumType
570
+ }).print(node);
571
+ if (!type) return;
572
+ if (["asConst", "asPascalConst"].includes(enumType) && enumSchemaNodes.length > 0) {
573
+ const isDirectEnum = node.type === "array" && node.items !== void 0;
574
+ const isEnumOnly = "enum" in node && node.enum;
575
+ if (isDirectEnum || isEnumOnly) {
576
+ const typeNameWithKey = `${enumSchemaNodes[0].name}Key`;
577
+ type = require_components.createTypeReferenceNode(typeNameWithKey);
578
+ if (isDirectEnum) if (arrayType === "generic") type = require_components.createTypeReferenceNode(require_components.createIdentifier("Array"), [type]);
579
+ else type = require_components.createArrayTypeNode(type);
580
+ }
581
+ }
582
+ if (node) {
583
+ if (node.nullable) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.null] });
584
+ if (node.nullish && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.undefined] });
585
+ if (node.optional && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) type = require_components.createUnionDeclaration({ nodes: [type, require_components.keywordTypeNodes.undefined] });
586
+ }
587
+ const useTypeGeneration = syntaxType === "type" || [require_components.syntaxKind.union].includes(type.kind) || !!keysToOmit?.length;
588
+ typeNodes.push(require_components.createTypeDeclaration({
589
+ name,
590
+ isExportable: true,
591
+ type: keysToOmit?.length ? require_components.createOmitDeclaration({
592
+ keys: keysToOmit,
593
+ type,
594
+ nonNullable: true
595
+ }) : type,
596
+ syntax: useTypeGeneration ? "type" : "interface",
597
+ comments: [
598
+ node?.title ? `${require_components.jsStringEscape(node.title)}` : void 0,
599
+ description ? `@description ${require_components.jsStringEscape(description)}` : void 0,
600
+ node?.deprecated ? "@deprecated" : void 0,
601
+ node && "min" in node && node.min !== void 0 ? `@minLength ${node.min}` : void 0,
602
+ node && "max" in node && node.max !== void 0 ? `@maxLength ${node.max}` : void 0,
603
+ node && "pattern" in node && node.pattern ? `@pattern ${node.pattern}` : void 0,
604
+ node?.default ? `@default ${node.default}` : void 0,
605
+ node?.example ? `@example ${node.example}` : void 0
606
+ ]
607
+ }));
608
+ const enums = [...new Map(enumSchemaNodes.map((n) => [n.name, n])).values()].map((enumSchemaNode) => {
609
+ const enumName = enumType === "asPascalConst" ? require_components.pascalCase(enumSchemaNode.name) : require_components.camelCase(enumSchemaNode.name);
610
+ const typeName = ["asConst", "asPascalConst"].includes(enumType) ? `${enumSchemaNode.name}Key` : enumSchemaNode.name;
611
+ const [nameNode, typeNode] = require_components.createEnumDeclaration({
612
+ name: enumName,
613
+ typeName,
614
+ enums: enumSchemaNode.namedEnumValues?.map((v) => [require_components.trimQuotes(v.name.toString()), v.value]) ?? enumSchemaNode.enumValues?.filter((v) => v !== null && v !== void 0).map((v) => [require_components.trimQuotes(v.toString()), v]) ?? [],
615
+ type: enumType,
616
+ enumKeyCasing
617
+ });
618
+ return {
619
+ nameNode,
620
+ typeNode,
621
+ name: enumName,
622
+ typeName
623
+ };
624
+ });
625
+ const shouldExportEnums = enumType !== "inlineLiteral";
626
+ const shouldExportType = enumType === "inlineLiteral" || enums.every((item) => item.typeName !== name);
627
+ return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric_jsx_runtime.Fragment, { children: [shouldExportEnums && enums.map(({ name: enumName, nameNode, typeName, typeNode }) => /* @__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, {
628
+ name: enumName,
629
+ isExportable: true,
630
+ isIndexable: true,
631
+ isTypeOnly: false,
632
+ children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(nameNode)
633
+ }), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
634
+ name: typeName,
635
+ isIndexable: true,
636
+ isExportable: [
637
+ "enum",
638
+ "asConst",
639
+ "asPascalConst",
640
+ "constEnum",
641
+ "literal",
642
+ void 0
643
+ ].includes(enumType),
644
+ isTypeOnly: [
645
+ "asConst",
646
+ "asPascalConst",
647
+ "literal",
648
+ void 0
649
+ ].includes(enumType),
650
+ children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(typeNode)
651
+ })] })), shouldExportType && /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Source, {
652
+ name: typedName,
653
+ isTypeOnly: true,
654
+ isExportable: true,
655
+ isIndexable: true,
656
+ children: (0, _kubb_fabric_core_parsers_typescript.safePrint)(...typeNodes)
657
+ })] });
658
+ }
659
+ //#endregion
660
+ //#region src/generators/v2/typeGenerator.tsx
661
+ const typeGenerator = (0, _kubb_core.defineGenerator)({
662
+ name: "typescript",
663
+ type: "react",
664
+ Operation({ node, adapter, options }) {
665
+ const { enumType, enumKeyCasing, optionalType, arrayType, syntaxType } = options;
666
+ const { plugin, mode, getFile, resolveName } = (0, _kubb_core_hooks.useKubb)();
667
+ const file = getFile({
668
+ name: node.operationId,
669
+ pluginName: plugin.name,
670
+ extname: ".ts",
671
+ mode
672
+ });
673
+ function renderSchemaType({ node: schemaNode, name, typedName, description }) {
674
+ const imports = adapter.getImports(schemaNode, (schemaName) => ({
675
+ name: resolveName({
676
+ name: schemaName,
677
+ pluginName: plugin.name,
678
+ type: "type"
679
+ }),
680
+ path: getFile({
681
+ name: schemaName,
682
+ pluginName: plugin.name,
683
+ extname: ".ts",
684
+ mode
685
+ }).path
686
+ }));
687
+ 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, {
688
+ root: file.path,
689
+ path: imp.path,
690
+ name: imp.name,
691
+ isTypeOnly: true
692
+ }, [
693
+ name,
694
+ imp.path,
695
+ imp.isTypeOnly
696
+ ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(Type, {
697
+ name,
698
+ typedName,
699
+ node: schemaNode,
700
+ description,
701
+ enumType,
702
+ enumKeyCasing,
703
+ optionalType,
704
+ arrayType,
705
+ syntaxType
706
+ })] });
707
+ }
708
+ const paramTypes = node.parameters.map((param) => {
709
+ const name = resolveName({
710
+ name: `${node.operationId} ${param.name}`,
711
+ pluginName: plugin.name,
712
+ type: "function"
713
+ });
714
+ const typedName = resolveName({
715
+ name: `${node.operationId} ${param.name}`,
716
+ pluginName: plugin.name,
717
+ type: "type"
718
+ });
719
+ return renderSchemaType({
720
+ node: param.schema,
721
+ name,
722
+ typedName
723
+ });
724
+ });
725
+ const responseTypes = node.responses.filter((res) => res.schema).map((res) => {
726
+ const schemaNode = res.schema;
727
+ const responseName = `${node.operationId} ${res.statusCode}`;
728
+ return renderSchemaType({
729
+ node: schemaNode,
730
+ name: resolveName({
731
+ name: responseName,
732
+ pluginName: plugin.name,
733
+ type: "function"
734
+ }),
735
+ typedName: resolveName({
736
+ name: responseName,
737
+ pluginName: plugin.name,
738
+ type: "type"
739
+ }),
740
+ description: res.description
741
+ });
742
+ });
743
+ const requestType = node.requestBody ? (() => {
744
+ const requestName = `${node.operationId} MutationRequest`;
745
+ const resolvedName = resolveName({
746
+ name: requestName,
747
+ pluginName: plugin.name,
748
+ type: "function"
749
+ });
750
+ const typedName = resolveName({
751
+ name: requestName,
752
+ pluginName: plugin.name,
753
+ type: "type"
754
+ });
755
+ return renderSchemaType({
756
+ node: node.requestBody,
757
+ name: resolvedName,
758
+ typedName,
759
+ description: node.requestBody.description
760
+ });
761
+ })() : null;
762
+ return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
763
+ baseName: file.baseName,
764
+ path: file.path,
765
+ meta: file.meta,
766
+ children: [
767
+ paramTypes,
768
+ responseTypes,
769
+ requestType
770
+ ]
771
+ });
772
+ },
773
+ Schema({ node, adapter, options }) {
774
+ const { enumType, enumKeyCasing, syntaxType, optionalType, arrayType } = options;
775
+ const { plugin, mode, resolveName, getFile } = (0, _kubb_core_hooks.useKubb)();
776
+ if (!node.name) return;
777
+ const imports = adapter.getImports(node, (schemaName) => ({
778
+ name: resolveName({
779
+ name: schemaName,
780
+ pluginName: plugin.name,
781
+ type: "type"
782
+ }),
783
+ path: getFile({
784
+ name: schemaName,
785
+ pluginName: plugin.name,
786
+ extname: ".ts",
787
+ mode
788
+ }).path
789
+ }));
790
+ const isEnumSchema = node.type === "enum";
791
+ let typedName = resolveName({
792
+ name: node.name,
793
+ pluginName: plugin.name,
794
+ type: "type"
795
+ });
796
+ if (["asConst", "asPascalConst"].includes(enumType) && isEnumSchema) typedName = typedName += "Key";
797
+ const type = {
798
+ name: resolveName({
799
+ name: node.name,
800
+ pluginName: plugin.name,
801
+ type: "function"
802
+ }),
803
+ typedName,
804
+ file: getFile({
805
+ name: node.name,
806
+ pluginName: plugin.name,
807
+ extname: ".ts",
808
+ mode
809
+ })
810
+ };
811
+ return /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsxs)(_kubb_react_fabric.File, {
812
+ baseName: type.file.baseName,
813
+ path: type.file.path,
814
+ meta: type.file.meta,
815
+ children: [mode === "split" && imports.map((imp) => /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(_kubb_react_fabric.File.Import, {
816
+ root: type.file.path,
817
+ path: imp.path,
818
+ name: imp.name,
819
+ isTypeOnly: true
820
+ }, [
821
+ node.name,
822
+ imp.path,
823
+ imp.isTypeOnly
824
+ ].join("-"))), /* @__PURE__ */ (0, _kubb_react_fabric_jsx_runtime.jsx)(Type, {
825
+ name: type.name,
826
+ typedName: type.typedName,
827
+ node,
828
+ enumType,
829
+ enumKeyCasing,
830
+ optionalType,
831
+ arrayType,
832
+ syntaxType
833
+ })]
834
+ });
835
+ }
836
+ });
837
+ //#endregion
838
+ //#region src/plugin.ts
839
+ const pluginTsName = "plugin-ts";
840
+ const pluginTs = (0, _kubb_core.definePlugin)((options) => {
841
+ const { output = {
842
+ path: "types",
843
+ barrelType: "named"
844
+ }, 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 = {}, mapper = {}, paramsCasing, generators = [typeGenerator$1, typeGenerator].filter(Boolean), contentType, UNSTABLE_NAMING } = options;
845
+ return {
846
+ name: pluginTsName,
847
+ options: {
848
+ output,
849
+ transformers,
850
+ dateType,
851
+ integerType,
852
+ optionalType,
853
+ arrayType,
854
+ enumType,
855
+ enumKeyCasing,
856
+ enumSuffix,
857
+ unknownType,
858
+ emptySchemaType,
859
+ syntaxType,
860
+ group,
861
+ override,
862
+ mapper,
863
+ paramsCasing,
864
+ usedEnumNames: {}
865
+ },
866
+ pre: [_kubb_plugin_oas.pluginOasName],
867
+ resolvePath(baseName, pathMode, options) {
868
+ const root = node_path.default.resolve(this.config.root, this.config.output.path);
869
+ if ((pathMode ?? (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path))) === "single")
870
+ /**
871
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
872
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
873
+ */
874
+ return node_path.default.resolve(root, output.path);
875
+ if (group && (options?.group?.path || options?.group?.tag)) {
876
+ const groupName = group?.name ? group.name : (ctx) => {
877
+ if (group?.type === "path") return `${ctx.group.split("/")[1]}`;
878
+ return `${require_components.camelCase(ctx.group)}Controller`;
879
+ };
880
+ return node_path.default.resolve(root, output.path, groupName({ group: group.type === "path" ? options.group.path : options.group.tag }), baseName);
881
+ }
882
+ return node_path.default.resolve(root, output.path, baseName);
883
+ },
884
+ resolveName(name, type) {
885
+ const resolvedName = require_components.pascalCase(name, { isFile: type === "file" });
886
+ if (type) return transformers?.name?.(resolvedName, type) || resolvedName;
887
+ return resolvedName;
888
+ },
889
+ async install() {
890
+ const { config, fabric, plugin, adapter, rootNode, pluginManager, openInStudio } = this;
891
+ const root = node_path.default.resolve(config.root, config.output.path);
892
+ const mode = (0, _kubb_core.getMode)(node_path.default.resolve(root, output.path));
893
+ if (adapter) {
894
+ await openInStudio({ ast: true });
895
+ await (0, _kubb_ast.walk)(rootNode, {
896
+ async schema(schemaNode) {
897
+ const writeTasks = generators.map(async (generator) => {
898
+ if (generator.type === "react" && generator.version === "2") {
899
+ const options = (0, _kubb_core.resolveOptions)(schemaNode, {
900
+ options: plugin.options,
901
+ exclude,
902
+ include,
903
+ override
904
+ });
905
+ if (options === null) return;
906
+ await (0, _kubb_plugin_oas.buildSchema)(schemaNode, {
907
+ options,
908
+ adapter,
909
+ config,
910
+ fabric,
911
+ Component: generator.Schema,
912
+ plugin,
913
+ pluginManager,
914
+ mode,
915
+ version: generator.version
916
+ });
917
+ }
918
+ });
919
+ await Promise.all(writeTasks);
920
+ },
921
+ async operation(operationNode) {
922
+ const writeTasks = generators.map(async (generator) => {
923
+ if (generator.type === "react" && generator.version === "2") {
924
+ const options = (0, _kubb_core.resolveOptions)(operationNode, {
925
+ options: plugin.options,
926
+ exclude,
927
+ include,
928
+ override
929
+ });
930
+ if (options === null) return;
931
+ await (0, _kubb_plugin_oas.buildOperation)(operationNode, {
932
+ options,
933
+ adapter,
934
+ config,
935
+ fabric,
936
+ Component: generator.Operation,
937
+ plugin,
938
+ pluginManager,
939
+ mode,
940
+ version: generator.version
941
+ });
942
+ }
943
+ });
944
+ await Promise.all(writeTasks);
945
+ }
946
+ }, { depth: "shallow" });
947
+ return;
948
+ }
949
+ const oas = await this.getOas();
950
+ const schemaFiles = await new _kubb_plugin_oas.SchemaGenerator(this.plugin.options, {
951
+ fabric: this.fabric,
952
+ oas,
953
+ pluginManager: this.pluginManager,
954
+ events: this.events,
955
+ plugin: this.plugin,
956
+ contentType,
957
+ include: void 0,
958
+ override,
959
+ mode,
960
+ output: output.path
961
+ }).build(...generators);
962
+ await this.upsertFile(...schemaFiles);
963
+ const operationFiles = await new _kubb_plugin_oas.OperationGenerator(this.plugin.options, {
964
+ fabric: this.fabric,
965
+ oas,
966
+ pluginManager: this.pluginManager,
967
+ events: this.events,
968
+ plugin: this.plugin,
969
+ contentType,
970
+ exclude,
971
+ include,
972
+ override,
973
+ mode,
974
+ UNSTABLE_NAMING
975
+ }).build(...generators);
976
+ await this.upsertFile(...operationFiles);
977
+ const barrelFiles = await (0, _kubb_core.getBarrelFiles)(this.fabric.files, {
978
+ type: output.barrelType ?? "named",
979
+ root,
980
+ output,
981
+ meta: { pluginName: this.plugin.name }
982
+ });
983
+ await this.upsertFile(...barrelFiles);
984
+ }
985
+ };
986
+ });
987
+ //#endregion
988
+ Object.defineProperty(exports, "pluginTs", {
989
+ enumerable: true,
990
+ get: function() {
991
+ return pluginTs;
992
+ }
993
+ });
994
+ Object.defineProperty(exports, "pluginTsName", {
995
+ enumerable: true,
996
+ get: function() {
997
+ return pluginTsName;
998
+ }
999
+ });
1000
+ Object.defineProperty(exports, "typeGenerator", {
1001
+ enumerable: true,
1002
+ get: function() {
1003
+ return typeGenerator;
1004
+ }
1005
+ });
1006
+ Object.defineProperty(exports, "typeGenerator$1", {
1007
+ enumerable: true,
1008
+ get: function() {
1009
+ return typeGenerator$1;
1010
+ }
1011
+ });
1012
+
1013
+ //# sourceMappingURL=plugin-DvK-Uhvv.cjs.map