@kubb/plugin-ts 5.0.0-alpha.1 → 5.0.0-alpha.3

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