@kubb/plugin-react-query 5.0.0-beta.42 → 5.0.0-beta.64

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.
Files changed (51) hide show
  1. package/dist/{components-DQAYLQW0.cjs → components-B0j2b6y8.cjs} +298 -416
  2. package/dist/components-B0j2b6y8.cjs.map +1 -0
  3. package/dist/{components-IArDg-DO.js → components-Ds-e_nnK.js} +294 -412
  4. package/dist/components-Ds-e_nnK.js.map +1 -0
  5. package/dist/components.cjs +1 -1
  6. package/dist/components.d.ts +1 -1
  7. package/dist/components.js +1 -1
  8. package/dist/{generators-BqGaMUH6.cjs → generators-CU6zfet7.cjs} +149 -229
  9. package/dist/generators-CU6zfet7.cjs.map +1 -0
  10. package/dist/{generators-B86BJkmW.js → generators-nnZhtlrk.js} +151 -231
  11. package/dist/generators-nnZhtlrk.js.map +1 -0
  12. package/dist/generators.cjs +1 -1
  13. package/dist/generators.d.ts +1 -1
  14. package/dist/generators.js +1 -1
  15. package/dist/index.cjs +41 -18
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.js +42 -19
  19. package/dist/index.js.map +1 -1
  20. package/dist/{types-Dh4HNR9K.d.ts → types-ANQXv7Gr.d.ts} +13 -17
  21. package/package.json +10 -18
  22. package/dist/components-DQAYLQW0.cjs.map +0 -1
  23. package/dist/components-IArDg-DO.js.map +0 -1
  24. package/dist/generators-B86BJkmW.js.map +0 -1
  25. package/dist/generators-BqGaMUH6.cjs.map +0 -1
  26. package/extension.yaml +0 -1484
  27. package/src/components/InfiniteQuery.tsx +0 -167
  28. package/src/components/InfiniteQueryOptions.tsx +0 -169
  29. package/src/components/Mutation.tsx +0 -123
  30. package/src/components/MutationKey.tsx +0 -1
  31. package/src/components/MutationOptions.tsx +0 -104
  32. package/src/components/Query.tsx +0 -125
  33. package/src/components/QueryKey.tsx +0 -1
  34. package/src/components/QueryOptions.tsx +0 -104
  35. package/src/components/SuspenseInfiniteQuery.tsx +0 -162
  36. package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -163
  37. package/src/components/SuspenseQuery.tsx +0 -120
  38. package/src/components/index.ts +0 -11
  39. package/src/generators/customHookOptionsFileGenerator.tsx +0 -90
  40. package/src/generators/hookOptionsGenerator.tsx +0 -129
  41. package/src/generators/index.ts +0 -7
  42. package/src/generators/infiniteQueryGenerator.tsx +0 -200
  43. package/src/generators/mutationGenerator.tsx +0 -173
  44. package/src/generators/queryGenerator.tsx +0 -185
  45. package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -209
  46. package/src/generators/suspenseQueryGenerator.tsx +0 -188
  47. package/src/index.ts +0 -2
  48. package/src/plugin.ts +0 -203
  49. package/src/resolvers/resolverReactQuery.ts +0 -118
  50. package/src/types.ts +0 -365
  51. package/src/utils.ts +0 -17
