1
- import { usePluginManager } from '@kubb/react'
1
+ import { useContext , usePlugin , usePluginManager } from '@kubb/react'
2
+
3
+ import { Oas } from '../components/Oas.tsx'
2
4
3
5
import type { KubbFile , Plugin , ResolveNameParams } from '@kubb/core'
6
+ import type { GetSchemas } from '../components/Oas.tsx'
4
7
import type { Operation as OperationType } from '../oas/index.ts'
5
8
6
9
type FileMeta = KubbFile . FileMetaBase & {
@@ -10,21 +13,23 @@ type FileMeta = KubbFile.FileMetaBase & {
10
13
}
11
14
12
15
type UseOperationHelpersResult = {
13
- getOperationName : ( operation : OperationType , params : { pluginKey ?: Plugin [ 'key' ] ; type : ResolveNameParams [ 'type' ] } ) => string
14
- getOperationFile : ( operation : OperationType , params : { pluginKey : Plugin [ 'key' ] ; extName ?: KubbFile . Extname } ) => KubbFile . File < FileMeta >
16
+ getName : ( operation : OperationType , params : { pluginKey ?: Plugin [ 'key' ] ; type : ResolveNameParams [ 'type' ] } ) => string
17
+ getFile : ( operation : OperationType , params : { pluginKey : Plugin [ 'key' ] ; extName ?: KubbFile . Extname } ) => KubbFile . File < FileMeta >
18
+ getSchemas : GetSchemas
15
19
}
16
20
17
21
export function useOperationHelpers ( ) : UseOperationHelpersResult {
18
22
const pluginManager = usePluginManager ( )
23
+ const { getSchemas } = useContext ( Oas . Context )
19
24
20
- const getOperationName : UseOperationHelpersResult [ 'getOperationName ' ] = ( operation , { pluginKey, type } ) => {
25
+ const getName : UseOperationHelpersResult [ 'getName ' ] = ( operation , { pluginKey, type } ) => {
21
26
return pluginManager . resolveName ( { name : operation . getOperationId ( ) , pluginKey, type } )
22
27
}
23
28
24
- const getOperationFile : UseOperationHelpersResult [ 'getOperationFile ' ] = ( operation , { pluginKey, extName = '.ts' } ) => {
29
+ const getFile : UseOperationHelpersResult [ 'getFile ' ] = ( operation , { pluginKey, extName = '.ts' } ) => {
25
30
// needed for the `output.group`
26
31
const tag = operation ?. getTags ( ) . at ( 0 ) ?. name
27
- const name = getOperationName ( operation , { type : 'file' , pluginKey } )
32
+ const name = getName ( operation , { type : 'file' , pluginKey } )
28
33
29
34
const file = pluginManager . getFile ( { name, extName, pluginKey, options : { type : 'file' , pluginKey, tag } } )
30
35
@@ -39,8 +44,13 @@ export function useOperationHelpers(): UseOperationHelpersResult {
39
44
}
40
45
}
41
46
47
+ if ( ! getSchemas ) {
48
+ throw new Error ( `'getSchemas' is not defined` )
49
+ }
50
+
42
51
return {
43
- getOperationName,
44
- getOperationFile,
52
+ getName,
53
+ getFile,
54
+ getSchemas,
45
55
}
46
56
}
0 commit comments