@kubb/plugin-zod 3.0.0-alpha.15 → 3.0.0-alpha.17

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 (77) hide show
  1. package/dist/chunk-35K4FTRK.js +367 -0
  2. package/dist/chunk-35K4FTRK.js.map +1 -0
  3. package/dist/{chunk-DL4VN2AP.cjs → chunk-HH7DZZUF.cjs} +271 -710
  4. package/dist/chunk-HH7DZZUF.cjs.map +1 -0
  5. package/dist/{chunk-BDYQNKJC.js → chunk-ORFIAMY6.js} +252 -689
  6. package/dist/chunk-ORFIAMY6.js.map +1 -0
  7. package/dist/chunk-Q7F7N57F.cjs +377 -0
  8. package/dist/chunk-Q7F7N57F.cjs.map +1 -0
  9. package/dist/components.cjs +5 -10
  10. package/dist/components.cjs.map +1 -1
  11. package/dist/components.d.cts +21 -19
  12. package/dist/components.d.ts +21 -19
  13. package/dist/components.js +1 -5
  14. package/dist/components.js.map +1 -1
  15. package/dist/generators.cjs +17 -0
  16. package/dist/generators.cjs.map +1 -0
  17. package/dist/generators.d.cts +11 -0
  18. package/dist/generators.d.ts +11 -0
  19. package/dist/generators.js +4 -0
  20. package/dist/generators.js.map +1 -0
  21. package/dist/index.cjs +5 -4
  22. package/dist/index.cjs.map +1 -1
  23. package/dist/index.d.cts +5 -137
  24. package/dist/index.d.ts +5 -137
  25. package/dist/index.js +2 -2
  26. package/dist/types-29VBUXHk.d.cts +126 -0
  27. package/dist/types-29VBUXHk.d.ts +126 -0
  28. package/package.json +19 -17
  29. package/src/components/Operations.tsx +12 -98
  30. package/src/components/Zod.tsx +66 -0
  31. package/src/components/index.ts +1 -2
  32. package/src/generators/__snapshots__/anyof.ts +3 -0
  33. package/src/generators/__snapshots__/createPet.ts +15 -0
  34. package/src/generators/__snapshots__/createPetWithUnknownTypeAny.ts +13 -0
  35. package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +15 -0
  36. package/src/generators/__snapshots__/deletePet.ts +3 -0
  37. package/src/generators/__snapshots__/enumNamesType.ts +3 -0
  38. package/src/generators/__snapshots__/enumNullable.ts +3 -0
  39. package/src/generators/__snapshots__/enumVarNamesType.ts +3 -0
  40. package/src/generators/__snapshots__/example.ts +3 -0
  41. package/src/generators/__snapshots__/getPets.ts +18 -0
  42. package/src/generators/__snapshots__/mixedValueTypeConst.ts +6 -0
  43. package/src/generators/__snapshots__/nullableString.ts +3 -0
  44. package/src/generators/__snapshots__/nullableStringUuid.ts +3 -0
  45. package/src/generators/__snapshots__/nullableStringWithAnyOf.ts +3 -0
  46. package/src/generators/__snapshots__/numberValueConst.ts +6 -0
  47. package/src/generators/__snapshots__/oneof.ts +3 -0
  48. package/src/generators/__snapshots__/operations.ts +60 -0
  49. package/src/generators/__snapshots__/optionalPetInfer.ts +5 -0
  50. package/src/generators/__snapshots__/optionalPetTyped.ts +3 -0
  51. package/src/generators/__snapshots__/order.ts +10 -0
  52. package/src/generators/__snapshots__/orderDateTyeString.ts +10 -0
  53. package/src/generators/__snapshots__/orderDateTypeFalse.ts +10 -0
  54. package/src/generators/__snapshots__/orderDateTypeString.ts +10 -0
  55. package/src/generators/__snapshots__/pet.ts +3 -0
  56. package/src/generators/__snapshots__/petArray.ts +10 -0
  57. package/src/generators/__snapshots__/petCoercion.ts +8 -0
  58. package/src/generators/__snapshots__/petTupleObject.ts +6 -0
  59. package/src/generators/__snapshots__/petWithMapper.ts +3 -0
  60. package/src/generators/__snapshots__/pets.ts +3 -0
  61. package/src/generators/__snapshots__/recursive.ts +3 -0
  62. package/src/generators/__snapshots__/showPetById.ts +21 -0
  63. package/src/generators/__snapshots__/stringValueConst.ts +6 -0
  64. package/src/generators/__snapshots__/uuidSchema.ts +3 -0
  65. package/src/generators/index.ts +2 -0
  66. package/src/generators/operationsGenerator.tsx +34 -0
  67. package/src/generators/zodGenerator.tsx +114 -0
  68. package/src/plugin.ts +8 -11
  69. package/src/types.ts +33 -42
  70. package/dist/Operations-BG26e_MW.d.cts +0 -47
  71. package/dist/Operations-BG26e_MW.d.ts +0 -47
  72. package/dist/chunk-BDYQNKJC.js.map +0 -1
  73. package/dist/chunk-DL4VN2AP.cjs.map +0 -1
  74. package/src/SchemaGenerator.tsx +0 -22
  75. package/src/components/OperationSchema.tsx +0 -60
  76. package/src/components/Schema.tsx +0 -166
  77. package/src/components/__snapshots__/operations.ts +0 -53
