@kubb/plugin-react-query 5.0.0-beta.56 → 5.0.0-beta.73

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 (48) hide show
  1. package/README.md +2 -2
  2. package/dist/index.cjs +2521 -59
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +521 -3
  5. package/dist/index.js +2488 -49
  6. package/dist/index.js.map +1 -1
  7. package/package.json +10 -20
  8. package/dist/components-DL0Cai7l.js +0 -1507
  9. package/dist/components-DL0Cai7l.js.map +0 -1
  10. package/dist/components-yMQOuFmI.cjs +0 -1657
  11. package/dist/components-yMQOuFmI.cjs.map +0 -1
  12. package/dist/components.cjs +0 -13
  13. package/dist/components.d.ts +0 -270
  14. package/dist/components.js +0 -2
  15. package/dist/generators-BG-Vcvfg.js +0 -1307
  16. package/dist/generators-BG-Vcvfg.js.map +0 -1
  17. package/dist/generators-zGKP8yII.cjs +0 -1349
  18. package/dist/generators-zGKP8yII.cjs.map +0 -1
  19. package/dist/generators.cjs +0 -9
  20. package/dist/generators.d.ts +0 -66
  21. package/dist/generators.js +0 -2
  22. package/dist/types-X7D0NSvJ.d.ts +0 -396
  23. package/src/components/InfiniteQuery.tsx +0 -166
  24. package/src/components/InfiniteQueryOptions.tsx +0 -166
  25. package/src/components/Mutation.tsx +0 -122
  26. package/src/components/MutationKey.tsx +0 -1
  27. package/src/components/MutationOptions.tsx +0 -104
  28. package/src/components/Query.tsx +0 -124
  29. package/src/components/QueryKey.tsx +0 -1
  30. package/src/components/QueryOptions.tsx +0 -85
  31. package/src/components/SuspenseInfiniteQuery.tsx +0 -161
  32. package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -162
  33. package/src/components/SuspenseQuery.tsx +0 -119
  34. package/src/components/index.ts +0 -11
  35. package/src/generators/customHookOptionsFileGenerator.tsx +0 -90
  36. package/src/generators/hookOptionsGenerator.tsx +0 -129
  37. package/src/generators/index.ts +0 -7
  38. package/src/generators/infiniteQueryGenerator.tsx +0 -196
  39. package/src/generators/mutationGenerator.tsx +0 -172
  40. package/src/generators/queryGenerator.tsx +0 -181
  41. package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -205
  42. package/src/generators/suspenseQueryGenerator.tsx +0 -184
  43. package/src/index.ts +0 -2
  44. package/src/plugin.ts +0 -203
  45. package/src/resolvers/resolverReactQuery.ts +0 -118
  46. package/src/types.ts +0 -361
  47. package/src/utils.ts +0 -18
  48. /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,205 +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, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
