@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
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/Schema.tsx
2
- var _components = require('@kubb/plugin-oas/components');
3
- var _react = require('@kubb/react');
4
-
5
- // ../plugin-ts/src/plugin.ts
6
- var _path = require('path'); var _path2 = _interopRequireDefault(_path);
7
- var _core = require('@kubb/core');
8
- var _transformers = require('@kubb/core/transformers'); var _transformers2 = _interopRequireDefault(_transformers);
9
- var _utils = require('@kubb/core/utils');
10
- var _pluginoas = require('@kubb/plugin-oas');
11
-
12
- // ../plugin-ts/src/OperationGenerator.tsx
13
-
14
-
15
-
16
-
17
- // ../plugin-ts/src/components/OasType.tsx
18
-
19
- var _hooks = require('@kubb/plugin-oas/hooks');
20
- var _jsxruntime = require('@kubb/react/jsx-runtime');
21
- function Template({ name, typeName, api }) {
22
- return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
23
- `export const ${name} = ${JSON.stringify(api, void 0, 2)} as const`,
24
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "br", {}),
25
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.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 = _hooks.useOas.call(void 0, );
31
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Template3, { name, typeName, api: oas.api });
32
- }
33
- OasType.File = function({ name, typeName, templates = defaultTemplates }) {
34
- const {
35
- pluginManager,
36
- plugin: { key: pluginKey }
37
- } = _react.useApp.call(void 0, );
38
- const file = pluginManager.getFile({ name, extName: ".ts", pluginKey });
39
- const Template3 = templates.default;
40
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
41
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: ["Infer"], path: "@kubb/oas", isTypeOnly: true }),
42
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OasType, { Template: Template3, name, typeName }) })
43
- ] }) });
44
- };
45
- OasType.templates = defaultTemplates;
46
-
47
- // ../plugin-ts/src/components/OperationSchema.tsx
48
-
49
- var _parserts = require('@kubb/parser-ts');
50
- var _factory = require('@kubb/parser-ts/factory'); var factory3 = _interopRequireWildcard(_factory); var factory2 = _interopRequireWildcard(_factory); var factory = _interopRequireWildcard(_factory);
51
-
52
-
53
-
54
-
55
- // ../plugin-ts/src/SchemaGenerator.tsx
56
-
57
-
58
-
59
-
60
- // ../plugin-ts/src/components/Schema.tsx
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
- // ../plugin-ts/src/parser/index.ts
70
-
71
-
72
-
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 (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.union)) {
172
- return typeKeywordMapper.union(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
173
- }
174
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.and)) {
175
- return typeKeywordMapper.and(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
176
- }
177
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.array)) {
178
- return typeKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
179
- }
180
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.enum)) {
181
- return typeKeywordMapper.enum(current.args.typeName);
182
- }
183
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.ref)) {
184
- return typeKeywordMapper.ref(current.args.name);
185
- }
186
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.blob)) {
187
- return value();
188
- }
189
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.tuple)) {
190
- return typeKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
191
- }
192
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.const)) {
193
- return typeKeywordMapper.const(current.args.name, current.args.format);
194
- }
195
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.object)) {
196
- const properties = Object.entries(_optionalChain([current, 'access', _ => _.args, 'optionalAccess', _2 => _2.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 === _pluginoas.schemaKeywords.name);
201
- const mappedName = _optionalChain([nameSchema, 'optionalAccess', _3 => _3.args]) || name;
202
- if (_optionalChain([options, 'access', _4 => _4.mapper, 'optionalAccess', _5 => _5[mappedName]])) {
203
- return _optionalChain([options, 'access', _6 => _6.mapper, 'optionalAccess', _7 => _7[mappedName]]);
204
- }
205
- const isNullish = schemas.some((schema) => schema.keyword === _pluginoas.schemaKeywords.nullish);
206
- const isNullable = schemas.some((schema) => schema.keyword === _pluginoas.schemaKeywords.nullable);
207
- const isOptional = schemas.some((schema) => schema.keyword === _pluginoas.schemaKeywords.optional);
208
- const isReadonly = schemas.some((schema) => schema.keyword === _pluginoas.schemaKeywords.readOnly);
209
- const describeSchema = schemas.find((schema) => schema.keyword === _pluginoas.schemaKeywords.describe);
210
- const deprecatedSchema = schemas.find((schema) => schema.keyword === _pluginoas.schemaKeywords.deprecated);
211
- const defaultSchema = schemas.find((schema) => schema.keyword === _pluginoas.schemaKeywords.default);
212
- const exampleSchema = schemas.find((schema) => schema.keyword === _pluginoas.schemaKeywords.example);
213
- const schemaSchema = schemas.find((schema) => schema.keyword === _pluginoas.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 ${_transformers2.default.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
- _optionalChain([schemaSchema, 'optionalAccess', _8 => _8.args, 'optionalAccess', _9 => _9.type]) || _optionalChain([schemaSchema, 'optionalAccess', _10 => _10.args, 'optionalAccess', _11 => _11.format]) ? [`@type ${_optionalChain([schemaSchema, 'optionalAccess', _12 => _12.args, 'optionalAccess', _13 => _13.type]) || "unknown"}${!isOptional ? "" : " | undefined"}`, _optionalChain([schemaSchema, 'optionalAccess', _14 => _14.args, 'optionalAccess', _15 => _15.format])].filter(Boolean).join(", ") : void 0
244
- ].filter(Boolean)
245
- });
246
- });
247
- const additionalProperties = _optionalChain([current, 'access', _16 => _16.args, 'optionalAccess', _17 => _17.additionalProperties, 'optionalAccess', _18 => _18.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 (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.datetime)) {
253
- return typeKeywordMapper.datetime();
254
- }
255
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.date)) {
256
- return typeKeywordMapper.date(current.args.type);
257
- }
258
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.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
-
269
- function Schema(props) {
270
- const { keysToOmit, description } = props;
271
- const { tree, name } = _hooks.useSchema.call(void 0, );
272
- const {
273
- pluginManager,
274
- plugin: {
275
- options: { mapper, enumType, optionalType }
276
- }
277
- } = _react.useApp.call(void 0, );
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 === _pluginoas.schemaKeywords.nullish);
294
- const isNullable = tree.some((item) => item.keyword === _pluginoas.schemaKeywords.nullable);
295
- const isOptional = tree.some((item) => item.keyword === _pluginoas.schemaKeywords.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: _optionalChain([keysToOmit, 'optionalAccess', _19 => _19.length]) ? factory2.createOmitDeclaration({
316
- keys: keysToOmit,
317
- type,
318
- nonNullable: true
319
- }) : type
320
- });
321
- const enumSchemas = _pluginoas.SchemaGenerator.deepSearch(tree, _pluginoas.schemaKeywords.enum);
322
- if (enumSchemas) {
323
- enumSchemas.forEach((enumSchema) => {
324
- extraNodes.push(
325
- ...factory2.createEnumDeclaration({
326
- name: _transformers2.default.camelCase(enumSchema.args.name),
327
- typeName: enumSchema.args.typeName,
328
- enums: enumSchema.args.items.map((item) => item.value === void 0 ? void 0 : [_transformers2.default.trimQuotes(_optionalChain([item, 'access', _20 => _20.name, 'optionalAccess', _21 => _21.toString, 'call', _22 => _22()])), item.value]).filter(Boolean),
329
- type: enumType
330
- })
331
- );
332
- });
333
- }
334
- nodes.push(
335
- factory2.appendJSDocToNode({
336
- node,
337
- comments: [description ? `@description ${_transformers2.default.jsStringEscape(description)}` : void 0].filter(Boolean)
338
- })
339
- );
340
- const filterdNodes = nodes.filter(
341
- (node2) => !extraNodes.some(
342
- (extraNode) => _optionalChain([extraNode, 'optionalAccess', _23 => _23.name, 'optionalAccess', _24 => _24.escapedText]) === _optionalChain([node2, 'optionalAccess', _25 => _25.name, 'optionalAccess', _26 => _26.escapedText])
343
- )
344
- );
345
- return _parserts.print.call(void 0, [...extraNodes, ...filterdNodes]);
346
- }
347
- Schema.File = function({}) {
348
- const { pluginManager } = _react.useApp.call(void 0, );
349
- const { schema } = _hooks.useSchema.call(void 0, );
350
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema.File, { output: pluginManager.config.output.path, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema, { description: _optionalChain([schema, 'optionalAccess', _27 => _27.description]) }) }) });
351
- };
352
-
353
- // ../plugin-ts/src/SchemaGenerator.tsx
354
-
355
- var SchemaGenerator2 = class extends _pluginoas.SchemaGenerator {
356
- async schema(name, schema, options) {
357
- const { oas, pluginManager, plugin, mode, output } = this.context;
358
- const root = _react.createRoot.call(void 0, {
359
- logger: pluginManager.logger
360
- });
361
- const tree = this.parse({ schema, name });
362
- root.render(
363
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema, { name, value: schema, tree, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema.File, {}) }) }) })
364
- );
365
- return root.files;
366
- }
367
- };
368
-
369
- // ../plugin-ts/src/components/OperationSchema.tsx
370
-
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: _transformers2.default.pascalCase(key),
440
- type
441
- });
442
- }).filter(Boolean)
443
- ),
444
- modifiers: [factory3.modifiers.export]
445
- });
446
- return _parserts.print.call(void 0, namespaceNode);
447
- }
448
- function OperationSchema({ keysToOmit, description }) {
449
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema, { keysToOmit, description });
450
- }
451
- OperationSchema.File = function({}) {
452
- const { pluginManager, plugin, mode, fileManager } = _react.useApp.call(void 0, );
453
- const oas = _hooks.useOas.call(void 0, );
454
- const { getSchemas, getFile, getName } = _hooks.useOperationManager.call(void 0, );
455
- const operation = _hooks.useOperation.call(void 0, );
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__ */ _jsxruntime.jsxs.call(void 0, _components.Oas.Schema, { name, value: schema, tree, children: [
470
- mode === "split" && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema.Imports, { extName: plugin.options.extName, isTypeOnly: true }),
471
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OperationSchema, { description, keysToOmit }) })
472
- ] }, i);
473
- };
474
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
475
- items.map(mapItem),
476
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: printCombinedSchema({ name: factoryName, operation, schemas, pluginManager }) })
477
- ] }) });
478
- };
479
-
480
- // ../plugin-ts/src/OperationGenerator.tsx
481
-
482
- var OperationGenerator = class extends _pluginoas.OperationGenerator {
483
- async all(operations) {
484
- const { oas, pluginManager, plugin, mode } = this.context;
485
- const root = _react.createRoot.call(void 0, {
486
- logger: pluginManager.logger
487
- });
488
- root.render(
489
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, operations, generator: this, children: plugin.options.oasType && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 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 = _react.createRoot.call(void 0, {
496
- logger: pluginManager.logger
497
- });
498
- root.render(
499
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Operation, { operation, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OperationSchema.File, {}) }) }) })
500
- );
501
- return root.files;
502
- }
503
- };
504
-
505
- // ../plugin-ts/src/plugin.ts
506
- var pluginTsName = "plugin-ts";
507
- var pluginTs = _core.createPlugin.call(void 0, (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 = _optionalChain([group, 'optionalAccess', _28 => _28.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: [_pluginoas.pluginOasName],
541
- resolvePath(baseName, pathMode, options2) {
542
- const root = _path2.default.resolve(this.config.root, this.config.output.path);
543
- const mode = _nullishCoalesce(pathMode, () => ( _core.FileManager.getMode(_path2.default.resolve(root, output.path))));
544
- if (mode === "single") {
545
- return _path2.default.resolve(root, output.path);
546
- }
547
- if (_optionalChain([options2, 'optionalAccess', _29 => _29.tag]) && _optionalChain([group, 'optionalAccess', _30 => _30.type]) === "tag") {
548
- const tag = _transformers.camelCase.call(void 0, options2.tag);
549
- return _path2.default.resolve(root, _utils.renderTemplate.call(void 0, template, { tag }), baseName);
550
- }
551
- return _path2.default.resolve(root, output.path, baseName);
552
- },
553
- resolveName(name, type) {
554
- const resolvedName = _transformers.pascalCase.call(void 0, name, { isFile: type === "file" });
555
- if (type) {
556
- return _optionalChain([transformers7, 'optionalAccess', _31 => _31.name, 'optionalCall', _32 => _32(resolvedName, type)]) || resolvedName;
557
- }
558
- return resolvedName;
559
- },
560
- async buildStart() {
561
- const [swaggerPlugin] = _core.PluginManager.getDependedPlugins(this.plugins, [_pluginoas.pluginOasName]);
562
- const oas = await swaggerPlugin.context.getOas();
563
- const root = _path2.default.resolve(this.config.root, this.config.output.path);
564
- const mode = _core.FileManager.getMode(_path2.default.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 = _path2.default.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
-
607
-
608
-
609
-
610
- // src/parser/index.ts
611
-
612
-
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(${_optionalChain([items, 'optionalAccess', _33 => _33.join, 'call', _34 => _34("")])})`, min !== void 0 ? `.min(${min})` : void 0, max !== void 0 ? `.max(${max})` : void 0].filter(Boolean).join("");
633
- },
634
- tuple: (items = []) => `z.tuple([${_optionalChain([items, 'optionalAccess', _35 => _35.join, 'call', _36 => _36(", ")])}])`,
635
- enum: (items = []) => `z.enum([${_optionalChain([items, 'optionalAccess', _37 => _37.join, 'call', _38 => _38(", ")])}])`,
636
- union: (items = []) => `z.union([${_optionalChain([items, 'optionalAccess', _39 => _39.join, 'call', _40 => _40(", ")])}])`,
637
- const: (value) => `z.literal(${_nullishCoalesce(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(${_nullishCoalesce(value, () => ( ""))})`,
682
- and: (items = []) => _optionalChain([items, 'optionalAccess', _41 => _41.map, 'call', _42 => _42((item) => `.and(${item})`), 'access', _43 => _43.join, 'call', _44 => _44("")]),
683
- describe: (value = "") => `.describe(${value})`,
684
- min: (value) => `.min(${_nullishCoalesce(value, () => ( ""))})`,
685
- max: (value) => `.max(${_nullishCoalesce(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
- _pluginoas.schemaKeywords.string,
705
- _pluginoas.schemaKeywords.datetime,
706
- _pluginoas.schemaKeywords.date,
707
- _pluginoas.schemaKeywords.time,
708
- _pluginoas.schemaKeywords.tuple,
709
- _pluginoas.schemaKeywords.number,
710
- _pluginoas.schemaKeywords.object,
711
- _pluginoas.schemaKeywords.enum,
712
- _pluginoas.schemaKeywords.url,
713
- _pluginoas.schemaKeywords.email,
714
- _pluginoas.schemaKeywords.firstName,
715
- _pluginoas.schemaKeywords.lastName,
716
- _pluginoas.schemaKeywords.password,
717
- _pluginoas.schemaKeywords.matches,
718
- _pluginoas.schemaKeywords.uuid,
719
- _pluginoas.schemaKeywords.min,
720
- _pluginoas.schemaKeywords.max,
721
- _pluginoas.schemaKeywords.default,
722
- _pluginoas.schemaKeywords.describe,
723
- _pluginoas.schemaKeywords.optional,
724
- _pluginoas.schemaKeywords.nullable,
725
- _pluginoas.schemaKeywords.nullish,
726
- _pluginoas.schemaKeywords.null
727
- ];
728
- if (!items) {
729
- return [];
730
- }
731
- return _transformers2.default.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 (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.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 (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.and)) {
750
- const items = sort(current.args).filter((schema) => {
751
- return ![_pluginoas.schemaKeywords.optional, _pluginoas.schemaKeywords.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 (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.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 (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.enum)) {
763
- if (current.args.asConst) {
764
- return zodKeywordMapper.union(
765
- current.args.items.map((schema) => {
766
- return parse2(
767
- current,
768
- {
769
- keyword: _pluginoas.schemaKeywords.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 _transformers2.default.stringify(schema.value);
781
- }
782
- return _transformers2.default.stringify(schema.value);
783
- })
784
- );
785
- }
786
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.ref)) {
787
- return zodKeywordMapper.ref(_optionalChain([current, 'access', _45 => _45.args, 'optionalAccess', _46 => _46.name]));
788
- }
789
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.object)) {
790
- const properties = Object.entries(_optionalChain([current, 'access', _47 => _47.args, 'optionalAccess', _48 => _48.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 === _pluginoas.schemaKeywords.name);
795
- const mappedName = _optionalChain([nameSchema, 'optionalAccess', _49 => _49.args]) || name;
796
- if (_optionalChain([options, 'access', _50 => _50.mapper, 'optionalAccess', _51 => _51[mappedName]])) {
797
- return `"${name}": ${_optionalChain([options, 'access', _52 => _52.mapper, 'optionalAccess', _53 => _53[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 = _optionalChain([current, 'access', _54 => _54.args, 'optionalAccess', _55 => _55.additionalProperties, 'optionalAccess', _56 => _56.length]) ? current.args.additionalProperties.map((schema) => parse2(current, schema, options)).filter(Boolean).at(0) : void 0;
804
- const text = [
805
- zodKeywordMapper.object(properties),
806
- _optionalChain([current, 'access', _57 => _57.args, 'optionalAccess', _58 => _58.strict]) ? zodKeywordMapper.strict() : void 0,
807
- additionalProperties ? zodKeywordMapper.catchall(additionalProperties) : void 0
808
- ].filter(Boolean);
809
- return text.join("");
810
- }
811
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.tuple)) {
812
- return zodKeywordMapper.tuple(
813
- sort(current.args.items).map((schema) => parse2(current, schema, options)).filter(Boolean)
814
- );
815
- }
816
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.const)) {
817
- if (current.args.format === "number" && current.args.value !== void 0) {
818
- return zodKeywordMapper.const(Number.parseInt(_optionalChain([current, 'access', _59 => _59.args, 'access', _60 => _60.value, 'optionalAccess', _61 => _61.toString, 'call', _62 => _62()])));
819
- }
820
- return zodKeywordMapper.const(_transformers2.default.stringify(current.args.value));
821
- }
822
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.matches)) {
823
- if (current.args) {
824
- return zodKeywordMapper.matches(_transformers2.default.toRegExpString(current.args));
825
- }
826
- }
827
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.default)) {
828
- if (current.args) {
829
- return zodKeywordMapper.default(current.args);
830
- }
831
- }
832
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.describe)) {
833
- if (current.args) {
834
- return zodKeywordMapper.describe(_transformers2.default.stringify(current.args.toString()));
835
- }
836
- }
837
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.string)) {
838
- return zodKeywordMapper.string(options.coercion);
839
- }
840
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.number) || _pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.integer)) {
841
- return zodKeywordMapper.number(options.coercion);
842
- }
843
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.min)) {
844
- return zodKeywordMapper.min(current.args);
845
- }
846
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.max)) {
847
- return zodKeywordMapper.max(current.args);
848
- }
849
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.datetime)) {
850
- return zodKeywordMapper.datetime(current.args.offset, current.args.local);
851
- }
852
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.date)) {
853
- return zodKeywordMapper.date(current.args.type, options.coercion);
854
- }
855
- if (_pluginoas.isKeyword.call(void 0, current, _pluginoas.schemaKeywords.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
-
870
-
871
-
872
-
873
-
874
- var _utils3 = require('@kubb/plugin-oas/utils');
875
-
876
- // src/OperationGenerator.tsx
877
-
878
-
879
-
880
-
881
- // src/components/OperationSchema.tsx
882
-
883
-
884
-
885
-
886
-
887
- // src/SchemaGenerator.tsx
888
-
889
-
890
-
891
-
892
- var SchemaGenerator3 = class extends _pluginoas.SchemaGenerator {
893
- async schema(name, schema, options) {
894
- const { oas, pluginManager, plugin, mode, output } = this.context;
895
- const root = _react.createRoot.call(void 0, {
896
- logger: pluginManager.logger
897
- });
898
- const tree = this.parse({ schema, name });
899
- root.render(
900
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema, { name, value: schema, tree, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema2.File, {}) }) }) })
901
- );
902
- return root.files;
903
- }
904
- };
905
-
906
- // src/components/OperationSchema.tsx
907
-
908
- function OperationSchema2({ description, keysToOmit }) {
909
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema2, { keysToOmit, withTypeAnnotation: false, description });
910
- }
911
- OperationSchema2.File = function({}) {
912
- const { pluginManager, plugin, mode } = _react.useApp.call(void 0, );
913
- const { getSchemas, getFile } = _hooks.useOperationManager.call(void 0, );
914
- const oas = _hooks.useOas.call(void 0, );
915
- const operation = _hooks.useOperation.call(void 0, );
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(_optionalChain([schema, 'optionalAccess', _63 => _63.required])) ? !!schema.required.length : !!_optionalChain([schema, 'optionalAccess', _64 => _64.required]);
928
- const optional = !required && !!name.includes("Params");
929
- const tree = generator.parse({ schema, name });
930
- return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _components.Oas.Schema, { name, value: schema, tree: [...tree, optional ? { keyword: _pluginoas.schemaKeywords.optional } : void 0].filter(Boolean), children: [
931
- mode === "split" && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Schema.Imports, { extName: plugin.options.extName }),
932
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OperationSchema2, { description, keysToOmit }) })
933
- ] }, i);
934
- };
935
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
936
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: ["z"], path: plugin.options.importPath }),
937
- items.map(mapItem)
938
- ] }) });
939
- };
940
-
941
- // src/components/Operations.tsx
942
-
943
-
944
-
945
-
946
- function Template2({ operationsName, pathsName, operations }) {
947
- const { groupSchemasByName } = _hooks.useOperationManager.call(void 0, );
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__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
967
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Const, { export: true, name: operationsName, asConst: true, children: `{${_transformers2.default.stringifyObject(operationsJSON)}}` }),
968
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Const, { export: true, name: pathsName, asConst: true, children: `{${_transformers2.default.stringifyObject(pathsJSON)}}` })
969
- ] });
970
- }
971
- function RootTemplate({ children }) {
972
- const {
973
- mode,
974
- pluginManager,
975
- plugin: {
976
- key: pluginKey,
977
- options: { extName }
978
- }
979
- } = _react.useApp.call(void 0, );
980
- const { getFile } = _hooks.useOperationManager.call(void 0, );
981
- const operations = _hooks.useOperations.call(void 0, );
982
- const { groupSchemasByName } = _hooks.useOperationManager.call(void 0, );
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__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { extName, name: names, root: file.path, path: getFile(operation).path }, index);
988
- }).filter(Boolean);
989
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, exportable: false, children: [
990
- mode === "split" && imports,
991
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children })
992
- ] }) });
993
- }
994
- var defaultTemplates2 = { default: Template2, root: RootTemplate };
995
- function Operations({ Template: Template3 = defaultTemplates2.default }) {
996
- const operations = _hooks.useOperations.call(void 0, );
997
- return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 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__ */ _jsxruntime.jsx.call(void 0, RootTemplate2, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Operations, { Template: Template3 }) });
1004
- };
1005
- Operations.templates = defaultTemplates2;
1006
-
1007
- // src/OperationGenerator.tsx
1008
-
1009
- var OperationGenerator2 = class extends _pluginoas.OperationGenerator {
1010
- async all(operations, _operationsByMethod) {
1011
- const { pluginManager, oas, plugin, mode } = this.context;
1012
- const root = _react.createRoot.call(void 0, {
1013
- logger: pluginManager.logger
1014
- });
1015
- const templates = {
1016
- operations: Operations.templates,
1017
- ...this.options.templates
1018
- };
1019
- root.render(
1020
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, operations, generator: this, children: templates.operations && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 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 = _react.createRoot.call(void 0, {
1027
- logger: pluginManager.logger
1028
- });
1029
- root.render(
1030
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _components.Oas.Operation, { operation, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, OperationSchema2.File, {}) }) }) })
1031
- );
1032
- return root.files;
1033
- }
1034
- };
1035
-
1036
- // src/plugin.ts
1037
- var pluginZodName = "plugin-zod";
1038
- var pluginZod = _core.createPlugin.call(void 0, (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 = _optionalChain([group, 'optionalAccess', _65 => _65.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: [_pluginoas.pluginOasName, typed ? pluginTsName : void 0].filter(Boolean),
1077
- resolvePath(baseName, pathMode, options2) {
1078
- const root = _path2.default.resolve(this.config.root, this.config.output.path);
1079
- const mode = _nullishCoalesce(pathMode, () => ( _core.FileManager.getMode(_path2.default.resolve(root, output.path))));
1080
- if (mode === "single") {
1081
- return _path2.default.resolve(root, output.path);
1082
- }
1083
- if (_optionalChain([options2, 'optionalAccess', _66 => _66.tag]) && _optionalChain([group, 'optionalAccess', _67 => _67.type]) === "tag") {
1084
- const tag = _transformers.camelCase.call(void 0, options2.tag);
1085
- return _path2.default.resolve(root, _utils.renderTemplate.call(void 0, template, { tag }), baseName);
1086
- }
1087
- return _path2.default.resolve(root, output.path, baseName);
1088
- },
1089
- resolveName(name, type) {
1090
- let resolvedName = _transformers.camelCase.call(void 0, name, {
1091
- suffix: type ? "schema" : void 0,
1092
- isFile: type === "file"
1093
- });
1094
- if (type === "type") {
1095
- resolvedName = _transformers.pascalCase.call(void 0, resolvedName);
1096
- }
1097
- if (type) {
1098
- return _optionalChain([transformers7, 'optionalAccess', _68 => _68.name, 'optionalCall', _69 => _69(resolvedName, type)]) || resolvedName;
1099
- }
1100
- return resolvedName;
1101
- },
1102
- async buildStart() {
1103
- const [swaggerPlugin] = _core.PluginManager.getDependedPlugins(this.plugins, [_pluginoas.pluginOasName]);
1104
- const oas = await swaggerPlugin.context.getOas();
1105
- const root = _path2.default.resolve(this.config.root, this.config.output.path);
1106
- const mode = _core.FileManager.getMode(_path2.default.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.default.resolve(this.config.root, this.config.output.path);
1137
- if (_optionalChain([group, 'optionalAccess', _70 => _70.type]) === "tag") {
1138
- const rootFiles = await _utils3.getGroupedByTagFiles.call(void 0, {
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
-
1161
- function Schema2(props) {
1162
- const { keysToOmit, withTypeAnnotation, description } = props;
1163
- const { tree, name } = _hooks.useSchema.call(void 0, );
1164
- const {
1165
- pluginManager,
1166
- plugin: {
1167
- options: { mapper, typedSchema, coercion }
1168
- }
1169
- } = _react.useApp.call(void 0, );
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__ */ _jsxruntime.jsx.call(void 0,
1187
- _react.Const,
1188
- {
1189
- name: resolvedName,
1190
- export: true,
1191
- JSDoc: {
1192
- comments: [description ? `@description ${_transformers2.default.jsStringEscape(description)}` : void 0].filter(Boolean)
1193
- },
1194
- children: "undefined"
1195
- }
1196
- );
1197
- }
1198
- const hasTuple = tree.some((item) => _pluginoas.isKeyword.call(void 0, item, _pluginoas.schemaKeywords.tuple));
1199
- const output = sort(tree).filter((item) => {
1200
- if (hasTuple && (_pluginoas.isKeyword.call(void 0, item, _pluginoas.schemaKeywords.min) || _pluginoas.isKeyword.call(void 0, item, _pluginoas.schemaKeywords.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__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
1207
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
1208
- _react.Const,
1209
- {
1210
- export: true,
1211
- name: resolvedName,
1212
- JSDoc: {
1213
- comments: [description ? `@description ${_transformers2.default.jsStringEscape(description)}` : void 0].filter(Boolean)
1214
- },
1215
- children: [
1216
- output,
1217
- _optionalChain([keysToOmit, 'optionalAccess', _71 => _71.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__ */ _jsxruntime.jsx.call(void 0, _react.Type, { 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
- } = _react.useApp.call(void 0, );
1232
- const { tree, schema } = _hooks.useSchema.call(void 0, );
1233
- const withData = tree.some(
1234
- (schema2) => schema2.keyword === _pluginoas.schemaKeywords.array || schema2.keyword === _pluginoas.schemaKeywords.and || schema2.keyword === _pluginoas.schemaKeywords.object || schema2.keyword === _pluginoas.schemaKeywords.union || schema2.keyword === _pluginoas.schemaKeywords.tuple
1235
- );
1236
- const withTypeAnnotation = !!typed;
1237
- return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _components.Oas.Schema.File, { output: pluginManager.config.output.path, children: [
1238
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema2.Imports, {}),
1239
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Schema2, { withTypeAnnotation, description: _optionalChain([schema, 'optionalAccess', _72 => _72.description]) }) })
1240
- ] });
1241
- };
1242
- Schema2.Imports = () => {
1243
- const {
1244
- pluginManager,
1245
- plugin: {
1246
- options: { typed, importPath }
1247
- }
1248
- } = _react.useApp.call(void 0, );
1249
- const { path: root } = _react.useFile.call(void 0, );
1250
- const { name, tree, schema } = _hooks.useSchema.call(void 0, );
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__ */ _jsxruntime.jsxs.call(void 0, _jsxruntime.Fragment, { children: [
1267
- /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: ["z"], path: importPath }),
1268
- withTypeAnnotation && typeName && typePath && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { isTypeOnly: true, root, path: typePath, name: [typeName] })
1269
- ] });
1270
- };
1271
-
1272
-
1273
-
1274
-
1275
-
1276
-
1277
-
1278
- exports.Schema = Schema2; exports.OperationSchema = OperationSchema2; exports.Operations = Operations; exports.pluginZodName = pluginZodName; exports.pluginZod = pluginZod;
1279
- //# sourceMappingURL=chunk-KP7GRUCO.cjs.map