@@ -0,0 +1,114 @@
1
+ import { type OperationSchema as OperationSchemaType, SchemaGenerator, createReactGenerator, schemaKeywords } from '@kubb/plugin-oas'
2
+ import { Oas } from '@kubb/plugin-oas/components'
3
+ import { useOas, useOperationManager, useSchemaManager } from '@kubb/plugin-oas/hooks'
4
+ import { pluginTsName } from '@kubb/plugin-ts'
5
+ import { File, useApp } from '@kubb/react'
6
+ import { Zod } from '../components'
7
+ import type { PluginZod } from '../types'
8
+
9
+ export const zodGenerator = createReactGenerator<PluginZod>({
10
+ name: 'zod',
11
+ Operation({ operation, options }) {
12
+ const { coercion, inferred, typed, mapper } = options
13
+
14
+ const { plugin, pluginManager, mode } = useApp<PluginZod>()
15
+ const oas = useOas()
16
+ const { getSchemas, getFile } = useOperationManager()
17
+ const schemaManager = useSchemaManager()
18
+
19
+ const file = getFile(operation)
20
+ const schemas = getSchemas(operation)
21
+ const schemaGenerator = new SchemaGenerator(options, {
22
+ oas,
23
+ plugin,
24
+ pluginManager,
25
+ mode,
26
+ override: options.override,
27
+ })
28
+
29
+ const operationSchemas = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response]
30
+ .flat()
31
+ .filter(Boolean)
32
+
33
+ const mapOperationSchema = ({ name, schema, description, keysToOmit, ...options }: OperationSchemaType, i: number) => {
34
+ // hack so Params can be optional when needed
35
+ const required = Array.isArray(schema?.required) ? !!schema.required.length : !!schema?.required
36
+ const optional = !required && !!name.includes('Params')
37
+ const tree = [...schemaGenerator.parse({ schema, name }), optional ? { keyword: schemaKeywords.optional } : undefined].filter(Boolean)
38
+ const imports = schemaManager.getImports(tree)
39
+
40
+ const zod = {
41
+ name: schemaManager.getName(name, { type: 'function' }),
42
+ inferTypeName: schemaManager.getName(name, { type: 'type' }),
43
+ file: schemaManager.getFile(name),
44
+ }
45
+
46
+ const type = {
47
+ name: schemaManager.getName(name, { type: 'type', pluginKey: [pluginTsName] }),
48
+ file: schemaManager.getFile(options.operationName || name, { pluginKey: [pluginTsName], tag: options.operation?.getTags()[0]?.name }),
49
+ }
50
+
51
+ return (
52
+ <Oas.Schema key={i} name={name} value={schema} tree={tree}>
53
+ {typed && <File.Import isTypeOnly root={file.path} path={type.file.path} name={[type.name]} />}
54
+ {imports.map((imp, index) => (
55
+ <File.Import key={index} root={file.path} path={imp.path} name={imp.name} />
56
+ ))}
57
+ <Zod
58
+ name={zod.name}
59
+ typeName={typed ? type.name : undefined}
60
+ inferTypeName={inferred ? zod.inferTypeName : undefined}
61
+ description={description}
62
+ tree={tree}
63
+ mapper={mapper}
64
+ coercion={coercion}
65
+ keysToOmit={keysToOmit}
66
+ />
67
+ </Oas.Schema>
68
+ )
69
+ }
70
+
71
+ return (
72
+ <File baseName={file.baseName} path={file.path} meta={file.meta}>
73
+ <File.Import name={['z']} path={plugin.options.importPath} />
74
+ {operationSchemas.map(mapOperationSchema)}
75
+ </File>
76
+ )
77
+ },
78
+ Schema({ schema, options }) {
79
+ const { coercion, inferred, typed, mapper, importPath } = options
80
+ const { getName, getFile, getImports } = useSchemaManager()
81
+ const imports = getImports(schema.tree)
82
+
83
+ const zod = {
84
+ name: getName(schema.name, { type: 'function' }),
85
+ inferTypeName: getName(schema.name, { type: 'type' }),
86
+ file: getFile(schema.name),
87
+ }
88
+
89
+ const type = {
90
+ name: getName(schema.name, { type: 'type', pluginKey: [pluginTsName] }),
91
+ file: getFile(schema.name, { pluginKey: [pluginTsName] }),
92
+ }
93
+
94
+ return (
95
+ <File baseName={zod.file.baseName} path={zod.file.path} meta={zod.file.meta}>
96
+ <File.Import name={['z']} path={importPath} />
97
+ {typed && <File.Import isTypeOnly root={zod.file.path} path={type.file.path} name={[type.name]} />}
98
+ {imports.map((imp, index) => (
99
+ <File.Import key={index} root={zod.file.path} path={imp.path} name={imp.name} />
100
+ ))}
101
+
102
+ <Zod
103
+ name={zod.name}
104
+ typeName={typed ? type.name : undefined}
105
+ inferTypeName={inferred ? zod.inferTypeName : undefined}
106
+ description={schema.value.description}
107
+ tree={schema.tree}
108
+ mapper={mapper}
109
+ coercion={coercion}
110
+ />
111
+ </File>
112
+ )
113
+ },
114
+ })
package/src/plugin.ts CHANGED
@@ -9,8 +9,8 @@ import { pluginTsName } from '@kubb/plugin-ts'
9
9
 
