@kubb/plugin-react-query 5.0.0-alpha.9 → 5.0.0-beta.100
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/LICENSE +17 -10
- package/README.md +38 -93
- package/dist/index.cjs +2317 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +494 -6
- package/dist/index.js +2283 -143
- package/dist/index.js.map +1 -1
- package/package.json +47 -75
- package/dist/components-BHQT9ZLc.cjs +0 -1634
- package/dist/components-BHQT9ZLc.cjs.map +0 -1
- package/dist/components-CpyHYGOw.js +0 -1520
- package/dist/components-CpyHYGOw.js.map +0 -1
- package/dist/components.cjs +0 -13
- package/dist/components.d.ts +0 -398
- package/dist/components.js +0 -2
- package/dist/generators-DP07m3rH.cjs +0 -1469
- package/dist/generators-DP07m3rH.cjs.map +0 -1
- package/dist/generators-DkQwKTc2.js +0 -1427
- package/dist/generators-DkQwKTc2.js.map +0 -1
- package/dist/generators.cjs +0 -9
- package/dist/generators.d.ts +0 -523
- package/dist/generators.js +0 -2
- package/dist/types-D5S7Ny9r.d.ts +0 -270
- package/src/components/InfiniteQuery.tsx +0 -220
- package/src/components/InfiniteQueryOptions.tsx +0 -289
- package/src/components/Mutation.tsx +0 -158
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/MutationOptions.tsx +0 -122
- package/src/components/Query.tsx +0 -194
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -181
- package/src/components/SuspenseInfiniteQuery.tsx +0 -220
- package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -289
- package/src/components/SuspenseQuery.tsx +0 -204
- package/src/components/index.ts +0 -11
- package/src/generators/customHookOptionsFileGenerator.tsx +0 -98
- package/src/generators/hookOptionsGenerator.tsx +0 -196
- package/src/generators/index.ts +0 -7
- package/src/generators/infiniteQueryGenerator.tsx +0 -214
- package/src/generators/mutationGenerator.tsx +0 -195
- package/src/generators/queryGenerator.tsx +0 -194
- package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -202
- package/src/generators/suspenseQueryGenerator.tsx +0 -207
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -239
- package/src/types.ts +0 -205
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
|
@@ -1,196 +0,0 @@
|
|
|
1
|
-
import { usePluginDriver } from '@kubb/core/hooks'
|
|
2
|
-
import type { Operation } from '@kubb/oas'
|
|
3
|
-
import { createReactGenerator } from '@kubb/plugin-oas/generators'
|
|
4
|
-
import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
-
import { getBanner, getFooter } from '@kubb/plugin-oas/utils'
|
|
6
|
-
import { File, Type } from '@kubb/react-fabric'
|
|
7
|
-
import { difference } from 'remeda'
|
|
8
|
-
import type { PluginReactQuery } from '../types'
|
|
9
|
-
|
|
10
|
-
export const hookOptionsGenerator = createReactGenerator<PluginReactQuery>({
|
|
11
|
-
name: 'react-query-hook-options',
|
|
12
|
-
Operations({ operations, plugin, generator }) {
|
|
13
|
-
const {
|
|
14
|
-
options,
|
|
15
|
-
options: { output },
|
|
16
|
-
name: pluginName,
|
|
17
|
-
} = plugin
|
|
18
|
-
const driver = usePluginDriver()
|
|
19
|
-
|
|
20
|
-
const oas = useOas()
|
|
21
|
-
const { getName, getFile } = useOperationManager(generator)
|
|
22
|
-
|
|
23
|
-
if (!options.customOptions) {
|
|
24
|
-
return null
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const name = 'HookOptions'
|
|
28
|
-
const file = driver.getFile({ name, extname: '.ts', pluginName })
|
|
29
|
-
|
|
30
|
-
const getOperationOptions = (operation: Operation) => {
|
|
31
|
-
const operationOptions = generator.getOptions(operation, operation.method)
|
|
32
|
-
return { ...options, ...operationOptions }
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const isQuery = (operation: Operation) => {
|
|
36
|
-
const operationOptions = getOperationOptions(operation)
|
|
37
|
-
return typeof operationOptions.query === 'boolean' ? true : operationOptions.query?.methods.some((method) => operation.method === method)
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const isMutation = (operation: Operation) => {
|
|
41
|
-
const operationOptions = getOperationOptions(operation)
|
|
42
|
-
return (
|
|
43
|
-
operationOptions.mutation !== false &&
|
|
44
|
-
!isQuery(operation) &&
|
|
45
|
-
difference(operationOptions.mutation ? operationOptions.mutation.methods : [], operationOptions.query ? operationOptions.query.methods : []).some(
|
|
46
|
-
(method) => operation.method === method,
|
|
47
|
-
)
|
|
48
|
-
)
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
const isSuspense = (operation: Operation) => {
|
|
52
|
-
const operationOptions = getOperationOptions(operation)
|
|
53
|
-
return !!operationOptions.suspense
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const isInfinite = (operation: Operation) => {
|
|
57
|
-
const operationOptions = getOperationOptions(operation)
|
|
58
|
-
const infiniteOptions = operationOptions.infinite && typeof operationOptions.infinite === 'object' ? operationOptions.infinite : undefined
|
|
59
|
-
return !!infiniteOptions
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Query/mutation hooks
|
|
63
|
-
const getHookName = (operation: Operation) => {
|
|
64
|
-
return getName(operation, { type: 'function', prefix: 'use' })
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const getHookFile = (operation: Operation) => {
|
|
68
|
-
return getFile(operation, { prefix: 'use' })
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// Query hooks
|
|
72
|
-
const getQueryHookOptions = (operation: Operation) => {
|
|
73
|
-
return getName(operation, { type: 'function', suffix: 'QueryOptions' })
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const getQueryHookOptionsImport = (operation: Operation) => {
|
|
77
|
-
return <File.Import name={[getQueryHookOptions(operation)]} root={file.path} path={getHookFile(operation).path} />
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
// Mutation hooks
|
|
81
|
-
const getMutationHookOptions = (operation: Operation) => {
|
|
82
|
-
return getName(operation, { type: 'function', suffix: 'MutationOptions' })
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
const getMutationHookOptionsImport = (operation: Operation) => {
|
|
86
|
-
return <File.Import name={[getMutationHookOptions(operation)]} root={file.path} path={getHookFile(operation).path} />
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Suspense hooks
|
|
90
|
-
const getSuspenseHookName = (operation: Operation) => {
|
|
91
|
-
return getName(operation, { type: 'function', prefix: 'use', suffix: 'suspense' })
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const getSuspenseHookFile = (operation: Operation) => {
|
|
95
|
-
return getFile(operation, { prefix: 'use', suffix: 'suspense' })
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
const getSuspenseHookOptions = (operation: Operation) => {
|
|
99
|
-
return getName(operation, { type: 'function', suffix: 'SuspenseQueryOptions' })
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const getSuspenseHookOptionsImport = (operation: Operation) => {
|
|
103
|
-
return <File.Import name={[getSuspenseHookOptions(operation)]} root={file.path} path={getSuspenseHookFile(operation).path} />
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// Infinite hooks
|
|
107
|
-
const getInfiniteHookName = (operation: Operation) => {
|
|
108
|
-
return getName(operation, { type: 'function', prefix: 'use', suffix: 'infinite' })
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
const getInfiniteHookFile = (operation: Operation) => {
|
|
112
|
-
return getFile(operation, { prefix: 'use', suffix: 'infinite' })
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const getInfiniteHookOptions = (operation: Operation) => {
|
|
116
|
-
return getName(operation, { type: 'function', suffix: 'InfiniteQueryOptions' })
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
const getInfiniteHookOptionsImport = (operation: Operation) => {
|
|
120
|
-
return <File.Import name={[getInfiniteHookOptions(operation)]} root={file.path} path={getInfiniteHookFile(operation).path} />
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Suspense infinite hooks
|
|
124
|
-
const getSuspenseInfiniteHookName = (operation: Operation) => {
|
|
125
|
-
return getName(operation, { type: 'function', prefix: 'use', suffix: 'suspenseInfinite' })
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
const getSuspenseInfiniteHookFile = (operation: Operation) => {
|
|
129
|
-
return getFile(operation, { prefix: 'use', suffix: 'suspenseInfinite' })
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const getSuspenseInfiniteHookOptions = (operation: Operation) => {
|
|
133
|
-
return getName(operation, { type: 'function', suffix: 'SuspenseInfiniteQueryOptions' })
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const getSuspenseInfiniteHookOptionsImport = (operation: Operation) => {
|
|
137
|
-
return <File.Import name={[getSuspenseInfiniteHookOptions(operation)]} root={file.path} path={getSuspenseInfiniteHookFile(operation).path} />
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const imports = operations
|
|
141
|
-
.flatMap((operation) => {
|
|
142
|
-
if (isQuery(operation)) {
|
|
143
|
-
return [
|
|
144
|
-
getQueryHookOptionsImport(operation),
|
|
145
|
-
isSuspense(operation) ? getSuspenseHookOptionsImport(operation) : undefined,
|
|
146
|
-
isInfinite(operation) ? getInfiniteHookOptionsImport(operation) : undefined,
|
|
147
|
-
isSuspense(operation) && isInfinite(operation) ? getSuspenseInfiniteHookOptionsImport(operation) : undefined,
|
|
148
|
-
].filter(Boolean)
|
|
149
|
-
}
|
|
150
|
-
if (isMutation(operation)) {
|
|
151
|
-
return [getMutationHookOptionsImport(operation)]
|
|
152
|
-
}
|
|
153
|
-
return []
|
|
154
|
-
})
|
|
155
|
-
.filter(Boolean)
|
|
156
|
-
|
|
157
|
-
const hookOptions = operations.reduce(
|
|
158
|
-
(acc, operation) => {
|
|
159
|
-
if (isQuery(operation)) {
|
|
160
|
-
acc[getHookName(operation)] = `Partial<ReturnType<typeof ${getQueryHookOptions(operation)}>>`
|
|
161
|
-
if (isSuspense(operation)) {
|
|
162
|
-
acc[getSuspenseHookName(operation)] = `Partial<ReturnType<typeof ${getSuspenseHookOptions(operation)}>>`
|
|
163
|
-
}
|
|
164
|
-
if (isInfinite(operation)) {
|
|
165
|
-
acc[getInfiniteHookName(operation)] = `Partial<ReturnType<typeof ${getInfiniteHookOptions(operation)}>>`
|
|
166
|
-
}
|
|
167
|
-
if (isSuspense(operation) && isInfinite(operation)) {
|
|
168
|
-
acc[getSuspenseInfiniteHookName(operation)] = `Partial<ReturnType<typeof ${getSuspenseInfiniteHookOptions(operation)}>>`
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
if (isMutation(operation)) {
|
|
172
|
-
acc[getHookName(operation)] = `Partial<ReturnType<typeof ${getMutationHookOptions(operation)}>>`
|
|
173
|
-
}
|
|
174
|
-
return acc
|
|
175
|
-
},
|
|
176
|
-
{} as Record<string, string>,
|
|
177
|
-
)
|
|
178
|
-
|
|
179
|
-
return (
|
|
180
|
-
<File
|
|
181
|
-
baseName={file.baseName}
|
|
182
|
-
path={file.path}
|
|
183
|
-
meta={file.meta}
|
|
184
|
-
banner={getBanner({ oas, output, config: driver.config })}
|
|
185
|
-
footer={getFooter({ oas, output })}
|
|
186
|
-
>
|
|
187
|
-
{imports}
|
|
188
|
-
<File.Source name={name} isExportable isIndexable isTypeOnly>
|
|
189
|
-
<Type export name={name}>
|
|
190
|
-
{`{ ${Object.keys(hookOptions).map((key) => `${JSON.stringify(key)}: ${hookOptions[key]}`)} }`}
|
|
191
|
-
</Type>
|
|
192
|
-
</File.Source>
|
|
193
|
-
</File>
|
|
194
|
-
)
|
|
195
|
-
},
|
|
196
|
-
})
|
package/src/generators/index.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
export { customHookOptionsFileGenerator } from './customHookOptionsFileGenerator.tsx'
|
|
2
|
-
export { hookOptionsGenerator } from './hookOptionsGenerator.tsx'
|
|
3
|
-
export { infiniteQueryGenerator } from './infiniteQueryGenerator.tsx'
|
|
4
|
-
export { mutationGenerator } from './mutationGenerator.tsx'
|
|
5
|
-
export { queryGenerator } from './queryGenerator.tsx'
|
|
6
|
-
export { suspenseInfiniteQueryGenerator } from './suspenseInfiniteQueryGenerator.tsx'
|
|
7
|
-
export { suspenseQueryGenerator } from './suspenseQueryGenerator.tsx'
|
|
@@ -1,214 +0,0 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import { usePluginDriver } from '@kubb/core/hooks'
|
|
3
|
-
import { pluginClientName } from '@kubb/plugin-client'
|
|
4
|
-
import { Client } from '@kubb/plugin-client/components'
|
|
5
|
-
import { createReactGenerator } from '@kubb/plugin-oas/generators'
|
|
6
|
-
import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
7
|
-
import { getBanner, getFooter } from '@kubb/plugin-oas/utils'
|
|
8
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
|
-
import { pluginZodName } from '@kubb/plugin-zod'
|
|
10
|
-
import { File } from '@kubb/react-fabric'
|
|
11
|
-
import { difference } from 'remeda'
|
|
12
|
-
import { InfiniteQuery, InfiniteQueryOptions, QueryKey } from '../components'
|
|
13
|
-
import type { PluginReactQuery } from '../types'
|
|
14
|
-
|
|
15
|
-
export const infiniteQueryGenerator = createReactGenerator<PluginReactQuery>({
|
|
16
|
-
name: 'react-infinite-query',
|
|
17
|
-
Operation({ config, operation, generator, plugin }) {
|
|
18
|
-
const {
|
|
19
|
-
options,
|
|
20
|
-
options: { output },
|
|
21
|
-
} = plugin
|
|
22
|
-
const driver = usePluginDriver()
|
|
23
|
-
|
|
24
|
-
const oas = useOas()
|
|
25
|
-
const { getSchemas, getName, getFile } = useOperationManager(generator)
|
|
26
|
-
|
|
27
|
-
const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
|
|
28
|
-
const isMutation = difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some(
|
|
29
|
-
(method) => operation.method === method,
|
|
30
|
-
)
|
|
31
|
-
const infiniteOptions = options.infinite && typeof options.infinite === 'object' ? options.infinite : undefined
|
|
32
|
-
|
|
33
|
-
const importPath = options.query ? options.query.importPath : '@tanstack/react-query'
|
|
34
|
-
|
|
35
|
-
const query = {
|
|
36
|
-
name: getName(operation, { type: 'function', prefix: 'use', suffix: 'infinite' }),
|
|
37
|
-
typeName: getName(operation, { type: 'type' }),
|
|
38
|
-
file: getFile(operation, { prefix: 'use', suffix: 'infinite' }),
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const hasClientPlugin = !!driver.getPluginByName(pluginClientName)
|
|
42
|
-
// Class-based clients are not compatible with query hooks, so we generate inline clients
|
|
43
|
-
const shouldUseClientPlugin = hasClientPlugin && options.client.clientType !== 'class'
|
|
44
|
-
const client = {
|
|
45
|
-
name: shouldUseClientPlugin
|
|
46
|
-
? getName(operation, {
|
|
47
|
-
type: 'function',
|
|
48
|
-
pluginName: pluginClientName,
|
|
49
|
-
})
|
|
50
|
-
: getName(operation, {
|
|
51
|
-
type: 'function',
|
|
52
|
-
suffix: 'infinite',
|
|
53
|
-
}),
|
|
54
|
-
file: getFile(operation, { pluginName: pluginClientName }),
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const queryOptions = {
|
|
58
|
-
name: getName(operation, { type: 'function', suffix: 'InfiniteQueryOptions' }),
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const queryKey = {
|
|
62
|
-
name: getName(operation, { type: 'const', suffix: 'InfiniteQueryKey' }),
|
|
63
|
-
typeName: getName(operation, { type: 'type', suffix: 'InfiniteQueryKey' }),
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const type = {
|
|
67
|
-
file: getFile(operation, { pluginName: pluginTsName }),
|
|
68
|
-
//todo remove type?
|
|
69
|
-
schemas: getSchemas(operation, { pluginName: pluginTsName, type: 'type' }),
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const zod = {
|
|
73
|
-
file: getFile(operation, { pluginName: pluginZodName }),
|
|
74
|
-
schemas: getSchemas(operation, { pluginName: pluginZodName, type: 'function' }),
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!isQuery || isMutation || !infiniteOptions) {
|
|
78
|
-
return null
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
const normalizeKey = (key?: string | null) => (key ?? '').replace(/\?$/, '')
|
|
82
|
-
const queryParam = infiniteOptions.queryParam
|
|
83
|
-
const cursorParam = infiniteOptions.cursorParam
|
|
84
|
-
const queryParamKeys = type.schemas.queryParams?.keys ?? []
|
|
85
|
-
const responseKeys = [...(type.schemas.responses?.flatMap((item) => item.keys ?? []) ?? []), ...(type.schemas.response?.keys ?? [])]
|
|
86
|
-
|
|
87
|
-
const hasQueryParam = queryParam ? queryParamKeys.some((key) => normalizeKey(key) === queryParam) : false
|
|
88
|
-
const hasCursorParam = cursorParam ? responseKeys.some((key) => normalizeKey(key) === cursorParam) : true
|
|
89
|
-
|
|
90
|
-
if (!hasQueryParam || !hasCursorParam) {
|
|
91
|
-
return null
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
return (
|
|
95
|
-
<File
|
|
96
|
-
baseName={query.file.baseName}
|
|
97
|
-
path={query.file.path}
|
|
98
|
-
meta={query.file.meta}
|
|
99
|
-
banner={getBanner({ oas, output, config: driver.config })}
|
|
100
|
-
footer={getFooter({ oas, output })}
|
|
101
|
-
>
|
|
102
|
-
{options.parser === 'zod' && (
|
|
103
|
-
<File.Import name={[zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean)} root={query.file.path} path={zod.file.path} />
|
|
104
|
-
)}
|
|
105
|
-
{options.client.importPath ? (
|
|
106
|
-
<>
|
|
107
|
-
{!shouldUseClientPlugin && <File.Import name={'fetch'} path={options.client.importPath} />}
|
|
108
|
-
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={options.client.importPath} isTypeOnly />
|
|
109
|
-
{options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
|
|
110
|
-
</>
|
|
111
|
-
) : (
|
|
112
|
-
<>
|
|
113
|
-
{!shouldUseClientPlugin && (
|
|
114
|
-
<File.Import name={['fetch']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} />
|
|
115
|
-
)}
|
|
116
|
-
<File.Import
|
|
117
|
-
name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
|
|
118
|
-
root={query.file.path}
|
|
119
|
-
path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
|
|
120
|
-
isTypeOnly
|
|
121
|
-
/>
|
|
122
|
-
{options.client.dataReturnType === 'full' && (
|
|
123
|
-
<File.Import name={['ResponseConfig']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} isTypeOnly />
|
|
124
|
-
)}
|
|
125
|
-
</>
|
|
126
|
-
)}
|
|
127
|
-
|
|
128
|
-
{shouldUseClientPlugin && <File.Import name={[client.name]} root={query.file.path} path={client.file.path} />}
|
|
129
|
-
{!shouldUseClientPlugin && (
|
|
130
|
-
<File.Import name={['buildFormData']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/config.ts')} />
|
|
131
|
-
)}
|
|
132
|
-
<File.Import
|
|
133
|
-
name={[
|
|
134
|
-
type.schemas.request?.name,
|
|
135
|
-
type.schemas.response.name,
|
|
136
|
-
type.schemas.pathParams?.name,
|
|
137
|
-
type.schemas.queryParams?.name,
|
|
138
|
-
type.schemas.headerParams?.name,
|
|
139
|
-
...(type.schemas.statusCodes?.map((item) => item.name) || []),
|
|
140
|
-
].filter(Boolean)}
|
|
141
|
-
root={query.file.path}
|
|
142
|
-
path={type.file.path}
|
|
143
|
-
isTypeOnly
|
|
144
|
-
/>
|
|
145
|
-
<QueryKey
|
|
146
|
-
name={queryKey.name}
|
|
147
|
-
typeName={queryKey.typeName}
|
|
148
|
-
operation={operation}
|
|
149
|
-
paramsCasing={options.paramsCasing}
|
|
150
|
-
pathParamsType={options.pathParamsType}
|
|
151
|
-
typeSchemas={type.schemas}
|
|
152
|
-
transformer={options.queryKey}
|
|
153
|
-
/>
|
|
154
|
-
{!shouldUseClientPlugin && (
|
|
155
|
-
<Client
|
|
156
|
-
name={client.name}
|
|
157
|
-
baseURL={options.client.baseURL}
|
|
158
|
-
operation={operation}
|
|
159
|
-
typeSchemas={type.schemas}
|
|
160
|
-
zodSchemas={zod.schemas}
|
|
161
|
-
dataReturnType={options.client.dataReturnType || 'data'}
|
|
162
|
-
paramsCasing={options.client?.paramsCasing || options.paramsCasing}
|
|
163
|
-
paramsType={options.paramsType}
|
|
164
|
-
pathParamsType={options.pathParamsType}
|
|
165
|
-
parser={options.parser}
|
|
166
|
-
/>
|
|
167
|
-
)}
|
|
168
|
-
{options.customOptions && <File.Import name={[options.customOptions.name]} path={options.customOptions.importPath} />}
|
|
169
|
-
{infiniteOptions && (
|
|
170
|
-
<>
|
|
171
|
-
<File.Import name={['InfiniteData']} isTypeOnly path={importPath} />
|
|
172
|
-
<File.Import name={['infiniteQueryOptions']} path={importPath} />
|
|
173
|
-
<InfiniteQueryOptions
|
|
174
|
-
name={queryOptions.name}
|
|
175
|
-
clientName={client.name}
|
|
176
|
-
queryKeyName={queryKey.name}
|
|
177
|
-
typeSchemas={type.schemas}
|
|
178
|
-
paramsCasing={options.paramsCasing}
|
|
179
|
-
paramsType={options.paramsType}
|
|
180
|
-
pathParamsType={options.pathParamsType}
|
|
181
|
-
dataReturnType={options.client.dataReturnType || 'data'}
|
|
182
|
-
cursorParam={infiniteOptions.cursorParam}
|
|
183
|
-
nextParam={infiniteOptions.nextParam}
|
|
184
|
-
previousParam={infiniteOptions.previousParam}
|
|
185
|
-
initialPageParam={infiniteOptions.initialPageParam}
|
|
186
|
-
queryParam={infiniteOptions.queryParam}
|
|
187
|
-
/>
|
|
188
|
-
</>
|
|
189
|
-
)}
|
|
190
|
-
{infiniteOptions && (
|
|
191
|
-
<>
|
|
192
|
-
<File.Import name={['useInfiniteQuery']} path={importPath} />
|
|
193
|
-
<File.Import name={['QueryKey', 'QueryClient', 'InfiniteQueryObserverOptions', 'UseInfiniteQueryResult']} path={importPath} isTypeOnly />
|
|
194
|
-
<InfiniteQuery
|
|
195
|
-
name={query.name}
|
|
196
|
-
queryOptionsName={queryOptions.name}
|
|
197
|
-
typeSchemas={type.schemas}
|
|
198
|
-
paramsCasing={options.paramsCasing}
|
|
199
|
-
paramsType={options.paramsType}
|
|
200
|
-
pathParamsType={options.pathParamsType}
|
|
201
|
-
operation={operation}
|
|
202
|
-
dataReturnType={options.client.dataReturnType || 'data'}
|
|
203
|
-
queryKeyName={queryKey.name}
|
|
204
|
-
queryKeyTypeName={queryKey.typeName}
|
|
205
|
-
initialPageParam={infiniteOptions.initialPageParam}
|
|
206
|
-
queryParam={infiniteOptions.queryParam}
|
|
207
|
-
customOptions={options.customOptions}
|
|
208
|
-
/>
|
|
209
|
-
</>
|
|
210
|
-
)}
|
|
211
|
-
</File>
|
|
212
|
-
)
|
|
213
|
-
},
|
|
214
|
-
})
|
|
@@ -1,195 +0,0 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import { usePluginDriver } from '@kubb/core/hooks'
|
|
3
|
-
import { pluginClientName } from '@kubb/plugin-client'
|
|
4
|
-
import { Client } from '@kubb/plugin-client/components'
|
|
5
|
-
import { createReactGenerator } from '@kubb/plugin-oas/generators'
|
|
6
|
-
import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
7
|
-
import { getBanner, getFooter } from '@kubb/plugin-oas/utils'
|
|
8
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
9
|
-
import { pluginZodName } from '@kubb/plugin-zod'
|
|
10
|
-
import { File } from '@kubb/react-fabric'
|
|
11
|
-
import { difference } from 'remeda'
|
|
12
|
-
import { Mutation, MutationKey } from '../components'
|
|
13
|
-
import { MutationOptions } from '../components/MutationOptions.tsx'
|
|
14
|
-
import type { PluginReactQuery } from '../types'
|
|
15
|
-
|
|
16
|
-
export const mutationGenerator = createReactGenerator<PluginReactQuery>({
|
|
17
|
-
name: 'react-query',
|
|
18
|
-
Operation({ config, plugin, operation, generator }) {
|
|
19
|
-
const {
|
|
20
|
-
options,
|
|
21
|
-
options: { output },
|
|
22
|
-
} = plugin
|
|
23
|
-
const driver = usePluginDriver()
|
|
24
|
-
|
|
25
|
-
const oas = useOas()
|
|
26
|
-
const { getSchemas, getName, getFile } = useOperationManager(generator)
|
|
27
|
-
|
|
28
|
-
const isQuery = !!options.query && options.query?.methods.some((method) => operation.method === method)
|
|
29
|
-
const isMutation =
|
|
30
|
-
options.mutation !== false &&
|
|
31
|
-
!isQuery &&
|
|
32
|
-
difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method)
|
|
33
|
-
|
|
34
|
-
const importPath = options.mutation ? options.mutation.importPath : '@tanstack/react-query'
|
|
35
|
-
|
|
36
|
-
const mutation = {
|
|
37
|
-
name: getName(operation, { type: 'function', prefix: 'use' }),
|
|
38
|
-
typeName: getName(operation, { type: 'type' }),
|
|
39
|
-
file: getFile(operation, { prefix: 'use' }),
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const type = {
|
|
43
|
-
file: getFile(operation, { pluginName: pluginTsName }),
|
|
44
|
-
//todo remove type?
|
|
45
|
-
schemas: getSchemas(operation, { pluginName: pluginTsName, type: 'type' }),
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const zod = {
|
|
49
|
-
file: getFile(operation, { pluginName: pluginZodName }),
|
|
50
|
-
schemas: getSchemas(operation, { pluginName: pluginZodName, type: 'function' }),
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const hasClientPlugin = !!driver.getPluginByName(pluginClientName)
|
|
54
|
-
// Class-based clients are not compatible with query hooks, so we generate inline clients
|
|
55
|
-
const shouldUseClientPlugin = hasClientPlugin && options.client.clientType !== 'class'
|
|
56
|
-
const client = {
|
|
57
|
-
name: shouldUseClientPlugin
|
|
58
|
-
? getName(operation, {
|
|
59
|
-
type: 'function',
|
|
60
|
-
pluginName: pluginClientName,
|
|
61
|
-
})
|
|
62
|
-
: getName(operation, {
|
|
63
|
-
type: 'function',
|
|
64
|
-
}),
|
|
65
|
-
file: getFile(operation, { pluginName: pluginClientName }),
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
const mutationOptions = {
|
|
69
|
-
name: getName(operation, { type: 'function', suffix: 'MutationOptions' }),
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
const mutationKey = {
|
|
73
|
-
name: getName(operation, { type: 'const', suffix: 'MutationKey' }),
|
|
74
|
-
typeName: getName(operation, { type: 'type', suffix: 'MutationKey' }),
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (!isMutation) {
|
|
78
|
-
return null
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return (
|
|
82
|
-
<File
|
|
83
|
-
baseName={mutation.file.baseName}
|
|
84
|
-
path={mutation.file.path}
|
|
85
|
-
meta={mutation.file.meta}
|
|
86
|
-
banner={getBanner({ oas, output, config: driver.config })}
|
|
87
|
-
footer={getFooter({ oas, output })}
|
|
88
|
-
>
|
|
89
|
-
{options.parser === 'zod' && (
|
|
90
|
-
<File.Import name={[zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean)} root={mutation.file.path} path={zod.file.path} />
|
|
91
|
-
)}
|
|
92
|
-
{options.client.importPath ? (
|
|
93
|
-
<>
|
|
94
|
-
{!shouldUseClientPlugin && <File.Import name={'fetch'} path={options.client.importPath} />}
|
|
95
|
-
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={options.client.importPath} isTypeOnly />
|
|
96
|
-
{options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
|
|
97
|
-
</>
|
|
98
|
-
) : (
|
|
99
|
-
<>
|
|
100
|
-
{!shouldUseClientPlugin && (
|
|
101
|
-
<File.Import name={['fetch']} root={mutation.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} />
|
|
102
|
-
)}
|
|
103
|
-
<File.Import
|
|
104
|
-
name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
|
|
105
|
-
root={mutation.file.path}
|
|
106
|
-
path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
|
|
107
|
-
isTypeOnly
|
|
108
|
-
/>
|
|
109
|
-
{options.client.dataReturnType === 'full' && (
|
|
110
|
-
<File.Import
|
|
111
|
-
name={['ResponseConfig']}
|
|
112
|
-
root={mutation.file.path}
|
|
113
|
-
path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
|
|
114
|
-
isTypeOnly
|
|
115
|
-
/>
|
|
116
|
-
)}
|
|
117
|
-
</>
|
|
118
|
-
)}
|
|
119
|
-
{shouldUseClientPlugin && <File.Import name={[client.name]} root={mutation.file.path} path={client.file.path} />}
|
|
120
|
-
{!shouldUseClientPlugin && (
|
|
121
|
-
<File.Import name={['buildFormData']} root={mutation.file.path} path={path.resolve(config.root, config.output.path, '.kubb/config.ts')} />
|
|
122
|
-
)}
|
|
123
|
-
{options.customOptions && <File.Import name={[options.customOptions.name]} path={options.customOptions.importPath} />}
|
|
124
|
-
<File.Import
|
|
125
|
-
name={[
|
|
126
|
-
type.schemas.request?.name,
|
|
127
|
-
type.schemas.response.name,
|
|
128
|
-
type.schemas.pathParams?.name,
|
|
129
|
-
type.schemas.queryParams?.name,
|
|
130
|
-
type.schemas.headerParams?.name,
|
|
131
|
-
...(type.schemas.statusCodes?.map((item) => item.name) || []),
|
|
132
|
-
].filter(Boolean)}
|
|
133
|
-
root={mutation.file.path}
|
|
134
|
-
path={type.file.path}
|
|
135
|
-
isTypeOnly
|
|
136
|
-
/>
|
|
137
|
-
|
|
138
|
-
<MutationKey
|
|
139
|
-
name={mutationKey.name}
|
|
140
|
-
typeName={mutationKey.typeName}
|
|
141
|
-
operation={operation}
|
|
142
|
-
pathParamsType={options.pathParamsType}
|
|
143
|
-
typeSchemas={type.schemas}
|
|
144
|
-
paramsCasing={options.paramsCasing}
|
|
145
|
-
transformer={options.mutationKey}
|
|
146
|
-
/>
|
|
147
|
-
|
|
148
|
-
{!shouldUseClientPlugin && (
|
|
149
|
-
<Client
|
|
150
|
-
name={client.name}
|
|
151
|
-
baseURL={options.client.baseURL}
|
|
152
|
-
operation={operation}
|
|
153
|
-
typeSchemas={type.schemas}
|
|
154
|
-
zodSchemas={zod.schemas}
|
|
155
|
-
dataReturnType={options.client.dataReturnType || 'data'}
|
|
156
|
-
paramsCasing={options.client?.paramsCasing || options.paramsCasing}
|
|
157
|
-
paramsType={options.paramsType}
|
|
158
|
-
pathParamsType={options.pathParamsType}
|
|
159
|
-
parser={options.parser}
|
|
160
|
-
/>
|
|
161
|
-
)}
|
|
162
|
-
<File.Import name={['mutationOptions']} path={importPath} />
|
|
163
|
-
|
|
164
|
-
<MutationOptions
|
|
165
|
-
name={mutationOptions.name}
|
|
166
|
-
clientName={client.name}
|
|
167
|
-
mutationKeyName={mutationKey.name}
|
|
168
|
-
typeSchemas={type.schemas}
|
|
169
|
-
paramsCasing={options.paramsCasing}
|
|
170
|
-
paramsType={options.paramsType}
|
|
171
|
-
pathParamsType={options.pathParamsType}
|
|
172
|
-
dataReturnType={options.client.dataReturnType || 'data'}
|
|
173
|
-
/>
|
|
174
|
-
{options.mutation && (
|
|
175
|
-
<>
|
|
176
|
-
<File.Import name={['useMutation']} path={importPath} />
|
|
177
|
-
<File.Import name={['UseMutationOptions', 'UseMutationResult', 'QueryClient']} path={importPath} isTypeOnly />
|
|
178
|
-
<Mutation
|
|
179
|
-
name={mutation.name}
|
|
180
|
-
mutationOptionsName={mutationOptions.name}
|
|
181
|
-
typeName={mutation.typeName}
|
|
182
|
-
typeSchemas={type.schemas}
|
|
183
|
-
operation={operation}
|
|
184
|
-
dataReturnType={options.client.dataReturnType || 'data'}
|
|
185
|
-
paramsCasing={options.paramsCasing}
|
|
186
|
-
pathParamsType={options.pathParamsType}
|
|
187
|
-
mutationKeyName={mutationKey.name}
|
|
188
|
-
customOptions={options.customOptions}
|
|
189
|
-
/>
|
|
190
|
-
</>
|
|
191
|
-
)}
|
|
192
|
-
</File>
|
|
193
|
-
)
|
|
194
|
-
},
|
|
195
|
-
})
|