@kubb/plugin-oas 3.0.0-alpha.2 → 3.0.0-alpha.21
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.
- package/README.md +13 -4
- package/dist/OperationGenerator-CHXolL0o.d.cts +553 -0
- package/dist/OperationGenerator-CHXolL0o.d.ts +553 -0
- package/dist/Schema-Ch0dHkM5.d.ts +22 -0
- package/dist/Schema-ClNodlyP.d.cts +22 -0
- package/dist/chunk-6UWOTOZJ.cjs +92 -0
- package/dist/chunk-6UWOTOZJ.cjs.map +1 -0
- package/dist/chunk-AZKUU24F.js +700 -0
- package/dist/chunk-AZKUU24F.js.map +1 -0
- package/dist/chunk-DFQJ57GH.js +45 -0
- package/dist/chunk-DFQJ57GH.js.map +1 -0
- package/dist/chunk-GF26SDHQ.js +28 -0
- package/dist/chunk-GF26SDHQ.js.map +1 -0
- package/dist/chunk-HP5WAFBU.cjs +48 -0
- package/dist/chunk-HP5WAFBU.cjs.map +1 -0
- package/dist/chunk-JNGUNN72.cjs +708 -0
- package/dist/chunk-JNGUNN72.cjs.map +1 -0
- package/dist/chunk-PADR76WZ.cjs +4 -0
- package/dist/chunk-PADR76WZ.cjs.map +1 -0
- package/dist/chunk-R47XMJ32.js +3 -0
- package/dist/chunk-R47XMJ32.js.map +1 -0
- package/dist/{chunk-Y4V7HHX7.js → chunk-THGCWF2O.js} +8 -13
- package/dist/{chunk-Y4V7HHX7.js.map → chunk-THGCWF2O.js.map} +1 -1
- package/dist/chunk-TRA5BTWI.js +88 -0
- package/dist/chunk-TRA5BTWI.js.map +1 -0
- package/dist/chunk-UO57DZPV.cjs +36 -0
- package/dist/chunk-UO57DZPV.cjs.map +1 -0
- package/dist/chunk-ZWHQ54JM.cjs +32 -0
- package/dist/chunk-ZWHQ54JM.cjs.map +1 -0
- package/dist/components.cjs +20 -17
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +12 -8
- package/dist/components.d.ts +12 -8
- package/dist/components.js +3 -17
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +14 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +5 -0
- package/dist/generators.js.map +1 -0
- package/dist/hooks.cjs +101 -57
- package/dist/hooks.cjs.map +1 -1
- package/dist/hooks.d.cts +40 -8
- package/dist/hooks.d.ts +40 -8
- package/dist/hooks.js +90 -53
- package/dist/hooks.js.map +1 -1
- package/dist/index.cjs +283 -261
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -77
- package/dist/index.d.ts +6 -77
- package/dist/index.js +235 -240
- package/dist/index.js.map +1 -1
- package/dist/utils.cjs +32 -87
- package/dist/utils.cjs.map +1 -1
- package/dist/utils.d.cts +7 -41
- package/dist/utils.d.ts +7 -41
- package/dist/utils.js +8 -86
- package/dist/utils.js.map +1 -1
- package/package.json +20 -14
- package/src/OperationGenerator.ts +75 -43
- package/src/SchemaGenerator.ts +44 -18
- package/src/SchemaMapper.ts +1 -1
- package/src/components/Oas.tsx +9 -3
- package/src/components/Operation.tsx +1 -1
- package/src/components/Schema.tsx +2 -102
- package/src/generator.tsx +133 -0
- package/src/generators/index.ts +1 -0
- package/src/generators/jsonGenerator.ts +32 -0
- package/src/hooks/index.ts +2 -0
- package/src/hooks/useOperationManager.ts +76 -28
- package/src/hooks/useSchemaManager.ts +77 -0
- package/src/index.ts +3 -9
- package/src/plugin.ts +64 -47
- package/src/types.ts +23 -12
- package/src/utils/getParams.ts +2 -2
- package/src/utils/index.ts +2 -2
- package/dist/OperationGenerator-CeM_9pxW.d.cts +0 -60
- package/dist/OperationGenerator-DhUhvRJE.d.ts +0 -60
- package/dist/Schema-DefwBJMc.d.ts +0 -35
- package/dist/Schema-an5hOrjZ.d.cts +0 -35
- package/dist/SchemaMapper-CsBQ6eEx.d.cts +0 -248
- package/dist/SchemaMapper-CsBQ6eEx.d.ts +0 -248
- package/dist/chunk-CJXRFYEF.cjs +0 -698
- package/dist/chunk-CJXRFYEF.cjs.map +0 -1
- package/dist/chunk-DRLYORTT.cjs +0 -3265
- package/dist/chunk-DRLYORTT.cjs.map +0 -1
- package/dist/chunk-K6KUETAI.js +0 -698
- package/dist/chunk-K6KUETAI.js.map +0 -1
- package/dist/chunk-LEACSHKT.js +0 -3265
- package/dist/chunk-LEACSHKT.js.map +0 -1
- package/dist/chunk-P42X362U.cjs +0 -101
- package/dist/chunk-P42X362U.cjs.map +0 -1
- package/dist/chunk-QLJIL3U5.cjs +0 -35
- package/dist/chunk-QLJIL3U5.cjs.map +0 -1
- package/dist/chunk-UB552H4J.js +0 -101
- package/dist/chunk-UB552H4J.js.map +0 -1
- package/dist/types-Dte3MA6H.d.cts +0 -143
- package/dist/types-Dte3MA6H.d.ts +0 -143
- package/src/utils/getGroupedByTagFiles.ts +0 -82
- package/src/utils/refSorter.ts +0 -13
|
@@ -0,0 +1,133 @@
|
|
|
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
|
+
schema: {
|
|
28
|
+
name: string
|
|
29
|
+
tree: Array<Schema>
|
|
30
|
+
value: SchemaObject
|
|
31
|
+
}
|
|
32
|
+
options: TOptions['resolvedOptions']
|
|
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
|
+
* Combine all react nodes and only render ones(to string or render)
|
|
55
|
+
*/
|
|
56
|
+
render?: () => any
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function createReactGenerator<TOptions extends PluginFactoryOptions>(parseOptions: ReactGeneratorOptions<TOptions>): Generator<TOptions> {
|
|
60
|
+
return {
|
|
61
|
+
...parseOptions,
|
|
62
|
+
async operations({ instance, options, operations, operationsByMethod }) {
|
|
63
|
+
if (!parseOptions.Operations) {
|
|
64
|
+
return []
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const { pluginManager, oas, plugin, mode } = instance.context
|
|
68
|
+
const root = createRoot({
|
|
69
|
+
logger: pluginManager.logger,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const Component = parseOptions.Operations.bind(this)
|
|
73
|
+
|
|
74
|
+
root.render(
|
|
75
|
+
<App pluginManager={pluginManager} plugin={plugin} mode={mode}>
|
|
76
|
+
<Oas oas={oas} operations={operations} generator={instance}>
|
|
77
|
+
<Component operations={operations} instance={instance} operationsByMethod={operationsByMethod} options={options} />
|
|
78
|
+
</Oas>
|
|
79
|
+
</App>,
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
return root.files
|
|
83
|
+
},
|
|
84
|
+
async operation({ instance, operation, options }) {
|
|
85
|
+
if (!parseOptions.Operation) {
|
|
86
|
+
return []
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const { pluginManager, oas, plugin, mode } = instance.context
|
|
90
|
+
const root = createRoot({
|
|
91
|
+
logger: pluginManager.logger,
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
const Component = parseOptions.Operation.bind(this)
|
|
95
|
+
|
|
96
|
+
root.render(
|
|
97
|
+
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
98
|
+
<Oas oas={oas} operations={[operation]} generator={instance}>
|
|
99
|
+
<Oas.Operation operation={operation}>
|
|
100
|
+
<Component operation={operation} options={options} instance={instance} />
|
|
101
|
+
</Oas.Operation>
|
|
102
|
+
</Oas>
|
|
103
|
+
</App>,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
return root.files
|
|
107
|
+
},
|
|
108
|
+
async schema({ instance, schema, options }) {
|
|
109
|
+
if (!parseOptions.Schema) {
|
|
110
|
+
return []
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const { pluginManager, oas, plugin, mode } = instance.context
|
|
114
|
+
const root = createRoot({
|
|
115
|
+
logger: pluginManager.logger,
|
|
116
|
+
})
|
|
117
|
+
|
|
118
|
+
const Component = parseOptions.Schema.bind(this)
|
|
119
|
+
|
|
120
|
+
root.render(
|
|
121
|
+
<App pluginManager={pluginManager} plugin={{ ...plugin, options }} mode={mode}>
|
|
122
|
+
<Oas oas={oas}>
|
|
123
|
+
<Oas.Schema name={schema.name} value={schema.value} tree={schema.tree}>
|
|
124
|
+
<Component schema={schema} options={options} instance={instance} />
|
|
125
|
+
</Oas.Schema>
|
|
126
|
+
</Oas>
|
|
127
|
+
</App>,
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
return root.files
|
|
131
|
+
},
|
|
132
|
+
}
|
|
133
|
+
}
|
|
@@ -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
|
+
})
|
package/src/hooks/index.ts
CHANGED
|
@@ -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,41 @@ type SchemaNames = {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
type UseOperationManagerResult = {
|
|
28
|
-
getName: (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
+
getSchemaName: (
|
|
32
38
|
operation: Operation,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
39
|
+
key: keyof Omit<OperationSchemas, 'errors' | 'statusCodes'>,
|
|
40
|
+
params?: {
|
|
41
|
+
pluginKey?: Plugin['key']
|
|
42
|
+
type?: ResolveNameParams['type']
|
|
43
|
+
},
|
|
44
|
+
) => string
|
|
45
|
+
getFile: (
|
|
46
|
+
operation: OperationType,
|
|
47
|
+
params?: {
|
|
48
|
+
prefix?: string
|
|
49
|
+
suffix?: string
|
|
50
|
+
pluginKey?: Plugin['key']
|
|
51
|
+
extName?: KubbFile.Extname
|
|
52
|
+
tag?: string
|
|
53
|
+
},
|
|
54
|
+
) => KubbFile.File<FileMeta>
|
|
55
|
+
groupSchemasByName: (
|
|
56
|
+
operation: OperationType,
|
|
57
|
+
params: {
|
|
58
|
+
pluginKey?: Plugin['key']
|
|
59
|
+
type: ResolveNameParams['type']
|
|
60
|
+
},
|
|
61
|
+
) => SchemaNames
|
|
62
|
+
getSchemas: (operation: Operation, params?: { pluginKey?: Plugin['key']; type?: ResolveNameParams['type'] }) => OperationSchemas
|
|
36
63
|
}
|
|
37
64
|
|
|
38
65
|
/**
|
|
@@ -42,23 +69,48 @@ export function useOperationManager(): UseOperationManagerResult {
|
|
|
42
69
|
const { plugin, pluginManager } = useApp()
|
|
43
70
|
const { generator } = useContext(Oas.Context)
|
|
44
71
|
|
|
45
|
-
|
|
46
|
-
throw new Error(`'generator' is not defined`)
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
const getName: UseOperationManagerResult['getName'] = (operation, { pluginKey = plugin.key, type }) => {
|
|
72
|
+
const getName: UseOperationManagerResult['getName'] = (operation, { prefix = '', suffix = '', pluginKey = plugin.key, type }) => {
|
|
50
73
|
return pluginManager.resolveName({
|
|
51
|
-
name: operation.getOperationId()
|
|
74
|
+
name: `${prefix} ${operation.getOperationId()} ${suffix}`,
|
|
52
75
|
pluginKey,
|
|
53
76
|
type,
|
|
54
77
|
})
|
|
55
78
|
}
|
|
56
79
|
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
80
|
+
const getSchemas: UseOperationManagerResult['getSchemas'] = (operation, params) => {
|
|
81
|
+
if (!generator) {
|
|
82
|
+
throw new Error(`'generator' is not defined`)
|
|
83
|
+
}
|
|
61
84
|
|
|
85
|
+
return generator.getSchemas(operation, {
|
|
86
|
+
resolveName: (name) =>
|
|
87
|
+
pluginManager.resolveName({
|
|
88
|
+
name,
|
|
89
|
+
pluginKey: params?.pluginKey,
|
|
90
|
+
type: params?.type,
|
|
91
|
+
}),
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const getSchemaName: UseOperationManagerResult['getSchemaName'] = (operation, key, { pluginKey = plugin.key, type } = {}) => {
|
|
96
|
+
const schemas = getSchemas(operation)
|
|
97
|
+
|
|
98
|
+
if (!schemas[key]?.name) {
|
|
99
|
+
throw new Error(`SchemaName not found for ${operation.getOperationId()}`)
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return pluginManager.resolveName({
|
|
103
|
+
name: schemas[key]?.name,
|
|
104
|
+
pluginKey,
|
|
105
|
+
type,
|
|
106
|
+
})
|
|
107
|
+
}
|
|
108
|
+
//TODO replace tag with group
|
|
109
|
+
const getFile: UseOperationManagerResult['getFile'] = (
|
|
110
|
+
operation,
|
|
111
|
+
{ prefix, suffix, pluginKey = plugin.key, tag = operation.getTags().at(0)?.name, extName = '.ts' } = {},
|
|
112
|
+
) => {
|
|
113
|
+
const name = getName(operation, { type: 'file', pluginKey, prefix, suffix })
|
|
62
114
|
const file = pluginManager.getFile({
|
|
63
115
|
name,
|
|
64
116
|
extName,
|
|
@@ -78,6 +130,10 @@ export function useOperationManager(): UseOperationManagerResult {
|
|
|
78
130
|
}
|
|
79
131
|
|
|
80
132
|
const groupSchemasByName: UseOperationManagerResult['groupSchemasByName'] = (operation, { pluginKey = plugin.key, type }) => {
|
|
133
|
+
if (!generator) {
|
|
134
|
+
throw new Error(`'generator' is not defined`)
|
|
135
|
+
}
|
|
136
|
+
|
|
81
137
|
const schemas = generator.getSchemas(operation)
|
|
82
138
|
|
|
83
139
|
const errors = (schemas.errors || []).reduce(
|
|
@@ -148,16 +204,8 @@ export function useOperationManager(): UseOperationManagerResult {
|
|
|
148
204
|
return {
|
|
149
205
|
getName,
|
|
150
206
|
getFile,
|
|
151
|
-
getSchemas
|
|
152
|
-
|
|
153
|
-
forStatusCode,
|
|
154
|
-
resolveName: (name) =>
|
|
155
|
-
pluginManager.resolveName({
|
|
156
|
-
name,
|
|
157
|
-
pluginKey: params?.pluginKey,
|
|
158
|
-
type: params?.type,
|
|
159
|
-
}),
|
|
160
|
-
}),
|
|
207
|
+
getSchemas,
|
|
208
|
+
getSchemaName,
|
|
161
209
|
groupSchemasByName,
|
|
162
210
|
}
|
|
163
211
|
}
|
|
@@ -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 { mode, plugin, pluginManager, fileManager } = 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) {
|
|
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,5 +1,3 @@
|
|
|
1
|
-
import { pluginOas } from './plugin.ts'
|
|
2
|
-
|
|
3
1
|
export type {
|
|
4
2
|
GetOperationGeneratorOptions,
|
|
5
3
|
OperationMethodResult,
|
|
@@ -22,10 +20,6 @@ export type {
|
|
|
22
20
|
} from './SchemaMapper.ts'
|
|
23
21
|
export { isKeyword, schemaKeywords } from './SchemaMapper.ts'
|
|
24
22
|
export type * from './types.ts'
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
*/
|
|
29
|
-
const definePluginDefault = pluginOas
|
|
30
|
-
|
|
31
|
-
export default definePluginDefault
|
|
23
|
+
export { createGenerator, createReactGenerator } from './generator.tsx'
|
|
24
|
+
export type { ReactGeneratorOptions } from './generator.tsx'
|
|
25
|
+
export type { Generator, GeneratorOptions } from './generator.tsx'
|
package/src/plugin.ts
CHANGED
|
@@ -1,25 +1,31 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import { createPlugin } from '@kubb/core'
|
|
4
|
-
import { camelCase } from '@kubb/core/transformers'
|
|
1
|
+
import { FileManager, createPlugin } from '@kubb/core'
|
|
5
2
|
|
|
6
3
|
import { getSchemas } from './utils/getSchemas.ts'
|
|
4
|
+
import { parseFromConfig } from './utils/parseFromConfig.ts'
|
|
7
5
|
|
|
6
|
+
import path from 'node:path'
|
|
8
7
|
import type { Config } from '@kubb/core'
|
|
9
8
|
import type { Logger } from '@kubb/core/logger'
|
|
10
|
-
import type { Oas
|
|
9
|
+
import type { Oas } from '@kubb/oas'
|
|
11
10
|
import type { FormatOptions } from '@kubb/oas/parser'
|
|
11
|
+
import { OperationGenerator } from './OperationGenerator.ts'
|
|
12
|
+
import { SchemaGenerator } from './SchemaGenerator.ts'
|
|
13
|
+
import { jsonGenerator } from './generators'
|
|
12
14
|
import type { PluginOas } from './types.ts'
|
|
13
|
-
import { parseFromConfig } from './utils/parseFromConfig.ts'
|
|
14
15
|
|
|
15
16
|
export const pluginOasName = 'plugin-oas' satisfies PluginOas['name']
|
|
16
17
|
|
|
17
18
|
export const pluginOas = createPlugin<PluginOas>((options) => {
|
|
18
19
|
const {
|
|
19
|
-
output = {
|
|
20
|
+
output = {
|
|
21
|
+
path: 'schemas',
|
|
22
|
+
export: false,
|
|
23
|
+
exportType: 'barrelNamed',
|
|
24
|
+
},
|
|
20
25
|
experimentalFilter: filter,
|
|
21
26
|
experimentalSort: sort,
|
|
22
27
|
validate = true,
|
|
28
|
+
generators = [jsonGenerator],
|
|
23
29
|
serverIndex = 0,
|
|
24
30
|
contentType,
|
|
25
31
|
oasClass,
|
|
@@ -45,7 +51,10 @@ export const pluginOas = createPlugin<PluginOas>((options) => {
|
|
|
45
51
|
|
|
46
52
|
return {
|
|
47
53
|
name: pluginOasName,
|
|
48
|
-
options
|
|
54
|
+
options: {
|
|
55
|
+
output,
|
|
56
|
+
...options,
|
|
57
|
+
},
|
|
49
58
|
context() {
|
|
50
59
|
const { config, logger } = this
|
|
51
60
|
|
|
@@ -59,24 +68,25 @@ export const pluginOas = createPlugin<PluginOas>((options) => {
|
|
|
59
68
|
},
|
|
60
69
|
async getBaseURL() {
|
|
61
70
|
const oasInstance = await this.getOas()
|
|
62
|
-
|
|
63
|
-
return baseURL
|
|
71
|
+
return oasInstance.api.servers?.at(serverIndex)?.url
|
|
64
72
|
},
|
|
65
73
|
contentType,
|
|
66
74
|
}
|
|
67
75
|
},
|
|
68
|
-
resolvePath(baseName) {
|
|
69
|
-
if (output === false) {
|
|
70
|
-
return undefined
|
|
71
|
-
}
|
|
72
|
-
|
|
76
|
+
resolvePath(baseName, pathMode, options) {
|
|
73
77
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
78
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
79
|
+
|
|
80
|
+
if (mode === 'single') {
|
|
81
|
+
/**
|
|
82
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
83
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
84
|
+
*/
|
|
85
|
+
return path.resolve(root, output.path)
|
|
86
|
+
}
|
|
74
87
|
|
|
75
88
|
return path.resolve(root, output.path, baseName)
|
|
76
89
|
},
|
|
77
|
-
resolveName(name, type) {
|
|
78
|
-
return camelCase(name, { isFile: type === 'file' })
|
|
79
|
-
},
|
|
80
90
|
async buildStart() {
|
|
81
91
|
if (!output) {
|
|
82
92
|
return
|
|
@@ -91,36 +101,43 @@ export const pluginOas = createPlugin<PluginOas>((options) => {
|
|
|
91
101
|
},
|
|
92
102
|
})
|
|
93
103
|
await oas.dereference()
|
|
94
|
-
const schemas = getSchemas({ oas, contentType })
|
|
95
|
-
|
|
96
|
-
const mapSchema = async ([name, schema]: [string, OasTypes.SchemaObject]) => {
|
|
97
|
-
const resolvedPath = this.resolvePath({
|
|
98
|
-
baseName: `${name}.json`,
|
|
99
|
-
pluginKey: this.plugin.key,
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
const resvoledFileName = this.resolveName({
|
|
103
|
-
name: `${name}.json`,
|
|
104
|
-
pluginKey: [pluginOasName],
|
|
105
|
-
type: 'file',
|
|
106
|
-
}) as `${string}.json`
|
|
107
|
-
|
|
108
|
-
if (!resolvedPath) {
|
|
109
|
-
return
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
await this.addFile({
|
|
113
|
-
path: resolvedPath,
|
|
114
|
-
baseName: resvoledFileName,
|
|
115
|
-
source: JSON.stringify(schema),
|
|
116
|
-
meta: {
|
|
117
|
-
pluginKey: this.plugin.key,
|
|
118
|
-
},
|
|
119
|
-
})
|
|
120
|
-
}
|
|
121
104
|
|
|
122
|
-
const
|
|
123
|
-
|
|
105
|
+
const schemaGenerator = new SchemaGenerator(
|
|
106
|
+
{
|
|
107
|
+
unknownType: 'unknown',
|
|
108
|
+
dateType: 'date',
|
|
109
|
+
transformers: {},
|
|
110
|
+
...this.plugin.options,
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
oas,
|
|
114
|
+
pluginManager: this.pluginManager,
|
|
115
|
+
plugin: this.plugin,
|
|
116
|
+
contentType,
|
|
117
|
+
include: undefined,
|
|
118
|
+
override: undefined,
|
|
119
|
+
mode: 'split',
|
|
120
|
+
output: output.path,
|
|
121
|
+
},
|
|
122
|
+
)
|
|
123
|
+
|
|
124
|
+
const schemaFiles = await schemaGenerator.build(...generators)
|
|
125
|
+
await this.addFile(...schemaFiles)
|
|
126
|
+
|
|
127
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
128
|
+
oas,
|
|
129
|
+
pluginManager: this.pluginManager,
|
|
130
|
+
plugin: this.plugin,
|
|
131
|
+
contentType,
|
|
132
|
+
exclude: undefined,
|
|
133
|
+
include: undefined,
|
|
134
|
+
override: undefined,
|
|
135
|
+
mode: 'split',
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
const operationFiles = await operationGenerator.build(...generators)
|
|
139
|
+
|
|
140
|
+
await this.addFile(...operationFiles)
|
|
124
141
|
},
|
|
125
142
|
}
|
|
126
143
|
})
|
package/src/types.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { Plugin } from '@kubb/core'
|
|
1
|
+
import type { Output, Plugin } from '@kubb/core'
|
|
2
2
|
import type { PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
3
3
|
import type * as KubbFile from '@kubb/fs/types'
|
|
4
4
|
|
|
5
5
|
import type { HttpMethod, Oas, Operation, SchemaObject, contentType } from '@kubb/oas'
|
|
6
6
|
import type { FormatOptions } from '@kubb/oas/parser'
|
|
7
|
+
import type { Generator } from './generator.tsx'
|
|
7
8
|
import type { GetSchemasProps } from './utils/getSchemas.ts'
|
|
8
9
|
|
|
9
10
|
export type ResolvePathOptions = {
|
|
@@ -25,16 +26,10 @@ export type Options = {
|
|
|
25
26
|
* @default true
|
|
26
27
|
*/
|
|
27
28
|
validate?: boolean
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
* False will not generate the schema JSON's.
|
|
33
|
-
* @default 'schemas'
|
|
34
|
-
*/
|
|
35
|
-
path: string
|
|
36
|
-
}
|
|
37
|
-
| false
|
|
29
|
+
/**
|
|
30
|
+
* @default 'schemas'
|
|
31
|
+
*/
|
|
32
|
+
output?: Output
|
|
38
33
|
/**
|
|
39
34
|
* Which server to use from the array of `servers.url[serverIndex]`
|
|
40
35
|
* @example
|
|
@@ -50,7 +45,14 @@ export type Options = {
|
|
|
50
45
|
contentType?: contentType
|
|
51
46
|
experimentalFilter?: FormatOptions['filterSet']
|
|
52
47
|
experimentalSort?: FormatOptions['sortSet']
|
|
48
|
+
/**
|
|
49
|
+
* Override some behaviour of the Oas class instance, see '@kubb/oas'
|
|
50
|
+
*/
|
|
53
51
|
oasClass?: typeof Oas
|
|
52
|
+
/**
|
|
53
|
+
* Define some generators next to the JSON generation
|
|
54
|
+
*/
|
|
55
|
+
generators?: Array<Generator<PluginOas>>
|
|
54
56
|
}
|
|
55
57
|
|
|
56
58
|
/**
|
|
@@ -128,6 +130,11 @@ type ByMethod = {
|
|
|
128
130
|
type: 'method'
|
|
129
131
|
pattern: HttpMethod | RegExp
|
|
130
132
|
}
|
|
133
|
+
// TODO implement as alternative for ByMethod
|
|
134
|
+
type ByMethods = {
|
|
135
|
+
type: 'methods'
|
|
136
|
+
pattern: Array<HttpMethod>
|
|
137
|
+
}
|
|
131
138
|
|
|
132
139
|
type BySchemaName = {
|
|
133
140
|
type: 'schemaName'
|
|
@@ -141,4 +148,8 @@ export type Override<TOptions> = (ByTag | ByOperationId | ByPath | ByMethod | By
|
|
|
141
148
|
options: Partial<TOptions>
|
|
142
149
|
}
|
|
143
150
|
|
|
144
|
-
|
|
151
|
+
type ResolvedOptions = Options & {
|
|
152
|
+
output: Output
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export type PluginOas = PluginFactoryOptions<'plugin-oas', Options, ResolvedOptions, API, never>
|
package/src/utils/getParams.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { isParameterObject } from '@kubb/oas'
|
|
2
2
|
|
|
3
|
+
import { camelCase } from '@kubb/core/transformers'
|
|
3
4
|
import type { FunctionParamsAST } from '@kubb/core/utils'
|
|
4
5
|
import type { OasTypes } from '@kubb/oas'
|
|
5
|
-
import type { Params } from '@kubb/react'
|
|
6
|
+
import type { Params } from '@kubb/react/types'
|
|
6
7
|
import type { OperationSchema } from '../types.ts'
|
|
7
|
-
import { camelCase } from '@kubb/core/transformers'
|
|
8
8
|
/**
|
|
9
9
|
*
|
|
10
10
|
* @deprecated
|
package/src/utils/index.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
export { getComments } from './getComments.ts'
|
|
2
|
-
export { getGroupedByTagFiles } from './getGroupedByTagFiles.ts'
|
|
3
2
|
export { getASTParams, getPathParams } from './getParams.ts'
|
|
4
3
|
export { getSchemaFactory } from './getSchemaFactory.ts'
|
|
5
4
|
export type { GetSchemasProps } from './getSchemas.ts'
|
|
6
5
|
export { getSchemas } from './getSchemas.ts'
|
|
7
|
-
export { refsSorter } from './refSorter.ts'
|
|
8
6
|
export { parseFromConfig } from './parseFromConfig.ts'
|
|
7
|
+
|
|
8
|
+
export { isOptional } from '@kubb/oas'
|