10
10
  import type { Plugin } from '@kubb/core'
11
11
  import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
12
- import { zodParser } from './SchemaGenerator.tsx'
13
- import { Operations } from './components/Operations.tsx'
12
+ import { operationsGenerator } from './generators'
13
+ import { zodGenerator } from './generators/zodGenerator.tsx'
14
14
  import type { PluginZod } from './types.ts'
15
15
 
16
16
  export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
@@ -26,11 +26,11 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
26
26
  dateType = 'string',
27
27
  unknownType = 'any',
28
28
  typed = false,
29
- typedSchema = false,
30
29
  mapper = {},
31
- templates,
30
+ operations = false,
32
31
  importPath = 'zod',
33
32
  coercion = false,
33
+ inferred = false,
34
34
  } = options
35
35
  const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
36
36
 
@@ -47,16 +47,13 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
47
47
  exclude,
48
48
  override,
49
49
  typed,
50
- typedSchema,
51
50
  dateType,
52
51
  unknownType,
53
52
  mapper,
54
53
  importPath,
55
54
  coercion,
56
- templates: {
57
- operations: Operations.templates,
58
- ...templates,
59
- },
55
+ operations,
56
+ inferred,
60
57
  },
61
58
  pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
62
59
  resolvePath(baseName, pathMode, options) {
@@ -113,7 +110,7 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
113
110
  output: output.path,
114
111
  })
