@kubb/plugin-react-query 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/chunk-ESENIDWJ.js +608 -0
- package/dist/chunk-ESENIDWJ.js.map +1 -0
- package/dist/chunk-HAC4WPCC.js +608 -0
- package/dist/chunk-HAC4WPCC.js.map +1 -0
- package/dist/chunk-JQEH3UHY.cjs +618 -0
- package/dist/chunk-JQEH3UHY.cjs.map +1 -0
- package/dist/chunk-JYBGTT23.cjs +618 -0
- package/dist/chunk-JYBGTT23.cjs.map +1 -0
- package/dist/components.cjs +35 -14
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +143 -7
- package/dist/components.d.ts +143 -7
- package/dist/components.js +2 -14
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +25 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +14 -0
- package/dist/generators.d.ts +14 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +77 -154
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -5
- package/dist/index.d.ts +1 -5
- package/dist/index.js +56 -140
- package/dist/index.js.map +1 -1
- package/dist/types-DFHc63S9.d.cts +179 -0
- package/dist/types-DFHc63S9.d.ts +179 -0
- package/package.json +22 -17
- package/src/components/InfiniteQuery.tsx +129 -0
- package/src/components/InfiniteQueryOptions.tsx +121 -0
- package/src/components/Mutation.tsx +113 -321
- package/src/components/Query.tsx +91 -598
- package/src/components/QueryKey.tsx +51 -178
- package/src/components/QueryOptions.tsx +65 -468
- package/src/components/SuspenseQuery.tsx +129 -0
- package/src/components/index.ts +3 -1
- package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +50 -0
- package/src/generators/__snapshots__/clientGetImportPath.ts +50 -0
- package/src/generators/__snapshots__/clientPostImportPath.ts +38 -0
- package/src/generators/__snapshots__/findByTags.ts +50 -0
- package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +50 -0
- package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +50 -0
- package/src/generators/__snapshots__/findByTagsWithZod.ts +50 -0
- package/src/generators/__snapshots__/findInfiniteByTags.ts +56 -0
- package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +56 -0
- package/src/generators/__snapshots__/getAsMutation.ts +31 -0
- package/src/generators/__snapshots__/postAsQuery.ts +48 -0
- package/src/generators/__snapshots__/updatePetById.ts +38 -0
- package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +40 -0
- package/src/generators/index.ts +4 -0
- package/src/generators/infiniteQueryGenerator.tsx +125 -0
- package/src/generators/mutationGenerator.tsx +96 -0
- package/src/generators/queryGenerator.tsx +122 -0
- package/src/generators/suspenseQueryGenerator.tsx +121 -0
- package/src/plugin.ts +51 -73
- package/src/types.ts +43 -91
- package/dist/chunk-5IL6M74X.js +0 -1504
- package/dist/chunk-5IL6M74X.js.map +0 -1
- package/dist/chunk-JFX7DCS7.cjs +0 -1504
- package/dist/chunk-JFX7DCS7.cjs.map +0 -1
- package/dist/index-C9fwRDH7.d.cts +0 -579
- package/dist/index-C9fwRDH7.d.ts +0 -579
- package/src/OperationGenerator.tsx +0 -86
- package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +0 -64
- package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +0 -57
- package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +0 -63
- package/src/__snapshots__/queryOptions/getPetById.ts +0 -37
- package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +0 -47
- package/src/__snapshots__/upload/UploadFile.ts +0 -67
- package/src/__snapshots__/uploadMutation/UploadFile.ts +0 -44
- package/src/__snapshots__/variablesTypeMutate/deletePet.ts +0 -21
- package/src/components/Operations.tsx +0 -74
- package/src/components/QueryImports.tsx +0 -167
- package/src/components/SchemaType.tsx +0 -55
- package/src/components/__snapshots__/gen/showPetById.ts +0 -57
- package/src/components/__snapshots__/gen/useCreatePets.ts +0 -46
- package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +0 -47
- package/src/utils.ts +0 -96
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
|
2
|
+
import { pluginClientName } from '@kubb/plugin-client'
|
|
3
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
4
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
5
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
6
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
|
+
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
|
+
import { File, useApp } from '@kubb/react'
|
|
9
|
+
import { InfiniteQuery, InfiniteQueryOptions, QueryKey } from '../components'
|
|
10
|
+
import type { PluginReactQuery } from '../types'
|
|
11
|
+
|
|
12
|
+
export const infiniteQueryGenerator = createReactGenerator<PluginReactQuery>({
|
|
13
|
+
name: 'react-infinite-query',
|
|
14
|
+
Operation({ options, operation }) {
|
|
15
|
+
const {
|
|
16
|
+
plugin: {
|
|
17
|
+
options: { output },
|
|
18
|
+
},
|
|
19
|
+
} = useApp<PluginReactQuery>()
|
|
20
|
+
const { getSchemas, getName, getFile } = useOperationManager()
|
|
21
|
+
const isQuery = typeof options.query === 'boolean' ? options.query : !!options.query.methods?.some((method) => operation.method === method)
|
|
22
|
+
const isInfinite = isQuery && !!options.infinite
|
|
23
|
+
|
|
24
|
+
const query = {
|
|
25
|
+
name: getName(operation, { type: 'function', prefix: 'use', suffix: 'infinite' }),
|
|
26
|
+
typeName: getName(operation, { type: 'type' }),
|
|
27
|
+
file: getFile(operation, { prefix: 'use', suffix: 'infinite' }),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const client = {
|
|
31
|
+
name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const queryOptions = {
|
|
35
|
+
name: transformers.camelCase(`${operation.getOperationId()} InfiniteQueryOptions`),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const queryKey = {
|
|
39
|
+
name: transformers.camelCase(`${operation.getOperationId()} InfiniteQueryKey`),
|
|
40
|
+
typeName: transformers.pascalCase(`${operation.getOperationId()} InfiniteQueryKey`),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const type = {
|
|
44
|
+
file: getFile(operation, { pluginKey: [pluginTsName] }),
|
|
45
|
+
//todo remove type?
|
|
46
|
+
schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const zod = {
|
|
50
|
+
file: getFile(operation, { pluginKey: [pluginZodName] }),
|
|
51
|
+
schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!isQuery || !isInfinite || typeof options.query === 'boolean' || typeof options.infinite === 'boolean') {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
60
|
+
{options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
|
|
61
|
+
<File.Import name={['useInfiniteQuery', 'infiniteQueryOptions']} path={options.query.importPath} />
|
|
62
|
+
<File.Import name={['QueryKey', 'WithRequired', 'InfiniteQueryObserverOptions', 'UseInfiniteQueryResult']} path={options.query.importPath} isTypeOnly />
|
|
63
|
+
<File.Import name={'client'} path={options.client.importPath} />
|
|
64
|
+
<File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
|
|
65
|
+
{options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
|
|
66
|
+
<File.Import
|
|
67
|
+
extName={output?.extName}
|
|
68
|
+
name={[
|
|
69
|
+
type.schemas.request?.name,
|
|
70
|
+
type.schemas.response.name,
|
|
71
|
+
type.schemas.pathParams?.name,
|
|
72
|
+
type.schemas.queryParams?.name,
|
|
73
|
+
type.schemas.headerParams?.name,
|
|
74
|
+
...(type.schemas.statusCodes?.map((item) => item.name) || []),
|
|
75
|
+
].filter(Boolean)}
|
|
76
|
+
root={query.file.path}
|
|
77
|
+
path={type.file.path}
|
|
78
|
+
isTypeOnly
|
|
79
|
+
/>
|
|
80
|
+
|
|
81
|
+
<QueryKey
|
|
82
|
+
name={queryKey.name}
|
|
83
|
+
typeName={queryKey.typeName}
|
|
84
|
+
operation={operation}
|
|
85
|
+
pathParamsType={options.pathParamsType}
|
|
86
|
+
typeSchemas={type.schemas}
|
|
87
|
+
keysFn={options.query.key}
|
|
88
|
+
/>
|
|
89
|
+
<Client
|
|
90
|
+
name={client.name}
|
|
91
|
+
isExportable={false}
|
|
92
|
+
isIndexable={false}
|
|
93
|
+
baseURL={options.baseURL}
|
|
94
|
+
operation={operation}
|
|
95
|
+
typeSchemas={type.schemas}
|
|
96
|
+
zodSchemas={zod.schemas}
|
|
97
|
+
dataReturnType={options.client.dataReturnType}
|
|
98
|
+
pathParamsType={options.pathParamsType}
|
|
99
|
+
parser={options.parser}
|
|
100
|
+
/>
|
|
101
|
+
<InfiniteQueryOptions
|
|
102
|
+
name={queryOptions.name}
|
|
103
|
+
clientName={client.name}
|
|
104
|
+
queryKeyName={queryKey.name}
|
|
105
|
+
typeSchemas={type.schemas}
|
|
106
|
+
pathParamsType={options.pathParamsType}
|
|
107
|
+
dataReturnType={options.client.dataReturnType}
|
|
108
|
+
cursorParam={options.infinite.cursorParam}
|
|
109
|
+
initialPageParam={options.infinite.initialPageParam}
|
|
110
|
+
queryParam={options.infinite.queryParam}
|
|
111
|
+
/>
|
|
112
|
+
<InfiniteQuery
|
|
113
|
+
name={query.name}
|
|
114
|
+
queryOptionsName={queryOptions.name}
|
|
115
|
+
typeSchemas={type.schemas}
|
|
116
|
+
pathParamsType={options.pathParamsType}
|
|
117
|
+
operation={operation}
|
|
118
|
+
dataReturnType={options.client.dataReturnType}
|
|
119
|
+
queryKeyName={queryKey.name}
|
|
120
|
+
queryKeyTypeName={queryKey.typeName}
|
|
121
|
+
/>
|
|
122
|
+
</File>
|
|
123
|
+
)
|
|
124
|
+
},
|
|
125
|
+
})
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { pluginClientName } from '@kubb/plugin-client'
|
|
2
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
3
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
4
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
|
+
import { pluginZodName } from '@kubb/plugin-zod'
|
|
7
|
+
import { File, useApp } from '@kubb/react'
|
|
8
|
+
import { Mutation } from '../components'
|
|
9
|
+
import type { PluginReactQuery } from '../types'
|
|
10
|
+
|
|
11
|
+
export const mutationGenerator = createReactGenerator<PluginReactQuery>({
|
|
12
|
+
name: 'react-query',
|
|
13
|
+
Operation({ options, operation }) {
|
|
14
|
+
const {
|
|
15
|
+
plugin: {
|
|
16
|
+
options: { output },
|
|
17
|
+
},
|
|
18
|
+
} = useApp<PluginReactQuery>()
|
|
19
|
+
const { getSchemas, getName, getFile } = useOperationManager()
|
|
20
|
+
|
|
21
|
+
const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
|
|
22
|
+
const isMutation = !isQuery && options.mutation && options.mutation.methods.some((method) => operation.method === method)
|
|
23
|
+
|
|
24
|
+
const mutation = {
|
|
25
|
+
name: getName(operation, { type: 'function', prefix: 'use' }),
|
|
26
|
+
typeName: getName(operation, { type: 'type' }),
|
|
27
|
+
file: getFile(operation, { prefix: 'use' }),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const type = {
|
|
31
|
+
file: getFile(operation, { pluginKey: [pluginTsName] }),
|
|
32
|
+
//todo remove type?
|
|
33
|
+
schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const zod = {
|
|
37
|
+
file: getFile(operation, { pluginKey: [pluginZodName] }),
|
|
38
|
+
schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const client = {
|
|
42
|
+
name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (!isMutation || typeof options.mutation === 'boolean') {
|
|
46
|
+
return null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return (
|
|
50
|
+
<File baseName={mutation.file.baseName} path={mutation.file.path} meta={mutation.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
51
|
+
{options.parser === 'zod' && (
|
|
52
|
+
<File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={mutation.file.path} path={zod.file.path} />
|
|
53
|
+
)}
|
|
54
|
+
<File.Import name={['useMutation']} path={options.mutation.importPath} />
|
|
55
|
+
<File.Import name={['UseMutationOptions']} path={options.mutation.importPath} isTypeOnly />
|
|
56
|
+
<File.Import name={'client'} path={options.client.importPath} />
|
|
57
|
+
<File.Import name={['RequestConfig', 'ResponseConfig']} path={options.client.importPath} isTypeOnly />
|
|
58
|
+
<File.Import
|
|
59
|
+
extName={output?.extName}
|
|
60
|
+
name={[
|
|
61
|
+
type.schemas.request?.name,
|
|
62
|
+
type.schemas.response.name,
|
|
63
|
+
type.schemas.pathParams?.name,
|
|
64
|
+
type.schemas.queryParams?.name,
|
|
65
|
+
type.schemas.headerParams?.name,
|
|
66
|
+
...(type.schemas.statusCodes?.map((item) => item.name) || []),
|
|
67
|
+
].filter(Boolean)}
|
|
68
|
+
root={mutation.file.path}
|
|
69
|
+
path={type.file.path}
|
|
70
|
+
isTypeOnly
|
|
71
|
+
/>
|
|
72
|
+
<Client
|
|
73
|
+
name={client.name}
|
|
74
|
+
isExportable={false}
|
|
75
|
+
isIndexable={false}
|
|
76
|
+
baseURL={options.baseURL}
|
|
77
|
+
operation={operation}
|
|
78
|
+
typeSchemas={type.schemas}
|
|
79
|
+
zodSchemas={zod.schemas}
|
|
80
|
+
dataReturnType={options.client.dataReturnType}
|
|
81
|
+
pathParamsType={options.pathParamsType}
|
|
82
|
+
parser={options.parser}
|
|
83
|
+
/>
|
|
84
|
+
<Mutation
|
|
85
|
+
name={mutation.name}
|
|
86
|
+
clientName={client.name}
|
|
87
|
+
typeName={mutation.typeName}
|
|
88
|
+
typeSchemas={type.schemas}
|
|
89
|
+
operation={operation}
|
|
90
|
+
dataReturnType={options.client.dataReturnType}
|
|
91
|
+
pathParamsType={options.pathParamsType}
|
|
92
|
+
/>
|
|
93
|
+
</File>
|
|
94
|
+
)
|
|
95
|
+
},
|
|
96
|
+
})
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
|
2
|
+
import { pluginClientName } from '@kubb/plugin-client'
|
|
3
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
4
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
5
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
6
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
|
+
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
|
+
import { File, useApp } from '@kubb/react'
|
|
9
|
+
import { Query, QueryKey, QueryOptions } from '../components'
|
|
10
|
+
import type { PluginReactQuery } from '../types'
|
|
11
|
+
|
|
12
|
+
export const queryGenerator = createReactGenerator<PluginReactQuery>({
|
|
13
|
+
name: 'react-query',
|
|
14
|
+
Operation({ options, operation }) {
|
|
15
|
+
const {
|
|
16
|
+
plugin: {
|
|
17
|
+
options: { output },
|
|
18
|
+
},
|
|
19
|
+
} = useApp<PluginReactQuery>()
|
|
20
|
+
const { getSchemas, getName, getFile } = useOperationManager()
|
|
21
|
+
|
|
22
|
+
const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
|
|
23
|
+
const isMutation = !isQuery && options.mutation && options.mutation.methods.some((method) => operation.method === method)
|
|
24
|
+
|
|
25
|
+
const query = {
|
|
26
|
+
name: getName(operation, { type: 'function', prefix: 'use' }),
|
|
27
|
+
typeName: getName(operation, { type: 'type' }),
|
|
28
|
+
file: getFile(operation, { prefix: 'use' }),
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const client = {
|
|
32
|
+
name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const queryOptions = {
|
|
36
|
+
name: transformers.camelCase(`${operation.getOperationId()} QueryOptions`),
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const queryKey = {
|
|
40
|
+
name: transformers.camelCase(`${operation.getOperationId()} QueryKey`),
|
|
41
|
+
typeName: transformers.pascalCase(`${operation.getOperationId()} QueryKey`),
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const type = {
|
|
45
|
+
file: getFile(operation, { pluginKey: [pluginTsName] }),
|
|
46
|
+
//todo remove type?
|
|
47
|
+
schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const zod = {
|
|
51
|
+
file: getFile(operation, { pluginKey: [pluginZodName] }),
|
|
52
|
+
schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (!isQuery || typeof options.query === 'boolean') {
|
|
56
|
+
return null
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return (
|
|
60
|
+
<File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
61
|
+
{options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
|
|
62
|
+
<File.Import name={['useQuery', 'queryOptions']} path={options.query.importPath} />
|
|
63
|
+
<File.Import name={['QueryKey', 'WithRequired', 'QueryObserverOptions', 'UseQueryResult']} path={options.query.importPath} isTypeOnly />
|
|
64
|
+
<File.Import name={'client'} path={options.client.importPath} />
|
|
65
|
+
<File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
|
|
66
|
+
{options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
|
|
67
|
+
<File.Import
|
|
68
|
+
extName={output?.extName}
|
|
69
|
+
name={[
|
|
70
|
+
type.schemas.request?.name,
|
|
71
|
+
type.schemas.response.name,
|
|
72
|
+
type.schemas.pathParams?.name,
|
|
73
|
+
type.schemas.queryParams?.name,
|
|
74
|
+
type.schemas.headerParams?.name,
|
|
75
|
+
...(type.schemas.statusCodes?.map((item) => item.name) || []),
|
|
76
|
+
].filter(Boolean)}
|
|
77
|
+
root={query.file.path}
|
|
78
|
+
path={type.file.path}
|
|
79
|
+
isTypeOnly
|
|
80
|
+
/>
|
|
81
|
+
|
|
82
|
+
<QueryKey
|
|
83
|
+
name={queryKey.name}
|
|
84
|
+
typeName={queryKey.typeName}
|
|
85
|
+
operation={operation}
|
|
86
|
+
pathParamsType={options.pathParamsType}
|
|
87
|
+
typeSchemas={type.schemas}
|
|
88
|
+
keysFn={options.query.key}
|
|
89
|
+
/>
|
|
90
|
+
<Client
|
|
91
|
+
name={client.name}
|
|
92
|
+
isExportable={false}
|
|
93
|
+
isIndexable={false}
|
|
94
|
+
baseURL={options.baseURL}
|
|
95
|
+
operation={operation}
|
|
96
|
+
typeSchemas={type.schemas}
|
|
97
|
+
zodSchemas={zod.schemas}
|
|
98
|
+
dataReturnType={options.client.dataReturnType}
|
|
99
|
+
pathParamsType={options.pathParamsType}
|
|
100
|
+
parser={options.parser}
|
|
101
|
+
/>
|
|
102
|
+
<QueryOptions
|
|
103
|
+
name={queryOptions.name}
|
|
104
|
+
clientName={client.name}
|
|
105
|
+
queryKeyName={queryKey.name}
|
|
106
|
+
typeSchemas={type.schemas}
|
|
107
|
+
pathParamsType={options.pathParamsType}
|
|
108
|
+
/>
|
|
109
|
+
<Query
|
|
110
|
+
name={query.name}
|
|
111
|
+
queryOptionsName={queryOptions.name}
|
|
112
|
+
typeSchemas={type.schemas}
|
|
113
|
+
pathParamsType={options.pathParamsType}
|
|
114
|
+
operation={operation}
|
|
115
|
+
dataReturnType={options.client.dataReturnType}
|
|
116
|
+
queryKeyName={queryKey.name}
|
|
117
|
+
queryKeyTypeName={queryKey.typeName}
|
|
118
|
+
/>
|
|
119
|
+
</File>
|
|
120
|
+
)
|
|
121
|
+
},
|
|
122
|
+
})
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import transformers from '@kubb/core/transformers'
|
|
2
|
+
import { pluginClientName } from '@kubb/plugin-client'
|
|
3
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
4
|
+
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
5
|
+
import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
6
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
|
+
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
|
+
import { File, useApp } from '@kubb/react'
|
|
9
|
+
import { QueryKey, QueryOptions, SuspenseQuery } from '../components'
|
|
10
|
+
import type { PluginReactQuery } from '../types'
|
|
11
|
+
|
|
12
|
+
export const suspenseQueryGenerator = createReactGenerator<PluginReactQuery>({
|
|
13
|
+
name: 'react-suspense-query',
|
|
14
|
+
Operation({ options, operation }) {
|
|
15
|
+
const {
|
|
16
|
+
plugin: {
|
|
17
|
+
options: { output },
|
|
18
|
+
},
|
|
19
|
+
} = useApp<PluginReactQuery>()
|
|
20
|
+
const { getSchemas, getName, getFile } = useOperationManager()
|
|
21
|
+
const isQuery = typeof options.query === 'boolean' ? options.query : !!options.query.methods?.some((method) => operation.method === method)
|
|
22
|
+
const isSuspense = isQuery && !!options.suspense
|
|
23
|
+
|
|
24
|
+
const query = {
|
|
25
|
+
name: getName(operation, { type: 'function', prefix: 'use', suffix: 'suspense' }),
|
|
26
|
+
typeName: getName(operation, { type: 'type' }),
|
|
27
|
+
file: getFile(operation, { prefix: 'use', suffix: 'suspense' }),
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const client = {
|
|
31
|
+
name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const queryOptions = {
|
|
35
|
+
name: transformers.camelCase(`${operation.getOperationId()} SuspenseQueryOptions`),
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const queryKey = {
|
|
39
|
+
name: transformers.camelCase(`${operation.getOperationId()} SuspenseQueryKey`),
|
|
40
|
+
typeName: transformers.pascalCase(`${operation.getOperationId()} SuspenseQueryKey`),
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const type = {
|
|
44
|
+
file: getFile(operation, { pluginKey: [pluginTsName] }),
|
|
45
|
+
//todo remove type?
|
|
46
|
+
schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const zod = {
|
|
50
|
+
file: getFile(operation, { pluginKey: [pluginZodName] }),
|
|
51
|
+
schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (!isQuery || !isSuspense || typeof options.query === 'boolean') {
|
|
55
|
+
return null
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
60
|
+
{options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
|
|
61
|
+
<File.Import name={['useSuspenseQuery', 'queryOptions']} path={options.query.importPath} />
|
|
62
|
+
<File.Import name={['QueryKey', 'WithRequired', 'UseSuspenseQueryOptions', 'UseSuspenseQueryResult']} path={options.query.importPath} isTypeOnly />
|
|
63
|
+
<File.Import name={'client'} path={options.client.importPath} />
|
|
64
|
+
<File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
|
|
65
|
+
{options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
|
|
66
|
+
<File.Import
|
|
67
|
+
extName={output?.extName}
|
|
68
|
+
name={[
|
|
69
|
+
type.schemas.request?.name,
|
|
70
|
+
type.schemas.response.name,
|
|
71
|
+
type.schemas.pathParams?.name,
|
|
72
|
+
type.schemas.queryParams?.name,
|
|
73
|
+
type.schemas.headerParams?.name,
|
|
74
|
+
...(type.schemas.statusCodes?.map((item) => item.name) || []),
|
|
75
|
+
].filter(Boolean)}
|
|
76
|
+
root={query.file.path}
|
|
77
|
+
path={type.file.path}
|
|
78
|
+
isTypeOnly
|
|
79
|
+
/>
|
|
80
|
+
|
|
81
|
+
<QueryKey
|
|
82
|
+
name={queryKey.name}
|
|
83
|
+
typeName={queryKey.typeName}
|
|
84
|
+
operation={operation}
|
|
85
|
+
pathParamsType={options.pathParamsType}
|
|
86
|
+
typeSchemas={type.schemas}
|
|
87
|
+
keysFn={options.query.key}
|
|
88
|
+
/>
|
|
89
|
+
<Client
|
|
90
|
+
name={client.name}
|
|
91
|
+
isExportable={false}
|
|
92
|
+
isIndexable={false}
|
|
93
|
+
baseURL={options.baseURL}
|
|
94
|
+
operation={operation}
|
|
95
|
+
typeSchemas={type.schemas}
|
|
96
|
+
zodSchemas={zod.schemas}
|
|
97
|
+
dataReturnType={options.client.dataReturnType}
|
|
98
|
+
pathParamsType={options.pathParamsType}
|
|
99
|
+
parser={options.parser}
|
|
100
|
+
/>
|
|
101
|
+
<QueryOptions
|
|
102
|
+
name={queryOptions.name}
|
|
103
|
+
clientName={client.name}
|
|
104
|
+
queryKeyName={queryKey.name}
|
|
105
|
+
typeSchemas={type.schemas}
|
|
106
|
+
pathParamsType={options.pathParamsType}
|
|
107
|
+
/>
|
|
108
|
+
<SuspenseQuery
|
|
109
|
+
name={query.name}
|
|
110
|
+
queryOptionsName={queryOptions.name}
|
|
111
|
+
typeSchemas={type.schemas}
|
|
112
|
+
pathParamsType={options.pathParamsType}
|
|
113
|
+
operation={operation}
|
|
114
|
+
dataReturnType={options.client.dataReturnType}
|
|
115
|
+
queryKeyName={queryKey.name}
|
|
116
|
+
queryKeyTypeName={queryKey.typeName}
|
|
117
|
+
/>
|
|
118
|
+
</File>
|
|
119
|
+
)
|
|
120
|
+
},
|
|
121
|
+
})
|
package/src/plugin.ts
CHANGED
|
@@ -3,17 +3,14 @@ import path from 'node:path'
|
|
|
3
3
|
import { FileManager, PluginManager, createPlugin } from '@kubb/core'
|
|
4
4
|
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
5
|
import { renderTemplate } from '@kubb/core/utils'
|
|
6
|
-
import { pluginOasName } from '@kubb/plugin-oas'
|
|
7
|
-
|
|
6
|
+
import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
7
|
+
|
|
8
8
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
9
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
10
10
|
|
|
11
|
-
import { OperationGenerator } from './OperationGenerator.tsx'
|
|
12
|
-
import { Mutation, Operations, Query, QueryKey, QueryOptions } from './components/index.ts'
|
|
13
|
-
|
|
14
11
|
import type { Plugin } from '@kubb/core'
|
|
15
12
|
import type { PluginOas } from '@kubb/plugin-oas'
|
|
16
|
-
import {
|
|
13
|
+
import { infiniteQueryGenerator, mutationGenerator, queryGenerator, suspenseQueryGenerator } from './generators'
|
|
17
14
|
import type { PluginReactQuery } from './types.ts'
|
|
18
15
|
|
|
19
16
|
export const pluginReactQueryName = 'plugin-react-query' satisfies PluginReactQuery['name']
|
|
@@ -25,28 +22,30 @@ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
|
|
|
25
22
|
exclude = [],
|
|
26
23
|
include,
|
|
27
24
|
override = [],
|
|
28
|
-
parser,
|
|
25
|
+
parser = 'client',
|
|
29
26
|
suspense = {},
|
|
30
27
|
infinite,
|
|
31
28
|
transformers = {},
|
|
32
|
-
dataReturnType = 'data',
|
|
33
29
|
pathParamsType = 'inline',
|
|
34
|
-
|
|
30
|
+
mutation = {},
|
|
35
31
|
query = {},
|
|
36
|
-
queryOptions = {},
|
|
37
|
-
templates,
|
|
38
32
|
} = options
|
|
39
33
|
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
40
34
|
|
|
41
35
|
return {
|
|
42
36
|
name: pluginReactQueryName,
|
|
43
37
|
options: {
|
|
38
|
+
output: {
|
|
39
|
+
exportType: 'barrelNamed',
|
|
40
|
+
...output,
|
|
41
|
+
},
|
|
42
|
+
baseURL: undefined,
|
|
44
43
|
client: {
|
|
45
44
|
importPath: '@kubb/plugin-client/client',
|
|
45
|
+
dataReturnType: 'data',
|
|
46
|
+
pathParamsType: 'inline',
|
|
46
47
|
...options.client,
|
|
47
48
|
},
|
|
48
|
-
dataReturnType,
|
|
49
|
-
pathParamsType,
|
|
50
49
|
infinite: infinite
|
|
51
50
|
? {
|
|
52
51
|
queryParam: 'id',
|
|
@@ -56,31 +55,18 @@ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
|
|
|
56
55
|
}
|
|
57
56
|
: false,
|
|
58
57
|
suspense,
|
|
59
|
-
query:
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
? {
|
|
70
|
-
variablesType: 'hook',
|
|
71
|
-
methods: ['post', 'put', 'patch', 'delete'],
|
|
72
|
-
...mutate,
|
|
73
|
-
}
|
|
74
|
-
: false,
|
|
75
|
-
templates: {
|
|
76
|
-
mutation: Mutation.templates,
|
|
77
|
-
query: Query.templates,
|
|
78
|
-
queryOptions: QueryOptions.templates,
|
|
79
|
-
queryKey: QueryKey.templates,
|
|
80
|
-
queryImports: QueryImports.templates,
|
|
81
|
-
operations: Operations.templates,
|
|
82
|
-
...templates,
|
|
58
|
+
query: {
|
|
59
|
+
key: (key: unknown[]) => key,
|
|
60
|
+
methods: ['get'],
|
|
61
|
+
importPath: '@tanstack/react-query',
|
|
62
|
+
...query,
|
|
63
|
+
},
|
|
64
|
+
mutation: {
|
|
65
|
+
methods: ['post', 'put', 'patch', 'delete'],
|
|
66
|
+
importPath: '@tanstack/react-query',
|
|
67
|
+
...mutation,
|
|
83
68
|
},
|
|
69
|
+
pathParamsType,
|
|
84
70
|
parser,
|
|
85
71
|
},
|
|
86
72
|
pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
|
|
@@ -109,7 +95,6 @@ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
|
|
|
109
95
|
|
|
110
96
|
if (type === 'file' || type === 'function') {
|
|
111
97
|
resolvedName = camelCase(name, {
|
|
112
|
-
prefix: 'use',
|
|
113
98
|
isFile: type === 'file',
|
|
114
99
|
})
|
|
115
100
|
}
|
|
@@ -129,48 +114,41 @@ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
|
|
|
129
114
|
const oas = await swaggerPlugin.context.getOas()
|
|
130
115
|
const root = path.resolve(this.config.root, this.config.output.path)
|
|
131
116
|
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
117
|
+
const baseURL = await swaggerPlugin.context.getBaseURL()
|
|
118
|
+
|
|
119
|
+
const operationGenerator = new OperationGenerator(
|
|
120
|
+
{
|
|
121
|
+
...this.plugin.options,
|
|
122
|
+
baseURL,
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
oas,
|
|
126
|
+
pluginManager: this.pluginManager,
|
|
127
|
+
plugin: this.plugin,
|
|
128
|
+
contentType: swaggerPlugin.context.contentType,
|
|
129
|
+
exclude,
|
|
130
|
+
include,
|
|
131
|
+
override,
|
|
132
|
+
mode,
|
|
133
|
+
},
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
const files = await operationGenerator.build(queryGenerator, suspenseQueryGenerator, infiniteQueryGenerator, mutationGenerator)
|
|
145
137
|
await this.addFile(...files)
|
|
146
|
-
},
|
|
147
|
-
async buildEnd() {
|
|
148
|
-
if (this.config.output.write === false) {
|
|
149
|
-
return
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
const root = path.resolve(this.config.root, this.config.output.path)
|
|
153
138
|
|
|
154
|
-
if (
|
|
155
|
-
const
|
|
156
|
-
logger: this.logger,
|
|
157
|
-
files: this.fileManager.files,
|
|
158
|
-
plugin: this.plugin,
|
|
159
|
-
template,
|
|
160
|
-
exportAs: group.exportAs || '{{tag}}Hooks',
|
|
139
|
+
if (this.config.output.exportType) {
|
|
140
|
+
const barrelFiles = await this.fileManager.getBarrelFiles({
|
|
161
141
|
root,
|
|
162
142
|
output,
|
|
143
|
+
files: this.fileManager.files,
|
|
144
|
+
meta: {
|
|
145
|
+
pluginKey: this.plugin.key,
|
|
146
|
+
},
|
|
147
|
+
logger: this.logger,
|
|
163
148
|
})
|
|
164
149
|
|
|
165
|
-
await this.addFile(...
|
|
150
|
+
await this.addFile(...barrelFiles)
|
|
166
151
|
}
|
|
167
|
-
|
|
168
|
-
await this.fileManager.addIndexes({
|
|
169
|
-
root,
|
|
170
|
-
output,
|
|
171
|
-
meta: { pluginKey: this.plugin.key },
|
|
172
|
-
logger: this.logger,
|
|
173
|
-
})
|
|
174
152
|
},
|
|
175
153
|
}
|
|
176
154
|
})
|