@kubb/plugin-zod 3.0.0-alpha.2 → 3.0.0-alpha.20

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 (83) hide show
  1. package/README.md +13 -4
  2. package/dist/chunk-GILKYHDS.cjs +250 -0
  3. package/dist/chunk-GILKYHDS.cjs.map +1 -0
  4. package/dist/chunk-KGKZCQKF.js +322 -0
  5. package/dist/chunk-KGKZCQKF.js.map +1 -0
  6. package/dist/chunk-RPZWETTK.cjs +329 -0
  7. package/dist/chunk-RPZWETTK.cjs.map +1 -0
  8. package/dist/chunk-WIOP5MTW.js +241 -0
  9. package/dist/chunk-WIOP5MTW.js.map +1 -0
  10. package/dist/components.cjs +11 -6
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +20 -19
  13. package/dist/components.d.ts +20 -19
  14. package/dist/components.js +2 -10
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +17 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +10 -0
  19. package/dist/generators.d.ts +10 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +12 -4
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +4 -137
  25. package/dist/index.d.ts +4 -137
  26. package/dist/index.js +3 -8
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-BRWHhUnI.d.cts +109 -0
  29. package/dist/types-BRWHhUnI.d.ts +109 -0
  30. package/package.json +18 -20
  31. package/src/components/Operations.tsx +19 -103
  32. package/src/components/Zod.tsx +66 -0
  33. package/src/components/index.ts +1 -2
  34. package/src/generators/__snapshots__/anyof.ts +3 -0
  35. package/src/generators/__snapshots__/createPet.ts +15 -0
  36. package/src/generators/__snapshots__/createPetWithUnknownTypeAny.ts +13 -0
  37. package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +15 -0
  38. package/src/generators/__snapshots__/deletePet.ts +3 -0
  39. package/src/generators/__snapshots__/enumNamesType.ts +3 -0
  40. package/src/generators/__snapshots__/enumNullable.ts +3 -0
  41. package/src/generators/__snapshots__/enumVarNamesType.ts +3 -0
  42. package/src/generators/__snapshots__/example.ts +3 -0
  43. package/src/generators/__snapshots__/getPets.ts +18 -0
  44. package/src/generators/__snapshots__/mixedValueTypeConst.ts +6 -0
  45. package/src/generators/__snapshots__/nullableString.ts +3 -0
  46. package/src/generators/__snapshots__/nullableStringUuid.ts +3 -0
  47. package/src/generators/__snapshots__/nullableStringWithAnyOf.ts +3 -0
  48. package/src/generators/__snapshots__/numberValueConst.ts +6 -0
  49. package/src/generators/__snapshots__/oneof.ts +3 -0
  50. package/src/generators/__snapshots__/operations.ts +46 -0
  51. package/src/generators/__snapshots__/optionalPetInfer.ts +5 -0
  52. package/src/generators/__snapshots__/optionalPetTyped.ts +3 -0
  53. package/src/generators/__snapshots__/order.ts +3 -0
  54. package/src/generators/__snapshots__/orderDateTyeString.ts +10 -0
  55. package/src/generators/__snapshots__/orderDateTypeFalse.ts +3 -0
  56. package/src/generators/__snapshots__/orderDateTypeString.ts +3 -0
  57. package/src/generators/__snapshots__/pet.ts +3 -0
  58. package/src/generators/__snapshots__/petArray.ts +6 -0
  59. package/src/generators/__snapshots__/petCoercion.ts +3 -0
  60. package/src/generators/__snapshots__/petTupleObject.ts +6 -0
  61. package/src/generators/__snapshots__/petWithMapper.ts +3 -0
  62. package/src/generators/__snapshots__/pets.ts +3 -0
  63. package/src/generators/__snapshots__/recursive.ts +3 -0
  64. package/src/generators/__snapshots__/showPetById.ts +18 -0
  65. package/src/generators/__snapshots__/stringValueConst.ts +6 -0
  66. package/src/generators/__snapshots__/uuidSchema.ts +3 -0
  67. package/src/generators/index.ts +2 -0
  68. package/src/generators/operationsGenerator.tsx +39 -0
  69. package/src/generators/zodGenerator.tsx +121 -0
  70. package/src/parser/index.ts +11 -3
  71. package/src/plugin.ts +22 -37
  72. package/src/types.ts +38 -70
  73. package/dist/Operations-BlQtRP31.d.cts +0 -47
  74. package/dist/Operations-BlQtRP31.d.ts +0 -47
  75. package/dist/chunk-KP7GRUCO.cjs +0 -1279
  76. package/dist/chunk-KP7GRUCO.cjs.map +0 -1
  77. package/dist/chunk-ORHAR2XI.js +0 -1279
  78. package/dist/chunk-ORHAR2XI.js.map +0 -1
  79. package/src/OperationGenerator.tsx +0 -54
  80. package/src/SchemaGenerator.tsx +0 -31
  81. package/src/components/OperationSchema.tsx +0 -66
  82. package/src/components/Schema.tsx +0 -171
  83. package/src/components/__snapshots__/operations.ts +0 -50