9
- import { QueryKey, SuspenseInfiniteQuery, SuspenseInfiniteQueryOptions } from '../components'
10
- import type { PluginReactQuery } from '../types'
11
-
12
- /**
13
- * Built-in generator for `useSuspenseInfiniteQuery` hooks. Enabled when both
14
- * `suspense` and `infinite` are configured. Combines suspense semantics with
15
- * cursor-based pagination — handlers throw promises while loading and pull
16
- * additional pages on demand.
17
- */
18
- export const suspenseInfiniteQueryGenerator = defineGenerator<PluginReactQuery>({
19
- name: 'react-suspense-infinite-query',
20
- renderer: jsxRenderer,
21
- operation(node, ctx) {
22
- if (!ast.isHttpOperationNode(node)) return null
23
- const { config, driver, resolver, root } = ctx
24
- const {
25
- output,
26
- query,
27
- mutation,
28
- infinite,
29
- suspense,
30
- paramsCasing,
31
- paramsType,
32
- pathParamsType,
33
- parser,
34
- client: clientOptions,
35
- group,
36
- customOptions,
37
- } = ctx.options
38
-
39
- const pluginTs = driver.getPlugin(pluginTsName)
40
- if (!pluginTs) return null
41
- const tsResolver = driver.getResolver(pluginTsName)
42
-
43
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
44
- const queryMethods = new Set(query ? query.methods : [])
45
- const isMutation =
46
- mutation !== false &&
47
- !isQuery &&
48
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
49
- const isSuspense = !!suspense
50
- const infiniteOptions = infinite && typeof infinite === 'object' ? infinite : null
51
-
52
- if (!isQuery || isMutation || !isSuspense || !infiniteOptions) return null
53
-
54
- // Validate queryParam exists in operation's query parameters
55
- const normalizeKey = (key: string) => key.replace(/\?$/, '')
56
- const queryParamKeys = getOperationParameters(node).query.map((p) => p.name)
57
- const hasQueryParam = infiniteOptions.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === infiniteOptions.queryParam) : false
58
- const hasCursorParam = !infiniteOptions.cursorParam || true
59
-
60
- if (!hasQueryParam || !hasCursorParam) return null
61
-
62
- const importPath = query ? query.importPath : '@tanstack/react-query'
63
-
64
- const queryName = resolver.resolveSuspenseInfiniteQueryName(node)
65
- const queryOptionsName = resolver.resolveSuspenseInfiniteQueryOptionsName(node)
66
- const queryKeyName = resolver.resolveSuspenseInfiniteQueryKeyName(node)
67
- const queryKeyTypeName = resolver.resolveSuspenseInfiniteQueryKeyTypeName(node)
68
- const clientBaseName = resolver.resolveSuspenseInfiniteClientName(node)
69
-
70
- const meta = {
71
- file: resolver.resolveFile(operationFileEntry(node, queryName), { root, output, group: group ?? undefined }),
72
- fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
73
- root,
74
- output: pluginTs.options?.output ?? output,
75
- group: pluginTs.options?.group ?? undefined,
76
- }),
77
- }
78
-
79
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, order: 'body-response-first' })
80
-
81
- const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : null
82
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
83
- const fileZod = zodResolver
84
- ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
85
- root,
86
- output: pluginZod?.options?.output ?? output,
87
- group: pluginZod?.options?.group ?? undefined,
88
- })
89
- : null
90
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
91
-
92
- const clientPlugin = driver.getPlugin(pluginClientName)
93
- const hasClientPlugin = clientPlugin?.name === pluginClientName
94
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
95
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : null
96
-
97
- const clientFile = shouldUseClientPlugin
98
- ? clientResolver?.resolveFile(operationFileEntry(node, node.operationId), {
99
- root,
100
- output: clientPlugin?.options?.output ?? output,
101
- group: clientPlugin?.options?.group ?? undefined,
102
- })
103
- : null
104
-
105
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientBaseName) : clientBaseName
106
-
107
- return (
108
- <File
109
- baseName={meta.file.baseName}
110
- path={meta.file.path}
111
- meta={meta.file.meta}
112
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
113
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
114
- >
115
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
116
- {clientOptions.importPath ? (
117
- <>
118
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
119
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
120
- </>
121
- ) : (
122
- <>
123
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
124
- <File.Import
125
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
126
- root={meta.file.path}
127
- path={path.resolve(root, '.kubb/client.ts')}
128
- isTypeOnly
129
- />
130
- </>
131
- )}
132
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
133
- {!shouldUseClientPlugin && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
134
- {customOptions && <File.Import name={[customOptions.name]} path={customOptions.importPath} />}
135
- {meta.fileTs && importedTypeNames.length > 0 && (
136
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
137
- )}
138
-
139
- <QueryKey
140
- name={queryKeyName}
141
- typeName={queryKeyTypeName}
142
- node={node}
143
- tsResolver={tsResolver}
144
- pathParamsType={pathParamsType}
145
- paramsCasing={paramsCasing}
146
- transformer={ctx.options.queryKey}
147
- />
148
-
149
- {!shouldUseClientPlugin && (
150
- <Client
151
- name={resolvedClientName}
152
- baseURL={clientOptions.baseURL}
153
- dataReturnType={clientOptions.dataReturnType || 'data'}
154
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
155
- paramsType={paramsType}
156
- pathParamsType={pathParamsType}
157
- parser={parser}
158
- node={node}
159
- tsResolver={tsResolver}
160
- zodResolver={zodResolver}
161
- />
162
- )}
163
-
164
- <File.Import name={['InfiniteData']} isTypeOnly path={importPath} />
165
- <File.Import name={['infiniteQueryOptions']} path={importPath} />
166
-
167
- <SuspenseInfiniteQueryOptions
168
- name={queryOptionsName}
169
- clientName={resolvedClientName}
170
- queryKeyName={queryKeyName}
171
- node={node}
172
- tsResolver={tsResolver}
173
- paramsCasing={paramsCasing}
174
- paramsType={paramsType}
175
- pathParamsType={pathParamsType}
176
- dataReturnType={clientOptions.dataReturnType || 'data'}
177
- cursorParam={infiniteOptions.cursorParam}
178
- nextParam={infiniteOptions.nextParam}
179
- previousParam={infiniteOptions.previousParam}
180
- initialPageParam={infiniteOptions.initialPageParam}
181
- queryParam={infiniteOptions.queryParam}
182
- />
183
-
184
- <File.Import name={['useSuspenseInfiniteQuery']} path={importPath} />
185
- <File.Import name={['QueryKey', 'QueryClient', 'UseSuspenseInfiniteQueryOptions', 'UseSuspenseInfiniteQueryResult']} path={importPath} isTypeOnly />
186
-
187
- <SuspenseInfiniteQuery
188
- name={queryName}
189
- queryOptionsName={queryOptionsName}
190
- queryKeyName={queryKeyName}
191
- queryKeyTypeName={queryKeyTypeName}
192
- node={node}
193
- tsResolver={tsResolver}
194
- paramsCasing={paramsCasing}
195
- paramsType={paramsType}
196
- pathParamsType={pathParamsType}
197
- dataReturnType={clientOptions.dataReturnType || 'data'}
198
- initialPageParam={infiniteOptions.initialPageParam}
199
- queryParam={infiniteOptions.queryParam}
200
- customOptions={customOptions}
201
- />
202
- </File>
203
- )
204
- },
205
- })
@@ -1,184 +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, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
9
- import { QueryKey, QueryOptions, SuspenseQuery } from '../components'
10
- import type { PluginReactQuery } from '../types'
11
-
12
- /**
13
- * Built-in generator for `useSuspenseQuery` hooks. Enabled when
14
- * `pluginReactQuery({ suspense: {} })`. Emits one `useFooSuspenseQuery` hook
15
- * per query operation. Suspense queries throw promises while loading and
16
- * require a `<Suspense>` boundary in the React tree. TanStack Query v5+ only.
17
- */
18
- export const suspenseQueryGenerator = defineGenerator<PluginReactQuery>({
19
- name: 'react-suspense-query',
20
- renderer: jsxRenderer,
21
- operation(node, ctx) {
22
- if (!ast.isHttpOperationNode(node)) return null
23
- const { config, driver, resolver, root } = ctx
24
- const { output, query, mutation, suspense, 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
- // query: false means "this IS a query op" (suspense hooks still generate)
31
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
32
- const queryMethods = new Set(query ? query.methods : [])
33
- const isMutation =
34
- mutation !== false &&
35
- !isQuery &&
36
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
37
- const isSuspense = !!suspense
38
-
39
- if (!isQuery || isMutation || !isSuspense) return null
40
-
41
- const importPath = query ? query.importPath : '@tanstack/react-query'
42
-
43
- const queryName = resolver.resolveSuspenseQueryName(node)
44
- const queryOptionsName = resolver.resolveSuspenseQueryOptionsName(node)
45
- const queryKeyName = resolver.resolveSuspenseQueryKeyName(node)
46
- const queryKeyTypeName = resolver.resolveSuspenseQueryKeyTypeName(node)
47
- const clientName = resolver.resolveSuspenseClientName(node)
48
-
49
- const meta = {
50
- file: resolver.resolveFile(operationFileEntry(node, queryName), { root, output, group: group ?? undefined }),
51
- fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
52
- root,
53
- output: pluginTs.options?.output ?? output,
54
- group: pluginTs.options?.group ?? undefined,
55
- }),
56
- }
57
-
58
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, {
59
- paramsCasing,
60
- exclude: [queryKeyTypeName],
61
- order: 'body-response-first',
62
- })
63
-
64
- const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : null
65
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
66
- const fileZod = zodResolver
67
- ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
68
- root,
69
- output: pluginZod?.options?.output ?? output,
70
- group: pluginZod?.options?.group ?? undefined,
71
- })
72
- : null
73
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
74
-
75
- const clientPlugin = driver.getPlugin(pluginClientName)
76
- const hasClientPlugin = clientPlugin?.name === pluginClientName
77
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
78
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : null
79
-
80
- const clientFile = shouldUseClientPlugin
81
- ? clientResolver?.resolveFile(operationFileEntry(node, node.operationId), {
82
- root,
83
- output: clientPlugin?.options?.output ?? output,
84
- group: clientPlugin?.options?.group ?? undefined,
85
- })
86
- : null
87
-
88
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
89
-
90
- return (
91
- <File
92
- baseName={meta.file.baseName}
93
- path={meta.file.path}
94
- meta={meta.file.meta}
95
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
96
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
97
- >
98
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
99
- {clientOptions.importPath ? (
100
- <>
101
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
102
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
103
- </>
104
- ) : (
105
- <>
106
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
107
- <File.Import
108
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
109
- root={meta.file.path}
110
- path={path.resolve(root, '.kubb/client.ts')}
111
- isTypeOnly
112
- />
113
- </>
114
- )}
115
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
116
- {!shouldUseClientPlugin && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
117
- {customOptions && <File.Import name={[customOptions.name]} path={customOptions.importPath} />}
118
- {meta.fileTs && importedTypeNames.length > 0 && (
119
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
120
- )}
121
-
122
- <QueryKey
123
- name={queryKeyName}
124
- typeName={queryKeyTypeName}
125
- node={node}
126
- tsResolver={tsResolver}
127
- pathParamsType={pathParamsType}
128
- paramsCasing={paramsCasing}
129
- transformer={ctx.options.queryKey}
130
- />
131
-
132
- {!shouldUseClientPlugin && (
133
- <Client
134
- name={resolvedClientName}
135
- baseURL={clientOptions.baseURL}
136
- dataReturnType={clientOptions.dataReturnType || 'data'}
137
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
138
- paramsType={paramsType}
139
- pathParamsType={pathParamsType}
140
- parser={parser}
141
- node={node}
142
- tsResolver={tsResolver}
143
- zodResolver={zodResolver}
144
- />
145
- )}
146
-
147
- <File.Import name={['queryOptions']} path={importPath} />
148
-
149
- <QueryOptions
150
- name={queryOptionsName}
151
- clientName={resolvedClientName}
152
- queryKeyName={queryKeyName}
153
- node={node}
154
- tsResolver={tsResolver}
155
- paramsCasing={paramsCasing}
156
- paramsType={paramsType}
157
- pathParamsType={pathParamsType}
158
- dataReturnType={clientOptions.dataReturnType || 'data'}
159
- suspense
160
- />
161
-
162
- {suspense && (
163
- <>
164
- <File.Import name={['useSuspenseQuery']} path={importPath} />
165
- <File.Import name={['QueryKey', 'QueryClient', 'UseSuspenseQueryOptions', 'UseSuspenseQueryResult']} path={importPath} isTypeOnly />
166
- <SuspenseQuery
167
- name={queryName}
168
- queryOptionsName={queryOptionsName}
169
- queryKeyName={queryKeyName}
170
- queryKeyTypeName={queryKeyTypeName}
171
- node={node}
172
- tsResolver={tsResolver}
173
- paramsCasing={paramsCasing}
174
- paramsType={paramsType}
175
- pathParamsType={pathParamsType}
176
- dataReturnType={clientOptions.dataReturnType || 'data'}
177
- customOptions={customOptions}
178
- />
179
- </>
180
- )}
181
- </File>
182
- )
183
- },
184
- })
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { default, pluginReactQuery, pluginReactQueryName } from './plugin.ts'
2
- export type { PluginReactQuery } from './types.ts'
package/src/plugin.ts DELETED
@@ -1,203 +0,0 @@
1
- import path from 'node:path'
2
- import { createGroupConfig } from '@internals/shared'
3
- import { ast, definePlugin } from '@kubb/core'
4
- import { isParserEnabled, pluginClientName } from '@kubb/plugin-client'
5
- import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
6
- import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
7
- import { source as configSource } from '@kubb/plugin-client/templates/config.source'
8
- import { pluginTsName } from '@kubb/plugin-ts'
9
- import { pluginZodName } from '@kubb/plugin-zod'
10
- import { mutationKeyTransformer, queryKeyTransformer } from '@internals/tanstack-query'
11
- import {
12
- customHookOptionsFileGenerator,
13
- hookOptionsGenerator,
14
- infiniteQueryGenerator,
15
- mutationGenerator,
16
- queryGenerator,
17
- suspenseInfiniteQueryGenerator,
18
- suspenseQueryGenerator,
19
- } from './generators'
20
- import { resolverReactQuery } from './resolvers/resolverReactQuery.ts'
21
- import type { PluginReactQuery } from './types.ts'
22
-
23
- /**
24
- * Canonical plugin name for `@kubb/plugin-react-query`. Used for driver lookups
25
- * and cross-plugin dependency references.
26
- */
27
- export const pluginReactQueryName = 'plugin-react-query' satisfies PluginReactQuery['name']
28
-
29
- /**
30
- * Generates one TanStack Query hook per OpenAPI operation for React. Queries
31
- * become `useFooQuery`/`useFooSuspenseQuery`/`useFooInfiniteQuery`; mutations
32
- * become `useFooMutation`. Each hook is fully typed: query keys, input
33
- * variables, response data, and error shape all come from the spec.
34
- *
35
- * @example
36
- * ```ts
37
- * import { defineConfig } from 'kubb'
38
- * import { pluginTs } from '@kubb/plugin-ts'
39
- * import { pluginReactQuery } from '@kubb/plugin-react-query'
40
- *
41
- * export default defineConfig({
42
- * input: { path: './petStore.yaml' },
43
- * output: { path: './src/gen' },
44
- * plugins: [
45
- * pluginTs(),
46
- * pluginReactQuery({
47
- * output: { path: './hooks' },
48
- * suspense: {},
49
- * }),
50
- * ],
51
- * })
52
- * ```
53
- */
54
- export const pluginReactQuery = definePlugin<PluginReactQuery>((options) => {
55
- const {
56
- output = { path: 'hooks', barrel: { type: 'named' } },
57
- group,
58
- exclude = [],
59
- include,
60
- override = [],
61
- parser = false,
62
- suspense = {},
63
- infinite = false,
64
- paramsType = 'inline',
65
- pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
66
- mutation = {},
67
- query = {},
68
- mutationKey = mutationKeyTransformer,
69
- queryKey = queryKeyTransformer,
70
- customOptions,
71
- paramsCasing,
72
- client,
73
- resolver: userResolver,
74
- transformer: userTransformer,
75
- generators: userGenerators = [],
76
- } = options
77
-
78
- const clientName = client?.client ?? 'axios'
79
- const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
80
-
81
- const selectedGenerators =
82
- options.generators ??
83
- [
84
- queryGenerator,
85
- suspenseQueryGenerator,
86
- infiniteQueryGenerator,
87
- suspenseInfiniteQueryGenerator,
88
- mutationGenerator,
89
- hookOptionsGenerator,
90
- customHookOptionsFileGenerator,
91
- ].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))
92
-
93
- const groupConfig = createGroupConfig(group)
94
-
95
- return {
96
- name: pluginReactQueryName,
97
- options,
98
- dependencies: [pluginTsName, isParserEnabled(parser) ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),
99
- hooks: {
100
- 'kubb:plugin:setup'(ctx) {
101
- const resolver = userResolver ? { ...resolverReactQuery, ...userResolver } : resolverReactQuery
102
-
103
- ctx.setOptions({
104
- output,
105
- client: {
106
- bundle: client?.bundle,
107
- baseURL: client?.baseURL,
108
- client: clientName,
109
- clientType: client?.clientType ?? 'function',
110
- importPath: clientImportPath,
111
- dataReturnType: client?.dataReturnType ?? 'data',
112
- paramsCasing,
113
- },
114
- queryKey,
115
- query:
116
- query === false
117
- ? false
118
- : {
119
- importPath: '@tanstack/react-query',
120
- methods: ['get'],
121
- ...query,
122
- },
123
- mutationKey,
124
- mutation:
125
- mutation === false
126
- ? false
127
- : {
128
- importPath: '@tanstack/react-query',
129
- methods: ['post', 'put', 'patch', 'delete'],
130
- ...mutation,
131
- },
132
- infinite: infinite
133
- ? {
134
- queryParam: 'id',
135
- initialPageParam: 0,
136
- cursorParam: null,
137
- nextParam: null,
138
- previousParam: null,
139
- ...infinite,
140
- }
141
- : false,
142
- suspense,
143
- customOptions: customOptions ? { name: 'useCustomHookOptions', ...customOptions } : null,
144
- parser,
145
- paramsType,
146
- pathParamsType,
147
- paramsCasing,
148
- group: groupConfig,
149
- exclude,
150
- include,
151
- override,
152
- resolver,
153
- })
154
- ctx.setResolver(resolver)
155
- if (userTransformer) {
156
- ctx.setTransformer(userTransformer)
157
- }
158
-
159
- for (const gen of selectedGenerators) {
160
- ctx.addGenerator(gen)
161
- }
162
- for (const gen of userGenerators) {
163
- ctx.addGenerator(gen)
164
- }
165
-
166
- const root = path.resolve(ctx.config.root, ctx.config.output.path)
167
- const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)
168
-
169
- if (client?.bundle && !hasClientPlugin && !clientImportPath) {
170
- ctx.injectFile({
171
- baseName: 'client.ts',
172
- path: path.resolve(root, '.kubb/client.ts'),
173
- sources: [
174
- ast.createSource({
175
- name: 'client',
176
- nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],
177
- isExportable: true,
178
- isIndexable: true,
179
- }),
180
- ],
181
- })
182
- }
183
-
184
- if (!hasClientPlugin) {
185
- ctx.injectFile({
186
- baseName: 'config.ts',
187
- path: path.resolve(root, '.kubb/config.ts'),
188
- sources: [
189
- ast.createSource({
190
- name: 'config',
191
- nodes: [ast.createText(configSource)],
192
- isExportable: false,
193
- isIndexable: false,
194
- }),
195
- ],
196
- })
197
- }
198
- },
199
- },
200
- }
201
- })
202
-
203
- export default pluginReactQuery