@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,166 +0,0 @@
1
- import { getOperationParameters } from '@internals/shared'
2
- import { getNestedAccessor } from '@kubb/ast/utils'
3
- import type { ast } from '@kubb/core'
4
- import type { ResolverTs } from '@kubb/plugin-ts'
5
- import { functionPrinter } from '@kubb/plugin-ts'
6
- import { File, Function } from '@kubb/renderer-jsx'
7
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
8
- import type { Infinite, PluginReactQuery } from '../types.ts'
9
- import { buildQueryKeyParams, buildStatusUnionType, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
10
- import { getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
11
- import { getQueryOptionsParams } from './QueryOptions.tsx'
12
-
13
- type Props = {
14
- name: string
15
- clientName: string
16
- queryKeyName: string
17
- node: ast.OperationNode
18
- tsResolver: ResolverTs
19
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
20
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
21
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
22
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
23
- initialPageParam: Infinite['initialPageParam']
24
- cursorParam: Infinite['cursorParam']
25
- nextParam: Infinite['nextParam']
26
- previousParam: Infinite['previousParam']
27
- queryParam: Infinite['queryParam']
28
- }
29
-
30
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
31
- const callPrinter = functionPrinter({ mode: 'call' })
32
-
33
- export function InfiniteQueryOptions({
34
- name,
35
- clientName,
36
- initialPageParam,
37
- cursorParam,
38
- nextParam,
39
- previousParam,
40
- node,
41
- tsResolver,
42
- paramsCasing,
43
- paramsType,
44
- dataReturnType,
45
- pathParamsType,
46
- queryParam,
47
- queryKeyName,
48
- }: Props): KubbReactNode {
49
- const successNames = resolveSuccessNames(node, tsResolver)
50
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
51
- const queryFnDataType = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
52
- const errorNames = resolveErrorNames(node, tsResolver)
53
- const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
54
-
55
- const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
56
- const fallbackPageParamType =
57
- typeof initialPageParam === 'number'
58
- ? 'number'
59
- : typeof initialPageParam === 'string'
60
- ? initialPageParam.includes(' as ')
61
- ? (() => {
62
- const parts = initialPageParam.split(' as ')
63
- return parts[parts.length - 1] ?? 'unknown'
64
- })()
65
- : 'string'
66
- : typeof initialPageParam === 'boolean'
67
- ? 'boolean'
68
- : 'unknown'
69
-
70
- const rawQueryParams = getOperationParameters(node).query
71
- const queryParamsTypeName =
72
- rawQueryParams.length > 0
73
- ? (() => {
74
- const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
75
- const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
76
- return groupName !== individualName ? groupName : null
77
- })()
78
- : null
79
-
80
- const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null
81
- const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
82
-
83
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
84
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
85
-
86
- const enabledNames = getEnabledParamNames(queryKeyParamsNode)
87
- const enabledText = enabledNames.length ? `enabled: !!(${enabledNames.join(' && ')}),` : ''
88
-
89
- const paramsNode = markParamsOptional(getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver }), enabledNames)
90
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
91
- const rawParamsCall = callPrinter.print(paramsNode) ?? ''
92
- const clientCallStr = injectNonNullAssertions(rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }'), enabledNames)
93
-
94
- const hasNewParams = nextParam != null || previousParam != null
95
-
96
- const [getNextPageParamExpr, getPreviousPageParamExpr] = (() => {
97
- if (hasNewParams) {
98
- const nextAccessor = nextParam ? getNestedAccessor(nextParam, 'lastPage') : null
99
- const prevAccessor = previousParam ? getNestedAccessor(previousParam, 'firstPage') : null
100
- return [
101
- nextAccessor ? `getNextPageParam: (lastPage) => ${nextAccessor}` : null,
102
- prevAccessor ? `getPreviousPageParam: (firstPage) => ${prevAccessor}` : null,
103
- ] as const
104
- }
105
- if (cursorParam) {
106
- return [`getNextPageParam: (lastPage) => lastPage['${cursorParam}']`, `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`] as const
107
- }
108
- return [
109
- dataReturnType === 'full'
110
- ? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
111
- : 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1',
112
- 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1',
113
- ] as const
114
- })()
115
-
116
- const queryOptionsArr = [
117
- `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
118
- getNextPageParamExpr,
119
- getPreviousPageParamExpr,
120
- ].filter(Boolean)
121
-
122
- const infiniteOverrideParams =
123
- queryParam && queryParamsTypeName
124
- ? `params = {
125
- ...(params ?? {}),
126
- ['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
127
- } as ${queryParamsTypeName}`
128
- : ''
129
-
130
- if (infiniteOverrideParams) {
131
- return (
132
- <File.Source name={name} isExportable isIndexable>
133
- <Function name={name} export params={paramsSignature}>
134
- {`
135
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
136
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({${enabledText ? `\n ${enabledText}` : ''}
137
- queryKey,
138
- queryFn: async ({ signal, pageParam }) => {
139
- ${infiniteOverrideParams}
140
- return ${clientName}(${clientCallStr})
141
- },
142
- ${queryOptionsArr.join(',\n ')}
143
- })
144
- `}
145
- </Function>
146
- </File.Source>
147
- )
148
- }
149
-
150
- return (
151
- <File.Source name={name} isExportable isIndexable>
152
- <Function name={name} export params={paramsSignature}>
153
- {`
154
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
155
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({${enabledText ? `\n ${enabledText}` : ''}
156
- queryKey,
157
- queryFn: async ({ signal }) => {
158
- return ${clientName}(${clientCallStr})
159
- },
160
- ${queryOptionsArr.join(',\n ')}
161
- })
162
- `}
163
- </Function>
164
- </File.Source>
165
- )
166
- }
@@ -1,122 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import type { PluginReactQuery } from '../types.ts'
7
- import { buildRequestConfigType, buildStatusUnionType, getComments, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
8
- import { buildMutationConfigParamsNode } from './MutationOptions.tsx'
9
-
10
- type Props = {
11
- name: string
12
- typeName: string
13
- mutationOptionsName: string
14
- mutationKeyName: string
15
- node: ast.OperationNode
16
- tsResolver: ResolverTs
17
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
18
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
19
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
20
- customOptions: PluginReactQuery['resolvedOptions']['customOptions']
21
- }
22
-
23
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
24
- const callPrinter = functionPrinter({ mode: 'call' })
25
-
26
- function createMutationArgParams(
27
- node: ast.OperationNode,
28
- options: {
29
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
30
- resolver: ResolverTs
31
- },
32
- ): ast.FunctionParametersNode {
33
- return ast.createOperationParams(node, {
34
- paramsType: 'inline',
35
- pathParamsType: 'inline',
36
- paramsCasing: options.paramsCasing,
37
- resolver: options.resolver,
38
- })
39
- }
40
-
41
- function buildMutationParamsNode(
42
- node: ast.OperationNode,
43
- options: {
44
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
45
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
46
- resolver: ResolverTs
47
- },
48
- ): ast.FunctionParametersNode {
49
- const { paramsCasing, dataReturnType, resolver } = options
50
- const successNames = resolveSuccessNames(node, resolver)
51
- const responseName = successNames.length > 0 ? successNames.join(' | ') : resolver.resolveResponseName(node)
52
- const errorNames = resolveErrorNames(node, resolver)
53
-
54
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
55
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
56
-
57
- const mutationArgParamsNode = createMutationArgParams(node, {
58
- paramsCasing,
59
- resolver,
60
- })
61
- const TRequest = mutationArgParamsNode.params.length > 0 ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
62
- const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'undefined', 'TContext'].join(', ')
63
-
64
- return ast.createFunctionParameters({
65
- params: [
66
- ast.createFunctionParameter({
67
- name: 'options',
68
- type: ast.createParamsType({
69
- variant: 'reference',
70
- name: `{
71
- mutation?: UseMutationOptions<${generics}> & { client?: QueryClient },
72
- client?: ${buildRequestConfigType(node, resolver)},
73
- }`,
74
- }),
75
- default: '{}',
76
- }),
77
- ],
78
- })
79
- }
80
-
81
- export function Mutation({ name, mutationOptionsName, paramsCasing, dataReturnType, node, tsResolver, mutationKeyName, customOptions }: Props): KubbReactNode {
82
- const successNames = resolveSuccessNames(node, tsResolver)
83
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
84
- const errorNames = resolveErrorNames(node, tsResolver)
85
-
86
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
87
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
88
-
89
- const mutationArgParamsNode = createMutationArgParams(node, {
90
- paramsCasing,
91
- resolver: tsResolver,
92
- })
93
- const TRequest = mutationArgParamsNode.params.length > 0 ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
94
- const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'undefined', 'TContext'].join(', ')
95
- const returnType = `UseMutationResult<${generics}>`
96
-
97
- const mutationOptionsConfigNode = buildMutationConfigParamsNode(node, tsResolver)
98
- const mutationOptionsParamsCall = callPrinter.print(mutationOptionsConfigNode) ?? ''
99
-
100
- const paramsNode = buildMutationParamsNode(node, { paramsCasing, dataReturnType, resolver: tsResolver })
101
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
102
-
103
- return (
104
- <File.Source name={name} isExportable isIndexable>
105
- <Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }} generics={['TContext']}>
106
- {`
107
- const { mutation = {}, client: config = {} } = options ?? {}
108
- const { client: queryClient, ...mutationOptions } = mutation;
109
- const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}()
110
-
111
- const baseOptions = ${mutationOptionsName}(${mutationOptionsParamsCall}) as UseMutationOptions<${generics}>${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' }) as UseMutationOptions<${generics}>` : ''}
112
-
113
- return useMutation<${generics}>({
114
- ...baseOptions,${customOptions ? '\n ...customOptions,' : ''}
115
- mutationKey,
116
- ...mutationOptions,
117
- }, queryClient) as ${returnType}
118
- `}
119
- </Function>
120
- </File.Source>
121
- )
122
- }
@@ -1 +0,0 @@
1
- export { MutationKey } from '@internals/tanstack-query'
@@ -1,104 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import type { PluginReactQuery } from '../types.ts'
7
- import { buildRequestConfigType, buildStatusUnionType, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
8
-
9
- type Props = {
10
- name: string
11
- clientName: string
12
- mutationKeyName: string
13
- node: ast.OperationNode
14
- tsResolver: ResolverTs
15
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
16
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
17
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
18
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
19
- }
20
-
21
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
22
- const callPrinter = functionPrinter({ mode: 'call' })
23
- const keysPrinter = functionPrinter({ mode: 'keys' })
24
-
25
- export function buildMutationConfigParamsNode(node: ast.OperationNode, resolver: ResolverTs): ast.FunctionParametersNode {
26
- return ast.createFunctionParameters({
27
- params: [
28
- ast.createFunctionParameter({
29
- name: 'config',
30
- type: ast.createParamsType({
31
- variant: 'reference',
32
- name: buildRequestConfigType(node, resolver),
33
- }),
34
- default: '{}',
35
- }),
36
- ],
37
- })
38
- }
39
-
40
- export function MutationOptions({
41
- name,
42
- clientName,
43
- dataReturnType,
44
- node,
45
- tsResolver,
46
- paramsCasing,
47
- paramsType,
48
- pathParamsType,
49
- mutationKeyName,
50
- }: Props): KubbReactNode {
51
- const successNames = resolveSuccessNames(node, tsResolver)
52
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
53
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
54
- const errorNames = resolveErrorNames(node, tsResolver)
55
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
56
-
57
- const configParamsNode = buildMutationConfigParamsNode(node, tsResolver)
58
- const paramsSignature = declarationPrinter.print(configParamsNode) ?? ''
59
-
60
- const mutationArgParamsNode = ast.createOperationParams(node, {
61
- paramsType: 'inline',
62
- pathParamsType: 'inline',
63
- paramsCasing,
64
- resolver: tsResolver,
65
- })
66
- const hasMutationParams = mutationArgParamsNode.params.length > 0
67
-
68
- const TRequest = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
69
- const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
70
-
71
- const clientCallParamsNode = ast.createOperationParams(node, {
72
- paramsType,
73
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
74
- paramsCasing,
75
- resolver: tsResolver,
76
- extraParams: [
77
- ast.createFunctionParameter({
78
- name: 'config',
79
- type: ast.createParamsType({
80
- variant: 'reference',
81
- name: buildRequestConfigType(node, tsResolver),
82
- }),
83
- default: '{}',
84
- }),
85
- ],
86
- })
87
- const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
88
-
89
- return (
90
- <File.Source name={name} isExportable isIndexable>
91
- <Function name={name} export params={paramsSignature} generics={['TContext = unknown']}>
92
- {`
93
- const mutationKey = ${mutationKeyName}()
94
- return mutationOptions<${TData}, ${TError}, ${TRequest ? `{${TRequest}}` : 'undefined'}, TContext>({
95
- mutationKey,
96
- mutationFn: async(${hasMutationParams ? `{ ${argKeysStr} }` : '_'}) => {
97
- return ${clientName}(${clientCallStr})
98
- },
99
- })
100
- `}
101
- </Function>
102
- </File.Source>
103
- )
104
- }
@@ -1,124 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import type { PluginReactQuery } from '../types.ts'
7
- import { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
8
- import { buildQueryKeyParams, buildStatusUnionType, getComments, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
9
- import { getQueryOptionsParams } from './QueryOptions.tsx'
10
-
11
- type Props = {
12
- name: string
13
- queryOptionsName: string
14
- queryKeyName: string
15
- queryKeyTypeName: string
16
- node: ast.OperationNode
17
- tsResolver: ResolverTs
18
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
19
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
20
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
21
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
22
- customOptions: PluginReactQuery['resolvedOptions']['customOptions']
23
- }
24
-
25
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
26
- const callPrinter = functionPrinter({ mode: 'call' })
27
-
28
- function buildQueryParamsNode(
29
- node: ast.OperationNode,
30
- options: {
31
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
32
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
33
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
34
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
35
- resolver: ResolverTs
36
- },
37
- ): ast.FunctionParametersNode {
38
- const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
39
- const successNames = resolveSuccessNames(node, resolver)
40
- const responseName = successNames.length > 0 ? successNames.join(' | ') : resolver.resolveResponseName(node)
41
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null
42
- const errorNames = resolveErrorNames(node, resolver)
43
-
44
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
45
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
46
-
47
- const optionsParam = ast.createFunctionParameter({
48
- name: 'options',
49
- type: ast.createParamsType({
50
- variant: 'reference',
51
- name: `{
52
- query?: Partial<QueryObserverOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
53
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
54
- }`,
55
- }),
56
- default: '{}',
57
- })
58
-
59
- return ast.createOperationParams(node, {
60
- paramsType,
61
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
62
- paramsCasing,
63
- resolver,
64
- extraParams: [optionsParam],
65
- })
66
- }
67
-
68
- export function Query({
69
- name,
70
- queryKeyTypeName,
71
- queryOptionsName,
72
- queryKeyName,
73
- paramsType,
74
- paramsCasing,
75
- pathParamsType,
76
- dataReturnType,
77
- node,
78
- tsResolver,
79
- customOptions,
80
- }: Props): KubbReactNode {
81
- const successNames = resolveSuccessNames(node, tsResolver)
82
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
83
- const errorNames = resolveErrorNames(node, tsResolver)
84
-
85
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
86
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
87
- const returnType = `UseQueryResult<${'TData'}, ${TError}> & { queryKey: TQueryKey }`
88
- const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
89
-
90
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
91
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
92
- const enabledNames = getEnabledParamNames(queryKeyParamsNode)
93
-
94
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
95
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
96
-
97
- const paramsNode = markParamsOptional(
98
- buildQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver }),
99
- enabledNames,
100
- )
101
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
102
-
103
- return (
104
- <File.Source name={name} isExportable isIndexable>
105
- <Function name={name} export generics={generics.join(', ')} params={paramsSignature} returnType={undefined} JSDoc={{ comments: getComments(node) }}>
106
- {`
107
- const { query: queryConfig = {}, client: config = {} } = options ?? {}
108
- const { client: queryClient, ...resolvedOptions } = queryConfig
109
- const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
110
-
111
- const query = useQuery({
112
- ...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n ...customOptions,' : ''}
113
- ...resolvedOptions,
114
- queryKey,
115
- } as unknown as QueryObserverOptions, queryClient) as ${returnType}
116
-
117
- query.queryKey = queryKey as TQueryKey
118
-
119
- return query
120
- `}
121
- </Function>
122
- </File.Source>
123
- )
124
- }
@@ -1 +0,0 @@
1
- export { QueryKey } from '@internals/tanstack-query'
@@ -1,85 +0,0 @@
1
- import type { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import { buildQueryOptionsParams, getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
7
- import type { PluginReactQuery } from '../types.ts'
8
- import { buildQueryKeyParams, buildStatusUnionType, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
9
-
10
- type Props = {
11
- name: string
12
- clientName: string
13
- queryKeyName: string
14
- node: ast.OperationNode
15
- tsResolver: ResolverTs
16
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
17
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
18
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
19
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
20
- suspense?: boolean
21
- }
22
-
23
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
24
- const callPrinter = functionPrinter({ mode: 'call' })
25
-
26
- export function getQueryOptionsParams(
27
- node: ast.OperationNode,
28
- options: {
29
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
30
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
31
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
32
- resolver: ResolverTs
33
- },
34
- ): ast.FunctionParametersNode {
35
- return buildQueryOptionsParams(node, options)
36
- }
37
-
38
- export function QueryOptions({
39
- name,
40
- clientName,
41
- dataReturnType,
42
- node,
43
- tsResolver,
44
- paramsCasing,
45
- paramsType,
46
- pathParamsType,
47
- queryKeyName,
48
- suspense,
49
- }: Props): KubbReactNode {
50
- const successNames = resolveSuccessNames(node, tsResolver)
51
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
52
- const errorNames = resolveErrorNames(node, tsResolver)
53
-
54
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
55
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
56
-
57
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
58
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
59
-
60
- const enabledNames = getEnabledParamNames(queryKeyParamsNode)
61
- // Suspense queries can't be disabled, so their params stay required.
62
- const optionalNames = suspense ? [] : enabledNames
63
- const enabledText = suspense ? '' : enabledNames.length ? `enabled: !!(${enabledNames.join(' && ')}),` : ''
64
-
65
- const paramsNode = markParamsOptional(getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver }), optionalNames)
66
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
67
- const rawParamsCall = callPrinter.print(paramsNode) ?? ''
68
- const clientCallStr = injectNonNullAssertions(rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }'), optionalNames)
69
-
70
- return (
71
- <File.Source name={name} isExportable isIndexable>
72
- <Function name={name} export params={paramsSignature}>
73
- {`
74
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
75
- return queryOptions<${TData}, ${TError}, ${TData}, typeof queryKey>({${enabledText ? `\n ${enabledText}` : ''}
76
- queryKey,
77
- queryFn: async ({ signal }) => {
78
- return ${clientName}(${clientCallStr})
79
- },
80
- })
81
- `}
82
- </Function>
83
- </File.Source>
84
- )
85
- }