115
112
 
116
- const schemaFiles = await schemaGenerator.build(zodParser)
113
+ const schemaFiles = await schemaGenerator.build(...[zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean))
117
114
  await this.addFile(...schemaFiles)
118
115
 
119
116
  const operationGenerator = new OperationGenerator(this.plugin.options, {
@@ -127,7 +124,7 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
127
124
  mode,
128
125
  })
129
126
 
130
- const operationFiles = await operationGenerator.build(zodParser)
127
+ const operationFiles = await operationGenerator.build(...[zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean))
131
128
  await this.addFile(...operationFiles)
132
129
 
133
130
  if (this.config.output.exportType) {
package/src/types.ts CHANGED
@@ -2,11 +2,6 @@ import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core
2
2
  import type * as KubbFile from '@kubb/fs/types'
3
3
  import type { SchemaObject } from '@kubb/oas'
4
4
  import type { Exclude, Include, Override, ResolvePathOptions, Schema } from '@kubb/plugin-oas'
5
- import type { Operations } from './components/Operations'
6
-
7
- type Templates = {
8
- operations?: typeof Operations.templates | false
9
- }
10
5
 
11
6
  export type Options = {
12
7
  output?: {
@@ -64,30 +59,15 @@ export type Options = {
64
59
  * Array containing override parameters to override `options` based on tags/operations/methods/paths.
65
60
  */
66
61
  override?: Array<Override<ResolvedOptions>>
67
- transformers?: {
68
- /**
69
- * Customize the names based on the type that is provided by the plugin.
70
- */
71
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
72
- /**
73
- * Receive schema and baseName(propertName) and return FakerMeta array
74
- * TODO TODO add docs
75
- * @beta
76
- */
77
- schema?: (
78
- props: {
79
- schema?: SchemaObject
80
- name?: string
81
- parentName?: string
82
- },
83
- defaultSchemas: Schema[],
84
- ) => Schema[] | undefined
85
- }
86
- mapper?: Record<string, string>
87
62
  /**
88
- * Make it possible to override one of the templates
63
+ * Path to Zod
64
+ * It will be used as `import { z } from '${importPath}'`.
65
+ * It allows both relative and absolute path.
66
+ * the path will be applied as is, so relative path should be based on the file being generated.
67
+ * @default 'zod'
89
68
  */
90
- templates?: Partial<Templates>
69
+ importPath?: string
70
+
91
71
  /**
92
72
  * Choose to use `date` or `datetime` as JavaScript `Date` instead of `string`.
93
73
  * False will fallback on a simple z.string() format
@@ -106,35 +86,46 @@ export type Options = {
106
86
  /**
107
87
  * Return Zod generated schema as type with z.infer<TYPE>
108
88
  */
109
- typedSchema?: boolean
89
+ inferred?: boolean
110
90
  /**
111
91
  * Use of z.coerce.string() instead of z.string()
112
92
  */
113
93
  coercion?: boolean
114
- /**
115
- * Path to Zod
116
- * It will be used as `import { z } from '${importPath}'`.
117
- * It allows both relative and absolute path.
118
- * the path will be applied as is, so relative path should be based on the file being generated.
119
- * @default 'zod'
120
- */
121
- importPath?: string
94
+ operations?: boolean
95
+ mapper?: Record<string, string>
96
+ transformers?: {
97
+ /**
98
+ * Customize the names based on the type that is provided by the plugin.
99
+ */
100
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
101
+ /**
102
+ * Receive schema and baseName(propertName) and return FakerMeta array
103
+ * TODO TODO add docs
104
+ * @beta
105
+ */
106
+ schema?: (
107
+ props: {
108
+ schema?: SchemaObject
109
+ name?: string
110
+ parentName?: string
111
+ },
112
+ defaultSchemas: Schema[],
113
+ ) => Schema[] | undefined
114
+ }
122
115
  }
123
116
 
124
117
  type ResolvedOptions = {
125
- extName: KubbFile.Extname | undefined
118
+ override: NonNullable<Options['override']>
119
+
126
120
  transformers: NonNullable<Options['transformers']>
127
- exclude: Options['exclude']
128
- include: Options['include']
129
- override: Options['override']
130
121
  dateType: NonNullable<Options['dateType']>
131
122
  unknownType: NonNullable<Options['unknownType']>
132
123
  typed: NonNullable<Options['typed']>
133
- typedSchema: NonNullable<Options['typedSchema']>
134
- templates: NonNullable<Templates>
124
+ inferred: NonNullable<Options['inferred']>
135
125
  mapper: NonNullable<Options['mapper']>
136
126
  importPath: NonNullable<Options['importPath']>
137
127
  coercion: NonNullable<Options['coercion']>
128
+ operations: NonNullable<Options['coercion']>
138
129
  }
139
130
 
140
131
  export type FileMeta = {
@@ -1,47 +0,0 @@
1
- import { Operation } from '@kubb/oas';
2
- import { KubbNode } from '@kubb/react/types';
3
- import { ComponentType, ComponentProps } from 'react';
4
-
5
- type TemplateProps = {
6
- /**
7
- * Name of the function
8
- */
9
- operationsName: string;
10
- /**
11
- * Name of the function
12
- */
13
- pathsName: string;
14
- operations: Operation[];
15
- };
16
- declare function Template({ operationsName, pathsName, operations }: TemplateProps): KubbNode;
17
- type RootTemplateProps = {
18
- children?: React.ReactNode;
19
- };
20
- declare function RootTemplate({ children }: RootTemplateProps): JSX.Element;
21
- declare const defaultTemplates: {
22
- readonly default: typeof Template;
23
- readonly root: typeof RootTemplate;
24
- };
25
- type Templates = Partial<typeof defaultTemplates>;
26
- type Props = {
27
- /**
28
- * This will make it possible to override the default behaviour.
29
- */
30
- Template?: ComponentType<ComponentProps<typeof Template>>;
31
- };
32
- declare function Operations({ Template }: Props): KubbNode;
33
- declare namespace Operations {
34
- var File: (props: FileProps) => KubbNode;
35
- var templates: {
36
- readonly default: typeof Template;
37
- readonly root: typeof RootTemplate;
38
- };
39
- }
40
- type FileProps = {
41
- /**
42
- * This will make it possible to override the default behaviour.
43
- */
44
- templates?: Templates;
45
- };
46
-
47
- export { Operations as O };
@@ -1,47 +0,0 @@
1
- import { Operation } from '@kubb/oas';
2
- import { KubbNode } from '@kubb/react/types';
3
- import { ComponentType, ComponentProps } from 'react';
4
-
5
- type TemplateProps = {
6
- /**
7
- * Name of the function
8
- */
9
- operationsName: string;
10
- /**
11
- * Name of the function
12
- */
13
- pathsName: string;
14
- operations: Operation[];
15
- };
16
- declare function Template({ operationsName, pathsName, operations }: TemplateProps): KubbNode;
17
- type RootTemplateProps = {
18
- children?: React.ReactNode;
19
- };
20
- declare function RootTemplate({ children }: RootTemplateProps): JSX.Element;
21
- declare const defaultTemplates: {
22
- readonly default: typeof Template;
23
- readonly root: typeof RootTemplate;
24
- };
25
- type Templates = Partial<typeof defaultTemplates>;
26
- type Props = {
27
- /**
28
- * This will make it possible to override the default behaviour.
29
- */
30
- Template?: ComponentType<ComponentProps<typeof Template>>;
31
- };
32
- declare function Operations({ Template }: Props): KubbNode;
33
- declare namespace Operations {
34
- var File: (props: FileProps) => KubbNode;
35
- var templates: {
36
- readonly default: typeof Template;
37
- readonly root: typeof RootTemplate;
38
- };
39
- }
40
- type FileProps = {
41
- /**
42
- * This will make it possible to override the default behaviour.
43
- */
44
- templates?: Templates;
45
- };
46
-
47
- export { Operations as O };