@kubb/swagger-ts 2.0.0-canary.20231030T125204 → 2.0.1

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