@@ -1,1279 +0,0 @@
1
- // src/components/Schema.tsx
2
- import { Oas as Oas8 } from "@kubb/plugin-oas/components";
3
- import { Const as Const2, File as File6, Type as Type2, useApp as useApp6, useFile } from "@kubb/react";
4
-
5
- // ../plugin-ts/src/plugin.ts
6
- import path from "path";
7
- import { FileManager, PluginManager, createPlugin } from "@kubb/core";
8
- import { camelCase, pascalCase } from "@kubb/core/transformers";
9
- import { renderTemplate } from "@kubb/core/utils";
10
- import { pluginOasName } from "@kubb/plugin-oas";
11
-
12
- // ../plugin-ts/src/OperationGenerator.tsx
13
- import { OperationGenerator as Generator2 } from "@kubb/plugin-oas";
14
- import { Oas as Oas4 } from "@kubb/plugin-oas/components";
15
- import { App as App2, createRoot as createRoot2 } from "@kubb/react";
16
-
17
- // ../plugin-ts/src/components/OasType.tsx
18
- import { Parser, File, Type, useApp } from "@kubb/react";
19
- import { useOas } from "@kubb/plugin-oas/hooks";
20
- import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime";
21
- function Template({ name, typeName, api }) {
22
- return /* @__PURE__ */ jsxs(Fragment, { children: [
23
- `export const ${name} = ${JSON.stringify(api, void 0, 2)} as const`,
24
- /* @__PURE__ */ jsx("br", {}),
25
- /* @__PURE__ */ jsx(Type, { name: typeName, export: true, children: `Infer<typeof ${name}>` })
26
- ] });
27
- }
28
- var defaultTemplates = { default: Template };
29
- function OasType({ name, typeName, Template: Template3 = defaultTemplates.default }) {
30
- const oas = useOas();
31
- return /* @__PURE__ */ jsx(Template3, { name, typeName, api: oas.api });
32
- }
33
- OasType.File = function({ name, typeName, templates = defaultTemplates }) {
34
- const {
35
- pluginManager,
36
- plugin: { key: pluginKey }
37
- } = useApp();
38
- const file = pluginManager.getFile({ name, extName: ".ts", pluginKey });
39
- const Template3 = templates.default;
40
- return /* @__PURE__ */ jsx(Parser, { language: "typescript", children: /* @__PURE__ */ jsxs(File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
41
- /* @__PURE__ */ jsx(File.Import, { name: ["Infer"], path: "@kubb/oas", isTypeOnly: true }),
42
- /* @__PURE__ */ jsx(File.Source, { children: /* @__PURE__ */ jsx(OasType, { Template: Template3, name, typeName }) })
43
- ] }) });
44
- };
45
- OasType.templates = defaultTemplates;
46
-
47
- // ../plugin-ts/src/components/OperationSchema.tsx
48
- import transformers3 from "@kubb/core/transformers";
49
- import { print as print2 } from "@kubb/parser-ts";
50
- import * as factory3 from "@kubb/parser-ts/factory";
51
- import { Oas as Oas3 } from "@kubb/plugin-oas/components";
52
- import { useOas as useOas2, useOperation, useOperationManager } from "@kubb/plugin-oas/hooks";
53
- import { File as File3, Parser as Parser2, useApp as useApp3 } from "@kubb/react";
54
-
55
- // ../plugin-ts/src/SchemaGenerator.tsx
56
- import { SchemaGenerator as Generator } from "@kubb/plugin-oas";
57
- import { Oas as Oas2 } from "@kubb/plugin-oas/components";
58
- import { App, createRoot } from "@kubb/react";
59
-
60
- // ../plugin-ts/src/components/Schema.tsx
61
- import { Oas } from "@kubb/plugin-oas/components";
62
- import { File as File2, useApp as useApp2 } from "@kubb/react";
63
- import transformers2 from "@kubb/core/transformers";
64
- import { print } from "@kubb/parser-ts";
65
- import * as factory2 from "@kubb/parser-ts/factory";
66
- import { SchemaGenerator, schemaKeywords as schemaKeywords2 } from "@kubb/plugin-oas";
67
- import { useSchema } from "@kubb/plugin-oas/hooks";
68
-
69
- // ../plugin-ts/src/parser/index.ts
70
- import transformers from "@kubb/core/transformers";
71
- import * as factory from "@kubb/parser-ts/factory";
72
- import { isKeyword, schemaKeywords } from "@kubb/plugin-oas";
73
- var typeKeywordMapper = {
74
- any: () => factory.keywordTypeNodes.any,
75
- unknown: () => factory.keywordTypeNodes.unknown,
76
- number: () => factory.keywordTypeNodes.number,
77
- integer: () => factory.keywordTypeNodes.number,
78
- object: (nodes) => {
79
- if (!nodes || !nodes.length) {
80
- return factory.keywordTypeNodes.object;
81
- }
82
- return factory.createTypeLiteralNode(nodes);
83
- },
84
- string: () => factory.keywordTypeNodes.string,
85
- boolean: () => factory.keywordTypeNodes.boolean,
86
- undefined: () => factory.keywordTypeNodes.undefined,
87
- nullable: void 0,
88
- null: () => factory.keywordTypeNodes.null,
89
- nullish: void 0,
90
- array: (nodes) => {
91
- if (!nodes) {
92
- return void 0;
93
- }
94
- return factory.createArrayDeclaration({ nodes });
95
- },
96
- tuple: (nodes) => {
97
- if (!nodes) {
98
- return void 0;
99
- }
100
- return factory.createTupleTypeNode(nodes);
101
- },
102
- enum: (name) => {
103
- if (!name) {
104
- return void 0;
105
- }
106
- return factory.createTypeReferenceNode(name, void 0);
107
- },
108
- union: (nodes) => {
109
- if (!nodes) {
110
- return void 0;
111
- }
112
- return factory.createUnionDeclaration({
113
- withParentheses: true,
114
- nodes
115
- });
116
- },
117
- const: (name, format) => {
118
- if (!name) {
119
- return void 0;
120
- }
121
- if (format === "number") {
122
- return factory.createLiteralTypeNode(factory.createNumericLiteral(name));
123
- }
124
- return factory.createLiteralTypeNode(factory.createStringLiteral(name.toString()));
125
- },
126
- datetime: () => factory.keywordTypeNodes.string,
127
- date: (type = "string") => type === "string" ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier("Date")),
128
- time: (type = "string") => type === "string" ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier("Date")),
129
- uuid: void 0,
130
- url: void 0,
131
- strict: void 0,
132
- default: void 0,
133
- and: (nodes) => {
134
- if (!nodes) {
135
- return void 0;
136
- }
137
- return factory.createIntersectionDeclaration({
138
- withParentheses: true,
139
- nodes
140
- });
141
- },
142
- describe: void 0,
143
- min: void 0,
144
- max: void 0,
145
- optional: void 0,
146
- matches: void 0,
147
- email: void 0,
148
- firstName: void 0,
149
- lastName: void 0,
150
- password: void 0,
151
- phone: void 0,
152
- readOnly: void 0,
153
- ref: (propertyName) => {
154
- if (!propertyName) {
155
- return void 0;
156
- }
157
- return factory.createTypeReferenceNode(propertyName, void 0);
158
- },
159
- blob: () => factory.createTypeReferenceNode("Blob", []),
160
- deprecated: void 0,
161
- example: void 0,
162
- schema: void 0,
163
- catchall: void 0,
164
- name: void 0
165
- };
166
- function parse(parent, current, options) {
167
- const value = typeKeywordMapper[current.keyword];
168
- if (!value) {
169
- return void 0;
170
- }
171
- if (isKeyword(current, schemaKeywords.union)) {
172
- return typeKeywordMapper.union(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
173
- }
174
- if (isKeyword(current, schemaKeywords.and)) {
175
- return typeKeywordMapper.and(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
176
- }
177
- if (isKeyword(current, schemaKeywords.array)) {
178
- return typeKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
179
- }
180
- if (isKeyword(current, schemaKeywords.enum)) {
181
- return typeKeywordMapper.enum(current.args.typeName);
182
- }
183
- if (isKeyword(current, schemaKeywords.ref)) {
184
- return typeKeywordMapper.ref(current.args.name);
185
- }
186
- if (isKeyword(current, schemaKeywords.blob)) {
187
- return value();
188
- }
189
- if (isKeyword(current, schemaKeywords.tuple)) {
190
- return typeKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
191
- }
192
- if (isKeyword(current, schemaKeywords.const)) {
193
- return typeKeywordMapper.const(current.args.name, current.args.format);
194
- }
195
- if (isKeyword(current, schemaKeywords.object)) {
196
- const properties = Object.entries(current.args?.properties || {}).filter((item) => {
197
- const schemas = item[1];
198
- return schemas && typeof schemas.map === "function";
199
- }).map(([name, schemas]) => {
200
- const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name);
201
- const mappedName = nameSchema?.args || name;
202
- if (options.mapper?.[mappedName]) {
203
- return options.mapper?.[mappedName];
204
- }
205
- const isNullish = schemas.some((schema) => schema.keyword === schemaKeywords.nullish);
206
- const isNullable = schemas.some((schema) => schema.keyword === schemaKeywords.nullable);
207
- const isOptional = schemas.some((schema) => schema.keyword === schemaKeywords.optional);
208
- const isReadonly = schemas.some((schema) => schema.keyword === schemaKeywords.readOnly);
209
- const describeSchema = schemas.find((schema) => schema.keyword === schemaKeywords.describe);
210
- const deprecatedSchema = schemas.find((schema) => schema.keyword === schemaKeywords.deprecated);
211
- const defaultSchema = schemas.find((schema) => schema.keyword === schemaKeywords.default);
212
- const exampleSchema = schemas.find((schema) => schema.keyword === schemaKeywords.example);
213
- const schemaSchema = schemas.find((schema) => schema.keyword === schemaKeywords.schema);
214
- let type = schemas.map((schema) => parse(current, schema, options)).filter(Boolean)[0];
215
- if (isNullable) {
216
- type = factory.createUnionDeclaration({
217
- nodes: [type, factory.keywordTypeNodes.null]
218
- });
219
- }
220
- if (isNullish && ["undefined", "questionTokenAndUndefined"].includes(options.optionalType)) {
221
- type = factory.createUnionDeclaration({
222
- nodes: [type, factory.keywordTypeNodes.undefined]
223
- });
224
- }
225
- if (isOptional && ["undefined", "questionTokenAndUndefined"].includes(options.optionalType)) {
226
- type = factory.createUnionDeclaration({
227
- nodes: [type, factory.keywordTypeNodes.undefined]
228
- });
229
- }
230
- const propertySignature = factory.createPropertySignature({
231
- questionToken: isOptional || isNullish ? ["questionToken", "questionTokenAndUndefined"].includes(options.optionalType) : false,
232
- name: mappedName,
233
- type,
234
- readOnly: isReadonly
235
- });
236
- return factory.appendJSDocToNode({
237
- node: propertySignature,
238
- comments: [
239
- describeSchema ? `@description ${transformers.jsStringEscape(describeSchema.args)}` : void 0,
240
- deprecatedSchema ? "@deprecated" : void 0,
241
- defaultSchema ? `@default ${defaultSchema.args}` : void 0,
242
- exampleSchema ? `@example ${exampleSchema.args}` : void 0,
243
- schemaSchema?.args?.type || schemaSchema?.args?.format ? [`@type ${schemaSchema?.args?.type || "unknown"}${!isOptional ? "" : " | undefined"}`, schemaSchema?.args?.format].filter(Boolean).join(", ") : void 0
244
- ].filter(Boolean)
245
- });
246
- });
247
- const additionalProperties = current.args?.additionalProperties?.length ? factory.createIndexSignature(
248
- current.args.additionalProperties.map((schema) => parse(current, schema, options)).filter(Boolean).at(0)
249
- ) : void 0;
250
- return typeKeywordMapper.object([...properties, additionalProperties].filter(Boolean));
251
- }
252
- if (isKeyword(current, schemaKeywords.datetime)) {
253
- return typeKeywordMapper.datetime();
254
- }
255
- if (isKeyword(current, schemaKeywords.date)) {
256
- return typeKeywordMapper.date(current.args.type);
257
- }
258
- if (isKeyword(current, schemaKeywords.time)) {
259
- return typeKeywordMapper.time(current.args.type);
260
- }
261
- if (current.keyword in typeKeywordMapper) {
262
- return value();
263
- }
264
- return void 0;
265
- }
266
-
267
- // ../plugin-ts/src/components/Schema.tsx
268
- import { jsx as jsx2 } from "@kubb/react/jsx-runtime";
269
- function Schema(props) {
270
- const { keysToOmit, description } = props;
271
- const { tree, name } = useSchema();
272
- const {
273
- pluginManager,
274
- plugin: {
275
- options: { mapper, enumType, optionalType }
276
- }
277
- } = useApp2();
278
- const resolvedName = pluginManager.resolveName({
279
- name,
280
- pluginKey: [pluginTsName],
281
- type: "function"
282
- });
283
- const typeName = pluginManager.resolveName({
284
- name,
285
- pluginKey: [pluginTsName],
286
- type: "type"
287
- });
288
- const nodes = [];
289
- const extraNodes = [];
290
- if (!tree.length) {
291
- return "";
292
- }
293
- const isNullish = tree.some((item) => item.keyword === schemaKeywords2.nullish);
294
- const isNullable = tree.some((item) => item.keyword === schemaKeywords2.nullable);
295
- const isOptional = tree.some((item) => item.keyword === schemaKeywords2.optional);
296
- let type = tree.map((schema) => parse(void 0, schema, { name: resolvedName, typeName, description, keysToOmit, optionalType, enumType, mapper })).filter(Boolean).at(0) || typeKeywordMapper.undefined();
297
- if (isNullable) {
298
- type = factory2.createUnionDeclaration({
299
- nodes: [type, factory2.keywordTypeNodes.null]
300
- });
301
- }
302
- if (isNullish && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
303
- type = factory2.createUnionDeclaration({
304
- nodes: [type, factory2.keywordTypeNodes.undefined]
305
- });
306
- }
307
- if (isOptional && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
308
- type = factory2.createUnionDeclaration({
309
- nodes: [type, factory2.keywordTypeNodes.undefined]
310
- });
311
- }
312
- const node = factory2.createTypeAliasDeclaration({
313
- modifiers: [factory2.modifiers.export],
314
- name: resolvedName,
315
- type: keysToOmit?.length ? factory2.createOmitDeclaration({
316
- keys: keysToOmit,
317
- type,
318
- nonNullable: true
319
- }) : type
320
- });
321
- const enumSchemas = SchemaGenerator.deepSearch(tree, schemaKeywords2.enum);
322
- if (enumSchemas) {
323
- enumSchemas.forEach((enumSchema) => {
324
- extraNodes.push(
325
- ...factory2.createEnumDeclaration({
326
- name: transformers2.camelCase(enumSchema.args.name),
327
- typeName: enumSchema.args.typeName,
328
- enums: enumSchema.args.items.map((item) => item.value === void 0 ? void 0 : [transformers2.trimQuotes(item.name?.toString()), item.value]).filter(Boolean),
329
- type: enumType
330
- })
331
- );
332
- });
333
- }
334
- nodes.push(
335
- factory2.appendJSDocToNode({
336
- node,
337
- comments: [description ? `@description ${transformers2.jsStringEscape(description)}` : void 0].filter(Boolean)
338
- })
339
- );
340
- const filterdNodes = nodes.filter(
341
- (node2) => !extraNodes.some(
342
- (extraNode) => extraNode?.name?.escapedText === node2?.name?.escapedText
343
- )
344
- );
345
- return print([...extraNodes, ...filterdNodes]);
346
- }
347
- Schema.File = function({}) {
348
- const { pluginManager } = useApp2();
349
- const { schema } = useSchema();
350
- return /* @__PURE__ */ jsx2(Oas.Schema.File, { output: pluginManager.config.output.path, children: /* @__PURE__ */ jsx2(File2.Source, { children: /* @__PURE__ */ jsx2(Schema, { description: schema?.description }) }) });
351
- };
352
-
353
- // ../plugin-ts/src/SchemaGenerator.tsx
354
- import { jsx as jsx3 } from "@kubb/react/jsx-runtime";
355
- var SchemaGenerator2 = class extends Generator {
356
- async schema(name, schema, options) {
357
- const { oas, pluginManager, plugin, mode, output } = this.context;
358
- const root = createRoot({
359
- logger: pluginManager.logger
360
- });
361
- const tree = this.parse({ schema, name });
362
- root.render(
363
- /* @__PURE__ */ jsx3(App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx3(Oas2, { oas, children: /* @__PURE__ */ jsx3(Oas2.Schema, { name, value: schema, tree, children: /* @__PURE__ */ jsx3(Schema.File, {}) }) }) })
364
- );
365
- return root.files;
366
- }
367
- };
368
-
369
- // ../plugin-ts/src/components/OperationSchema.tsx
370
- import { jsx as jsx4, jsxs as jsxs2 } from "@kubb/react/jsx-runtime";
371
- function printCombinedSchema({
372
- name,
373
- operation,
374
- schemas,
375
- pluginManager
376
- }) {
377
- const properties = {};
378
- if (schemas.response) {
379
- const identifier = pluginManager.resolveName({
380
- name: schemas.response.name,
381
- pluginKey: [pluginTsName],
382
- type: "function"
383
- });
384
- properties["response"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
385
- }
386
- if (schemas.request) {
387
- const identifier = pluginManager.resolveName({
388
- name: schemas.request.name,
389
- pluginKey: [pluginTsName],
390
- type: "function"
391
- });
392
- properties["request"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
393
- }
394
- if (schemas.pathParams) {
395
- const identifier = pluginManager.resolveName({
396
- name: schemas.pathParams.name,
397
- pluginKey: [pluginTsName],
398
- type: "function"
399
- });
400
- properties["pathParams"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
401
- }
402
- if (schemas.queryParams) {
403
- const identifier = pluginManager.resolveName({
404
- name: schemas.queryParams.name,
405
- pluginKey: [pluginTsName],
406
- type: "function"
407
- });
408
- properties["queryParams"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
409
- }
410
- if (schemas.headerParams) {
411
- const identifier = pluginManager.resolveName({
412
- name: schemas.headerParams.name,
413
- pluginKey: [pluginTsName],
414
- type: "function"
415
- });
416
- properties["headerParams"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
417
- }
418
- if (schemas.errors) {
419
- properties["errors"] = factory3.createUnionDeclaration({
420
- nodes: schemas.errors.map((error) => {
421
- const identifier = pluginManager.resolveName({
422
- name: error.name,
423
- pluginKey: [pluginTsName],
424
- type: "function"
425
- });
426
- return factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
427
- })
428
- });
429
- }
430
- const namespaceNode = factory3.createTypeAliasDeclaration({
431
- name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
432
- type: factory3.createTypeLiteralNode(
433
- Object.keys(properties).map((key) => {
434
- const type = properties[key];
435
- if (!type) {
436
- return void 0;
437
- }
438
- return factory3.createPropertySignature({
439
- name: transformers3.pascalCase(key),
440
- type
441
- });
442
- }).filter(Boolean)
443
- ),
444
- modifiers: [factory3.modifiers.export]
445
- });
446
- return print2(namespaceNode);
447
- }
448
- function OperationSchema({ keysToOmit, description }) {
449
- return /* @__PURE__ */ jsx4(Schema, { keysToOmit, description });
450
- }
451
- OperationSchema.File = function({}) {
452
- const { pluginManager, plugin, mode, fileManager } = useApp3();
453
- const oas = useOas2();
454
- const { getSchemas, getFile, getName } = useOperationManager();
455
- const operation = useOperation();
456
- const file = getFile(operation);
457
- const schemas = getSchemas(operation);
458
- const factoryName = getName(operation, { type: "type" });
459
- const generator = new SchemaGenerator2(plugin.options, {
460
- oas,
461
- plugin,
462
- pluginManager,
463
- mode,
464
- override: plugin.options.override
465
- });
466
- const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
467
- const mapItem = ({ name, schema, description, keysToOmit, ...options }, i) => {
468
- const tree = generator.parse({ schema, name });
469
- return /* @__PURE__ */ jsxs2(Oas3.Schema, { name, value: schema, tree, children: [
470
- mode === "split" && /* @__PURE__ */ jsx4(Oas3.Schema.Imports, { extName: plugin.options.extName, isTypeOnly: true }),
471
- /* @__PURE__ */ jsx4(File3.Source, { children: /* @__PURE__ */ jsx4(OperationSchema, { description, keysToOmit }) })
472
- ] }, i);
473
- };
474
- return /* @__PURE__ */ jsx4(Parser2, { language: "typescript", children: /* @__PURE__ */ jsxs2(File3, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
475
- items.map(mapItem),
476
- /* @__PURE__ */ jsx4(File3.Source, { children: printCombinedSchema({ name: factoryName, operation, schemas, pluginManager }) })
477
- ] }) });
478
- };
479
-
480
- // ../plugin-ts/src/OperationGenerator.tsx
481
- import { jsx as jsx5 } from "@kubb/react/jsx-runtime";
482
- var OperationGenerator = class extends Generator2 {
483
- async all(operations) {
484
- const { oas, pluginManager, plugin, mode } = this.context;
485
- const root = createRoot2({
486
- logger: pluginManager.logger
487
- });
488
- root.render(
489
- /* @__PURE__ */ jsx5(App2, { pluginManager, plugin, mode, children: /* @__PURE__ */ jsx5(Oas4, { oas, operations, generator: this, children: plugin.options.oasType && /* @__PURE__ */ jsx5(OasType.File, { name: "oas", typeName: "Oas" }) }) })
490
- );
491
- return root.files;
492
- }
493
- async operation(operation, options) {
494
- const { oas, pluginManager, plugin, mode } = this.context;
495
- const root = createRoot2({
496
- logger: pluginManager.logger
497
- });
498
- root.render(
499
- /* @__PURE__ */ jsx5(App2, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx5(Oas4, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ jsx5(Oas4.Operation, { operation, children: /* @__PURE__ */ jsx5(OperationSchema.File, {}) }) }) })
500
- );
501
- return root.files;
502
- }
503
- };
504
-
505
- // ../plugin-ts/src/plugin.ts
506
- var pluginTsName = "plugin-ts";
507
- var pluginTs = createPlugin((options) => {
508
- const {
509
- output = { path: "types" },
510
- group,
511
- exclude = [],
512
- include,
513
- override = [],
514
- enumType = "asConst",
515
- enumSuffix = "",
516
- dateType = "string",
517
- unknownType = "any",
518
- optionalType = "questionToken",
519
- transformers: transformers7 = {},
520
- oasType = false,
521
- mapper = {}
522
- } = options;
523
- const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
524
- return {
525
- name: pluginTsName,
526
- options: {
527
- extName: output.extName,
528
- transformers: transformers7,
529
- dateType,
530
- optionalType,
531
- oasType,
532
- enumType,
533
- enumSuffix,
534
- // keep the used enumnames between SchemaGenerator and OperationGenerator per plugin(pluginKey)
535
- usedEnumNames: {},
536
- unknownType,
537
- override,
538
- mapper
539
- },
540
- pre: [pluginOasName],
541
- resolvePath(baseName, pathMode, options2) {
542
- const root = path.resolve(this.config.root, this.config.output.path);
543
- const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
544
- if (mode === "single") {
545
- return path.resolve(root, output.path);
546
- }
547
- if (options2?.tag && group?.type === "tag") {
548
- const tag = camelCase(options2.tag);
549
- return path.resolve(root, renderTemplate(template, { tag }), baseName);
550
- }
551
- return path.resolve(root, output.path, baseName);
552
- },
553
- resolveName(name, type) {
554
- const resolvedName = pascalCase(name, { isFile: type === "file" });
555
- if (type) {
556
- return transformers7?.name?.(resolvedName, type) || resolvedName;
557
- }
558
- return resolvedName;
559
- },
560
- async buildStart() {
561
- const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]);
562
- const oas = await swaggerPlugin.context.getOas();
563
- const root = path.resolve(this.config.root, this.config.output.path);
564
- const mode = FileManager.getMode(path.resolve(root, output.path));
565
- const schemaGenerator = new SchemaGenerator2(this.plugin.options, {
566
- oas,
567
- pluginManager: this.pluginManager,
568
- plugin: this.plugin,
569
- contentType: swaggerPlugin.context.contentType,
570
- include: void 0,
571
- override,
572
- mode,
573
- output: output.path
574
- });
575
- const schemaFiles = await schemaGenerator.build();
576
- await this.addFile(...schemaFiles);
577
- const operationGenerator = new OperationGenerator(this.plugin.options, {
578
- oas,
579
- pluginManager: this.pluginManager,
580
- plugin: this.plugin,
581
- contentType: swaggerPlugin.context.contentType,
582
- exclude,
583
- include,
584
- override,
585
- mode
586
- });
587
- const operationFiles = await operationGenerator.build();
588
- await this.addFile(...operationFiles);
589
- },
590
- async buildEnd() {
591
- if (this.config.output.write === false) {
592
- return;
593
- }
594
- const root = path.resolve(this.config.root, this.config.output.path);
595
- await this.fileManager.addIndexes({
596
- root,
597
- output,
598
- meta: { pluginKey: this.plugin.key },
599
- logger: this.logger
600
- });
601
- }
602
- };
603
- });
604
-
605
- // src/components/Schema.tsx
606
- import transformers6 from "@kubb/core/transformers";
607
- import { isKeyword as isKeyword3, schemaKeywords as schemaKeywords5 } from "@kubb/plugin-oas";
608
- import { useSchema as useSchema2 } from "@kubb/plugin-oas/hooks";
609
-
610
- // src/parser/index.ts
611
- import transformers4 from "@kubb/core/transformers";
612
- import { isKeyword as isKeyword2, schemaKeywords as schemaKeywords3 } from "@kubb/plugin-oas";
613
- var zodKeywordMapper = {
614
- any: () => "z.any()",
615
- unknown: () => "z.unknown()",
616
- number: (coercion, min, max) => {
617
- return [coercion ? "z.coerce.number()" : "z.number()", min !== void 0 ? `.min(${min})` : void 0, max !== void 0 ? `.max(${max})` : void 0].filter(Boolean).join("");
618
- },
619
- integer: (coercion, min, max) => {
620
- return [coercion ? "z.coerce.number()" : "z.number()", min !== void 0 ? `.min(${min})` : void 0, max !== void 0 ? `.max(${max})` : void 0].filter(Boolean).join("");
621
- },
622
- object: (value) => `z.object({${value}})`,
623
- string: (coercion, min, max) => {
624
- return [coercion ? "z.coerce.string()" : "z.string()", min !== void 0 ? `.min(${min})` : void 0, max !== void 0 ? `.max(${max})` : void 0].filter(Boolean).join("");
625
- },
626
- boolean: () => "z.boolean()",
627
- undefined: () => "z.undefined()",
628
- nullable: () => ".nullable()",
629
- null: () => "z.null()",
630
- nullish: () => ".nullish()",
631
- array: (items = [], min, max) => {
632
- return [`z.array(${items?.join("")})`, min !== void 0 ? `.min(${min})` : void 0, max !== void 0 ? `.max(${max})` : void 0].filter(Boolean).join("");
633
- },
634
- tuple: (items = []) => `z.tuple([${items?.join(", ")}])`,
635
- enum: (items = []) => `z.enum([${items?.join(", ")}])`,
636
- union: (items = []) => `z.union([${items?.join(", ")}])`,
637
- const: (value) => `z.literal(${value ?? ""})`,
638
- /**
639
- * ISO 8601
640
- */
641
- datetime: (offset = false, local = false) => {
642
- if (offset) {
643
- return `z.string().datetime({ offset: ${offset} })`;
644
- }
645
- if (local) {
646
- return `z.string().datetime({ local: ${local} })`;
647
- }
648
- return "z.string().datetime()";
649
- },
650
- /**
651
- * Type `'date'` Date
652
- * Type `'string'` ISO date format (YYYY-MM-DD)
653
- * @default ISO date format (YYYY-MM-DD)
654
- */
655
- date: (type = "string", coercion) => {
656
- if (type === "string") {
657
- return "z.string().date()";
658
- }
659
- if (coercion) {
660
- return "z.coerce.date()";
661
- }
662
- return "z.date()";
663
- },
664
- /**
665
- * Type `'date'` Date
666
- * Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
667
- * @default ISO time format (HH:mm:ss[.SSSSSS])
668
- */
669
- time: (type = "string", coercion) => {
670
- if (type === "string") {
671
- return "z.string().time()";
672
- }
673
- if (coercion) {
674
- return "z.coerce.date()";
675
- }
676
- return "z.date()";
677
- },
678
- uuid: () => ".uuid()",
679
- url: () => ".url()",
680
- strict: () => ".strict()",
681
- default: (value) => `.default(${value ?? ""})`,
682
- and: (items = []) => items?.map((item) => `.and(${item})`).join(""),
683
- describe: (value = "") => `.describe(${value})`,
684
- min: (value) => `.min(${value ?? ""})`,
685
- max: (value) => `.max(${value ?? ""})`,
686
- optional: () => ".optional()",
687
- matches: (value = "") => `.regex(${value})`,
688
- email: () => ".email()",
689
- firstName: void 0,
690
- lastName: void 0,
691
- password: void 0,
692
- phone: void 0,
693
- readOnly: void 0,
694
- ref: (value) => value ? `z.lazy(() => ${value})` : void 0,
695
- blob: () => "z.string()",
696
- deprecated: void 0,
697
- example: void 0,
698
- schema: void 0,
699
- catchall: (value) => value ? `.catchall(${value})` : void 0,
700
- name: void 0
701
- };
702
- function sort(items) {
703
- const order = [
704
- schemaKeywords3.string,
705
- schemaKeywords3.datetime,
706
- schemaKeywords3.date,
707
- schemaKeywords3.time,
708
- schemaKeywords3.tuple,
709
- schemaKeywords3.number,
710
- schemaKeywords3.object,
711
- schemaKeywords3.enum,
712
- schemaKeywords3.url,
713
- schemaKeywords3.email,
714
- schemaKeywords3.firstName,
715
- schemaKeywords3.lastName,
716
- schemaKeywords3.password,
717
- schemaKeywords3.matches,
718
- schemaKeywords3.uuid,
719
- schemaKeywords3.min,
720
- schemaKeywords3.max,
721
- schemaKeywords3.default,
722
- schemaKeywords3.describe,
723
- schemaKeywords3.optional,
724
- schemaKeywords3.nullable,
725
- schemaKeywords3.nullish,
726
- schemaKeywords3.null
727
- ];
728
- if (!items) {
729
- return [];
730
- }
731
- return transformers4.orderBy(items, [(v) => order.indexOf(v.keyword)], ["asc"]);
732
- }
733
- function parse2(parent, current, options) {
734
- const value = zodKeywordMapper[current.keyword];
735
- if (!value) {
736
- return void 0;
737
- }
738
- if (isKeyword2(current, schemaKeywords3.union)) {
739
- if (Array.isArray(current.args) && current.args.length === 1) {
740
- return parse2(parent, current.args[0], options);
741
- }
742
- if (Array.isArray(current.args) && !current.args.length) {
743
- return "";
744
- }
745
- return zodKeywordMapper.union(
746
- sort(current.args).map((schema) => parse2(current, schema, options)).filter(Boolean)
747
- );
748
- }
749
- if (isKeyword2(current, schemaKeywords3.and)) {
750
- const items = sort(current.args).filter((schema) => {
751
- return ![schemaKeywords3.optional, schemaKeywords3.describe].includes(schema.keyword);
752
- }).map((schema) => parse2(current, schema, options)).filter(Boolean);
753
- return `${items.slice(0, 1)}${zodKeywordMapper.and(items.slice(1))}`;
754
- }
755
- if (isKeyword2(current, schemaKeywords3.array)) {
756
- return zodKeywordMapper.array(
757
- sort(current.args.items).map((schemas) => parse2(current, schemas, options)).filter(Boolean),
758
- current.args.min,
759
- current.args.max
760
- );
761
- }
762
- if (isKeyword2(current, schemaKeywords3.enum)) {
763
- if (current.args.asConst) {
764
- return zodKeywordMapper.union(
765
- current.args.items.map((schema) => {
766
- return parse2(
767
- current,
768
- {
769
- keyword: schemaKeywords3.const,
770
- args: schema
771
- },
772
- options
773
- );
774
- }).filter(Boolean)
775
- );
776
- }
777
- return zodKeywordMapper.enum(
778
- current.args.items.map((schema) => {
779
- if (schema.format === "number") {
780
- return transformers4.stringify(schema.value);
781
- }
782
- return transformers4.stringify(schema.value);
783
- })
784
- );
785
- }
786
- if (isKeyword2(current, schemaKeywords3.ref)) {
787
- return zodKeywordMapper.ref(current.args?.name);
788
- }
789
- if (isKeyword2(current, schemaKeywords3.object)) {
790
- const properties = Object.entries(current.args?.properties || {}).filter((item) => {
791
- const schema = item[1];
792
- return schema && typeof schema.map === "function";
793
- }).map(([name, schemas]) => {
794
- const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords3.name);
795
- const mappedName = nameSchema?.args || name;
796
- if (options.mapper?.[mappedName]) {
797
- return `"${name}": ${options.mapper?.[mappedName]}`;
798
- }
799
- return `"${name}": ${sort(schemas).map((schema, array) => {
800
- return parse2(current, schema, options);
801
- }).filter(Boolean).join("")}`;
802
- }).join(",");
803
- const additionalProperties = current.args?.additionalProperties?.length ? current.args.additionalProperties.map((schema) => parse2(current, schema, options)).filter(Boolean).at(0) : void 0;
804
- const text = [
805
- zodKeywordMapper.object(properties),
806
- current.args?.strict ? zodKeywordMapper.strict() : void 0,
807
- additionalProperties ? zodKeywordMapper.catchall(additionalProperties) : void 0
808
- ].filter(Boolean);
809
- return text.join("");
810
- }
811
- if (isKeyword2(current, schemaKeywords3.tuple)) {
812
- return zodKeywordMapper.tuple(
813
- sort(current.args.items).map((schema) => parse2(current, schema, options)).filter(Boolean)
814
- );
815
- }
816
- if (isKeyword2(current, schemaKeywords3.const)) {
817
- if (current.args.format === "number" && current.args.value !== void 0) {
818
- return zodKeywordMapper.const(Number.parseInt(current.args.value?.toString()));
819
- }
820
- return zodKeywordMapper.const(transformers4.stringify(current.args.value));
821
- }
822
- if (isKeyword2(current, schemaKeywords3.matches)) {
823
- if (current.args) {
824
- return zodKeywordMapper.matches(transformers4.toRegExpString(current.args));
825
- }
826
- }
827
- if (isKeyword2(current, schemaKeywords3.default)) {
828
- if (current.args) {
829
- return zodKeywordMapper.default(current.args);
830
- }
831
- }
832
- if (isKeyword2(current, schemaKeywords3.describe)) {
833
- if (current.args) {
834
- return zodKeywordMapper.describe(transformers4.stringify(current.args.toString()));
835
- }
836
- }
837
- if (isKeyword2(current, schemaKeywords3.string)) {
838
- return zodKeywordMapper.string(options.coercion);
839
- }
840
- if (isKeyword2(current, schemaKeywords3.number) || isKeyword2(current, schemaKeywords3.integer)) {
841
- return zodKeywordMapper.number(options.coercion);
842
- }
843
- if (isKeyword2(current, schemaKeywords3.min)) {
844
- return zodKeywordMapper.min(current.args);
845
- }
846
- if (isKeyword2(current, schemaKeywords3.max)) {
847
- return zodKeywordMapper.max(current.args);
848
- }
849
- if (isKeyword2(current, schemaKeywords3.datetime)) {
850
- return zodKeywordMapper.datetime(current.args.offset, current.args.local);
851
- }
852
- if (isKeyword2(current, schemaKeywords3.date)) {
853
- return zodKeywordMapper.date(current.args.type, options.coercion);
854
- }
855
- if (isKeyword2(current, schemaKeywords3.time)) {
856
- return zodKeywordMapper.time(current.args.type, options.coercion);
857
- }
858
- if (current.keyword in zodKeywordMapper && "args" in current) {
859
- const value2 = zodKeywordMapper[current.keyword];
860
- return value2(current.args);
861
- }
862
- if (current.keyword in zodKeywordMapper) {
863
- return value();
864
- }
865
- return void 0;
866
- }
867
-
868
- // src/plugin.ts
869
- import path2 from "path";
870
- import { FileManager as FileManager2, PluginManager as PluginManager2, createPlugin as createPlugin2 } from "@kubb/core";
871
- import { camelCase as camelCase2, pascalCase as pascalCase2 } from "@kubb/core/transformers";
872
- import { renderTemplate as renderTemplate2 } from "@kubb/core/utils";
873
- import { pluginOasName as pluginOasName2 } from "@kubb/plugin-oas";
874
- import { getGroupedByTagFiles } from "@kubb/plugin-oas/utils";
875
-
876
- // src/OperationGenerator.tsx
877
- import { OperationGenerator as Generator4 } from "@kubb/plugin-oas";
878
- import { Oas as Oas7 } from "@kubb/plugin-oas/components";
879
- import { App as App4, createRoot as createRoot4 } from "@kubb/react";
880
-
881
- // src/components/OperationSchema.tsx
882
- import { schemaKeywords as schemaKeywords4 } from "@kubb/plugin-oas";
883
- import { Oas as Oas6 } from "@kubb/plugin-oas/components";
884
- import { useOas as useOas3, useOperation as useOperation2, useOperationManager as useOperationManager2 } from "@kubb/plugin-oas/hooks";
885
- import { File as File4, Parser as Parser3, useApp as useApp4 } from "@kubb/react";
886
-
887
- // src/SchemaGenerator.tsx
888
- import { SchemaGenerator as Generator3 } from "@kubb/plugin-oas";
889
- import { Oas as Oas5 } from "@kubb/plugin-oas/components";
890
- import { App as App3, createRoot as createRoot3 } from "@kubb/react";
891
- import { jsx as jsx6 } from "@kubb/react/jsx-runtime";
892
- var SchemaGenerator3 = class extends Generator3 {
893
- async schema(name, schema, options) {
894
- const { oas, pluginManager, plugin, mode, output } = this.context;
895
- const root = createRoot3({
896
- logger: pluginManager.logger
897
- });
898
- const tree = this.parse({ schema, name });
899
- root.render(
900
- /* @__PURE__ */ jsx6(App3, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx6(Oas5, { oas, children: /* @__PURE__ */ jsx6(Oas5.Schema, { name, value: schema, tree, children: /* @__PURE__ */ jsx6(Schema2.File, {}) }) }) })
901
- );
902
- return root.files;
903
- }
904
- };
905
-
906
- // src/components/OperationSchema.tsx
907
- import { jsx as jsx7, jsxs as jsxs3 } from "@kubb/react/jsx-runtime";
908
- function OperationSchema2({ description, keysToOmit }) {
909
- return /* @__PURE__ */ jsx7(Schema2, { keysToOmit, withTypeAnnotation: false, description });
910
- }
911
- OperationSchema2.File = function({}) {
912
- const { pluginManager, plugin, mode } = useApp4();
913
- const { getSchemas, getFile } = useOperationManager2();
914
- const oas = useOas3();
915
- const operation = useOperation2();
916
- const file = getFile(operation);
917
- const schemas = getSchemas(operation);
918
- const generator = new SchemaGenerator3(plugin.options, {
919
- oas,
920
- plugin,
921
- pluginManager,
922
- mode,
923
- override: plugin.options.override
924
- });
925
- const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
926
- const mapItem = ({ name, schema, description, keysToOmit, ...options }, i) => {
927
- const required = Array.isArray(schema?.required) ? !!schema.required.length : !!schema?.required;
928
- const optional = !required && !!name.includes("Params");
929
- const tree = generator.parse({ schema, name });
930
- return /* @__PURE__ */ jsxs3(Oas6.Schema, { name, value: schema, tree: [...tree, optional ? { keyword: schemaKeywords4.optional } : void 0].filter(Boolean), children: [
931
- mode === "split" && /* @__PURE__ */ jsx7(Oas6.Schema.Imports, { extName: plugin.options.extName }),
932
- /* @__PURE__ */ jsx7(File4.Source, { children: /* @__PURE__ */ jsx7(OperationSchema2, { description, keysToOmit }) })
933
- ] }, i);
934
- };
935
- return /* @__PURE__ */ jsx7(Parser3, { language: "typescript", children: /* @__PURE__ */ jsxs3(File4, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
936
- /* @__PURE__ */ jsx7(File4.Import, { name: ["z"], path: plugin.options.importPath }),
937
- items.map(mapItem)
938
- ] }) });
939
- };
940
-
941
- // src/components/Operations.tsx
942
- import { useOperationManager as useOperationManager3, useOperations } from "@kubb/plugin-oas/hooks";
943
- import { Const, File as File5, Parser as Parser4, useApp as useApp5 } from "@kubb/react";
944
- import transformers5 from "@kubb/core/transformers";
945
- import { Fragment as Fragment2, jsx as jsx8, jsxs as jsxs4 } from "@kubb/react/jsx-runtime";
946
- function Template2({ operationsName, pathsName, operations }) {
947
- const { groupSchemasByName } = useOperationManager3();
948
- const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: "function" }) }));
949
- const operationsJSON = transformedOperations.reduce(
950
- (prev, acc) => {
951
- prev[`"${acc.operation.getOperationId()}"`] = acc.data;
952
- return prev;
953
- },
954
- {}
955
- );
956
- const pathsJSON = transformedOperations.reduce(
957
- (prev, acc) => {
958
- prev[`"${acc.operation.path}"`] = {
959
- ...prev[`"${acc.operation.path}"`] || {},
960
- [acc.operation.method]: `operations["${acc.operation.getOperationId()}"]`
961
- };
962
- return prev;
963
- },
964
- {}
965
- );
966
- return /* @__PURE__ */ jsxs4(Fragment2, { children: [
967
- /* @__PURE__ */ jsx8(Const, { export: true, name: operationsName, asConst: true, children: `{${transformers5.stringifyObject(operationsJSON)}}` }),
968
- /* @__PURE__ */ jsx8(Const, { export: true, name: pathsName, asConst: true, children: `{${transformers5.stringifyObject(pathsJSON)}}` })
969
- ] });
970
- }
971
- function RootTemplate({ children }) {
972
- const {
973
- mode,
974
- pluginManager,
975
- plugin: {
976
- key: pluginKey,
977
- options: { extName }
978
- }
979
- } = useApp5();
980
- const { getFile } = useOperationManager3();
981
- const operations = useOperations();
982
- const { groupSchemasByName } = useOperationManager3();
983
- const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: "function" }) }));
984
- const file = pluginManager.getFile({ name: "operations", extName: ".ts", pluginKey });
985
- const imports = Object.entries(transformedOperations).map(([_key, { data, operation }], index) => {
986
- const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean);
987
- return /* @__PURE__ */ jsx8(File5.Import, { extName, name: names, root: file.path, path: getFile(operation).path }, index);
988
- }).filter(Boolean);
989
- return /* @__PURE__ */ jsx8(Parser4, { language: "typescript", children: /* @__PURE__ */ jsxs4(File5, { baseName: file.baseName, path: file.path, meta: file.meta, exportable: false, children: [
990
- mode === "split" && imports,
991
- /* @__PURE__ */ jsx8(File5.Source, { children })
992
- ] }) });
993
- }
994
- var defaultTemplates2 = { default: Template2, root: RootTemplate };
995
- function Operations({ Template: Template3 = defaultTemplates2.default }) {
996
- const operations = useOperations();
997
- return /* @__PURE__ */ jsx8(Template3, { operationsName: "operations", pathsName: "paths", operations });
998
- }
999
- Operations.File = function(props) {
1000
- const templates = { ...defaultTemplates2, ...props.templates };
1001
- const Template3 = templates.default;
1002
- const RootTemplate2 = templates.root;
1003
- return /* @__PURE__ */ jsx8(RootTemplate2, { children: /* @__PURE__ */ jsx8(Operations, { Template: Template3 }) });
1004
- };
1005
- Operations.templates = defaultTemplates2;
1006
-
1007
- // src/OperationGenerator.tsx
1008
- import { jsx as jsx9 } from "@kubb/react/jsx-runtime";
1009
- var OperationGenerator2 = class extends Generator4 {
1010
- async all(operations, _operationsByMethod) {
1011
- const { pluginManager, oas, plugin, mode } = this.context;
1012
- const root = createRoot4({
1013
- logger: pluginManager.logger
1014
- });
1015
- const templates = {
1016
- operations: Operations.templates,
1017
- ...this.options.templates
1018
- };
1019
- root.render(
1020
- /* @__PURE__ */ jsx9(App4, { pluginManager, plugin, mode, children: /* @__PURE__ */ jsx9(Oas7, { oas, operations, generator: this, children: templates.operations && /* @__PURE__ */ jsx9(Operations.File, { templates: templates.operations }) }) })
1021
- );
1022
- return root.files;
1023
- }
1024
- async operation(operation, options) {
1025
- const { oas, pluginManager, plugin, mode } = this.context;
1026
- const root = createRoot4({
1027
- logger: pluginManager.logger
1028
- });
1029
- root.render(
1030
- /* @__PURE__ */ jsx9(App4, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx9(Oas7, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ jsx9(Oas7.Operation, { operation, children: /* @__PURE__ */ jsx9(OperationSchema2.File, {}) }) }) })
1031
- );
1032
- return root.files;
1033
- }
1034
- };
1035
-
1036
- // src/plugin.ts
1037
- var pluginZodName = "plugin-zod";
1038
- var pluginZod = createPlugin2((options) => {
1039
- const {
1040
- output = { path: "zod" },
1041
- group,
1042
- exclude = [],
1043
- include,
1044
- override = [],
1045
- transformers: transformers7 = {},
1046
- dateType = "string",
1047
- unknownType = "any",
1048
- typed = false,
1049
- typedSchema = false,
1050
- mapper = {},
1051
- templates,
1052
- importPath = "zod",
1053
- coercion = false
1054
- } = options;
1055
- const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
1056
- return {
1057
- name: pluginZodName,
1058
- options: {
1059
- extName: output.extName,
1060
- transformers: transformers7,
1061
- include,
1062
- exclude,
1063
- override,
1064
- typed,
1065
- typedSchema,
1066
- dateType,
1067
- unknownType,
1068
- mapper,
1069
- importPath,
1070
- coercion,
1071
- templates: {
1072
- operations: Operations.templates,
1073
- ...templates
1074
- }
1075
- },
1076
- pre: [pluginOasName2, typed ? pluginTsName : void 0].filter(Boolean),
1077
- resolvePath(baseName, pathMode, options2) {
1078
- const root = path2.resolve(this.config.root, this.config.output.path);
1079
- const mode = pathMode ?? FileManager2.getMode(path2.resolve(root, output.path));
1080
- if (mode === "single") {
1081
- return path2.resolve(root, output.path);
1082
- }
1083
- if (options2?.tag && group?.type === "tag") {
1084
- const tag = camelCase2(options2.tag);
1085
- return path2.resolve(root, renderTemplate2(template, { tag }), baseName);
1086
- }
1087
- return path2.resolve(root, output.path, baseName);
1088
- },
1089
- resolveName(name, type) {
1090
- let resolvedName = camelCase2(name, {
1091
- suffix: type ? "schema" : void 0,
1092
- isFile: type === "file"
1093
- });
1094
- if (type === "type") {
1095
- resolvedName = pascalCase2(resolvedName);
1096
- }
1097
- if (type) {
1098
- return transformers7?.name?.(resolvedName, type) || resolvedName;
1099
- }
1100
- return resolvedName;
1101
- },
1102
- async buildStart() {
1103
- const [swaggerPlugin] = PluginManager2.getDependedPlugins(this.plugins, [pluginOasName2]);
1104
- const oas = await swaggerPlugin.context.getOas();
1105
- const root = path2.resolve(this.config.root, this.config.output.path);
1106
- const mode = FileManager2.getMode(path2.resolve(root, output.path));
1107
- const schemaGenerator = new SchemaGenerator3(this.plugin.options, {
1108
- oas,
1109
- pluginManager: this.pluginManager,
1110
- plugin: this.plugin,
1111
- contentType: swaggerPlugin.context.contentType,
1112
- include: void 0,
1113
- override,
1114
- mode,
1115
- output: output.path
1116
- });
1117
- const schemaFiles = await schemaGenerator.build();
1118
- await this.addFile(...schemaFiles);
1119
- const operationGenerator = new OperationGenerator2(this.plugin.options, {
1120
- oas,
1121
- pluginManager: this.pluginManager,
1122
- plugin: this.plugin,
1123
- contentType: swaggerPlugin.context.contentType,
1124
- exclude,
1125
- include,
1126
- override,
1127
- mode
1128
- });
1129
- const operationFiles = await operationGenerator.build();
1130
- await this.addFile(...operationFiles);
1131
- },
1132
- async buildEnd() {
1133
- if (this.config.output.write === false) {
1134
- return;
1135
- }
1136
- const root = path2.resolve(this.config.root, this.config.output.path);
1137
- if (group?.type === "tag") {
1138
- const rootFiles = await getGroupedByTagFiles({
1139
- logger: this.logger,
1140
- files: this.fileManager.files,
1141
- plugin: this.plugin,
1142
- template,
1143
- exportAs: group.exportAs || "{{tag}}Schemas",
1144
- root,
1145
- output
1146
- });
1147
- await this.addFile(...rootFiles);
1148
- }
1149
- await this.fileManager.addIndexes({
1150
- root,
1151
- output,
1152
- meta: { pluginKey: this.plugin.key },
1153
- logger: this.logger
1154
- });
1155
- }
1156
- };
1157
- });
1158
-
1159
- // src/components/Schema.tsx
1160
- import { Fragment as Fragment3, jsx as jsx10, jsxs as jsxs5 } from "@kubb/react/jsx-runtime";
1161
- function Schema2(props) {
1162
- const { keysToOmit, withTypeAnnotation, description } = props;
1163
- const { tree, name } = useSchema2();
1164
- const {
1165
- pluginManager,
1166
- plugin: {
1167
- options: { mapper, typedSchema, coercion }
1168
- }
1169
- } = useApp6();
1170
- const resolvedName = pluginManager.resolveName({
1171
- name,
1172
- pluginKey: [pluginZodName],
1173
- type: "function"
1174
- });
1175
- const resolvedTypeName = pluginManager.resolveName({
1176
- name,
1177
- pluginKey: [pluginZodName],
1178
- type: "type"
1179
- });
1180
- const typeName = pluginManager.resolveName({
1181
- name,
1182
- pluginKey: [pluginTsName],
1183
- type: "type"
1184
- });
1185
- if (!tree.length) {
1186
- return /* @__PURE__ */ jsx10(
1187
- Const2,
1188
- {
1189
- name: resolvedName,
1190
- export: true,
1191
- JSDoc: {
1192
- comments: [description ? `@description ${transformers6.jsStringEscape(description)}` : void 0].filter(Boolean)
1193
- },
1194
- children: "undefined"
1195
- }
1196
- );
1197
- }
1198
- const hasTuple = tree.some((item) => isKeyword3(item, schemaKeywords5.tuple));
1199
- const output = sort(tree).filter((item) => {
1200
- if (hasTuple && (isKeyword3(item, schemaKeywords5.min) || isKeyword3(item, schemaKeywords5.max))) {
1201
- return false;
1202
- }
1203
- return true;
1204
- }).map((item) => parse2(void 0, item, { name, typeName, description, mapper, coercion, keysToOmit })).filter(Boolean).join("");
1205
- const suffix = output.endsWith(".nullable()") ? ".unwrap().and" : ".and";
1206
- return /* @__PURE__ */ jsxs5(Fragment3, { children: [
1207
- /* @__PURE__ */ jsx10(
1208
- Const2,
1209
- {
1210
- export: true,
1211
- name: resolvedName,
1212
- JSDoc: {
1213
- comments: [description ? `@description ${transformers6.jsStringEscape(description)}` : void 0].filter(Boolean)
1214
- },
1215
- children: [
1216
- output,
1217
- keysToOmit?.length ? `${suffix}(z.object({ ${keysToOmit.map((key) => `${key}: z.never()`).join(",")} }))` : void 0,
1218
- withTypeAnnotation && typeName ? ` as z.ZodType<${typeName}>` : ""
1219
- ].filter(Boolean).join("") || ""
1220
- }
1221
- ),
1222
- typedSchema && /* @__PURE__ */ jsx10(Type2, { export: true, name: resolvedTypeName, children: `z.infer<typeof ${resolvedName}>` })
1223
- ] });
1224
- }
1225
- Schema2.File = function({}) {
1226
- const {
1227
- pluginManager,
1228
- plugin: {
1229
- options: { typed }
1230
- }
1231
- } = useApp6();
1232
- const { tree, schema } = useSchema2();
1233
- const withData = tree.some(
1234
- (schema2) => schema2.keyword === schemaKeywords5.array || schema2.keyword === schemaKeywords5.and || schema2.keyword === schemaKeywords5.object || schema2.keyword === schemaKeywords5.union || schema2.keyword === schemaKeywords5.tuple
1235
- );
1236
- const withTypeAnnotation = !!typed;
1237
- return /* @__PURE__ */ jsxs5(Oas8.Schema.File, { output: pluginManager.config.output.path, children: [
1238
- /* @__PURE__ */ jsx10(Schema2.Imports, {}),
1239
- /* @__PURE__ */ jsx10(File6.Source, { children: /* @__PURE__ */ jsx10(Schema2, { withTypeAnnotation, description: schema?.description }) })
1240
- ] });
1241
- };
1242
- Schema2.Imports = () => {
1243
- const {
1244
- pluginManager,
1245
- plugin: {
1246
- options: { typed, importPath }
1247
- }
1248
- } = useApp6();
1249
- const { path: root } = useFile();
1250
- const { name, tree, schema } = useSchema2();
1251
- const typeName = pluginManager.resolveName({
1252
- name,
1253
- pluginKey: [pluginTsName],
1254
- type: "type"
1255
- });
1256
- const typeFileName = pluginManager.resolveName({
1257
- name,
1258
- pluginKey: [pluginTsName],
1259
- type: "file"
1260
- });
1261
- const typePath = pluginManager.resolvePath({
1262
- baseName: typeFileName,
1263
- pluginKey: [pluginTsName]
1264
- });
1265
- const withTypeAnnotation = !!typed;
1266
- return /* @__PURE__ */ jsxs5(Fragment3, { children: [
1267
- /* @__PURE__ */ jsx10(File6.Import, { name: ["z"], path: importPath }),
1268
- withTypeAnnotation && typeName && typePath && /* @__PURE__ */ jsx10(File6.Import, { isTypeOnly: true, root, path: typePath, name: [typeName] })
1269
- ] });
1270
- };
1271
-
1272
- export {
1273
- Schema2 as Schema,
1274
- OperationSchema2 as OperationSchema,
1275
- Operations,
1276
- pluginZodName,
1277
- pluginZod
1278
- };
1279
- //# sourceMappingURL=chunk-ORHAR2XI.js.map