@kubb/plugin-oas 0.0.0-canary-20240806053140 → 0.0.0-canary-20241106205013

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 (105) hide show
  1. package/README.md +14 -5
  2. package/dist/OperationGenerator-Bw6sj3Eb.d.cts +560 -0
  3. package/dist/OperationGenerator-Bw6sj3Eb.d.ts +560 -0
  4. package/dist/Schema-4FwWfdim.d.cts +22 -0
  5. package/dist/Schema-BlGtSgNB.d.ts +22 -0
  6. package/dist/chunk-2TGWPVZN.cjs +92 -0
  7. package/dist/chunk-2TGWPVZN.cjs.map +1 -0
  8. package/dist/chunk-A3ROGKLW.cjs +752 -0
  9. package/dist/chunk-A3ROGKLW.cjs.map +1 -0
  10. package/dist/chunk-ABOQ73FL.cjs +36 -0
  11. package/dist/chunk-ABOQ73FL.cjs.map +1 -0
  12. package/dist/chunk-BG77DP54.js +30 -0
  13. package/dist/chunk-BG77DP54.js.map +1 -0
  14. package/dist/chunk-GF26SDHQ.js +28 -0
  15. package/dist/chunk-GF26SDHQ.js.map +1 -0
  16. package/dist/chunk-JA75IPYU.js +744 -0
  17. package/dist/chunk-JA75IPYU.js.map +1 -0
  18. package/dist/chunk-PADR76WZ.cjs +4 -0
  19. package/dist/chunk-PADR76WZ.cjs.map +1 -0
  20. package/dist/chunk-QAFBZLJA.cjs +48 -0
  21. package/dist/chunk-QAFBZLJA.cjs.map +1 -0
  22. package/dist/chunk-R47XMJ32.js +3 -0
  23. package/dist/chunk-R47XMJ32.js.map +1 -0
  24. package/dist/chunk-TNWNNVQW.js +88 -0
  25. package/dist/chunk-TNWNNVQW.js.map +1 -0
  26. package/dist/chunk-XNCEFOE6.js +45 -0
  27. package/dist/chunk-XNCEFOE6.js.map +1 -0
  28. package/dist/chunk-ZWHQ54JM.cjs +32 -0
  29. package/dist/chunk-ZWHQ54JM.cjs.map +1 -0
  30. package/dist/components.cjs +20 -17
  31. package/dist/components.cjs.map +1 -1
  32. package/dist/components.d.cts +12 -9
  33. package/dist/components.d.ts +12 -9
  34. package/dist/components.js +3 -17
  35. package/dist/components.js.map +1 -1
  36. package/dist/generators.cjs +14 -0
  37. package/dist/generators.cjs.map +1 -0
  38. package/dist/generators.d.cts +9 -0
  39. package/dist/generators.d.ts +9 -0
  40. package/dist/generators.js +5 -0
  41. package/dist/generators.js.map +1 -0
  42. package/dist/hooks.cjs +90 -58
  43. package/dist/hooks.cjs.map +1 -1
  44. package/dist/hooks.d.cts +37 -10
  45. package/dist/hooks.d.ts +37 -10
  46. package/dist/hooks.js +79 -54
  47. package/dist/hooks.js.map +1 -1
  48. package/dist/index.cjs +277 -270
  49. package/dist/index.cjs.map +1 -1
  50. package/dist/index.d.cts +6 -78
  51. package/dist/index.d.ts +6 -78
  52. package/dist/index.js +235 -255
  53. package/dist/index.js.map +1 -1
  54. package/dist/utils.cjs +32 -87
  55. package/dist/utils.cjs.map +1 -1
  56. package/dist/utils.d.cts +8 -43
  57. package/dist/utils.d.ts +8 -43
  58. package/dist/utils.js +8 -86
  59. package/dist/utils.js.map +1 -1
  60. package/package.json +21 -15
  61. package/src/OperationGenerator.ts +91 -64
  62. package/src/SchemaGenerator.ts +102 -22
  63. package/src/SchemaMapper.ts +24 -5
  64. package/src/components/Oas.tsx +9 -3
  65. package/src/components/Operation.tsx +1 -1
  66. package/src/components/Schema.tsx +2 -102
  67. package/src/generator.tsx +129 -0
  68. package/src/generators/index.ts +1 -0
  69. package/src/generators/jsonGenerator.ts +32 -0
  70. package/src/hooks/index.ts +2 -0
  71. package/src/hooks/useOperationManager.ts +56 -30
  72. package/src/hooks/useSchemaManager.ts +77 -0
  73. package/src/index.ts +5 -13
  74. package/src/plugin.ts +73 -69
  75. package/src/types.ts +29 -27
  76. package/src/utils/getParams.ts +2 -2
  77. package/src/utils/getSchemaFactory.ts +1 -1
  78. package/src/utils/index.ts +2 -2
  79. package/src/utils/parseFromConfig.ts +7 -7
  80. package/dist/OperationGenerator-BV6QRtkW.d.cts +0 -60
  81. package/dist/OperationGenerator-DO5tNGuT.d.ts +0 -60
  82. package/dist/Schema-DefwBJMc.d.ts +0 -35
  83. package/dist/Schema-an5hOrjZ.d.cts +0 -35
  84. package/dist/SchemaMapper-CsBQ6eEx.d.cts +0 -248
  85. package/dist/SchemaMapper-CsBQ6eEx.d.ts +0 -248
  86. package/dist/chunk-6QFVU3BI.js +0 -3265
  87. package/dist/chunk-6QFVU3BI.js.map +0 -1
  88. package/dist/chunk-7BYOJGO3.js +0 -35
  89. package/dist/chunk-7BYOJGO3.js.map +0 -1
  90. package/dist/chunk-DHPXS6V7.cjs +0 -35
  91. package/dist/chunk-DHPXS6V7.cjs.map +0 -1
  92. package/dist/chunk-E4NYDEAU.js +0 -101
  93. package/dist/chunk-E4NYDEAU.js.map +0 -1
  94. package/dist/chunk-KGR7UP2W.js +0 -695
  95. package/dist/chunk-KGR7UP2W.js.map +0 -1
  96. package/dist/chunk-M62LERKB.cjs +0 -695
  97. package/dist/chunk-M62LERKB.cjs.map +0 -1
  98. package/dist/chunk-NLQIW5KR.cjs +0 -3265
  99. package/dist/chunk-NLQIW5KR.cjs.map +0 -1
  100. package/dist/chunk-PHNRHM3I.cjs +0 -101
  101. package/dist/chunk-PHNRHM3I.cjs.map +0 -1
  102. package/dist/types-Db0qp8u-.d.cts +0 -148
  103. package/dist/types-Db0qp8u-.d.ts +0 -148
  104. package/src/utils/getGroupedByTagFiles.ts +0 -82
  105. package/src/utils/refSorter.ts +0 -13
