@kubb/swagger-ts 2.10.0 → 2.11.0

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.
@@ -1,823 +0,0 @@
1
- var __accessCheck = (obj, member, msg) => {
2
- if (!member.has(obj))
3
- throw TypeError("Cannot " + msg);
4
- };
5
- var __privateGet = (obj, member, getter) => {
6
- __accessCheck(obj, member, "read from private field");
7
- return getter ? getter.call(obj) : member.get(obj);
8
- };
9
- var __privateAdd = (obj, member, value) => {
10
- if (member.has(obj))
11
- throw TypeError("Cannot add the same private member more than once");
12
- member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
13
- };
14
- var __privateMethod = (obj, member, method) => {
15
- __accessCheck(obj, member, "access private method");
16
- return method;
17
- };
18
-
19
- // src/components/Mutation.tsx
20
- import transformers4 from "@kubb/core/transformers";
21
- import { print as print3 } from "@kubb/parser";
22
- import * as factory3 from "@kubb/parser/factory";
23
- import { Editor as Editor3, File as File3, usePlugin as usePlugin3, usePluginManager as usePluginManager2 } from "@kubb/react";
24
- import { useOas as useOas3, useOperation as useOperation2, useOperationFile as useOperationFile2, useOperationName as useOperationName2, useSchemas as useSchemas2 } from "@kubb/swagger/hooks";
25
-
26
- // src/TypeBuilder.ts
27
- import transformers3 from "@kubb/core/transformers";
28
- import { print as print2 } from "@kubb/parser";
29
- import { OasBuilder } from "@kubb/swagger";
30
- import { refsSorter } from "@kubb/swagger/utils";
31
-
32
- // src/TypeGenerator.ts
33
- import { Generator as Generator2 } from "@kubb/core";
34
- import transformers2, { pascalCase as pascalCase2 } from "@kubb/core/transformers";
35
- import { getUniqueName } from "@kubb/core/utils";
36
- import * as factory2 from "@kubb/parser/factory";
37
- import { keywordTypeNodes as keywordTypeNodes2 } from "@kubb/parser/factory";
38
- import { getSchemaFactory, isReference } from "@kubb/swagger/utils";
39
-
40
- // src/plugin.ts
41
- import path from "path";
42
- import { createPlugin, FileManager, PluginManager } from "@kubb/core";
43
- import { camelCase, pascalCase } from "@kubb/core/transformers";
44
- import { renderTemplate } from "@kubb/core/utils";
45
- import { pluginName as swaggerPluginName } from "@kubb/swagger";
46
-
47
- // src/OperationGenerator.tsx
48
- import { createRoot } from "@kubb/react";
49
- import { OperationGenerator as Generator } from "@kubb/swagger";
50
- import { Oas } from "@kubb/swagger/components";
51
-
52
- // src/components/OasType.tsx
53
- import { Editor, File, Type, usePlugin } from "@kubb/react";
54
- import { useFile } from "@kubb/react";
55
- import { useOas } from "@kubb/swagger/hooks";
56
- import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime";
57
- function Template({
58
- name,
59
- typeName,
60
- api
61
- }) {
62
- return /* @__PURE__ */ jsxs(Fragment, { children: [
63
- `export const ${name} = ${JSON.stringify(api, void 0, 2)} as const`,
64
- /* @__PURE__ */ jsx("br", {}),
65
- /* @__PURE__ */ jsx(Type, { name: typeName, export: true, children: `Infer<typeof ${name}>` })
66
- ] });
67
- }
68
- var defaultTemplates = { default: Template };
69
- function OasType({
70
- name,
71
- typeName,
72
- Template: Template2 = defaultTemplates.default
73
- }) {
74
- const oas = useOas();
75
- return /* @__PURE__ */ jsx(Template2, { name, typeName, api: oas.api });
76
- }
77
- OasType.File = function({ name, typeName, templates = defaultTemplates }) {
78
- const { key: pluginKey2 } = usePlugin();
79
- const file = useFile({ name, extName: ".ts", pluginKey: pluginKey2 });
80
- const Template2 = templates.default;
81
- return /* @__PURE__ */ jsx(Editor, { language: "typescript", children: /* @__PURE__ */ jsxs(
82
- File,
83
- {
84
- baseName: file.baseName,
85
- path: file.path,
86
- meta: file.meta,
87
- children: [
88
- /* @__PURE__ */ jsx(File.Import, { name: ["Infer"], path: "@kubb/swagger-ts/oas", isTypeOnly: true }),
89
- /* @__PURE__ */ jsx(File.Source, { children: /* @__PURE__ */ jsx(OasType, { Template: Template2, name, typeName }) })
90
- ]
91
- }
92
- ) });
93
- };
94
- OasType.templates = defaultTemplates;
95
-
96
- // src/components/Query.tsx
97
- import transformers from "@kubb/core/transformers";
98
- import { print } from "@kubb/parser";
99
- import * as factory from "@kubb/parser/factory";
100
- import { Editor as Editor2, File as File2, usePlugin as usePlugin2, usePluginManager } from "@kubb/react";
101
- import { useOas as useOas2, useOperation, useOperationFile, useOperationName, useSchemas } from "@kubb/swagger/hooks";
102
- import { Fragment as Fragment2, jsx as jsx2, jsxs as jsxs2 } from "@kubb/react/jsx-runtime";
103
- function printCombinedSchema(name, operation, schemas) {
104
- const properties = {
105
- "response": factory.createTypeReferenceNode(
106
- factory.createIdentifier(schemas.response.name),
107
- void 0
108
- )
109
- };
110
- if (schemas.request) {
111
- properties["request"] = factory.createTypeReferenceNode(
112
- factory.createIdentifier(schemas.request.name),
113
- void 0
114
- );
115
- }
116
- if (schemas.pathParams) {
117
- properties["pathParams"] = factory.createTypeReferenceNode(
118
- factory.createIdentifier(schemas.pathParams.name),
119
- void 0
120
- );
121
- }
122
- if (schemas.queryParams) {
123
- properties["queryParams"] = factory.createTypeReferenceNode(
124
- factory.createIdentifier(schemas.queryParams.name),
125
- void 0
126
- );
127
- }
128
- if (schemas.headerParams) {
129
- properties["headerParams"] = factory.createTypeReferenceNode(
130
- factory.createIdentifier(schemas.headerParams.name),
131
- void 0
132
- );
133
- }
134
- if (schemas.errors) {
135
- properties["errors"] = factory.createUnionDeclaration({
136
- nodes: schemas.errors.map((error) => {
137
- return factory.createTypeReferenceNode(
138
- factory.createIdentifier(error.name),
139
- void 0
140
- );
141
- })
142
- });
143
- }
144
- const namespaceNode = factory.createTypeAliasDeclaration({
145
- name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
146
- type: factory.createTypeLiteralNode(
147
- Object.keys(properties).map((key) => {
148
- const type = properties[key];
149
- if (!type) {
150
- return void 0;
151
- }
152
- return factory.createPropertySignature(
153
- {
154
- name: transformers.pascalCase(key),
155
- type
156
- }
157
- );
158
- }).filter(Boolean)
159
- ),
160
- modifiers: [factory.modifiers.export]
161
- });
162
- return print(namespaceNode);
163
- }
164
- function Query({
165
- builder
166
- }) {
167
- const { source } = builder.build();
168
- return /* @__PURE__ */ jsx2(Fragment2, { children: source });
169
- }
170
- Query.File = function({ mode }) {
171
- const plugin = usePlugin2();
172
- const schemas = useSchemas();
173
- const pluginManager = usePluginManager();
174
- const oas = useOas2();
175
- const file = useOperationFile();
176
- const factoryName = useOperationName({ type: "type" });
177
- const operation = useOperation();
178
- const builder = new TypeBuilder(plugin.options, { oas, plugin, pluginManager }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.response).add(schemas.statusCodes);
179
- const { source, imports } = builder.build();
180
- return /* @__PURE__ */ jsx2(Editor2, { language: "typescript", children: /* @__PURE__ */ jsxs2(
181
- File2,
182
- {
183
- baseName: file.baseName,
184
- path: file.path,
185
- meta: file.meta,
186
- children: [
187
- mode === "directory" && imports.map((item, index) => {
188
- return /* @__PURE__ */ jsx2(File2.Import, { root: file.path, ...item }, index);
189
- }),
190
- /* @__PURE__ */ jsxs2(File2.Source, { children: [
191
- source,
192
- printCombinedSchema(factoryName, operation, schemas)
193
- ] })
194
- ]
195
- }
196
- ) });
197
- };
198
-
199
- // src/OperationGenerator.tsx
200
- import { jsx as jsx3 } from "@kubb/react/jsx-runtime";
201
- var OperationGenerator = class extends Generator {
202
- async all(operations) {
203
- const { oas, pluginManager, plugin } = this.context;
204
- const root = createRoot({ logger: pluginManager.logger });
205
- root.render(
206
- /* @__PURE__ */ jsx3(Oas, { oas, operations, getSchemas: (...props) => this.getSchemas(...props), children: plugin.options.oasType && /* @__PURE__ */ jsx3(OasType.File, { name: "oas", typeName: "Oas" }) }),
207
- { meta: { pluginManager, plugin } }
208
- );
209
- return root.files;
210
- }
211
- async get(operation, options) {
212
- const { oas, pluginManager, plugin, mode = "directory" } = this.context;
213
- const root = createRoot({ logger: pluginManager.logger });
214
- root.render(
215
- /* @__PURE__ */ jsx3(Oas, { oas, operations: [operation], getSchemas: (...props) => this.getSchemas(...props), children: /* @__PURE__ */ jsx3(Oas.Operation, { operation, children: /* @__PURE__ */ jsx3(Query.File, { mode }) }) }),
216
- { meta: { pluginManager, plugin: { ...plugin, options } } }
217
- );
218
- return root.files;
219
- }
220
- async post(operation, options) {
221
- const { oas, pluginManager, plugin, mode = "directory" } = this.context;
222
- const root = createRoot({ logger: pluginManager.logger });
223
- root.render(
224
- /* @__PURE__ */ jsx3(Oas, { oas, operations: [operation], getSchemas: (...props) => this.getSchemas(...props), children: /* @__PURE__ */ jsx3(Oas.Operation, { operation, children: /* @__PURE__ */ jsx3(Mutation.File, { mode }) }) }),
225
- { meta: { pluginManager, plugin: { ...plugin, options } } }
226
- );
227
- return root.files;
228
- }
229
- async put(operation, options) {
230
- return this.post(operation, options);
231
- }
232
- async patch(operation, options) {
233
- return this.post(operation, options);
234
- }
235
- async delete(operation, options) {
236
- return this.post(operation, options);
237
- }
238
- };
239
-
240
- // src/plugin.ts
241
- var pluginName = "swagger-ts";
242
- var pluginKey = [pluginName];
243
- var definePlugin = createPlugin((options) => {
244
- const {
245
- output = { path: "types" },
246
- group,
247
- exclude = [],
248
- include,
249
- override = [],
250
- enumType = "asConst",
251
- enumSuffix = "",
252
- dateType = "string",
253
- unknownType = "any",
254
- optionalType = "questionToken",
255
- transformers: transformers5 = {},
256
- oasType = false
257
- } = options;
258
- const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
259
- return {
260
- name: pluginName,
261
- options: {
262
- transformers: transformers5,
263
- dateType,
264
- enumType,
265
- enumSuffix,
266
- optionalType,
267
- oasType,
268
- // keep the used enumnames between TypeBuilder and OperationGenerator per plugin(pluginKey)
269
- usedEnumNames: {},
270
- unknownType
271
- },
272
- pre: [swaggerPluginName],
273
- resolvePath(baseName, pathMode, options2) {
274
- const root = path.resolve(this.config.root, this.config.output.path);
275
- const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
276
- if (mode === "file") {
277
- return path.resolve(root, output.path);
278
- }
279
- if (options2?.tag && group?.type === "tag") {
280
- const tag = camelCase(options2.tag);
281
- return path.resolve(root, renderTemplate(template, { tag }), baseName);
282
- }
283
- return path.resolve(root, output.path, baseName);
284
- },
285
- resolveName(name, type) {
286
- const resolvedName = pascalCase(name, { isFile: type === "file" });
287
- if (type) {
288
- return transformers5?.name?.(resolvedName, type) || resolvedName;
289
- }
290
- return resolvedName;
291
- },
292
- async writeFile(source, writePath) {
293
- if (!writePath.endsWith(".ts") || !source) {
294
- return;
295
- }
296
- return this.fileManager.write(source, writePath, { sanity: false });
297
- },
298
- async buildStart() {
299
- const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [swaggerPluginName]);
300
- const oas = await swaggerPlugin.api.getOas();
301
- const schemas = await swaggerPlugin.api.getSchemas();
302
- const root = path.resolve(this.config.root, this.config.output.path);
303
- const mode = FileManager.getMode(path.resolve(root, output.path));
304
- const builder = new TypeBuilder(this.plugin.options, { oas, plugin: this.plugin, pluginManager: this.pluginManager });
305
- builder.add(
306
- Object.entries(schemas).map(([name, schema]) => ({ name, schema }))
307
- );
308
- if (mode === "directory") {
309
- const mapFolderSchema = async ([name]) => {
310
- const baseName = `${this.resolveName({ name, pluginKey: this.plugin.key, type: "file" })}.ts`;
311
- const resolvedPath = this.resolvePath({ baseName, pluginKey: this.plugin.key });
312
- const { source, imports } = builder.build(name);
313
- if (!resolvedPath) {
314
- return null;
315
- }
316
- return this.addFile({
317
- path: resolvedPath,
318
- baseName,
319
- source,
320
- imports: imports.map((item) => ({ ...item, root: resolvedPath })),
321
- meta: {
322
- pluginKey: this.plugin.key
323
- }
324
- });
325
- };
326
- const promises = Object.entries(schemas).map(mapFolderSchema);
327
- await Promise.all(promises);
328
- }
329
- if (mode === "file") {
330
- const resolvedPath = this.resolvePath({ baseName: "", pluginKey: this.plugin.key });
331
- const { source } = builder.build();
332
- if (!resolvedPath) {
333
- return;
334
- }
335
- await this.addFile({
336
- path: resolvedPath,
337
- baseName: output.path,
338
- source,
339
- imports: [],
340
- meta: {
341
- pluginKey: this.plugin.key
342
- }
343
- });
344
- }
345
- const operationGenerator = new OperationGenerator(
346
- this.plugin.options,
347
- {
348
- oas,
349
- pluginManager: this.pluginManager,
350
- plugin: this.plugin,
351
- contentType: swaggerPlugin.api.contentType,
352
- exclude,
353
- include,
354
- override,
355
- mode
356
- }
357
- );
358
- const files = await operationGenerator.build();
359
- await this.addFile(...files);
360
- },
361
- async buildEnd() {
362
- if (this.config.output.write === false) {
363
- return;
364
- }
365
- const root = path.resolve(this.config.root, this.config.output.path);
366
- await this.fileManager.addIndexes({
367
- root,
368
- output,
369
- meta: { pluginKey: this.plugin.key }
370
- });
371
- }
372
- };
373
- });
374
-
375
- // src/TypeGenerator.ts
376
- var _usedAliasNames, _getTypeFromProperties, getTypeFromProperties_fn, _getRefAlias, getRefAlias_fn, _getParsedSchema, getParsedSchema_fn, _getBaseTypeFromSchema, getBaseTypeFromSchema_fn, _unknownReturn, unknownReturn_get;
377
- var TypeGenerator = class extends Generator2 {
378
- constructor() {
379
- super(...arguments);
380
- /**
381
- * Recursively creates a type literal with the given props.
382
- */
383
- __privateAdd(this, _getTypeFromProperties);
384
- /**
385
- * Create a type alias for the schema referenced by the given ReferenceObject
386
- */
387
- __privateAdd(this, _getRefAlias);
388
- __privateAdd(this, _getParsedSchema);
389
- /**
390
- * This is the very core of the OpenAPI to TS conversion - it takes a
391
- * schema and returns the appropriate type.
392
- */
393
- __privateAdd(this, _getBaseTypeFromSchema);
394
- __privateAdd(this, _unknownReturn);
395
- this.refs = {};
396
- this.imports = [];
397
- this.extraNodes = [];
398
- this.aliases = [];
399
- // Keep track of already used type aliases
400
- __privateAdd(this, _usedAliasNames, {});
401
- }
402
- build({
403
- schema,
404
- optional,
405
- baseName,
406
- description,
407
- keysToOmit
408
- }) {
409
- const nodes = [];
410
- let type = this.getTypeFromSchema(schema, baseName);
411
- if (!type) {
412
- return this.extraNodes;
413
- }
414
- if (optional) {
415
- type = factory2.createUnionDeclaration({ nodes: [type, factory2.keywordTypeNodes.undefined] });
416
- }
417
- const node = factory2.createTypeAliasDeclaration({
418
- modifiers: [factory2.modifiers.export],
419
- name: this.context.pluginManager.resolveName({ name: baseName, pluginKey, type: "type" }),
420
- type: keysToOmit?.length ? factory2.createOmitDeclaration({ keys: keysToOmit, type, nonNullable: true }) : type
421
- });
422
- if (description) {
423
- nodes.push(
424
- factory2.appendJSDocToNode({
425
- node,
426
- comments: [`@description ${transformers2.trim(description)}`]
427
- })
428
- );
429
- } else {
430
- nodes.push(node);
431
- }
432
- const filterdNodes = nodes.filter(
433
- (node2) => !this.extraNodes.some(
434
- (extraNode) => extraNode?.name?.escapedText === node2?.name?.escapedText
435
- )
436
- );
437
- return [...this.extraNodes, ...filterdNodes];
438
- }
439
- /**
440
- * Creates a type node from a given schema.
441
- * Delegates to getBaseTypeFromSchema internally and
442
- * optionally adds a union with null.
443
- */
444
- getTypeFromSchema(schema, name) {
445
- const type = __privateMethod(this, _getBaseTypeFromSchema, getBaseTypeFromSchema_fn).call(this, schema, name);
446
- if (!type) {
447
- return null;
448
- }
449
- const nullable = schema?.nullable ?? schema?.["x-nullable"] ?? false;
450
- if (nullable) {
451
- return factory2.createUnionDeclaration({ nodes: [type, factory2.keywordTypeNodes.null] });
452
- }
453
- return type;
454
- }
455
- };
456
- _usedAliasNames = new WeakMap();
457
- _getTypeFromProperties = new WeakSet();
458
- getTypeFromProperties_fn = function(baseSchema, baseName) {
459
- const { optionalType } = this.options;
460
- const properties = baseSchema?.properties || {};
461
- const required = baseSchema?.required;
462
- const additionalProperties = baseSchema?.additionalProperties;
463
- const members = Object.keys(properties).map((name) => {
464
- const schema = properties[name];
465
- const isRequired = Array.isArray(required) ? required.includes(name) : !!required;
466
- let type = this.getTypeFromSchema(schema, this.context.pluginManager.resolveName({ name: `${baseName || ""} ${name}`, pluginKey, type: "type" }));
467
- if (!type) {
468
- return null;
469
- }
470
- if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
471
- type = factory2.createUnionDeclaration({ nodes: [type, factory2.keywordTypeNodes.undefined] });
472
- }
473
- const propertySignature = factory2.createPropertySignature({
474
- questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
475
- name,
476
- type,
477
- readOnly: schema.readOnly
478
- });
479
- return factory2.appendJSDocToNode({
480
- node: propertySignature,
481
- comments: [
482
- schema.description ? `@description ${schema.description}` : void 0,
483
- schema.type ? `@type ${schema.type?.toString()}${isRequired ? "" : " | undefined"} ${schema.format || ""}` : void 0,
484
- schema.example ? `@example ${schema.example}` : void 0,
485
- schema.deprecated ? `@deprecated` : void 0,
486
- schema.default !== void 0 && typeof schema.default === "string" ? `@default '${schema.default}'` : void 0,
487
- schema.default !== void 0 && typeof schema.default !== "string" ? `@default ${schema.default}` : void 0
488
- ].filter(Boolean)
489
- });
490
- });
491
- if (additionalProperties) {
492
- const type = additionalProperties === true ? __privateGet(this, _unknownReturn, unknownReturn_get) : this.getTypeFromSchema(additionalProperties);
493
- if (type) {
494
- members.push(factory2.createIndexSignature(type));
495
- }
496
- }
497
- return factory2.createTypeLiteralNode(members.filter(Boolean));
498
- };
499
- _getRefAlias = new WeakSet();
500
- getRefAlias_fn = function(obj, _baseName) {
501
- const { $ref } = obj;
502
- let ref = this.refs[$ref];
503
- if (ref) {
504
- return factory2.createTypeReferenceNode(ref.propertyName, void 0);
505
- }
506
- const originalName = getUniqueName($ref.replace(/.+\//, ""), __privateGet(this, _usedAliasNames));
507
- const propertyName = this.context.pluginManager.resolveName({ name: originalName, pluginKey, type: "type" });
508
- ref = this.refs[$ref] = {
509
- propertyName,
510
- originalName
511
- };
512
- const fileName = this.context.pluginManager.resolveName({ name: originalName, pluginKey, type: "file" });
513
- const path2 = this.context.pluginManager.resolvePath({ baseName: fileName, pluginKey });
514
- this.imports.push({
515
- ref,
516
- path: path2 || "",
517
- isTypeOnly: true
518
- });
519
- return factory2.createTypeReferenceNode(ref.propertyName, void 0);
520
- };
521
- _getParsedSchema = new WeakSet();
522
- getParsedSchema_fn = function(schema) {
523
- const parsedSchema = getSchemaFactory(this.context.oas)(schema);
524
- return parsedSchema;
525
- };
526
- _getBaseTypeFromSchema = new WeakSet();
527
- getBaseTypeFromSchema_fn = function(_schema, baseName) {
528
- const { schema, version } = __privateMethod(this, _getParsedSchema, getParsedSchema_fn).call(this, _schema);
529
- if (!schema) {
530
- return __privateGet(this, _unknownReturn, unknownReturn_get);
531
- }
532
- if (isReference(schema)) {
533
- return __privateMethod(this, _getRefAlias, getRefAlias_fn).call(this, schema, baseName);
534
- }
535
- if (schema.oneOf) {
536
- const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
537
- const union = factory2.createUnionDeclaration({
538
- withParentheses: true,
539
- nodes: schema.oneOf.map((item) => {
540
- return item && this.getTypeFromSchema(item);
541
- }).filter((item) => {
542
- return item && item !== __privateGet(this, _unknownReturn, unknownReturn_get);
543
- })
544
- });
545
- if (schemaWithoutOneOf.properties) {
546
- return factory2.createIntersectionDeclaration({
547
- nodes: [this.getTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean)
548
- });
549
- }
550
- return union;
551
- }
552
- if (schema.anyOf) {
553
- const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
554
- const union = factory2.createUnionDeclaration({
555
- withParentheses: true,
556
- nodes: schema.anyOf.map((item) => {
557
- return item && this.getTypeFromSchema(item);
558
- }).filter((item) => {
559
- return item && item !== __privateGet(this, _unknownReturn, unknownReturn_get);
560
- })
561
- });
562
- if (schemaWithoutAnyOf.properties) {
563
- return factory2.createIntersectionDeclaration({
564
- nodes: [this.getTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean)
565
- });
566
- }
567
- return union;
568
- }
569
- if (schema.allOf) {
570
- const schemaWithoutAllOf = { ...schema, allOf: void 0 };
571
- const and = factory2.createIntersectionDeclaration({
572
- withParentheses: true,
573
- nodes: schema.allOf.map((item) => {
574
- return item && this.getTypeFromSchema(item);
575
- }).filter((item) => {
576
- return item && item !== __privateGet(this, _unknownReturn, unknownReturn_get);
577
- })
578
- });
579
- if (schemaWithoutAllOf.properties) {
580
- return factory2.createIntersectionDeclaration({
581
- nodes: [this.getTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean)
582
- });
583
- }
584
- return and;
585
- }
586
- if (schema.enum && baseName) {
587
- const enumName = getUniqueName(pascalCase2([baseName, this.options.enumSuffix].join(" ")), this.options.usedEnumNames);
588
- let enums = [...new Set(schema.enum)].map((key) => [key, key]);
589
- const extensionEnums = ["x-enumNames", "x-enum-varnames"].filter((extensionKey) => extensionKey in schema).map(
590
- (extensionKey) => [...new Set(schema[extensionKey])].map((key, index) => [key, schema.enum?.[index]])
591
- );
592
- if (extensionEnums.length > 0 && extensionEnums[0]) {
593
- enums = extensionEnums[0];
594
- }
595
- this.extraNodes.push(
596
- ...factory2.createEnumDeclaration({
597
- name: transformers2.camelCase(enumName),
598
- typeName: this.context.pluginManager.resolveName({ name: enumName, pluginKey, type: "type" }),
599
- enums,
600
- type: this.options.enumType
601
- })
602
- );
603
- return factory2.createTypeReferenceNode(this.context.pluginManager.resolveName({ name: enumName, pluginKey, type: "type" }), void 0);
604
- }
605
- if (schema.enum) {
606
- return factory2.createUnionDeclaration({
607
- nodes: schema.enum.map((name) => {
608
- return factory2.createLiteralTypeNode(typeof name === "number" ? factory2.createNumericLiteral(name) : factory2.createStringLiteral(`${name}`));
609
- })
610
- });
611
- }
612
- if ("items" in schema) {
613
- const node = this.getTypeFromSchema(schema.items, baseName);
614
- if (node) {
615
- return factory2.createArrayTypeNode(node);
616
- }
617
- }
618
- if ("prefixItems" in schema) {
619
- const prefixItems = schema.prefixItems;
620
- return factory2.createTupleDeclaration({
621
- nodes: prefixItems.map((item) => {
622
- return this.getTypeFromSchema(item, void 0);
623
- })
624
- });
625
- }
626
- if (schema.properties || schema.additionalProperties) {
627
- return __privateMethod(this, _getTypeFromProperties, getTypeFromProperties_fn).call(this, schema, baseName);
628
- }
629
- if (version === "3.1" && "const" in schema) {
630
- if (schema["const"]) {
631
- if (typeof schema["const"] === "string") {
632
- return factory2.createLiteralTypeNode(factory2.createStringLiteral(schema["const"]));
633
- } else if (typeof schema["const"] === "number") {
634
- return factory2.createLiteralTypeNode(factory2.createNumericLiteral(schema["const"]));
635
- }
636
- } else {
637
- return keywordTypeNodes2.null;
638
- }
639
- }
640
- if (schema.type) {
641
- if (Array.isArray(schema.type)) {
642
- const [type, nullable] = schema.type;
643
- return factory2.createUnionDeclaration({
644
- nodes: [
645
- this.getTypeFromSchema(
646
- {
647
- ...schema,
648
- type
649
- },
650
- baseName
651
- ),
652
- nullable ? factory2.createLiteralTypeNode(factory2.createNull()) : void 0
653
- ].filter(Boolean)
654
- });
655
- }
656
- if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
657
- return factory2.createTypeReferenceNode(factory2.createIdentifier("Date"));
658
- }
659
- if (schema.type in factory2.keywordTypeNodes) {
660
- return factory2.keywordTypeNodes[schema.type];
661
- }
662
- }
663
- if (schema.format === "binary") {
664
- return factory2.createTypeReferenceNode("Blob", []);
665
- }
666
- return __privateGet(this, _unknownReturn, unknownReturn_get);
667
- };
668
- _unknownReturn = new WeakSet();
669
- unknownReturn_get = function() {
670
- if (this.options.unknownType === "any") {
671
- return factory2.keywordTypeNodes.any;
672
- }
673
- return factory2.keywordTypeNodes.unknown;
674
- };
675
-
676
- // src/TypeBuilder.ts
677
- var TypeBuilder = class extends OasBuilder {
678
- build(name) {
679
- const importMeta = [];
680
- const codes = [];
681
- const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(transformers3.nameSorter).map((operationSchema) => {
682
- const generator = new TypeGenerator(this.options, this.context);
683
- const required = Array.isArray(operationSchema.schema?.required) ? !!operationSchema.schema.required.length : !!operationSchema.schema?.required;
684
- const sources = generator.build({
685
- schema: operationSchema.schema,
686
- baseName: operationSchema.name,
687
- description: operationSchema.description,
688
- keysToOmit: operationSchema.keysToOmit,
689
- optional: !required && !!operationSchema.name.includes("Params")
690
- });
691
- importMeta.push(...generator.imports);
692
- return {
693
- import: {
694
- refs: generator.refs,
695
- name: operationSchema.name
696
- },
697
- sources
698
- };
699
- }).sort(refsSorter);
700
- generated.forEach((item) => {
701
- codes.push(print2(item.sources));
702
- });
703
- const imports = importMeta.map((item) => {
704
- return {
705
- name: [item.ref.propertyName],
706
- path: item.path,
707
- isTypeOnly: item.isTypeOnly
708
- };
709
- });
710
- return {
711
- imports,
712
- source: transformers3.combineCodes(codes)
713
- };
714
- }
715
- };
716
-
717
- // src/components/Mutation.tsx
718
- import { Fragment as Fragment3, jsx as jsx4, jsxs as jsxs3 } from "@kubb/react/jsx-runtime";
719
- function printCombinedSchema2(name, operation, schemas) {
720
- const properties = {
721
- "response": factory3.createTypeReferenceNode(
722
- factory3.createIdentifier(schemas.response.name),
723
- void 0
724
- )
725
- };
726
- if (schemas.request) {
727
- properties["request"] = factory3.createTypeReferenceNode(
728
- factory3.createIdentifier(schemas.request.name),
729
- void 0
730
- );
731
- }
732
- if (schemas.pathParams) {
733
- properties["pathParams"] = factory3.createTypeReferenceNode(
734
- factory3.createIdentifier(schemas.pathParams.name),
735
- void 0
736
- );
737
- }
738
- if (schemas.queryParams) {
739
- properties["queryParams"] = factory3.createTypeReferenceNode(
740
- factory3.createIdentifier(schemas.queryParams.name),
741
- void 0
742
- );
743
- }
744
- if (schemas.headerParams) {
745
- properties["headerParams"] = factory3.createTypeReferenceNode(
746
- factory3.createIdentifier(schemas.headerParams.name),
747
- void 0
748
- );
749
- }
750
- if (schemas.errors) {
751
- properties["errors"] = factory3.createUnionDeclaration({
752
- nodes: schemas.errors.map((error) => {
753
- return factory3.createTypeReferenceNode(
754
- factory3.createIdentifier(error.name),
755
- void 0
756
- );
757
- })
758
- });
759
- }
760
- const namespaceNode = factory3.createTypeAliasDeclaration({
761
- name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
762
- type: factory3.createTypeLiteralNode(
763
- Object.keys(properties).map((key) => {
764
- const type = properties[key];
765
- if (!type) {
766
- return void 0;
767
- }
768
- return factory3.createPropertySignature(
769
- {
770
- name: transformers4.pascalCase(key),
771
- type
772
- }
773
- );
774
- }).filter(Boolean)
775
- ),
776
- modifiers: [factory3.modifiers.export]
777
- });
778
- return print3(namespaceNode);
779
- }
780
- function Mutation({
781
- builder
782
- }) {
783
- const { source } = builder.build();
784
- return /* @__PURE__ */ jsx4(Fragment3, { children: source });
785
- }
786
- Mutation.File = function({ mode }) {
787
- const plugin = usePlugin3();
788
- const schemas = useSchemas2();
789
- const pluginManager = usePluginManager2();
790
- const oas = useOas3();
791
- const file = useOperationFile2();
792
- const factoryName = useOperationName2({ type: "type" });
793
- const operation = useOperation2();
794
- const builder = new TypeBuilder(plugin.options, { plugin, oas, pluginManager }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.response).add(schemas.request).add(schemas.statusCodes);
795
- const { source, imports } = builder.build();
796
- return /* @__PURE__ */ jsx4(Editor3, { language: "typescript", children: /* @__PURE__ */ jsxs3(
797
- File3,
798
- {
799
- baseName: file.baseName,
800
- path: file.path,
801
- meta: file.meta,
802
- children: [
803
- mode === "directory" && imports.map((item, index) => {
804
- return /* @__PURE__ */ jsx4(File3.Import, { root: file.path, ...item }, index);
805
- }),
806
- /* @__PURE__ */ jsxs3(File3.Source, { children: [
807
- source,
808
- printCombinedSchema2(factoryName, operation, schemas)
809
- ] })
810
- ]
811
- }
812
- ) });
813
- };
814
-
815
- export {
816
- Mutation,
817
- OasType,
818
- Query,
819
- pluginName,
820
- pluginKey,
821
- definePlugin
822
- };
823
- //# sourceMappingURL=chunk-EX53MIMI.js.map