@@ -1,11 +0,0 @@
1
- export { InfiniteQuery } from './InfiniteQuery.tsx'
2
- export { InfiniteQueryOptions } from './InfiniteQueryOptions.tsx'
3
- export { Mutation } from './Mutation.tsx'
4
- export { MutationKey } from './MutationKey.tsx'
5
- export { MutationOptions } from './MutationOptions.tsx'
6
- export { Query } from './Query.tsx'
7
- export { QueryKey } from './QueryKey.tsx'
8
- export { QueryOptions } from './QueryOptions.tsx'
9
- export { SuspenseInfiniteQuery } from './SuspenseInfiniteQuery.tsx'
10
- export { SuspenseInfiniteQueryOptions } from './SuspenseInfiniteQueryOptions.tsx'
11
- export { SuspenseQuery } from './SuspenseQuery.tsx'
@@ -1,90 +0,0 @@
1
- import fs from 'node:fs'
2
- import path from 'node:path'
3
-
4
- import { defineGenerator } from '@kubb/core'
5
- import { File, Function, jsxRendererSync } from '@kubb/renderer-jsx'
6
- import type { PluginReactQuery } from '../types'
7
-
8
- /**
9
- * Scaffolds the user-editable `useCustomHookOptions` file when
10
- * `pluginReactQuery({ customOptions: { ... } })` is configured. The file is
11
- * only created when it does not already exist, so user edits persist across
12
- * regeneration.
13
- */
14
- export const customHookOptionsFileGenerator = defineGenerator<PluginReactQuery>({
15
- name: 'react-query-custom-hook-options-file',
16
- renderer: jsxRendererSync,
17
- operations(nodes, ctx) {
18
- const { resolver, config, root } = ctx
19
- const { output, customOptions, query, group } = ctx.options
20
-
21
- if (!customOptions) return null
22
-
23
- const override = output.override ?? config.output.override ?? false
24
- const { importPath, name } = customOptions
25
- const hookOptionsName = resolver.resolveHookOptionsName()
26
- const customHookOptionsName = resolver.resolveCustomHookOptionsName()
27
-
28
- const reactQueryImportPath = query ? query.importPath : '@tanstack/react-query'
29
-
30
- let hookFilePath: string
31
- const firstNode = nodes[0]
32
- if (firstNode) {
33
- const hookName = resolver.resolveQueryName(firstNode)
34
- const hookFile = resolver.resolveFile(
35
- { name: hookName, extname: '.ts', tag: firstNode.tags[0] ?? 'default', path: firstNode.path },
36
- { root, output, group: group ?? undefined },
37
- )
38
- hookFilePath = hookFile.path
39
- } else {
40
- hookFilePath = path.resolve(root, 'index.ts')
41
- }
42
-
43
- const ensureExtension = (filePath: string, extname: string) => {
44
- if (path.extname(filePath) === '') return filePath + extname
45
- return filePath
46
- }
47
-
48
- const basePath = path.dirname(hookFilePath)
49
- const actualFilePath = ensureExtension(importPath, '.ts')
50
- const file = {
51
- baseName: path.basename(actualFilePath) as `${string}.${string}`,
52
- name: path.basename(actualFilePath, path.extname(actualFilePath)),
53
- path: path.resolve(basePath, actualFilePath),
54
- }
55
-
56
- if (fs.existsSync(file.path) && !override) return null
57
-
58
- return (
59
- <File baseName={file.baseName} path={file.path}>
60
- <File.Import name={['QueryClient']} path={reactQueryImportPath} isTypeOnly />
61
- <File.Import name={['useQueryClient']} path={reactQueryImportPath} />
62
- <File.Import name={[hookOptionsName]} root={file.path} path={path.resolve(root, './index.ts')} />
63
- <File.Source name={file.name} isExportable isIndexable>
64
- <Function name={customHookOptionsName} params="{ queryClient }: { queryClient: QueryClient }" returnType={`Partial<${hookOptionsName}>`}>
65
- {`return {
66
- // TODO: Define custom hook options here
67
- // Example:
68
- // useUpdatePetHook: {
69
- // onSuccess: () => {
70
- // void queryClient.invalidateQueries({ queryKey: ['pet'] })
71
- // }
72
- // }
73
- }`}
74
- </Function>
75
- <Function
76
- name={name}
77
- generics={`T extends keyof ${hookOptionsName}`}
78
- params="{ hookName, operationId }: { hookName: T, operationId: string }"
79
- returnType={`${hookOptionsName}[T]`}
80
- export
81
- >
82
- {`const queryClient = useQueryClient()
83
- const customOptions = ${customHookOptionsName}({ queryClient })
84
- return customOptions[hookName] ?? {}`}
85
- </Function>
86
- </File.Source>
87
- </File>
88
- )
89
- },
90
- })
@@ -1,129 +0,0 @@
1
- import { getOperationParameters, operationFileEntry } from '@internals/shared'
2
- import { ast, defineGenerator } from '@kubb/core'
3
- import { File, jsxRendererSync, Type } from '@kubb/renderer-jsx'
4
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
5
- import type { PluginReactQuery } from '../types'
6
- import { resolveOperationOverrides } from '../utils.ts'
7
-
8
- type QueryOption = PluginReactQuery['resolvedOptions']['query']
9
- type MutationOption = PluginReactQuery['resolvedOptions']['mutation']
10
-
11
- /**
12
- * Emits the `HookOptions` type used by `customOptions`. Enabled when
13
- * `pluginReactQuery({ customOptions: { ... } })`. The generated type lists
14
- * every hook keyed by name so user-supplied options stay in sync with the
15
- * generated hooks at compile time.
16
- */
17
- export const hookOptionsGenerator = defineGenerator<PluginReactQuery>({
18
- name: 'react-query-hook-options',
19
- renderer: jsxRendererSync,
20
- operations(nodes, ctx) {
21
- const { resolver, config, root } = ctx
22
- const { output, customOptions, query, mutation, suspense, infinite, group, override } = ctx.options
23
-
24
- if (!customOptions) return null
25
-
26
- const name = resolver.resolveHookOptionsName()
27
- const resolvedFile = resolver.resolveFile({ name, extname: '.ts' }, { root, output, group: group ?? undefined })
28
- const hookOptionsFile = {
29
- ...resolvedFile,
30
- baseName: `${name}.ts` as const,
31
- path: resolvedFile.path.replace(/[^/\\]+\.ts$/, `${name}.ts`),
32
- }
33
-
34
- const imports: Array<KubbReactNode> = []
35
- const hookOptions: Record<string, string> = {}
36
-
37
- for (const node of nodes) {
38
- if (!ast.isHttpOperationNode(node)) continue
39
- const opOverrides = resolveOperationOverrides(node, override)
40
- const nodeQuery: QueryOption = 'query' in opOverrides ? (opOverrides.query as QueryOption) : query
41
- const nodeMutation: MutationOption = 'mutation' in opOverrides ? (opOverrides.mutation as MutationOption) : mutation
42
- const nodeInfinite = 'infinite' in opOverrides ? opOverrides.infinite : infinite
43
- const nodeInfiniteOptions = nodeInfinite && typeof nodeInfinite === 'object' ? nodeInfinite : null
44
-
45
- // query: false means "still a query but skip the useQuery hook"
46
- const isQueryOp =
47
- nodeQuery === false
48
- ? !!query && query.methods.some((m) => node.method.toLowerCase() === m.toLowerCase())
49
- : !!nodeQuery && nodeQuery.methods.some((m) => node.method.toLowerCase() === m.toLowerCase())
50
- const nodeQueryMethods = new Set(nodeQuery ? nodeQuery.methods : [])
51
- const isMutationOp =
52
- nodeMutation !== false &&
53
- !isQueryOp &&
54
- (nodeMutation ? nodeMutation.methods : []).some((m) => !nodeQueryMethods.has(m) && node.method.toLowerCase() === m.toLowerCase())
55
- const isSuspenseOp = !!suspense
56
- const isInfiniteOp = !!nodeInfiniteOptions
57
-
58
- if (isQueryOp) {
59
- const queryOptionsName = resolver.resolveQueryOptionsName(node)
60
- const queryHookName = resolver.resolveQueryName(node)
61
- const queryHookFile = resolver.resolveFile(operationFileEntry(node, queryHookName), { root, output, group: group ?? undefined })
62
- imports.push(<File.Import name={[queryOptionsName]} root={hookOptionsFile.path} path={queryHookFile.path} />)
63
- hookOptions[queryHookName] = `Partial<ReturnType<typeof ${queryOptionsName}>>`
64
-
65
- if (isSuspenseOp) {
66
- const suspenseOptionsName = resolver.resolveSuspenseQueryOptionsName(node)
67
- const suspenseHookName = resolver.resolveSuspenseQueryName(node)
68
- const suspenseHookFile = resolver.resolveFile(operationFileEntry(node, suspenseHookName), { root, output, group: group ?? undefined })
69
- imports.push(<File.Import name={[suspenseOptionsName]} root={hookOptionsFile.path} path={suspenseHookFile.path} />)
70
- hookOptions[suspenseHookName] = `Partial<ReturnType<typeof ${suspenseOptionsName}>>`
71
- }
72
-
73
- if (isInfiniteOp) {
74
- // Validate queryParam
75
- const normalizeKey = (key: string) => key.replace(/\?$/, '')
76
- const queryParamKeys = getOperationParameters(node).query.map((p) => p.name)
77
- const hasQueryParam = nodeInfiniteOptions!.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === nodeInfiniteOptions!.queryParam) : false
78
-
79
- if (hasQueryParam) {
80
- const infiniteOptionsName = resolver.resolveInfiniteQueryOptionsName(node)
81
- const infiniteHookName = resolver.resolveInfiniteQueryName(node)
82
- const infiniteHookFile = resolver.resolveFile(operationFileEntry(node, infiniteHookName), { root, output, group: group ?? undefined })
83
- imports.push(<File.Import name={[infiniteOptionsName]} root={hookOptionsFile.path} path={infiniteHookFile.path} />)
84
- hookOptions[infiniteHookName] = `Partial<ReturnType<typeof ${infiniteOptionsName}>>`
85
-
86
- if (isSuspenseOp) {
87
- const suspenseInfiniteOptionsName = resolver.resolveSuspenseInfiniteQueryOptionsName(node)
88
- const suspenseInfiniteHookName = resolver.resolveSuspenseInfiniteQueryName(node)
89
- const suspenseInfiniteHookFile = resolver.resolveFile(operationFileEntry(node, suspenseInfiniteHookName), {
90
- root,
91
- output,
92
- group: group ?? undefined,
93
- })
94
- imports.push(<File.Import name={[suspenseInfiniteOptionsName]} root={hookOptionsFile.path} path={suspenseInfiniteHookFile.path} />)
95
- hookOptions[suspenseInfiniteHookName] = `Partial<ReturnType<typeof ${suspenseInfiniteOptionsName}>>`
96
- }
97
- }
98
- }
99
- }
100
-
101
- if (isMutationOp) {
102
- const mutationOptionsName = resolver.resolveMutationOptionsName(node)
103
- const mutationHookName = resolver.resolveMutationName(node)
104
- const mutationHookFile = resolver.resolveFile(operationFileEntry(node, mutationHookName), { root, output, group: group ?? undefined })
105
- imports.push(<File.Import name={[mutationOptionsName]} root={hookOptionsFile.path} path={mutationHookFile.path} />)
106
- hookOptions[mutationHookName] = `Partial<ReturnType<typeof ${mutationOptionsName}>>`
107
- }
108
- }
109
-
110
- return (
111
- <File
112
- baseName={hookOptionsFile.baseName}
113
- path={hookOptionsFile.path}
114
- meta={hookOptionsFile.meta}
115
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: hookOptionsFile.path, baseName: hookOptionsFile.baseName } })}
116
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: hookOptionsFile.path, baseName: hookOptionsFile.baseName } })}
117
- >
118
- {imports}
119
- <File.Source name={name} isExportable isIndexable isTypeOnly>
120
- <Type export name={name}>
121
- {`{ ${Object.keys(hookOptions)
122
- .map((key) => `${JSON.stringify(key)}: ${hookOptions[key]}`)
123
- .join(', ')} }`}
124
- </Type>
125
- </File.Source>
126
- </File>
127
- )
128
- },
129
- })
@@ -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,200 +0,0 @@
1
- import path from 'node:path'
2
- import { getOperationParameters, operationFileEntry, resolveOperationTypeNames } from '@internals/shared'
3
- import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
- import { ast, defineGenerator } from '@kubb/core'
5
- import { Client, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRendererSync } from '@kubb/renderer-jsx'
9
- import { InfiniteQuery, InfiniteQueryOptions, QueryKey } from '../components'
10
- import type { PluginReactQuery } from '../types'
11
-
12
- /**
13
- * Built-in generator for `useInfiniteQuery` hooks. Enabled when
14
- * `pluginReactQuery({ infinite: { ... } })`. Emits one `useFooInfiniteQuery`
15
- * hook per query operation, wiring the configured `nextParam` /
16
- * `previousParam` paths into TanStack Query's cursor-based pagination.
17
- */
18
- export const infiniteQueryGenerator = defineGenerator<PluginReactQuery>({
19
- name: 'react-infinite-query',
20
- renderer: jsxRendererSync,
21
- operation(node, ctx) {
22
- if (!ast.isHttpOperationNode(node)) return null
23
- const { config, driver, resolver, root } = ctx
24
- const { output, query, mutation, infinite, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group, customOptions } = ctx.options
25
-
26
- const pluginTs = driver.getPlugin(pluginTsName)
27
- if (!pluginTs) return null
28
- const tsResolver = driver.getResolver(pluginTsName)
29
-
30
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
31
- const queryMethods = new Set(query ? query.methods : [])
32
- const isMutation =
33
- mutation !== false &&
34
- !isQuery &&
35
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
36
- const infiniteOptions = infinite && typeof infinite === 'object' ? infinite : null
37
-
38
- if (!isQuery || isMutation || !infiniteOptions) return null
39
-
40
- // Validate queryParam exists in operation's query parameters
41
- const normalizeKey = (key: string) => key.replace(/\?$/, '')
42
- const queryParamKeys = getOperationParameters(node).query.map((p) => p.name)
43
- const hasQueryParam = infiniteOptions.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === infiniteOptions.queryParam) : false
44
- // cursorParam validation against response schema keys is skipped in v5 (complex schema inspection)
45
- const hasCursorParam = !infiniteOptions.cursorParam || true
46
-
47
- if (!hasQueryParam || !hasCursorParam) return null
48
-
49
- const importPath = query ? query.importPath : '@tanstack/react-query'
50
-
51
- const queryName = resolver.resolveInfiniteQueryName(node)
52
- const queryOptionsName = resolver.resolveInfiniteQueryOptionsName(node)
53
- const queryKeyName = resolver.resolveInfiniteQueryKeyName(node)
54
- const queryKeyTypeName = resolver.resolveInfiniteQueryKeyTypeName(node)
55
- const clientBaseName = resolver.resolveInfiniteClientName(node)
56
-
57
- const meta = {
58
- file: resolver.resolveFile(operationFileEntry(node, queryName), { root, output, group: group ?? undefined }),
59
- fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
60
- root,
61
- output: pluginTs.options?.output ?? output,
62
- group: pluginTs.options?.group ?? undefined,
63
- }),
64
- }
65
-
66
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, {
67
- paramsCasing,
68
- exclude: [queryKeyTypeName],
69
- order: 'body-response-first',
70
- })
71
-
72
- const pluginZod = parser === 'zod' ? driver.getPlugin(pluginZodName) : null
73
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
74
- const fileZod = zodResolver
75
- ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
76
- root,
77
- output: pluginZod?.options?.output ?? output,
78
- group: pluginZod?.options?.group ?? undefined,
79
- })
80
- : null
81
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver)
82
-
83
- const clientPlugin = driver.getPlugin(pluginClientName)
84
- const hasClientPlugin = clientPlugin?.name === pluginClientName
85
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
86
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : null
87
-
88
- const clientFile = shouldUseClientPlugin
89
- ? clientResolver?.resolveFile(operationFileEntry(node, node.operationId), {
90
- root,
91
- output: clientPlugin?.options?.output ?? output,
92
- group: clientPlugin?.options?.group ?? undefined,
93
- })
94
- : null
95
-
96
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientBaseName) : clientBaseName
97
-
98
- return (
99
- <File
100
- baseName={meta.file.baseName}
101
- path={meta.file.path}
102
- meta={meta.file.meta}
103
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
104
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
105
- >
106
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
107
- {clientOptions.importPath ? (
108
- <>
109
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
110
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
111
- {clientOptions.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={clientOptions.importPath} isTypeOnly />}
112
- </>
113
- ) : (
114
- <>
115
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
116
- <File.Import
117
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
118
- root={meta.file.path}
119
- path={path.resolve(root, '.kubb/client.ts')}
120
- isTypeOnly
121
- />
122
- {clientOptions.dataReturnType === 'full' && (
123
- <File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
124
- )}
125
- </>
126
- )}
127
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
128
- {!shouldUseClientPlugin && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
129
- {customOptions && <File.Import name={[customOptions.name]} path={customOptions.importPath} />}
130
- {meta.fileTs && importedTypeNames.length > 0 && (
131
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
132
- )}
133
-
134
- <QueryKey
135
- name={queryKeyName}
136
- typeName={queryKeyTypeName}
137
- node={node}
138
- tsResolver={tsResolver}
139
- pathParamsType={pathParamsType}
140
- paramsCasing={paramsCasing}
141
- transformer={ctx.options.queryKey}
142
- />
143
-
144
- {!shouldUseClientPlugin && (
145
- <Client
146
- name={resolvedClientName}
147
- baseURL={clientOptions.baseURL}
148
- dataReturnType={clientOptions.dataReturnType || 'data'}
149
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
150
- paramsType={paramsType}
151
- pathParamsType={pathParamsType}
152
- parser={parser}
153
- node={node}
154
- tsResolver={tsResolver}
155
- zodResolver={zodResolver}
156
- />
157
- )}
158
-
159
- <File.Import name={['InfiniteData']} isTypeOnly path={importPath} />
160
- <File.Import name={['infiniteQueryOptions']} path={importPath} />
161
-
162
- <InfiniteQueryOptions
163
- name={queryOptionsName}
164
- clientName={resolvedClientName}
165
- queryKeyName={queryKeyName}
166
- node={node}
167
- tsResolver={tsResolver}
168
- paramsCasing={paramsCasing}
169
- paramsType={paramsType}
170
- pathParamsType={pathParamsType}
171
- dataReturnType={clientOptions.dataReturnType || 'data'}
172
- cursorParam={infiniteOptions.cursorParam}
173
- nextParam={infiniteOptions.nextParam}
174
- previousParam={infiniteOptions.previousParam}
175
- initialPageParam={infiniteOptions.initialPageParam}
176
- queryParam={infiniteOptions.queryParam}
177
- />
178
-
179
- <File.Import name={['useInfiniteQuery']} path={importPath} />
180
- <File.Import name={['QueryKey', 'QueryClient', 'InfiniteQueryObserverOptions', 'UseInfiniteQueryResult']} path={importPath} isTypeOnly />
181
-
182
- <InfiniteQuery
183
- name={queryName}
184
- queryOptionsName={queryOptionsName}
185
- queryKeyName={queryKeyName}
186
- queryKeyTypeName={queryKeyTypeName}
187
- node={node}
188
- tsResolver={tsResolver}
189
- paramsCasing={paramsCasing}
190
- paramsType={paramsType}
191
- pathParamsType={pathParamsType}
192
- dataReturnType={clientOptions.dataReturnType || 'data'}
193
- initialPageParam={infiniteOptions.initialPageParam}
194
- queryParam={infiniteOptions.queryParam}
195
- customOptions={customOptions}
196
- />
197
- </File>
198
- )
199
- },
200
- })
@@ -1,173 +0,0 @@
1
- import path from 'node:path'
2
- import { operationFileEntry, resolveOperationTypeNames } from '@internals/shared'
3
- import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
- import { ast, defineGenerator } from '@kubb/core'
5
- import { Client, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRendererSync } from '@kubb/renderer-jsx'
9
- import { Mutation, MutationKey, MutationOptions } from '../components'
10
- import type { PluginReactQuery } from '../types'
11
-
12
- /**
13
- * Built-in generator for `useMutation` hooks. Emits one `useFooMutation` hook
14
- * per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
15
- * the matching `fooMutationKey` / `fooMutationOptions` helpers.
16
- */
17
- export const mutationGenerator = defineGenerator<PluginReactQuery>({
18
- name: 'react-query-mutation',
19
- renderer: jsxRendererSync,
20
- operation(node, ctx) {
21
- if (!ast.isHttpOperationNode(node)) return null
22
- const { config, driver, resolver, root } = ctx
23
- const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group, customOptions } = ctx.options
24
-
25
- const pluginTs = driver.getPlugin(pluginTsName)
26
- if (!pluginTs) return null
27
- const tsResolver = driver.getResolver(pluginTsName)
28
-
29
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
30
- const queryMethods = new Set(query ? query.methods : [])
31
- const isMutation =
32
- mutation !== false &&
33
- !isQuery &&
34
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
35
-
36
- if (!isMutation) return null
37
-
38
- const importPath = mutation ? mutation.importPath : '@tanstack/react-query'
39
-
40
- const mutationHookName = resolver.resolveMutationName(node)
41
- const mutationTypeName = resolver.resolveMutationTypeName(node)
42
- const mutationOptionsName = resolver.resolveMutationOptionsName(node)
43
- const mutationKeyName = resolver.resolveMutationKeyName(node)
44
- const clientName = resolver.resolveClientName(node)
45
-
46
- const meta = {
47
- file: resolver.resolveFile(operationFileEntry(node, mutationHookName), { root, output, group: group ?? undefined }),
48
- fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
49
- root,
50
- output: pluginTs.options?.output ?? output,
51
- group: pluginTs.options?.group ?? undefined,
52
- }),
53
- }
54
-
55
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, order: 'body-response-first' })
56
-
57
- const pluginZod = parser === 'zod' ? driver.getPlugin(pluginZodName) : null
58
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
59
- const fileZod = zodResolver
60
- ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
61
- root,
62
- output: pluginZod?.options?.output ?? output,
63
- group: pluginZod?.options?.group ?? undefined,
64
- })
65
- : null
66
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver)
67
-
68
- const clientPlugin = driver.getPlugin(pluginClientName)
69
- const hasClientPlugin = clientPlugin?.name === pluginClientName
70
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
71
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : null
72
-
73
- const clientFile = shouldUseClientPlugin
74
- ? clientResolver?.resolveFile(operationFileEntry(node, node.operationId), {
75
- root,
76
- output: clientPlugin?.options?.output ?? output,
77
- group: clientPlugin?.options?.group ?? undefined,
78
- })
79
- : null
80
-
81
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
82
-
83
- return (
84
- <File
85
- baseName={meta.file.baseName}
86
- path={meta.file.path}
87
- meta={meta.file.meta}
88
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
89
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
90
- >
91
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
92
- {clientOptions.importPath ? (
93
- <>
94
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
95
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
96
- {clientOptions.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={clientOptions.importPath} isTypeOnly />}
97
- </>
98
- ) : (
99
- <>
100
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
101
- <File.Import
102
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
103
- root={meta.file.path}
104
- path={path.resolve(root, '.kubb/client.ts')}
105
- isTypeOnly
106
- />
107
- {clientOptions.dataReturnType === 'full' && (
108
- <File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
109
- )}
110
- </>
111
- )}
112
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
113
- {!shouldUseClientPlugin && node.requestBody?.content?.some((e) => e.contentType === 'multipart/form-data') && (
114
- <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />
115
- )}
116
- {customOptions && <File.Import name={[customOptions.name]} path={customOptions.importPath} />}
117
- {meta.fileTs && importedTypeNames.length > 0 && (
118
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
119
- )}
120
-
121
- <MutationKey name={mutationKeyName} node={node} pathParamsType={pathParamsType} paramsCasing={paramsCasing} transformer={ctx.options.mutationKey} />
122
-
123
- {!shouldUseClientPlugin && (
124
- <Client
125
- name={resolvedClientName}
126
- baseURL={clientOptions.baseURL}
127
- dataReturnType={clientOptions.dataReturnType || 'data'}
128
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
129
- paramsType={paramsType}
130
- pathParamsType={pathParamsType}
131
- parser={parser}
132
- node={node}
133
- tsResolver={tsResolver}
134
- zodResolver={zodResolver}
135
- />
136
- )}
137
-
138
- <File.Import name={['mutationOptions']} path={importPath} />
139
-
140
- <MutationOptions
141
- name={mutationOptionsName}
142
- clientName={resolvedClientName}
143
- mutationKeyName={mutationKeyName}
144
- node={node}
145
- tsResolver={tsResolver}
146
- paramsCasing={paramsCasing}
147
- paramsType={paramsType}
148
- pathParamsType={pathParamsType}
149
- dataReturnType={clientOptions.dataReturnType || 'data'}
150
- />
151
-
152
- {mutation && (
153
- <>
154
- <File.Import name={['useMutation']} path={importPath} />
155
- <File.Import name={['UseMutationOptions', 'UseMutationResult', 'QueryClient']} path={importPath} isTypeOnly />
156
- <Mutation
157
- name={mutationHookName}
158
- typeName={mutationTypeName}
159
- mutationOptionsName={mutationOptionsName}
160
- mutationKeyName={mutationKeyName}
161
- node={node}
162
- tsResolver={tsResolver}
163
- dataReturnType={clientOptions.dataReturnType || 'data'}
164
- paramsCasing={paramsCasing}
165
- pathParamsType={pathParamsType}
166
- customOptions={customOptions}
167
- />
168
- </>
169
- )}
170
- </File>
171
- )
172
- },
173
- })