@@ -4,20 +4,26 @@ import { Operation } from './Operation.tsx'
4
4
  import { Schema } from './Schema.tsx'
5
5
 
6
6
  import type { Oas as OasType, Operation as OperationType } from '@kubb/oas'
7
- import type { KubbNode } from '@kubb/react'
7
+ import type { KubbNode } from '@kubb/react/types'
8
8
  import type { OperationGenerator } from '../OperationGenerator.ts'
9
9
 
10
10
  type Props = {
11
11
  oas: OasType
12
12
  operations?: OperationType[]
13
- generator?: OperationGenerator
13
+ /**
14
+ * @deprecated
15
+ */
16
+ generator?: Omit<OperationGenerator, 'build'>
14
17
  children?: KubbNode
15
18
  }
16
19
 
17
20
  type OasContextProps = {
18
21
  oas?: OasType
19
22
  operations?: OperationType[]
20
- generator?: OperationGenerator
23
+ /**
24
+ * @deprecated
25
+ */
26
+ generator?: Omit<OperationGenerator, 'build'>
21
27
  }
22
28
 
23
29
  const OasContext = createContext<OasContextProps>({})
@@ -1,7 +1,7 @@
1
1
  import { createContext } from '@kubb/react'
2
2
 
3
3
  import type { Operation as OperationType } from '@kubb/oas'
4
- import type { KubbNode } from '@kubb/react'
4
+ import type { KubbNode } from '@kubb/react/types'
5
5
 
