@kubb/plugin-zod 3.0.0-alpha.2 → 3.0.0-alpha.20
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/chunk-GILKYHDS.cjs +250 -0
- package/dist/chunk-GILKYHDS.cjs.map +1 -0
- package/dist/chunk-KGKZCQKF.js +322 -0
- package/dist/chunk-KGKZCQKF.js.map +1 -0
- package/dist/chunk-RPZWETTK.cjs +329 -0
- package/dist/chunk-RPZWETTK.cjs.map +1 -0
- package/dist/chunk-WIOP5MTW.js +241 -0
- package/dist/chunk-WIOP5MTW.js.map +1 -0
- package/dist/components.cjs +11 -6
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +20 -19
- package/dist/components.d.ts +20 -19
- package/dist/components.js +2 -10
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +17 -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 +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +12 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -137
- package/dist/index.d.ts +4 -137
- package/dist/index.js +3 -8
- package/dist/index.js.map +1 -1
- package/dist/types-BRWHhUnI.d.cts +109 -0
- package/dist/types-BRWHhUnI.d.ts +109 -0
- package/package.json +18 -20
- package/src/components/Operations.tsx +19 -103
- package/src/components/Zod.tsx +66 -0
- package/src/components/index.ts +1 -2
- package/src/generators/__snapshots__/anyof.ts +3 -0
- package/src/generators/__snapshots__/createPet.ts +15 -0
- package/src/generators/__snapshots__/createPetWithUnknownTypeAny.ts +13 -0
- package/src/generators/__snapshots__/createPetWithUnknownTypeUnknown.ts +15 -0
- package/src/generators/__snapshots__/deletePet.ts +3 -0
- package/src/generators/__snapshots__/enumNamesType.ts +3 -0
- package/src/generators/__snapshots__/enumNullable.ts +3 -0
- package/src/generators/__snapshots__/enumVarNamesType.ts +3 -0
- package/src/generators/__snapshots__/example.ts +3 -0
- package/src/generators/__snapshots__/getPets.ts +18 -0
- package/src/generators/__snapshots__/mixedValueTypeConst.ts +6 -0
- package/src/generators/__snapshots__/nullableString.ts +3 -0
- package/src/generators/__snapshots__/nullableStringUuid.ts +3 -0
- package/src/generators/__snapshots__/nullableStringWithAnyOf.ts +3 -0
- package/src/generators/__snapshots__/numberValueConst.ts +6 -0
- package/src/generators/__snapshots__/oneof.ts +3 -0
- package/src/generators/__snapshots__/operations.ts +46 -0
- package/src/generators/__snapshots__/optionalPetInfer.ts +5 -0
- package/src/generators/__snapshots__/optionalPetTyped.ts +3 -0
- package/src/generators/__snapshots__/order.ts +3 -0
- package/src/generators/__snapshots__/orderDateTyeString.ts +10 -0
- package/src/generators/__snapshots__/orderDateTypeFalse.ts +3 -0
- package/src/generators/__snapshots__/orderDateTypeString.ts +3 -0
- package/src/generators/__snapshots__/pet.ts +3 -0
- package/src/generators/__snapshots__/petArray.ts +6 -0
- package/src/generators/__snapshots__/petCoercion.ts +3 -0
- package/src/generators/__snapshots__/petTupleObject.ts +6 -0
- package/src/generators/__snapshots__/petWithMapper.ts +3 -0
- package/src/generators/__snapshots__/pets.ts +3 -0
- package/src/generators/__snapshots__/recursive.ts +3 -0
- package/src/generators/__snapshots__/showPetById.ts +18 -0
- package/src/generators/__snapshots__/stringValueConst.ts +6 -0
- package/src/generators/__snapshots__/uuidSchema.ts +3 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/operationsGenerator.tsx +39 -0
- package/src/generators/zodGenerator.tsx +121 -0
- package/src/parser/index.ts +11 -3
- package/src/plugin.ts +22 -37
- package/src/types.ts +38 -70
- package/dist/Operations-BlQtRP31.d.cts +0 -47
- package/dist/Operations-BlQtRP31.d.ts +0 -47
- package/dist/chunk-KP7GRUCO.cjs +0 -1279
- package/dist/chunk-KP7GRUCO.cjs.map +0 -1
- package/dist/chunk-ORHAR2XI.js +0 -1279
- package/dist/chunk-ORHAR2XI.js.map +0 -1
- package/src/OperationGenerator.tsx +0 -54
- package/src/SchemaGenerator.tsx +0 -31
- package/src/components/OperationSchema.tsx +0 -66
- package/src/components/Schema.tsx +0 -171
- package/src/components/__snapshots__/operations.ts +0 -50
|
@@ -1,29 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Const, File
|
|
1
|
+
import type { SchemaNames } from '@kubb/plugin-oas/hooks'
|
|
2
|
+
import { Const, File } from '@kubb/react'
|
|
3
3
|
|
|
4
4
|
import transformers from '@kubb/core/transformers'
|
|
5
5
|
import type { HttpMethod, Operation } from '@kubb/oas'
|
|
6
|
-
import type { KubbNode } from '@kubb/react'
|
|
7
|
-
import type { ComponentProps, ComponentType } from 'react'
|
|
8
|
-
import type { FileMeta, PluginZod } from '../types.ts'
|
|
6
|
+
import type { KubbNode } from '@kubb/react/types'
|
|
9
7
|
|
|
10
|
-
type
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
*/
|
|
14
|
-
operationsName: string
|
|
15
|
-
/**
|
|
16
|
-
* Name of the function
|
|
17
|
-
*/
|
|
18
|
-
pathsName: string
|
|
19
|
-
operations: Operation[]
|
|
8
|
+
type Props = {
|
|
9
|
+
name: string
|
|
10
|
+
operations: Array<{ operation: Operation; data: SchemaNames }>
|
|
20
11
|
}
|
|
21
12
|
|
|
22
|
-
function
|
|
23
|
-
const
|
|
24
|
-
const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: 'function' }) }))
|
|
25
|
-
|
|
26
|
-
const operationsJSON = transformedOperations.reduce(
|
|
13
|
+
export function Operations({ name, operations }: Props): KubbNode {
|
|
14
|
+
const operationsJSON = operations.reduce(
|
|
27
15
|
(prev, acc) => {
|
|
28
16
|
prev[`"${acc.operation.getOperationId()}"`] = acc.data
|
|
29
17
|
|
|
@@ -32,7 +20,7 @@ function Template({ operationsName, pathsName, operations }: TemplateProps): Kub
|
|
|
32
20
|
{} as Record<string, unknown>,
|
|
33
21
|
)
|
|
34
22
|
|
|
35
|
-
const pathsJSON =
|
|
23
|
+
const pathsJSON = operations.reduce(
|
|
36
24
|
(prev, acc) => {
|
|
37
25
|
prev[`"${acc.operation.path}"`] = {
|
|
38
26
|
...(prev[`"${acc.operation.path}"`] || ({} as Record<HttpMethod, string>)),
|
|
@@ -46,88 +34,16 @@ function Template({ operationsName, pathsName, operations }: TemplateProps): Kub
|
|
|
46
34
|
|
|
47
35
|
return (
|
|
48
36
|
<>
|
|
49
|
-
<
|
|
50
|
-
{
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
37
|
+
<File.Source name={name} isExportable isIndexable>
|
|
38
|
+
<Const export name={name} asConst>
|
|
39
|
+
{`{${transformers.stringifyObject(operationsJSON)}}`}
|
|
40
|
+
</Const>
|
|
41
|
+
</File.Source>
|
|
42
|
+
<File.Source name={'paths'} isExportable isIndexable>
|
|
43
|
+
<Const export name={'paths'} asConst>
|
|
44
|
+
{`{${transformers.stringifyObject(pathsJSON)}}`}
|
|
45
|
+
</Const>
|
|
46
|
+
</File.Source>
|
|
55
47
|
</>
|
|
56
48
|
)
|
|
57
49
|
}
|
|
58
|
-
|
|
59
|
-
type RootTemplateProps = {
|
|
60
|
-
children?: React.ReactNode
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function RootTemplate({ children }: RootTemplateProps) {
|
|
64
|
-
const {
|
|
65
|
-
mode,
|
|
66
|
-
pluginManager,
|
|
67
|
-
plugin: {
|
|
68
|
-
key: pluginKey,
|
|
69
|
-
options: { extName },
|
|
70
|
-
},
|
|
71
|
-
} = useApp<PluginZod>()
|
|
72
|
-
const { getFile } = useOperationManager()
|
|
73
|
-
const operations = useOperations()
|
|
74
|
-
const { groupSchemasByName } = useOperationManager()
|
|
75
|
-
const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: 'function' }) }))
|
|
76
|
-
|
|
77
|
-
const file = pluginManager.getFile({ name: 'operations', extName: '.ts', pluginKey })
|
|
78
|
-
const imports = Object.entries(transformedOperations)
|
|
79
|
-
.map(([_key, { data, operation }], index) => {
|
|
80
|
-
const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean)
|
|
81
|
-
|
|
82
|
-
return <File.Import key={index} extName={extName} name={names} root={file.path} path={getFile(operation).path} />
|
|
83
|
-
})
|
|
84
|
-
.filter(Boolean)
|
|
85
|
-
|
|
86
|
-
return (
|
|
87
|
-
<Parser language="typescript">
|
|
88
|
-
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta} exportable={false}>
|
|
89
|
-
{mode === 'split' && imports}
|
|
90
|
-
<File.Source>{children}</File.Source>
|
|
91
|
-
</File>
|
|
92
|
-
</Parser>
|
|
93
|
-
)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
const defaultTemplates = { default: Template, root: RootTemplate } as const
|
|
97
|
-
|
|
98
|
-
type Templates = Partial<typeof defaultTemplates>
|
|
99
|
-
|
|
100
|
-
type Props = {
|
|
101
|
-
/**
|
|
102
|
-
* This will make it possible to override the default behaviour.
|
|
103
|
-
*/
|
|
104
|
-
Template?: ComponentType<ComponentProps<typeof Template>>
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function Operations({ Template = defaultTemplates.default }: Props): KubbNode {
|
|
108
|
-
const operations = useOperations()
|
|
109
|
-
|
|
110
|
-
return <Template operationsName="operations" pathsName="paths" operations={operations} />
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
type FileProps = {
|
|
114
|
-
/**
|
|
115
|
-
* This will make it possible to override the default behaviour.
|
|
116
|
-
*/
|
|
117
|
-
templates?: Templates
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
Operations.File = function (props: FileProps): KubbNode {
|
|
121
|
-
const templates = { ...defaultTemplates, ...props.templates }
|
|
122
|
-
|
|
123
|
-
const Template = templates.default
|
|
124
|
-
const RootTemplate = templates.root
|
|
125
|
-
|
|
126
|
-
return (
|
|
127
|
-
<RootTemplate>
|
|
128
|
-
<Operations Template={Template} />
|
|
129
|
-
</RootTemplate>
|
|
130
|
-
)
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
Operations.templates = defaultTemplates
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
|
2
|
+
import { type Schema, schemaKeywords } from '@kubb/plugin-oas'
|
|
3
|
+
import { isKeyword } from '@kubb/plugin-oas'
|
|
4
|
+
import { Const, File, Type } from '@kubb/react'
|
|
5
|
+
import type { KubbNode } from '@kubb/react/types'
|
|
6
|
+
import * as parserZod from '../parser/index.ts'
|
|
7
|
+
import type { PluginZod } from '../types.ts'
|
|
8
|
+
|
|
9
|
+
type Props = {
|
|
10
|
+
name: string
|
|
11
|
+
typeName?: string
|
|
12
|
+
inferTypeName?: string
|
|
13
|
+
tree: Array<Schema>
|
|
14
|
+
description?: string
|
|
15
|
+
coercion: PluginZod['resolvedOptions']['coercion']
|
|
16
|
+
mapper: PluginZod['resolvedOptions']['mapper']
|
|
17
|
+
keysToOmit?: string[]
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function Zod({ name, typeName, tree, inferTypeName, mapper, coercion, keysToOmit, description }: Props): KubbNode {
|
|
21
|
+
const hasTuple = tree.some((item) => isKeyword(item, schemaKeywords.tuple))
|
|
22
|
+
|
|
23
|
+
const output = parserZod
|
|
24
|
+
.sort(tree)
|
|
25
|
+
.filter((item) => {
|
|
26
|
+
if (hasTuple && (isKeyword(item, schemaKeywords.min) || isKeyword(item, schemaKeywords.max))) {
|
|
27
|
+
return false
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return true
|
|
31
|
+
})
|
|
32
|
+
.map((item) => parserZod.parse(undefined, item, { name, keysToOmit, typeName: typeName, description, mapper, coercion }))
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
.join('')
|
|
35
|
+
|
|
36
|
+
const suffix = output.endsWith('.nullable()') ? '.unwrap().and' : '.and'
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<>
|
|
40
|
+
<File.Source name={name} isExportable isIndexable>
|
|
41
|
+
<Const
|
|
42
|
+
export
|
|
43
|
+
name={name}
|
|
44
|
+
JSDoc={{
|
|
45
|
+
comments: [description ? `@description ${transformers.jsStringEscape(description)}` : undefined].filter(Boolean),
|
|
46
|
+
}}
|
|
47
|
+
>
|
|
48
|
+
{[
|
|
49
|
+
output,
|
|
50
|
+
keysToOmit?.length ? `${suffix}(z.object({ ${keysToOmit.map((key) => `${key}: z.never()`).join(',')} }))` : undefined,
|
|
51
|
+
typeName ? ` as z.ZodType<${typeName}>` : '',
|
|
52
|
+
]
|
|
53
|
+
.filter(Boolean)
|
|
54
|
+
.join('') || ''}
|
|
55
|
+
</Const>
|
|
56
|
+
</File.Source>
|
|
57
|
+
{inferTypeName && (
|
|
58
|
+
<File.Source name={inferTypeName} isExportable isIndexable isTypeOnly>
|
|
59
|
+
<Type export name={inferTypeName}>
|
|
60
|
+
{`z.infer<typeof ${name}>`}
|
|
61
|
+
</Type>
|
|
62
|
+
</File.Source>
|
|
63
|
+
)}
|
|
64
|
+
</>
|
|
65
|
+
)
|
|
66
|
+
}
|
package/src/components/index.ts
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description Null response
|
|
5
|
+
*/
|
|
6
|
+
export const createPets201 = z.any();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @description unexpected error
|
|
10
|
+
*/
|
|
11
|
+
export const createPetsError = z.lazy(() => error);
|
|
12
|
+
|
|
13
|
+
export const createPetsMutationRequest = z.object({ "name": z.string(), "tag": z.string() });
|
|
14
|
+
|
|
15
|
+
export const createPetsMutationResponse = z.any();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Null response
|
|
3
|
+
*/
|
|
4
|
+
export const createPetWithUnknownTypeAny = z.any()
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @description unexpected error
|
|
8
|
+
*/
|
|
9
|
+
export const createPetWithUnknownTypeAny = z.lazy(() => createPetWithUnknownTypeAny)
|
|
10
|
+
|
|
11
|
+
export const createPetWithUnknownTypeAny = z.object({ name: z.string(), tag: z.string() })
|
|
12
|
+
|
|
13
|
+
export const createPetWithUnknownTypeAny = z.any()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @description Null response
|
|
5
|
+
*/
|
|
6
|
+
export const createPets201 = z.unknown();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @description unexpected error
|
|
10
|
+
*/
|
|
11
|
+
export const createPetsError = z.lazy(() => error);
|
|
12
|
+
|
|
13
|
+
export const createPetsMutationRequest = z.object({ "name": z.string(), "tag": z.string() });
|
|
14
|
+
|
|
15
|
+
export const createPetsMutationResponse = z.unknown();
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const listPetsQueryParams = z.object({ "limit": z.string().describe("How many items to return at one time (max 100)").optional() }).optional();
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description A paged array of pets
|
|
7
|
+
*/
|
|
8
|
+
export const listPets200 = z.lazy(() => pets);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @description unexpected error
|
|
12
|
+
*/
|
|
13
|
+
export const listPetsError = z.lazy(() => error);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @description A paged array of pets
|
|
17
|
+
*/
|
|
18
|
+
export const listPetsQueryResponse = z.lazy(() => pets);
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { listPetsQueryResponse, listPetsQueryParams, createPetsMutationRequest, createPetsMutationResponse, showPetByIdQueryResponse, showPetByIdPathParams } from "./showPetById.ts";
|
|
2
|
+
|
|
3
|
+
export const operations = { "listPets": {
|
|
4
|
+
request: undefined,
|
|
5
|
+
parameters: {
|
|
6
|
+
path: undefined,
|
|
7
|
+
query: listPetsQueryParams,
|
|
8
|
+
header: undefined
|
|
9
|
+
},
|
|
10
|
+
responses: {
|
|
11
|
+
200: listPetsQueryResponse,
|
|
12
|
+
default: listPetsQueryResponse
|
|
13
|
+
},
|
|
14
|
+
errors: {}
|
|
15
|
+
}, "createPets": {
|
|
16
|
+
request: createPetsMutationRequest,
|
|
17
|
+
parameters: {
|
|
18
|
+
path: undefined,
|
|
19
|
+
query: undefined,
|
|
20
|
+
header: undefined
|
|
21
|
+
},
|
|
22
|
+
responses: {
|
|
23
|
+
201: createPetsMutationResponse,
|
|
24
|
+
default: createPetsMutationResponse
|
|
25
|
+
},
|
|
26
|
+
errors: {}
|
|
27
|
+
}, "showPetById": {
|
|
28
|
+
request: undefined,
|
|
29
|
+
parameters: {
|
|
30
|
+
path: showPetByIdPathParams,
|
|
31
|
+
query: undefined,
|
|
32
|
+
header: undefined
|
|
33
|
+
},
|
|
34
|
+
responses: {
|
|
35
|
+
200: showPetByIdQueryResponse,
|
|
36
|
+
default: showPetByIdQueryResponse
|
|
37
|
+
},
|
|
38
|
+
errors: {}
|
|
39
|
+
} } as const;
|
|
40
|
+
|
|
41
|
+
export const paths = { "/pets": {
|
|
42
|
+
get: operations["listPets"],
|
|
43
|
+
post: operations["createPets"]
|
|
44
|
+
}, "/pets/{petId}": {
|
|
45
|
+
get: operations["showPetById"]
|
|
46
|
+
} } as const;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const order = z.object({ "id": z.number().int().optional(), "petId": z.number().int().optional(), "quantity": z.number().int().optional(), "shipDate": z.date().optional(), "status": z.enum(["placed", "approved", "delivered"]).describe("Order Status").optional(), "complete": z.boolean().optional() });
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { z } from 'zod'
|
|
2
|
+
|
|
3
|
+
export const order = z.object({
|
|
4
|
+
id: z.number().int().optional(),
|
|
5
|
+
petId: z.number().int().optional(),
|
|
6
|
+
quantity: z.number().int().optional(),
|
|
7
|
+
shipDate: z.string().datetime().optional(),
|
|
8
|
+
status: z.enum(['placed', 'approved', 'delivered']).describe('Order Status').optional(),
|
|
9
|
+
complete: z.boolean().optional(),
|
|
10
|
+
})
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const order = z.object({ "id": z.number().int().optional(), "petId": z.number().int().optional(), "quantity": z.number().int().optional(), "shipDate": z.string().optional(), "status": z.enum(["placed", "approved", "delivered"]).describe("Order Status").optional(), "complete": z.boolean().optional() });
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const order = z.object({ "id": z.number().int().optional(), "petId": z.number().int().optional(), "quantity": z.number().int().optional(), "shipDate": z.string().datetime().optional(), "status": z.enum(["placed", "approved", "delivered"]).describe("Order Status").optional(), "complete": z.boolean().optional() });
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const showPetByIdPathParams = z.object({ "petId": z.string().describe("The id of the pet to retrieve"), "testId": z.string().describe("The id of the pet to retrieve") });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @description Expected response to a valid request
|
|
7
|
+
*/
|
|
8
|
+
export const showPetById200 = z.lazy(() => pet);
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @description unexpected error
|
|
12
|
+
*/
|
|
13
|
+
export const showPetByIdError = z.lazy(() => error);
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @description Expected response to a valid request
|
|
17
|
+
*/
|
|
18
|
+
export const showPetByIdQueryResponse = z.lazy(() => pet);
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
2
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
3
|
+
import { File, useApp } from '@kubb/react'
|
|
4
|
+
import { Operations } from '../components/Operations'
|
|
5
|
+
import { pluginZodName } from '../plugin.ts'
|
|
6
|
+
import type { PluginZod } from '../types'
|
|
7
|
+
|
|
8
|
+
export const operationsGenerator = createReactGenerator<PluginZod>({
|
|
9
|
+
name: 'operations',
|
|
10
|
+
Operations({ operations }) {
|
|
11
|
+
const {
|
|
12
|
+
pluginManager,
|
|
13
|
+
plugin: {
|
|
14
|
+
options: { output },
|
|
15
|
+
},
|
|
16
|
+
} = useApp<PluginZod>()
|
|
17
|
+
const { getFile, groupSchemasByName } = useOperationManager()
|
|
18
|
+
|
|
19
|
+
const name = 'operations'
|
|
20
|
+
const file = pluginManager.getFile({ name, extName: '.ts', pluginKey: [pluginZodName] })
|
|
21
|
+
|
|
22
|
+
const transformedOperations = operations.map((operation) => ({ operation, data: groupSchemasByName(operation, { type: 'function' }) }))
|
|
23
|
+
|
|
24
|
+
const imports = Object.entries(transformedOperations)
|
|
25
|
+
.map(([key, { data, operation }]) => {
|
|
26
|
+
const names = [data.request, ...Object.values(data.responses), ...Object.values(data.parameters)].filter(Boolean)
|
|
27
|
+
|
|
28
|
+
return <File.Import key={key} name={names} root={file.path} path={getFile(operation).path} />
|
|
29
|
+
})
|
|
30
|
+
.filter(Boolean)
|
|
31
|
+
|
|
32
|
+
return (
|
|
33
|
+
<File baseName={file.baseName} path={file.path} meta={file.meta} banner={output?.banner} footer={output?.footer}>
|
|
34
|
+
{imports}
|
|
35
|
+
<Operations name={name} operations={transformedOperations} />
|
|
36
|
+
</File>
|
|
37
|
+
)
|
|
38
|
+
},
|
|
39
|
+
})
|