Skip to content

Commit a60fd4c

Browse files
committed
chore: cleanup
1 parent df3f3db commit a60fd4c

File tree

9 files changed

+60
-53
lines changed

9 files changed

+60
-53
lines changed

examples/react-query-v5/templates/operations/index.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@ import { usePluginManager } from '@kubb/react'
88

99
export const templates = {
1010
...Operations.templates,
11-
editor: function({}: React.ComponentProps<typeof Operations.templates.editor>) {
11+
root: function({}: React.ComponentProps<typeof Operations.templates.root>) {
1212
const pluginManager = usePluginManager()
1313
const { key: pluginKey } = usePlugin<PluginOptions>()
1414
const operations = useOperations()
15-
const { getSchemas } = useOas()
16-
const { getOperationName } = useOperationHelpers()
15+
const { getName, getSchemas } = useOperationHelpers()
1716

1817
const root = path.resolve(pluginManager.config.root, pluginManager.config.output.path)
1918

@@ -24,7 +23,7 @@ export const templates = {
2423
}).flat().filter(Boolean)
2524

2625
const invalidations = operations.reduce((acc, operation) => {
27-
const name = getOperationName(operation, { pluginKey, type: 'function' })
26+
const name = getName(operation, { pluginKey, type: 'function' })
2827
const schemas = getSchemas(operation)
2928

3029
acc[name] = `UseMutationOptions<${schemas.response.name}, unknown, ${schemas.request?.name || 'void'}>['onSuccess']`

packages/swagger-client/src/OperationGenerator.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('OperationGenerator', async () => {
9393
operations: Operations.templates,
9494
client: {
9595
default: CustomClientTemplate,
96-
editor: Client.templates.editor,
96+
root: Client.templates.root,
9797
},
9898
},
9999
client: {

packages/swagger-client/src/components/Client.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ return ${client.dataReturnType === 'data' ? 'res.data' : 'res'}
7676
)
7777
}
7878

79-
type EditorTemplateProps = {
79+
type RootTemplateProps = {
8080
children?: React.ReactNode
8181
}
8282

83-
function EditorTemplate({ children }: EditorTemplateProps) {
83+
function RootTemplate({ children }: RootTemplateProps) {
8484
const { options: { client: { importPath } } } = usePlugin<PluginOptions>()
8585

8686
const schemas = useSchemas()
@@ -112,7 +112,7 @@ function EditorTemplate({ children }: EditorTemplateProps) {
112112
)
113113
}
114114

115-
const defaultTemplates = { default: Template, editor: EditorTemplate } as const
115+
const defaultTemplates = { default: Template, root: RootTemplate } as const
116116

117117
type Templates = Partial<typeof defaultTemplates>
118118

@@ -195,12 +195,12 @@ Client.File = function(props: FileProps): KubbNode {
195195
const templates = { ...defaultTemplates, ...props.templates }
196196

197197
const Template = templates.default
198-
const EditorTemplate = templates.editor
198+
const RootTemplate = templates.root
199199

200200
return (
201-
<EditorTemplate>
201+
<RootTemplate>
202202
<Client Template={Template} />
203-
</EditorTemplate>
203+
</RootTemplate>
204204
)
205205
}
206206

packages/swagger-client/src/components/Operations.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ function Template({
3636
)
3737
}
3838

39-
type EditorTemplateProps = {
39+
type RootTemplateProps = {
4040
children?: React.ReactNode
4141
}
4242

43-
function EditorTemplate({ children }: EditorTemplateProps) {
43+
function RootTemplate({ children }: RootTemplateProps) {
4444
const { key: pluginKey } = usePlugin<PluginOptions>()
4545
const file = useFile({ name: 'operations', extName: '.ts', pluginKey })
4646

@@ -59,7 +59,7 @@ function EditorTemplate({ children }: EditorTemplateProps) {
5959
)
6060
}
6161

62-
const defaultTemplates = { default: Template, editor: EditorTemplate } as const
62+
const defaultTemplates = { default: Template, root: RootTemplate } as const
6363

6464
type Templates = Partial<typeof defaultTemplates>
6565

@@ -94,12 +94,12 @@ Operations.File = function(props: FileProps): KubbNode {
9494
const templates = { ...defaultTemplates, ...props.templates }
9595

9696
const Template = templates.default
97-
const EditorTemplate = templates.editor
97+
const RootTemplate = templates.root
9898

9999
return (
100-
<EditorTemplate>
100+
<RootTemplate>
101101
<Operations Template={Template} />
102-
</EditorTemplate>
102+
</RootTemplate>
103103
)
104104
}
105105

packages/swagger-msw/src/components/Operations.tsx

+10-10
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ type EditorTemplateProps = {
2929
children?: React.ReactNode
3030
}
3131

32-
function EditorTemplate({ children }: EditorTemplateProps) {
32+
function RootTemplate({ children }: EditorTemplateProps) {
3333
const { key: pluginKey } = usePlugin<PluginOptions>()
3434

3535
const file = useFile({ name: 'handlers', extName: '.ts', pluginKey })
3636
const operations = useOperations()
3737

38-
const { getOperationName, getOperationFile } = useOperationHelpers()
38+
const { getName, getFile } = useOperationHelpers()
3939

4040
const imports = operations.map(operation => {
41-
const operationFile = getOperationFile(operation, { pluginKey })
42-
const operationName = getOperationName(operation, { pluginKey, type: 'function' })
41+
const operationFile = getFile(operation, { pluginKey })
42+
const operationName = getName(operation, { pluginKey, type: 'function' })
4343

4444
return <File.Import key={operationFile.path} name={[operationName]} root={file.path} path={operationFile.path} />
4545
}).filter(Boolean)
@@ -60,7 +60,7 @@ function EditorTemplate({ children }: EditorTemplateProps) {
6060
)
6161
}
6262

63-
const defaultTemplates = { default: Template, editor: EditorTemplate } as const
63+
const defaultTemplates = { default: Template, root: RootTemplate } as const
6464

6565
type Templates = Partial<typeof defaultTemplates>
6666

@@ -77,12 +77,12 @@ export function Operations({
7777
const { key: pluginKey } = usePlugin<PluginOptions>()
7878

7979
const operations = useOperations()
80-
const { getOperationName } = useOperationHelpers()
80+
const { getName } = useOperationHelpers()
8181

8282
return (
8383
<Template
8484
name="handlers"
85-
handlers={operations.map(operation => getOperationName(operation, { type: 'function', pluginKey }))}
85+
handlers={operations.map(operation => getName(operation, { type: 'function', pluginKey }))}
8686
/>
8787
)
8888
}
@@ -98,12 +98,12 @@ Operations.File = function(props: FileProps): KubbNode {
9898
const templates = { ...defaultTemplates, ...props.templates }
9999

100100
const Template = templates.default
101-
const EditorTemplate = templates.editor
101+
const RootTemplate = templates.root
102102

103103
return (
104-
<EditorTemplate>
104+
<RootTemplate>
105105
<Operations Template={Template} />
106-
</EditorTemplate>
106+
</RootTemplate>
107107
)
108108
}
109109

packages/swagger-tanstack-query/src/components/Operations.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ function Template({}: TemplateProps): KubbNode {
1313
return null
1414
}
1515

16-
type EditorTemplateProps = {
16+
type RootTemplateProps = {
1717
children?: React.ReactNode
1818
}
1919

20-
function EditorTemplate({ children }: EditorTemplateProps) {
20+
function RootTemplate({ children }: RootTemplateProps) {
2121
const { key: pluginKey } = usePlugin<PluginOptions>()
2222
const file = useFile({ name: 'operations', mode: 'directory', extName: '.ts', pluginKey })
2323

@@ -36,7 +36,7 @@ function EditorTemplate({ children }: EditorTemplateProps) {
3636
)
3737
}
3838

39-
const defaultTemplates = { default: Template, editor: EditorTemplate } as const
39+
const defaultTemplates = { default: Template, root: RootTemplate } as const
4040

4141
type Templates = Partial<typeof defaultTemplates>
4242

@@ -64,12 +64,12 @@ Operations.File = function(props: FileProps): KubbNode {
6464
const templates = { ...defaultTemplates, ...props.templates }
6565

6666
const Template = templates.default
67-
const EditorTemplate = templates.editor
67+
const RootTemplate = templates.root
6868

6969
return (
70-
<EditorTemplate>
70+
<RootTemplate>
7171
<Operations Template={Template} />
72-
</EditorTemplate>
72+
</RootTemplate>
7373
)
7474
}
7575

packages/swagger/src/hooks/useOas.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import type { Oas as OasType } from '../oas/index.ts'
77

88
type Result = {
99
oas: OasType
10+
/**
11+
* @deprecated replace 'getSchemas' with `useOperationHelpers`
12+
*/
1013
getSchemas: GetSchemas
1114
}
1215

packages/swagger/src/hooks/useOperation.ts

+7-12
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,10 @@ type UseOperationNameProps = {
2424
export function useOperationName({ type, ...rest }: UseOperationNameProps): string {
2525
const plugin = usePlugin()
2626
const operation = useOperation()
27-
const { getOperationName } = useOperationHelpers()
27+
const { getName } = useOperationHelpers()
2828

29-
const pluginKey = rest.pluginKey || plugin.key
30-
31-
return getOperationName(operation, {
32-
pluginKey,
29+
return getName(operation, {
30+
pluginKey: rest.pluginKey || plugin.key,
3331
type,
3432
})
3533
}
@@ -49,13 +47,10 @@ export function useOperationFile(props: UseOperationFileProps = {}): KubbFile.Fi
4947
const plugin = usePlugin()
5048
const operation = useOperation()
5149

52-
const { getOperationFile } = useOperationHelpers()
53-
54-
const pluginKey = props.pluginKey || plugin.key
55-
const extName = props.extName || '.ts'
50+
const { getFile } = useOperationHelpers()
5651

57-
return getOperationFile(operation, {
58-
pluginKey,
59-
extName,
52+
return getFile(operation, {
53+
pluginKey: props.pluginKey || plugin.key,
54+
extName: props.extName,
6055
})
6156
}

packages/swagger/src/hooks/useOperationHelpers.ts

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
import { usePluginManager } from '@kubb/react'
1+
import { useContext, usePlugin, usePluginManager } from '@kubb/react'
2+
3+
import { Oas } from '../components/Oas.tsx'
24

35
import type { KubbFile, Plugin, ResolveNameParams } from '@kubb/core'
6+
import type { GetSchemas } from '../components/Oas.tsx'
47
import type { Operation as OperationType } from '../oas/index.ts'
58

69
type FileMeta = KubbFile.FileMetaBase & {
@@ -10,21 +13,23 @@ type FileMeta = KubbFile.FileMetaBase & {
1013
}
1114

1215
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
1519
}
1620

1721
export function useOperationHelpers(): UseOperationHelpersResult {
1822
const pluginManager = usePluginManager()
23+
const { getSchemas } = useContext(Oas.Context)
1924

20-
const getOperationName: UseOperationHelpersResult['getOperationName'] = (operation, { pluginKey, type }) => {
25+
const getName: UseOperationHelpersResult['getName'] = (operation, { pluginKey, type }) => {
2126
return pluginManager.resolveName({ name: operation.getOperationId(), pluginKey, type })
2227
}
2328

24-
const getOperationFile: UseOperationHelpersResult['getOperationFile'] = (operation, { pluginKey, extName = '.ts' }) => {
29+
const getFile: UseOperationHelpersResult['getFile'] = (operation, { pluginKey, extName = '.ts' }) => {
2530
// needed for the `output.group`
2631
const tag = operation?.getTags().at(0)?.name
27-
const name = getOperationName(operation, { type: 'file', pluginKey })
32+
const name = getName(operation, { type: 'file', pluginKey })
2833

2934
const file = pluginManager.getFile({ name, extName, pluginKey, options: { type: 'file', pluginKey, tag } })
3035

@@ -39,8 +44,13 @@ export function useOperationHelpers(): UseOperationHelpersResult {
3944
}
4045
}
4146

47+
if (!getSchemas) {
48+
throw new Error(`'getSchemas' is not defined`)
49+
}
50+
4251
return {
43-
getOperationName,
44-
getOperationFile,
52+
getName,
53+
getFile,
54+
getSchemas,
4555
}
4656
}

0 commit comments

Comments
 (0)