6
6
  type Props = {
7
7
  operation: OperationType
@@ -1,15 +1,8 @@
1
- import { File, Parser, createContext, useApp, useFile } from '@kubb/react'
1
+ import { createContext } from '@kubb/react'
2
2
 
3
- import { schemaKeywords } from '../SchemaMapper.ts'
4
- import { useSchema } from '../hooks/useSchema.ts'
5
-
6
- import type * as KubbFile from '@kubb/fs/types'
7
3
  import type { SchemaObject } from '@kubb/oas'
8
- import type { KubbNode } from '@kubb/react'
9
- import type { ReactNode } from 'react'
10
- import { SchemaGenerator } from '../SchemaGenerator.ts'
4
+ import type { KubbNode } from '@kubb/react/types'
11
5
  import type { Schema as SchemaType } from '../SchemaMapper.ts'
12
- import type { PluginOas } from '../types.ts'
13
6
 
14
7
  export type SchemaContextProps = {
15
8
  name: string
@@ -33,97 +26,4 @@ export function Schema({ name, value, tree = [], children }: Props): KubbNode {
33
26
  return <SchemaContext.Provider value={{ name, schema: value, tree }}>{children}</SchemaContext.Provider>
34
27
  }
35
28
 
36
- type FileProps = {
37
- isTypeOnly?: boolean
38
- output: string | undefined
39
- children?: KubbNode
40
- }
41
-
42
- Schema.File = function ({ output, isTypeOnly, children }: FileProps): ReactNode {
43
- const { plugin, pluginManager, mode } = useApp<PluginOas>()
44
- const { name } = useSchema()
45
-
46
- if (mode === 'single') {
47
- const baseName = output as KubbFile.BaseName
48
- const resolvedPath = pluginManager.resolvePath({
49
- baseName: '',
50
- pluginKey: plugin.key,
51
- })
52
-
53
- if (!resolvedPath) {
54
- return null
55
- }
56
-
57
- return (
58
- <Parser language="typescript">
59
- <File
60
- baseName={baseName}
61
- path={resolvedPath}
62
- meta={{
63
- pluginKey: plugin.key,
64
- }}
65
- >
66
- {children}
67
- </File>
68
- </Parser>
69
- )
70
- }
71
-
72
- const baseName = `${pluginManager.resolveName({
73
- name,
74
- pluginKey: plugin.key,
75
- type: 'file',
76
- })}.ts` as const
77
- const resolvedPath = pluginManager.resolvePath({
78
- baseName,
79
- pluginKey: plugin.key,
80
- })
81
-
82
- if (!resolvedPath) {
83
- return null
84
- }
85
-
86
- return (
87
- <Parser language="typescript">
88
- <File
89
- baseName={baseName}
90
- path={resolvedPath}
91
- meta={{
92
- pluginKey: plugin.key,
93
- }}
94
- >
95
- <Schema.Imports isTypeOnly={isTypeOnly} />
96
- {children}
97
- </File>
98
- </Parser>
99
- )
100
- }
101
-
102
- type SchemaImportsProps = {
103
- isTypeOnly?: boolean
104
- extName?: KubbFile.Extname
105
- }
106
-
107
- Schema.Imports = ({ isTypeOnly, extName }: SchemaImportsProps): ReactNode => {
108
- const { tree } = useSchema()
109
- const { path: root } = useFile()
110
-
111
- const refs = SchemaGenerator.deepSearch(tree, schemaKeywords.ref)
112
-
113
- return (
114
- <>
115
- {refs
116
- ?.map((item, i) => {
117
- if (!item.args.path) {
118
- return undefined
119
- }
120
-
121
- return (
122
- <File.Import key={i} extName={extName} root={root} name={[item.args.name]} path={item.args.path} isTypeOnly={item.args.isTypeOnly ?? isTypeOnly} />
123
- )
124
- })
125
- .filter(Boolean)}
126
- </>
127
- )
128
- }
129
29
  Schema.Context = SchemaContext
@@ -0,0 +1,129 @@
1
+ import type { PluginFactoryOptions } from '@kubb/core'
2
+ import type * as KubbFile from '@kubb/fs/types'
3
+ import type { Operation, SchemaObject } from '@kubb/oas'
4
+ import { Oas } from '@kubb/plugin-oas/components'
5
+ import { App, createRoot } from '@kubb/react'
6
+ import type { KubbNode } from '@kubb/react/types'
7
+ import type { OperationGenerator } from './OperationGenerator.ts'
8
+ import type { SchemaGenerator, SchemaGeneratorOptions } from './SchemaGenerator.ts'
9
+ import type { Schema } from './SchemaMapper.ts'
10
+ import type { OperationsByMethod } from './types.ts'
11
+
12
+ type OperationsProps<TOptions extends PluginFactoryOptions> = {
13
+ instance: Omit<OperationGenerator<TOptions>, 'build'>
14
+ options: TOptions['resolvedOptions']
15
+ operations: Array<Operation>
16
+ operationsByMethod: OperationsByMethod
17
+ }
18
+
19
+ type OperationProps<TOptions extends PluginFactoryOptions> = {
20
+ instance: Omit<OperationGenerator<TOptions>, 'build'>
21
+ options: TOptions['resolvedOptions']
22
+ operation: Operation
23
+ }
24
+
25
+ type SchemaProps<TOptions extends PluginFactoryOptions> = {
26
+ instance: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>
27
+ options: TOptions['resolvedOptions']
28
+ schema: {
29
+ name: string
30
+ tree: Array<Schema>
31
+ value: SchemaObject
32
+ }
33
+ }
34
+
35
+ export type GeneratorOptions<TOptions extends PluginFactoryOptions> = {
36
+ name: string
37
+ operations?: (this: GeneratorOptions<TOptions>, props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>
38
+ operation?: (this: GeneratorOptions<TOptions>, props: OperationProps<TOptions>) => Promise<KubbFile.File[]>
39
+ schema?: (this: GeneratorOptions<TOptions>, props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>
40
+ }
41
+
42
+ export type Generator<TOptions extends PluginFactoryOptions> = GeneratorOptions<TOptions>
43
+
44
+ export function createGenerator<TOptions extends PluginFactoryOptions>(parseOptions: GeneratorOptions<TOptions>): Generator<TOptions> {
45
+ return parseOptions
46
+ }
47
+
48
+ export type ReactGeneratorOptions<TOptions extends PluginFactoryOptions> = {
49
+ name: string
50
+ Operations?: (this: ReactGeneratorOptions<TOptions>, props: OperationsProps<TOptions>) => KubbNode
51
+ Operation?: (this: ReactGeneratorOptions<TOptions>, props: OperationProps<TOptions>) => KubbNode
52
+ Schema?: (this: ReactGeneratorOptions<TOptions>, props: SchemaProps<TOptions>) => KubbNode
53
+ }
54
+
55
+ export function createReactGenerator<TOptions extends PluginFactoryOptions>(parseOptions: ReactGeneratorOptions<TOptions>): Generator<TOptions> {
56
+ return {
57
+ ...parseOptions,
58
+ async operations({ instance, options, operations, operationsByMethod }) {
59
+ if (!parseOptions.Operations) {
60
+ return []
61
+ }
62
+
63
+ const { pluginManager, oas, plugin, mode } = instance.context
64
+ const root = createRoot({
65
+ logger: pluginManager.logger,
66
+ })
67
+
68
+ const Component = parseOptions.Operations.bind(this)
69
+
70
+ root.render(
71
+ <App pluginManager={pluginManager} plugin={plugin} mode={mode}>
72
+ <Oas oas={oas} operations={operations} generator={instance}>
73
+ <Component operations={operations} instance={instance} operationsByMethod={operationsByMethod} options={options} />
74
+ </Oas>
75
+ </App>,
76
+ )
77
+
78
+ return root.files
79
+ },
80
+ async operation({ instance, operation, options }) {
81
+ if (!parseOptions.Operation) {
82
+ return []
83
+ }
84
+
85
+ const { pluginManager, oas, plugin, mode } = instance.context
86
+ const root = createRoot({
87
+ logger: pluginManager.logger,
88
+ })
89
+
90
+ const Component = parseOptions.Operation.bind(this)
91
+
92
+ root.render(
93
+ <App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
94
+ <Oas oas={oas} operations={[operation]} generator={instance}>
95
+ <Oas.Operation operation={operation}>
96
+ <Component operation={operation} options={options} instance={instance} />
97
+ </Oas.Operation>
98
+ </Oas>
99
+ </App>,
100
+ )
101
+
102
+ return root.files
103
+ },
104
+ async schema({ instance, schema, options }) {
105
+ if (!parseOptions.Schema) {
106
+ return []
107
+ }
108
+
109
+ const { pluginManager, oas, plugin, mode } = instance.context
110
+ const root = createRoot({
111
+ logger: pluginManager.logger,
112
+ })
113
+
114
+ const Component = parseOptions.Schema.bind(this)
115
+
116
+ root.render(
117
+ <App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
118
+ <Oas oas={oas}>
119
+ <Oas.Schema name={schema.name} value={schema.value} tree={schema.tree}>
120
+ <Component schema={schema} options={options} instance={instance} />
121
+ </Oas.Schema>
122
+ </Oas>
123
+ </App>,
124
+ )
125
+
126
+ return root.files
127
+ },
128
+ }
129
+ }
@@ -0,0 +1 @@
1
+ export { jsonGenerator } from './jsonGenerator.ts'
@@ -0,0 +1,32 @@
1
+ import { camelCase } from '@kubb/core/transformers'
2
+ import { createGenerator } from '../generator.tsx'
3
+ import type { PluginOas } from '../types.ts'
4
+
5
+ export const jsonGenerator = createGenerator<PluginOas>({
6
+ name: 'plugin-oas',
7
+ async schema({ schema, instance }) {
8
+ const { pluginManager, plugin } = instance.context
9
+ const file = pluginManager.getFile({
10
+ name: camelCase(schema.name),
11
+ extname: '.json',
12
+ mode: 'split',
13
+ pluginKey: plugin.key,
14
+ })
15
+
16
+ return [
17
+ {
18
+ ...file,
19
+ sources: [
20
+ {
21
+ name: camelCase(schema.name),
22
+ isExportable: false,
23
+ isIndexable: false,
24
+ value: JSON.stringify(schema.value),
25
+ },
26
+ ],
27
+ banner: plugin.options.output?.banner,
28
+ format: plugin.options.output?.footer,
29
+ },
30
+ ]
31
+ },
32
+ })
@@ -1,5 +1,7 @@
1
1
  export { useOas } from './useOas.ts'
2
2
  export { useOperation } from './useOperation.ts'
3
3
  export { useOperationManager } from './useOperationManager.ts'
4
+ export type { SchemaNames } from './useOperationManager.ts'
4
5
  export { useOperations } from './useOperations.ts'
5
6
  export { useSchema } from './useSchema.ts'
7
+ export { useSchemaManager } from './useSchemaManager.ts'
@@ -13,7 +13,7 @@ type FileMeta = FileMetaBase & {
13
13
  tag?: string
14
14
  }
15
15
 
16
- type SchemaNames = {
16
+ export type SchemaNames = {
17
17
  request: string | undefined
18
18
  parameters: {
19
19
  path: string | undefined
@@ -25,14 +25,33 @@ type SchemaNames = {
25
25
  }
26
26
 
27
27
  type UseOperationManagerResult = {
28
- getName: (operation: OperationType, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => string
29
- getFile: (operation: OperationType, params?: { pluginKey?: Plugin['key']; extName?: KubbFile.Extname }) => KubbFile.File<FileMeta>
30
- groupSchemasByName: (operation: OperationType, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => SchemaNames
31
- getSchemas: (
32
- operation: Operation,
33
- params?: { pluginKey?: Plugin['key']; type?: ResolveNameParams['type'] },
34
- forStatusCode?: string | number,
35
- ) => OperationSchemas
28
+ getName: (
29
+ operation: OperationType,
30
+ params: {
31
+ prefix?: string
32
+ suffix?: string
33
+ pluginKey?: Plugin['key']
34
+ type: ResolveNameParams['type']
35
+ },
36
+ ) => string
37
+ getFile: (
38
+ operation: OperationType,
39
+ params?: {
40
+ prefix?: string
41
+ suffix?: string
42
+ pluginKey?: Plugin['key']
43
+ extname?: KubbFile.Extname
44
+ tag?: string
45
+ },
46
+ ) => KubbFile.File<FileMeta>
47
+ groupSchemasByName: (
48
+ operation: OperationType,
49
+ params: {
50
+ pluginKey?: Plugin['key']
51
+ type: ResolveNameParams['type']
52
+ },
53
+ ) => SchemaNames
54
+ getSchemas: (operation: Operation, params?: { pluginKey?: Plugin['key']; type?: ResolveNameParams['type'] }) => OperationSchemas
36
55
  }
37
56
 
38
57
  /**
@@ -42,26 +61,38 @@ export function useOperationManager(): UseOperationManagerResult {
42
61
  const { plugin, pluginManager } = useApp()
43
62
  const { generator } = useContext(Oas.Context)
44
63
 
45
- if (!generator) {
46
- throw new Error(`'generator' is not defined`)
47
- }
48
-
49
- const getName: UseOperationManagerResult['getName'] = (operation, { pluginKey = plugin.key, type }) => {
64
+ const getName: UseOperationManagerResult['getName'] = (operation, { prefix = '', suffix = '', pluginKey = plugin.key, type }) => {
50
65
  return pluginManager.resolveName({
51
- name: operation.getOperationId(),
66
+ name: `${prefix} ${operation.getOperationId()} ${suffix}`,
52
67
  pluginKey,
53
68
  type,
54
69
  })
55
70
  }
56
71
 
57
- const getFile: UseOperationManagerResult['getFile'] = (operation, { pluginKey = plugin.key, extName = '.ts' } = {}) => {
58
- // needed for the `output.group`
59
- const tag = operation.getTags().at(0)?.name
60
- const name = getName(operation, { type: 'file', pluginKey })
72
+ const getSchemas: UseOperationManagerResult['getSchemas'] = (operation, params) => {
73
+ if (!generator) {
74
+ throw new Error(`'generator' is not defined`)
75
+ }
61
76
 
77
+ return generator.getSchemas(operation, {
78
+ resolveName: (name) =>
79
+ pluginManager.resolveName({
80
+ name,
81
+ pluginKey: params?.pluginKey,
82
+ type: params?.type,
83
+ }),
84
+ })
85
+ }
86
+
87
+ //TODO replace tag with group
88
+ const getFile: UseOperationManagerResult['getFile'] = (
89
+ operation,
90
+ { prefix, suffix, pluginKey = plugin.key, tag = operation.getTags().at(0)?.name, extname = '.ts' } = {},
91
+ ) => {
92
+ const name = getName(operation, { type: 'file', pluginKey, prefix, suffix })
62
93
  const file = pluginManager.getFile({
63
94
  name,
64
- extName,
95
+ extname,
65
96
  pluginKey,
66
97
  options: { type: 'file', pluginKey, tag },
67
98
  })
@@ -78,6 +109,10 @@ export function useOperationManager(): UseOperationManagerResult {
78
109
  }
79
110
 
80
111
  const groupSchemasByName: UseOperationManagerResult['groupSchemasByName'] = (operation, { pluginKey = plugin.key, type }) => {
112
+ if (!generator) {
113
+ throw new Error(`'generator' is not defined`)
114
+ }
115
+
81
116
  const schemas = generator.getSchemas(operation)
82
117
 
83
118
  const errors = (schemas.errors || []).reduce(
@@ -148,16 +183,7 @@ export function useOperationManager(): UseOperationManagerResult {
148
183
  return {
149
184
  getName,
150
185
  getFile,
151
- getSchemas: (operation, params, forStatusCode) =>
152
- generator.getSchemas(operation, {
153
- forStatusCode,
154
- resolveName: (name) =>
155
- pluginManager.resolveName({
156
- name,
157
- pluginKey: params?.pluginKey,
158
- type: params?.type,
159
- }),
160
- }),
186
+ getSchemas,
161
187
  groupSchemasByName,
162
188
  }
163
189
  }
@@ -0,0 +1,77 @@
1
+ import { useApp } from '@kubb/react'
2
+
3
+ import type { FileMetaBase, Plugin, ResolveNameParams } from '@kubb/core'
4
+ import type * as KubbFile from '@kubb/fs/types'
5
+ import type { Mode } from '@kubb/fs/types'
6
+ import { SchemaGenerator } from '../SchemaGenerator.ts'
7
+ import { type Schema, schemaKeywords } from '../SchemaMapper'
8
+
9
+ type FileMeta = FileMetaBase & {
10
+ pluginKey: Plugin['key']
11
+ name: string
12
+ tag?: string
13
+ }
14
+
15
+ type UseSchemaManagerResult = {
16
+ getName: (name: string, params: { pluginKey?: Plugin['key']; type: ResolveNameParams['type'] }) => string
17
+ getFile: (name: string, params?: { pluginKey?: Plugin['key']; mode?: Mode; extname?: KubbFile.Extname; tag?: string }) => KubbFile.File<FileMeta>
18
+ getImports: (tree: Array<Schema>) => Array<KubbFile.Import>
19
+ }
20
+
21
+ /**
22
+ * `useSchemaManager` will return some helper functions that can be used to get the schema file, get the schema name.
23
+ */
24
+ export function useSchemaManager(): UseSchemaManagerResult {
25
+ const { plugin, pluginManager } = useApp()
26
+
27
+ const getName: UseSchemaManagerResult['getName'] = (name, { pluginKey = plugin.key, type }) => {
28
+ return pluginManager.resolveName({
29
+ name,
30
+ pluginKey,
31
+ type,
32
+ })
33
+ }
34
+ //TODO replace tag with group
35
+ const getFile: UseSchemaManagerResult['getFile'] = (name, { mode = 'split', pluginKey = plugin.key, extname = '.ts', tag } = {}) => {
36
+ const resolvedName = mode === 'single' ? '' : getName(name, { type: 'file', pluginKey })
37
+
38
+ const file = pluginManager.getFile({
39
+ name: resolvedName,
40
+ extname,
41
+ pluginKey,
42
+ options: { type: 'file', pluginKey, tag },
43
+ })
44
+
45
+ return {
46
+ ...file,
47
+ meta: {
48
+ ...file.meta,
49
+ name: resolvedName,
50
+ pluginKey,
51
+ },
52
+ }
53
+ }
54
+
55
+ const getImports: UseSchemaManagerResult['getImports'] = (tree) => {
56
+ const refs = SchemaGenerator.deepSearch(tree, schemaKeywords.ref)
57
+
58
+ return refs
59
+ ?.map((item, i) => {
60
+ if (!item.args.path || !item.args.isImportable) {
61
+ return undefined
62
+ }
63
+
64
+ return {
65
+ name: [item.args.name],
66
+ path: item.args.path,
67
+ }
68
+ })
69
+ .filter(Boolean)
70
+ }
71
+
72
+ return {
73
+ getName,
74
+ getFile,
75
+ getImports,
76
+ }
77
+ }
package/src/index.ts CHANGED
@@ -1,9 +1,4 @@
1
- import { pluginOas } from './plugin.ts'
2
-
3
- export type {
4
- GetOperationGeneratorOptions,
5
- OperationMethodResult,
6
- } from './OperationGenerator.ts'
1
+ export type { OperationMethodResult } from './OperationGenerator.ts'
7
2
  export { OperationGenerator } from './OperationGenerator.ts'
8
3
  export { pluginOas, pluginOasName } from './plugin.ts'
9
4
  export type {
@@ -15,6 +10,7 @@ export type { SchemaMethodResult } from './SchemaGenerator.ts'
15
10
  export { SchemaGenerator } from './SchemaGenerator.ts'
16
11
  export type {
17
12
  Schema,
13
+ SchemaTree,
18
14
  SchemaKeyword,
19
15
  SchemaKeywordBase,
20
16
  SchemaKeywordMapper,
@@ -22,10 +18,6 @@ export type {
22
18
  } from './SchemaMapper.ts'
23
19
  export { isKeyword, schemaKeywords } from './SchemaMapper.ts'
24
20
  export type * from './types.ts'
25
-
26
- /**
27
- * @deprecated Use `import { pluginOas } from '@kubb/plugin-oas'` instead
28
- */
29
- const definePluginDefault = pluginOas
30
-
31
- export default definePluginDefault
21
+ export { createGenerator, createReactGenerator } from './generator.tsx'
22
+ export type { ReactGeneratorOptions } from './generator.tsx'
23
+ export type { Generator, GeneratorOptions } from './generator.tsx'