@kubb/plugin-oas 4.34.0 → 4.35.0

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 (40) hide show
  1. package/dist/{createGenerator-Z5-TQKKC.d.ts → createGenerator-CBXpHeVY.d.ts} +51 -56
  2. package/dist/{generators-CBlzDhTh.cjs → generators-CyWd3UXA.cjs} +3 -1
  3. package/dist/generators-CyWd3UXA.cjs.map +1 -0
  4. package/dist/{generators-DRPPf38p.js → generators-D7C3CXsN.js} +3 -1
  5. package/dist/generators-D7C3CXsN.js.map +1 -0
  6. package/dist/generators.cjs +1 -1
  7. package/dist/generators.d.ts +2 -2
  8. package/dist/generators.js +1 -1
  9. package/dist/hooks.cjs +24 -0
  10. package/dist/hooks.cjs.map +1 -1
  11. package/dist/hooks.d.ts +23 -2
  12. package/dist/hooks.js +24 -1
  13. package/dist/hooks.js.map +1 -1
  14. package/dist/index.cjs +10 -8
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.ts +39 -32
  17. package/dist/index.js +10 -8
  18. package/dist/index.js.map +1 -1
  19. package/dist/{requestBody-BDtQ-KsD.cjs → requestBody-DIM-iDpM.cjs} +108 -55
  20. package/dist/requestBody-DIM-iDpM.cjs.map +1 -0
  21. package/dist/{requestBody-CB2dFgmS.js → requestBody-mUXoBwsu.js} +108 -55
  22. package/dist/requestBody-mUXoBwsu.js.map +1 -0
  23. package/dist/utils.cjs +1 -1
  24. package/dist/utils.d.ts +1 -1
  25. package/dist/utils.js +1 -1
  26. package/package.json +5 -5
  27. package/src/OperationGenerator.ts +18 -10
  28. package/src/SchemaGenerator.ts +20 -8
  29. package/src/generators/createGenerator.ts +15 -10
  30. package/src/generators/createReactGenerator.ts +15 -10
  31. package/src/generators/types.ts +39 -4
  32. package/src/hooks/index.ts +1 -0
  33. package/src/hooks/useRootNode.ts +25 -0
  34. package/src/index.ts +1 -1
  35. package/src/plugin.ts +1 -1
  36. package/src/utils.tsx +138 -42
  37. package/dist/generators-CBlzDhTh.cjs.map +0 -1
  38. package/dist/generators-DRPPf38p.js.map +0 -1
  39. package/dist/requestBody-BDtQ-KsD.cjs.map +0 -1
  40. package/dist/requestBody-CB2dFgmS.js.map +0 -1
@@ -1,12 +1,14 @@
1
- import { type AsyncEventEmitter, getUniqueName, pascalCase, stringify } from '@internals/utils'
2
- import type { FileMetaBase, KubbEvents, Plugin, PluginFactoryOptions, PluginManager, ResolveNameParams } from '@kubb/core'
1
+ import { getUniqueName, pascalCase, stringify } from '@internals/utils'
2
+ import type { AsyncEventEmitter, FileMetaBase, KubbEvents, Plugin, PluginFactoryOptions, PluginManager, ResolveNameParams } from '@kubb/core'
3
3
  import type { KubbFile } from '@kubb/fabric-core/types'
4
4
  import type { contentType, Oas, OasTypes, OpenAPIV3, SchemaObject } from '@kubb/oas'
5
5
  import { isDiscriminator, isNullable, isReference, KUBB_INLINE_REF_PREFIX } from '@kubb/oas'
6
6
  import type { Fabric } from '@kubb/react-fabric'
7
7
  import pLimit from 'p-limit'
8
8
  import { isDeepEqual, isNumber, uniqueWith } from 'remeda'
9
- import type { Generator } from './generators/types.ts'
9
+ import type { CoreGenerator } from './generators/createGenerator.ts'
10
+ import type { ReactGenerator } from './generators/createReactGenerator.ts'
11
+ import type { Generator, Version } from './generators/types.ts'
10
12
  import { isKeyword, type Schema, type SchemaKeywordMapper, schemaKeywords } from './SchemaMapper.ts'
11
13
  import type { OperationSchema, Override, Refs } from './types.ts'
12
14
  import { getSchemaFactory } from './utils/getSchemaFactory.ts'
@@ -1335,7 +1337,7 @@ export class SchemaGenerator<
1335
1337
  return [{ keyword: emptyType }, ...baseItems]
1336
1338
  }
1337
1339
 
1338
- async build(...generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>> {
1340
+ async build(...generators: Array<Generator<TPluginOptions, Version>>): Promise<Array<KubbFile.File<TFileMeta>>> {
1339
1341
  const { oas, contentType, include } = this.context
1340
1342
 
1341
1343
  // Initialize the name mapping if not already done
@@ -1358,7 +1360,10 @@ export class SchemaGenerator<
1358
1360
  return this.#doBuild(schemas, generators)
1359
1361
  }
1360
1362
 
1361
- async #doBuild(schemas: Record<string, OasTypes.SchemaObject>, generators: Array<Generator<TPluginOptions>>): Promise<Array<KubbFile.File<TFileMeta>>> {
1363
+ async #doBuild(
1364
+ schemas: Record<string, OasTypes.SchemaObject>,
1365
+ generators: Array<Generator<TPluginOptions, Version>>,
1366
+ ): Promise<Array<KubbFile.File<TFileMeta>>> {
1362
1367
  const schemaEntries = Object.entries(schemas)
1363
1368
 
1364
1369
  const generatorLimit = pLimit(GENERATOR_CONCURRENCY)
@@ -1366,13 +1371,20 @@ export class SchemaGenerator<
1366
1371
 
1367
1372
  const writeTasks = generators.map((generator) =>
1368
1373
  generatorLimit(async () => {
1374
+ if (generator.version === '2') {
1375
+ return []
1376
+ }
1377
+
1378
+ // After the v2 guard above, all generators here are v1
1379
+ const v1Generator = generator as ReactGenerator<TPluginOptions, '1'> | CoreGenerator<TPluginOptions, '1'>
1380
+
1369
1381
  const schemaTasks = schemaEntries.map(([name, schemaObject]) =>
1370
1382
  schemaLimit(async () => {
1371
1383
  const options = this.#getOptions(name)
1372
1384
 
1373
1385
  const tree = this.parse({ schema: schemaObject as SchemaObject, name, parentName: null, rootName: name })
1374
1386
 
1375
- if (generator.type === 'react') {
1387
+ if (v1Generator.type === 'react') {
1376
1388
  await buildSchema(
1377
1389
  {
1378
1390
  name,
@@ -1382,7 +1394,7 @@ export class SchemaGenerator<
1382
1394
  {
1383
1395
  config: this.context.pluginManager.config,
1384
1396
  fabric: this.context.fabric,
1385
- Component: generator.Schema,
1397
+ Component: v1Generator.Schema,
1386
1398
  generator: this,
1387
1399
  plugin: {
1388
1400
  ...this.context.plugin,
@@ -1397,7 +1409,7 @@ export class SchemaGenerator<
1397
1409
  return []
1398
1410
  }
1399
1411
 
1400
- const result = await generator.schema?.({
1412
+ const result = await v1Generator.schema?.({
1401
1413
  config: this.context.pluginManager.config,
1402
1414
  generator: this,
1403
1415
  schema: {
@@ -1,25 +1,30 @@
1
1
  import type { PluginFactoryOptions } from '@kubb/core'
2
2
  import type { KubbFile } from '@kubb/fabric-core/types'
3
- import type { OperationProps, OperationsProps, SchemaProps } from './types.ts'
3
+ import type { OperationProps, OperationsProps, SchemaProps, Version } from './types.ts'
4
4
 
5
- type UserGenerator<TOptions extends PluginFactoryOptions> = {
5
+ type UserGenerator<TOptions extends PluginFactoryOptions, TVersion extends Version> = {
6
6
  name: string
7
- operations?: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>
8
- operation?: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>
9
- schema?: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>
7
+ version?: TVersion
8
+ operations?: (props: OperationsProps<TOptions, TVersion>) => Promise<KubbFile.File[]>
9
+ operation?: (props: OperationProps<TOptions, TVersion>) => Promise<KubbFile.File[]>
10
+ schema?: (props: SchemaProps<TOptions, TVersion>) => Promise<KubbFile.File[]>
10
11
  }
11
12
 
12
- export type CoreGenerator<TOptions extends PluginFactoryOptions> = {
13
+ export type CoreGenerator<TOptions extends PluginFactoryOptions, TVersion extends Version> = {
13
14
  name: string
14
15
  type: 'core'
15
- operations: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>
16
- operation: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>
17
- schema: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>
16
+ version: TVersion
17
+ operations: (props: OperationsProps<TOptions, TVersion>) => Promise<KubbFile.File[]>
18
+ operation: (props: OperationProps<TOptions, TVersion>) => Promise<KubbFile.File[]>
19
+ schema: (props: SchemaProps<TOptions, TVersion>) => Promise<KubbFile.File[]>
18
20
  }
19
21
 
20
- export function createGenerator<TOptions extends PluginFactoryOptions>(generator: UserGenerator<TOptions>): CoreGenerator<TOptions> {
22
+ export function createGenerator<TOptions extends PluginFactoryOptions, TVersion extends Version = '1'>(
23
+ generator: UserGenerator<TOptions, TVersion>,
24
+ ): CoreGenerator<TOptions, TVersion> {
21
25
  return {
22
26
  type: 'core',
27
+ version: (generator.version ?? '1') as TVersion,
23
28
  async operations() {
24
29
  return []
25
30
  },
@@ -1,20 +1,22 @@
1
1
  import type { PluginFactoryOptions } from '@kubb/core'
2
2
  import type { FabricReactNode } from '@kubb/react-fabric/types'
3
- import type { OperationProps, OperationsProps, SchemaProps } from './types.ts'
3
+ import type { OperationProps, OperationsProps, SchemaProps, Version } from './types.ts'
4
4
 
5
- type UserGenerator<TOptions extends PluginFactoryOptions> = {
5
+ type UserGenerator<TOptions extends PluginFactoryOptions, TVersion extends Version> = {
6
6
  name: string
7
- Operations?: (props: OperationsProps<TOptions>) => FabricReactNode
8
- Operation?: (props: OperationProps<TOptions>) => FabricReactNode
9
- Schema?: (props: SchemaProps<TOptions>) => FabricReactNode
7
+ version?: TVersion
8
+ Operations?: (props: OperationsProps<TOptions, TVersion>) => FabricReactNode
9
+ Operation?: (props: OperationProps<TOptions, TVersion>) => FabricReactNode
10
+ Schema?: (props: SchemaProps<TOptions, TVersion>) => FabricReactNode
10
11
  }
11
12
 
12
- export type ReactGenerator<TOptions extends PluginFactoryOptions> = {
13
+ export type ReactGenerator<TOptions extends PluginFactoryOptions, TVersion extends Version> = {
13
14
  name: string
14
15
  type: 'react'
15
- Operations: (props: OperationsProps<TOptions>) => FabricReactNode
16
- Operation: (props: OperationProps<TOptions>) => FabricReactNode
17
- Schema: (props: SchemaProps<TOptions>) => FabricReactNode
16
+ version: TVersion
17
+ Operations: (props: OperationsProps<TOptions, TVersion>) => FabricReactNode
18
+ Operation: (props: OperationProps<TOptions, TVersion>) => FabricReactNode
19
+ Schema: (props: SchemaProps<TOptions, TVersion>) => FabricReactNode
18
20
  }
19
21
 
20
22
  /****
@@ -24,9 +26,12 @@ export type ReactGenerator<TOptions extends PluginFactoryOptions> = {
24
26
  *
25
27
  * @returns A generator object with async methods for operations, operation, and schema file generation.
26
28
  */
27
- export function createReactGenerator<TOptions extends PluginFactoryOptions>(generator: UserGenerator<TOptions>): ReactGenerator<TOptions> {
29
+ export function createReactGenerator<TOptions extends PluginFactoryOptions, TVersion extends Version = '1'>(
30
+ generator: UserGenerator<TOptions, TVersion>,
31
+ ): ReactGenerator<TOptions, TVersion> {
28
32
  return {
29
33
  type: 'react',
34
+ version: (generator.version ?? '1') as TVersion,
30
35
  Operations() {
31
36
  return null
32
37
  },
@@ -1,3 +1,4 @@
1
+ import type { OperationNode, SchemaNode } from '@kubb/ast/types'
1
2
  import type { Config, Plugin, PluginFactoryOptions } from '@kubb/core'
2
3
  import type { Operation, SchemaObject } from '@kubb/oas'
3
4
  import type { OperationGenerator } from '../OperationGenerator.ts'
@@ -6,21 +7,43 @@ import type { Schema } from '../SchemaMapper.ts'
6
7
  import type { CoreGenerator } from './createGenerator.ts'
7
8
  import type { ReactGenerator } from './createReactGenerator.ts'
8
9
 
9
- export type OperationsProps<TOptions extends PluginFactoryOptions> = {
10
+ export type Version = '1' | '2'
11
+
12
+ export type OperationsV1Props<TOptions extends PluginFactoryOptions> = {
10
13
  config: Config
11
14
  generator: Omit<OperationGenerator<TOptions>, 'build'>
12
15
  plugin: Plugin<TOptions>
13
16
  operations: Array<Operation>
14
17
  }
15
18
 
16
- export type OperationProps<TOptions extends PluginFactoryOptions> = {
19
+ export type OperationsV2Props<TOptions extends PluginFactoryOptions> = {
20
+ config: Config
21
+ plugin: Plugin<TOptions>
22
+ nodes: Array<OperationNode>
23
+ }
24
+
25
+ export type OperationV1Props<TOptions extends PluginFactoryOptions> = {
17
26
  config: Config
18
27
  generator: Omit<OperationGenerator<TOptions>, 'build'>
19
28
  plugin: Plugin<TOptions>
20
29
  operation: Operation
21
30
  }
22
31
 
23
- export type SchemaProps<TOptions extends PluginFactoryOptions> = {
32
+ export type OperationV2Props<TOptions extends PluginFactoryOptions> = {
33
+ config: Config
34
+ plugin: Plugin<TOptions>
35
+ node: OperationNode
36
+ }
37
+
38
+ export type OperationsProps<TOptions extends PluginFactoryOptions, TVersion extends Version = '1'> = TVersion extends '2'
39
+ ? OperationsV2Props<TOptions>
40
+ : OperationsV1Props<TOptions>
41
+
42
+ export type OperationProps<TOptions extends PluginFactoryOptions, TVersion extends Version = '1'> = TVersion extends '2'
43
+ ? OperationV2Props<TOptions>
44
+ : OperationV1Props<TOptions>
45
+
46
+ export type SchemaV1Props<TOptions extends PluginFactoryOptions> = {
24
47
  config: Config
25
48
  generator: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>
26
49
  plugin: Plugin<TOptions>
@@ -31,4 +54,16 @@ export type SchemaProps<TOptions extends PluginFactoryOptions> = {
31
54
  }
32
55
  }
33
56
 
34
- export type Generator<TOptions extends PluginFactoryOptions> = CoreGenerator<TOptions> | ReactGenerator<TOptions>
57
+ export type SchemaV2Props<TOptions extends PluginFactoryOptions> = {
58
+ config: Config
59
+ plugin: Plugin<TOptions>
60
+ node: SchemaNode
61
+ }
62
+
63
+ export type SchemaProps<TOptions extends PluginFactoryOptions, TVersion extends Version = '1'> = TVersion extends '2'
64
+ ? SchemaV2Props<TOptions>
65
+ : SchemaV1Props<TOptions>
66
+
67
+ export type Generator<TOptions extends PluginFactoryOptions, TVersion extends Version = Version> =
68
+ | CoreGenerator<TOptions, TVersion>
69
+ | ReactGenerator<TOptions, TVersion>
@@ -1,4 +1,5 @@
1
1
  export { useOas } from './useOas.ts'
2
2
  export type { SchemaNames } from './useOperationManager.ts'
3
3
  export { useOperationManager } from './useOperationManager.ts'
4
+ export { useRootNode } from './useRootNode.ts'
4
5
  export { useSchemaManager } from './useSchemaManager.ts'
@@ -0,0 +1,25 @@
1
+ import type { RootNode } from '@kubb/ast/types'
2
+ import { useApp } from '@kubb/react-fabric'
3
+
4
+ /**
5
+ * Returns the universal `@kubb/ast` `RootNode` produced by the configured adapter.
6
+ *
7
+ * Use this hook inside generator components when you want to consume the
8
+ * format-agnostic AST directly instead of going through `useOas()`.
9
+ *
10
+ * Returns `undefined` when no adapter was configured (legacy OAS-only mode).
11
+ *
12
+ * @example
13
+ * ```tsx
14
+ * function MyComponent() {
15
+ * const rootNode = useRootNode()
16
+ * if (!rootNode) return null
17
+ * return <>{rootNode.schemas.map(s => <Schema key={s.name} node={s} />)}</>
18
+ * }
19
+ * ```
20
+ */
21
+ export function useRootNode(): RootNode | undefined {
22
+ const { meta } = useApp<{ pluginManager?: { rootNode?: RootNode } }>()
23
+
24
+ return meta.pluginManager?.rootNode
25
+ }
package/src/index.ts CHANGED
@@ -40,4 +40,4 @@ export const createReactGenerator = _createReactGenerator
40
40
  /**
41
41
  * @deprecated use `import { Generator } from '@kubb/plugin-oas/generators'`
42
42
  */
43
- export type Generator<TOptions extends PluginFactoryOptions> = _Generator<TOptions>
43
+ export type Generator<TOptions extends PluginFactoryOptions, TVersion extends import('./generators/types.ts').Version = '1'> = _Generator<TOptions, TVersion>
package/src/plugin.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import path from 'node:path'
2
- import type { AsyncEventEmitter } from '@internals/utils'
3
2
  import { camelCase } from '@internals/utils'
3
+ import type { AsyncEventEmitter } from '@kubb/core'
4
4
  import { type Config, definePlugin, type Group, getMode, type KubbEvents } from '@kubb/core'
5
5
  import type { Oas } from '@kubb/oas'
6
6
  import { parseFromConfig, resolveServerUrl } from '@kubb/oas'
package/src/utils.tsx CHANGED
@@ -1,3 +1,4 @@
1
+ import type { OperationNode, SchemaNode } from '@kubb/ast/types'
1
2
  import type { Config, Plugin, PluginFactoryOptions } from '@kubb/core'
2
3
  import type { Operation, SchemaObject } from '@kubb/oas'
3
4
  import { App, createReactFabric, type Fabric } from '@kubb/react-fabric'
@@ -6,92 +7,187 @@ import type { OperationGenerator } from './OperationGenerator.ts'
6
7
  import type { SchemaGenerator, SchemaGeneratorOptions } from './SchemaGenerator.ts'
7
8
  import type { Schema } from './SchemaMapper.ts'
8
9
 
9
- type BuildOperationsOptions<TOptions extends PluginFactoryOptions> = {
10
+ type BuildOperationsBaseOptions<TOptions extends PluginFactoryOptions> = {
10
11
  config: Config
11
12
  fabric: Fabric
12
- Component: ReactGenerator<any>['Operations']
13
- generator: Omit<OperationGenerator<TOptions>, 'build'>
14
13
  plugin: Plugin<TOptions>
15
14
  }
16
15
 
16
+ type BuildOperationsV1Options<TOptions extends PluginFactoryOptions> = BuildOperationsBaseOptions<TOptions> & {
17
+ version?: '1'
18
+ Component: ReactGenerator<any, '1'>['Operations']
19
+ generator: Omit<OperationGenerator<TOptions>, 'build'>
20
+ }
21
+
22
+ type BuildOperationsV2Options<TOptions extends PluginFactoryOptions> = BuildOperationsBaseOptions<TOptions> & {
23
+ version: '2'
24
+ Component: ReactGenerator<any, '2'>['Operations']
25
+ }
26
+
27
+ function isBuildOperationsV1Options<TOptions extends PluginFactoryOptions>(
28
+ options: BuildOperationsV1Options<TOptions> | BuildOperationsV2Options<TOptions>,
29
+ ): options is BuildOperationsV1Options<TOptions> {
30
+ return (options.version ?? '1') === '1'
31
+ }
32
+
17
33
  export async function buildOperations<TOptions extends PluginFactoryOptions>(
18
34
  operations: Array<Operation>,
19
- { config, fabric, plugin, generator, Component }: BuildOperationsOptions<TOptions>,
35
+ options: BuildOperationsV1Options<TOptions>,
36
+ ): Promise<void>
37
+ export async function buildOperations<TOptions extends PluginFactoryOptions>(
38
+ nodes: Array<OperationNode>,
39
+ options: BuildOperationsV2Options<TOptions>,
40
+ ): Promise<void>
41
+ export async function buildOperations<TOptions extends PluginFactoryOptions>(
42
+ operationsOrNodes: Array<Operation> | Array<OperationNode>,
43
+ options: BuildOperationsV1Options<TOptions> | BuildOperationsV2Options<TOptions>,
20
44
  ): Promise<void> {
21
- if (!Component) {
45
+ const { config, fabric, plugin } = options
46
+
47
+ if (!options.Component) {
22
48
  return undefined
23
49
  }
24
50
 
25
- const { pluginManager, oas, mode } = generator.context
26
-
27
51
  const fabricChild = createReactFabric()
28
- await fabricChild.render(
29
- <App meta={{ pluginManager, plugin, mode, oas }}>
30
- <Component config={config} operations={operations} generator={generator} plugin={plugin} />
31
- </App>,
32
- )
52
+
53
+ if (isBuildOperationsV1Options(options)) {
54
+ const { generator, Component } = options
55
+ const { pluginManager, oas, mode } = generator.context
56
+
57
+ await fabricChild.render(
58
+ <App meta={{ pluginManager, plugin, mode, oas }}>
59
+ <Component config={config} operations={operationsOrNodes as Array<Operation>} generator={generator} plugin={plugin} />
60
+ </App>,
61
+ )
62
+ } else {
63
+ const { Component } = options
64
+ await fabricChild.render(
65
+ <App meta={{ plugin }}>
66
+ <Component config={config} nodes={operationsOrNodes as Array<OperationNode>} plugin={plugin} />
67
+ </App>,
68
+ )
69
+ }
33
70
 
34
71
  await fabric.context.fileManager.upsert(...fabricChild.files)
35
72
  fabricChild.unmount()
36
73
  }
37
74
 
38
- type BuildOperationOptions<TOptions extends PluginFactoryOptions> = {
75
+ type BuildOperationBaseOptions<TOptions extends PluginFactoryOptions> = {
39
76
  config: Config
40
77
  fabric: Fabric
41
- Component: ReactGenerator<any>['Operation']
42
- generator: Omit<OperationGenerator<TOptions>, 'build'>
43
78
  plugin: Plugin<TOptions>
44
79
  }
45
80
 
81
+ type BuildOperationV1Options<TOptions extends PluginFactoryOptions> = BuildOperationBaseOptions<TOptions> & {
82
+ version?: '1'
83
+ Component: ReactGenerator<any, '1'>['Operation']
84
+ generator: Omit<OperationGenerator<TOptions>, 'build'>
85
+ }
86
+
87
+ type BuildOperationV2Options<TOptions extends PluginFactoryOptions> = BuildOperationBaseOptions<TOptions> & {
88
+ version: '2'
89
+ Component: ReactGenerator<any, '2'>['Operation']
90
+ }
91
+
92
+ function isBuildOperationV1Options<TOptions extends PluginFactoryOptions>(
93
+ options: BuildOperationV1Options<TOptions> | BuildOperationV2Options<TOptions>,
94
+ ): options is BuildOperationV1Options<TOptions> {
95
+ return (options.version ?? '1') === '1'
96
+ }
97
+
98
+ export async function buildOperation<TOptions extends PluginFactoryOptions>(operation: Operation, options: BuildOperationV1Options<TOptions>): Promise<void>
99
+ export async function buildOperation<TOptions extends PluginFactoryOptions>(node: OperationNode, options: BuildOperationV2Options<TOptions>): Promise<void>
46
100
  export async function buildOperation<TOptions extends PluginFactoryOptions>(
47
- operation: Operation,
48
- { config, fabric, plugin, generator, Component }: BuildOperationOptions<TOptions>,
101
+ operationOrNode: Operation | OperationNode,
102
+ options: BuildOperationV1Options<TOptions> | BuildOperationV2Options<TOptions>,
49
103
  ): Promise<void> {
50
- if (!Component) {
104
+ const { config, fabric, plugin } = options
105
+
106
+ if (!options.Component) {
51
107
  return undefined
52
108
  }
53
109
 
54
- const { pluginManager, oas, mode } = generator.context
55
-
56
110
  const fabricChild = createReactFabric()
57
- await fabricChild.render(
58
- <App meta={{ pluginManager, plugin, mode, oas }}>
59
- <Component config={config} operation={operation} plugin={plugin} generator={generator} />
60
- </App>,
61
- )
111
+
112
+ if (isBuildOperationV1Options(options)) {
113
+ const { generator, Component } = options
114
+ const { pluginManager, oas, mode } = generator.context
115
+
116
+ await fabricChild.render(
117
+ <App meta={{ pluginManager, plugin, mode, oas }}>
118
+ <Component config={config} operation={operationOrNode as Operation} plugin={plugin} generator={generator} />
119
+ </App>,
120
+ )
121
+ } else {
122
+ const { Component } = options
123
+ await fabricChild.render(
124
+ <App meta={{ plugin }}>
125
+ <Component config={config} node={operationOrNode as OperationNode} plugin={plugin} />
126
+ </App>,
127
+ )
128
+ }
62
129
 
63
130
  await fabric.context.fileManager.upsert(...fabricChild.files)
64
131
  fabricChild.unmount()
65
132
  }
66
133
 
67
- type BuildSchemaOptions<TOptions extends PluginFactoryOptions> = {
134
+ type BuildSchemaBaseOptions<TOptions extends PluginFactoryOptions> = {
68
135
  config: Config
69
136
  fabric: Fabric
70
- Component: ReactGenerator<any>['Schema']
71
- generator: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>
72
137
  plugin: Plugin<TOptions>
73
138
  }
74
139
 
140
+ type BuildSchemaV1Options<TOptions extends PluginFactoryOptions> = BuildSchemaBaseOptions<TOptions> & {
141
+ version?: '1'
142
+ Component: ReactGenerator<any, '1'>['Schema']
143
+ generator: Omit<SchemaGenerator<SchemaGeneratorOptions, TOptions>, 'build'>
144
+ }
145
+
146
+ type BuildSchemaV2Options<TOptions extends PluginFactoryOptions> = BuildSchemaBaseOptions<TOptions> & {
147
+ version: '2'
148
+ Component: ReactGenerator<any, '2'>['Schema']
149
+ }
150
+
151
+ function isBuildSchemaV1Options<TOptions extends PluginFactoryOptions>(
152
+ options: BuildSchemaV1Options<TOptions> | BuildSchemaV2Options<TOptions>,
153
+ ): options is BuildSchemaV1Options<TOptions> {
154
+ return (options.version ?? '1') === '1'
155
+ }
156
+
157
+ export async function buildSchema<TOptions extends PluginFactoryOptions>(
158
+ schema: { name: string; tree: Array<Schema>; value: SchemaObject },
159
+ options: BuildSchemaV1Options<TOptions>,
160
+ ): Promise<void>
161
+ export async function buildSchema<TOptions extends PluginFactoryOptions>(schema: SchemaNode, options: BuildSchemaV2Options<TOptions>): Promise<void>
75
162
  export async function buildSchema<TOptions extends PluginFactoryOptions>(
76
- schema: {
77
- name: string
78
- tree: Array<Schema>
79
- value: SchemaObject
80
- },
81
- { config, fabric, plugin, Component, generator }: BuildSchemaOptions<TOptions>,
163
+ schema: { name: string; tree: Array<Schema>; value: SchemaObject } | SchemaNode,
164
+ options: BuildSchemaV1Options<TOptions> | BuildSchemaV2Options<TOptions>,
82
165
  ): Promise<void> {
83
- if (!Component) {
166
+ const { config, fabric, plugin } = options
167
+
168
+ if (!options.Component) {
84
169
  return undefined
85
170
  }
86
171
 
87
- const { pluginManager, oas, mode } = generator.context
88
-
89
172
  const fabricChild = createReactFabric()
90
- await fabricChild.render(
91
- <App meta={{ pluginManager, plugin, mode, oas }}>
92
- <Component config={config} schema={schema} plugin={plugin} generator={generator} />
93
- </App>,
94
- )
173
+
174
+ if (isBuildSchemaV1Options(options)) {
175
+ const { generator, Component } = options
176
+ const { pluginManager, oas, mode } = generator.context
177
+
178
+ await fabricChild.render(
179
+ <App meta={{ pluginManager, plugin, mode, oas }}>
180
+ <Component config={config} schema={schema as { name: string; tree: Array<Schema>; value: SchemaObject }} plugin={plugin} generator={generator} />
181
+ </App>,
182
+ )
183
+ } else {
184
+ const { Component } = options
185
+ await fabricChild.render(
186
+ <App meta={{ plugin }}>
187
+ <Component config={config} node={schema as SchemaNode} plugin={plugin} />
188
+ </App>,
189
+ )
190
+ }
95
191
 
96
192
  await fabric.context.fileManager.upsert(...fabricChild.files)
97
193
  fabricChild.unmount()
@@ -1 +0,0 @@
1
- {"version":3,"file":"generators-CBlzDhTh.cjs","names":["camelCase","getBanner","getFooter"],"sources":["../src/generators/createGenerator.ts","../src/generators/createReactGenerator.ts","../src/generators/jsonGenerator.ts"],"sourcesContent":["import type { PluginFactoryOptions } from '@kubb/core'\nimport type { KubbFile } from '@kubb/fabric-core/types'\nimport type { OperationProps, OperationsProps, SchemaProps } from './types.ts'\n\ntype UserGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n operations?: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>\n operation?: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>\n schema?: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>\n}\n\nexport type CoreGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n type: 'core'\n operations: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>\n operation: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>\n schema: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>\n}\n\nexport function createGenerator<TOptions extends PluginFactoryOptions>(generator: UserGenerator<TOptions>): CoreGenerator<TOptions> {\n return {\n type: 'core',\n async operations() {\n return []\n },\n async operation() {\n return []\n },\n async schema() {\n return []\n },\n ...generator,\n }\n}\n","import type { PluginFactoryOptions } from '@kubb/core'\nimport type { FabricReactNode } from '@kubb/react-fabric/types'\nimport type { OperationProps, OperationsProps, SchemaProps } from './types.ts'\n\ntype UserGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n Operations?: (props: OperationsProps<TOptions>) => FabricReactNode\n Operation?: (props: OperationProps<TOptions>) => FabricReactNode\n Schema?: (props: SchemaProps<TOptions>) => FabricReactNode\n}\n\nexport type ReactGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n type: 'react'\n Operations: (props: OperationsProps<TOptions>) => FabricReactNode\n Operation: (props: OperationProps<TOptions>) => FabricReactNode\n Schema: (props: SchemaProps<TOptions>) => FabricReactNode\n}\n\n/****\n * Creates a generator that uses React component functions to generate files for OpenAPI operations and schemas.\n *\n * The returned generator exposes async methods for generating files from operations, a single operation, or a schema, using the corresponding React components if provided. If a component is not defined, the method returns an empty array.\n *\n * @returns A generator object with async methods for operations, operation, and schema file generation.\n */\nexport function createReactGenerator<TOptions extends PluginFactoryOptions>(generator: UserGenerator<TOptions>): ReactGenerator<TOptions> {\n return {\n type: 'react',\n Operations() {\n return null\n },\n Operation() {\n return null\n },\n Schema() {\n return null\n },\n ...generator,\n }\n}\n","import { camelCase } from '@internals/utils'\nimport type { PluginOas } from '../types.ts'\nimport { getBanner } from '../utils/getBanner.ts'\nimport { getFooter } from '../utils/getFooter.ts'\nimport { createGenerator } from './createGenerator.ts'\n\nexport const jsonGenerator = createGenerator<PluginOas>({\n name: 'plugin-oas',\n async schema({ schema, generator }) {\n const { pluginManager, plugin } = generator.context\n const file = pluginManager.getFile({\n name: camelCase(schema.name),\n extname: '.json',\n mode: 'split',\n pluginKey: plugin.key,\n })\n\n return [\n {\n ...file,\n sources: [\n {\n name: camelCase(schema.name),\n isExportable: false,\n isIndexable: false,\n value: JSON.stringify(schema.value),\n },\n ],\n banner: getBanner({\n oas: generator.context.oas,\n output: plugin.options.output,\n config: pluginManager.config,\n }),\n format: getFooter({ oas: generator.context.oas, output: plugin.options.output }),\n },\n ]\n },\n})\n"],"mappings":";;;AAmBA,SAAgB,gBAAuD,WAA6D;AAClI,QAAO;EACL,MAAM;EACN,MAAM,aAAa;AACjB,UAAO,EAAE;;EAEX,MAAM,YAAY;AAChB,UAAO,EAAE;;EAEX,MAAM,SAAS;AACb,UAAO,EAAE;;EAEX,GAAG;EACJ;;;;;;;;;;;ACNH,SAAgB,qBAA4D,WAA8D;AACxI,QAAO;EACL,MAAM;EACN,aAAa;AACX,UAAO;;EAET,YAAY;AACV,UAAO;;EAET,SAAS;AACP,UAAO;;EAET,GAAG;EACJ;;;;ACjCH,MAAa,gBAAgB,gBAA2B;CACtD,MAAM;CACN,MAAM,OAAO,EAAE,QAAQ,aAAa;EAClC,MAAM,EAAE,eAAe,WAAW,UAAU;AAQ5C,SAAO,CACL;GACE,GATS,cAAc,QAAQ;IACjC,MAAMA,kBAAAA,UAAU,OAAO,KAAK;IAC5B,SAAS;IACT,MAAM;IACN,WAAW,OAAO;IACnB,CAAC;GAKE,SAAS,CACP;IACE,MAAMA,kBAAAA,UAAU,OAAO,KAAK;IAC5B,cAAc;IACd,aAAa;IACb,OAAO,KAAK,UAAU,OAAO,MAAM;IACpC,CACF;GACD,QAAQC,kBAAAA,UAAU;IAChB,KAAK,UAAU,QAAQ;IACvB,QAAQ,OAAO,QAAQ;IACvB,QAAQ,cAAc;IACvB,CAAC;GACF,QAAQC,kBAAAA,UAAU;IAAE,KAAK,UAAU,QAAQ;IAAK,QAAQ,OAAO,QAAQ;IAAQ,CAAC;GACjF,CACF;;CAEJ,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"generators-DRPPf38p.js","names":[],"sources":["../src/generators/createGenerator.ts","../src/generators/createReactGenerator.ts","../src/generators/jsonGenerator.ts"],"sourcesContent":["import type { PluginFactoryOptions } from '@kubb/core'\nimport type { KubbFile } from '@kubb/fabric-core/types'\nimport type { OperationProps, OperationsProps, SchemaProps } from './types.ts'\n\ntype UserGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n operations?: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>\n operation?: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>\n schema?: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>\n}\n\nexport type CoreGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n type: 'core'\n operations: (props: OperationsProps<TOptions>) => Promise<KubbFile.File[]>\n operation: (props: OperationProps<TOptions>) => Promise<KubbFile.File[]>\n schema: (props: SchemaProps<TOptions>) => Promise<KubbFile.File[]>\n}\n\nexport function createGenerator<TOptions extends PluginFactoryOptions>(generator: UserGenerator<TOptions>): CoreGenerator<TOptions> {\n return {\n type: 'core',\n async operations() {\n return []\n },\n async operation() {\n return []\n },\n async schema() {\n return []\n },\n ...generator,\n }\n}\n","import type { PluginFactoryOptions } from '@kubb/core'\nimport type { FabricReactNode } from '@kubb/react-fabric/types'\nimport type { OperationProps, OperationsProps, SchemaProps } from './types.ts'\n\ntype UserGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n Operations?: (props: OperationsProps<TOptions>) => FabricReactNode\n Operation?: (props: OperationProps<TOptions>) => FabricReactNode\n Schema?: (props: SchemaProps<TOptions>) => FabricReactNode\n}\n\nexport type ReactGenerator<TOptions extends PluginFactoryOptions> = {\n name: string\n type: 'react'\n Operations: (props: OperationsProps<TOptions>) => FabricReactNode\n Operation: (props: OperationProps<TOptions>) => FabricReactNode\n Schema: (props: SchemaProps<TOptions>) => FabricReactNode\n}\n\n/****\n * Creates a generator that uses React component functions to generate files for OpenAPI operations and schemas.\n *\n * The returned generator exposes async methods for generating files from operations, a single operation, or a schema, using the corresponding React components if provided. If a component is not defined, the method returns an empty array.\n *\n * @returns A generator object with async methods for operations, operation, and schema file generation.\n */\nexport function createReactGenerator<TOptions extends PluginFactoryOptions>(generator: UserGenerator<TOptions>): ReactGenerator<TOptions> {\n return {\n type: 'react',\n Operations() {\n return null\n },\n Operation() {\n return null\n },\n Schema() {\n return null\n },\n ...generator,\n }\n}\n","import { camelCase } from '@internals/utils'\nimport type { PluginOas } from '../types.ts'\nimport { getBanner } from '../utils/getBanner.ts'\nimport { getFooter } from '../utils/getFooter.ts'\nimport { createGenerator } from './createGenerator.ts'\n\nexport const jsonGenerator = createGenerator<PluginOas>({\n name: 'plugin-oas',\n async schema({ schema, generator }) {\n const { pluginManager, plugin } = generator.context\n const file = pluginManager.getFile({\n name: camelCase(schema.name),\n extname: '.json',\n mode: 'split',\n pluginKey: plugin.key,\n })\n\n return [\n {\n ...file,\n sources: [\n {\n name: camelCase(schema.name),\n isExportable: false,\n isIndexable: false,\n value: JSON.stringify(schema.value),\n },\n ],\n banner: getBanner({\n oas: generator.context.oas,\n output: plugin.options.output,\n config: pluginManager.config,\n }),\n format: getFooter({ oas: generator.context.oas, output: plugin.options.output }),\n },\n ]\n },\n})\n"],"mappings":";;;AAmBA,SAAgB,gBAAuD,WAA6D;AAClI,QAAO;EACL,MAAM;EACN,MAAM,aAAa;AACjB,UAAO,EAAE;;EAEX,MAAM,YAAY;AAChB,UAAO,EAAE;;EAEX,MAAM,SAAS;AACb,UAAO,EAAE;;EAEX,GAAG;EACJ;;;;;;;;;;;ACNH,SAAgB,qBAA4D,WAA8D;AACxI,QAAO;EACL,MAAM;EACN,aAAa;AACX,UAAO;;EAET,YAAY;AACV,UAAO;;EAET,SAAS;AACP,UAAO;;EAET,GAAG;EACJ;;;;ACjCH,MAAa,gBAAgB,gBAA2B;CACtD,MAAM;CACN,MAAM,OAAO,EAAE,QAAQ,aAAa;EAClC,MAAM,EAAE,eAAe,WAAW,UAAU;AAQ5C,SAAO,CACL;GACE,GATS,cAAc,QAAQ;IACjC,MAAM,UAAU,OAAO,KAAK;IAC5B,SAAS;IACT,MAAM;IACN,WAAW,OAAO;IACnB,CAAC;GAKE,SAAS,CACP;IACE,MAAM,UAAU,OAAO,KAAK;IAC5B,cAAc;IACd,aAAa;IACb,OAAO,KAAK,UAAU,OAAO,MAAM;IACpC,CACF;GACD,QAAQ,UAAU;IAChB,KAAK,UAAU,QAAQ;IACvB,QAAQ,OAAO,QAAQ;IACvB,QAAQ,cAAc;IACvB,CAAC;GACF,QAAQ,UAAU;IAAE,KAAK,UAAU,QAAQ;IAAK,QAAQ,OAAO,QAAQ;IAAQ,CAAC;GACjF,CACF;;CAEJ,CAAC"}