@kubb/plugin-react-query 5.0.0-beta.10 → 5.0.0-beta.101

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 (49) hide show
  1. package/README.md +22 -26
  2. package/dist/index.cjs +2288 -191
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +494 -6
  5. package/dist/index.js +2258 -185
  6. package/dist/index.js.map +1 -1
  7. package/package.json +11 -34
  8. package/dist/components-Dow6tde8.js +0 -1459
  9. package/dist/components-Dow6tde8.js.map +0 -1
  10. package/dist/components-HwdCDefj.cjs +0 -1603
  11. package/dist/components-HwdCDefj.cjs.map +0 -1
  12. package/dist/components.cjs +0 -13
  13. package/dist/components.d.ts +0 -268
  14. package/dist/components.js +0 -2
  15. package/dist/generators-CcOmnTPa.cjs +0 -1454
  16. package/dist/generators-CcOmnTPa.cjs.map +0 -1
  17. package/dist/generators-yfZr_qfT.js +0 -1412
  18. package/dist/generators-yfZr_qfT.js.map +0 -1
  19. package/dist/generators.cjs +0 -9
  20. package/dist/generators.d.ts +0 -27
  21. package/dist/generators.js +0 -2
  22. package/dist/types-DG_OxOym.d.ts +0 -363
  23. package/extension.yaml +0 -911
  24. package/src/components/InfiniteQuery.tsx +0 -161
  25. package/src/components/InfiniteQueryOptions.tsx +0 -178
  26. package/src/components/Mutation.tsx +0 -121
  27. package/src/components/MutationKey.tsx +0 -1
  28. package/src/components/MutationOptions.tsx +0 -103
  29. package/src/components/Query.tsx +0 -118
  30. package/src/components/QueryKey.tsx +0 -1
  31. package/src/components/QueryOptions.tsx +0 -99
  32. package/src/components/SuspenseInfiniteQuery.tsx +0 -161
  33. package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -178
  34. package/src/components/SuspenseQuery.tsx +0 -118
  35. package/src/components/index.ts +0 -11
  36. package/src/generators/customHookOptionsFileGenerator.tsx +0 -84
  37. package/src/generators/hookOptionsGenerator.tsx +0 -133
  38. package/src/generators/index.ts +0 -7
  39. package/src/generators/infiniteQueryGenerator.tsx +0 -194
  40. package/src/generators/mutationGenerator.tsx +0 -168
  41. package/src/generators/queryGenerator.tsx +0 -180
  42. package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -203
  43. package/src/generators/suspenseQueryGenerator.tsx +0 -181
  44. package/src/index.ts +0 -2
  45. package/src/plugin.ts +0 -186
  46. package/src/resolvers/resolverReactQuery.ts +0 -107
  47. package/src/types.ts +0 -328
  48. package/src/utils.ts +0 -10
  49. /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
@@ -1,99 +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 { buildEnabledCheck } from '@internals/tanstack-query'
7
- import type { PluginReactQuery } from '../types.ts'
8
- import { buildQueryKeyParams, resolveErrorNames } 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
- }
21
-
22
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
23
- const callPrinter = functionPrinter({ mode: 'call' })
24
-
25
- export function getQueryOptionsParams(
26
- node: ast.OperationNode,
27
- options: {
28
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
29
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
30
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
31
- resolver: ResolverTs
32
- },
33
- ): ast.FunctionParametersNode {
34
- const { paramsType, paramsCasing, pathParamsType, resolver } = options
35
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
36
-
37
- return ast.createOperationParams(node, {
38
- paramsType,
39
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
40
- paramsCasing,
41
- resolver,
42
- extraParams: [
43
- ast.createFunctionParameter({
44
- name: 'config',
45
- type: ast.createParamsType({
46
- variant: 'reference',
47
- name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
48
- }),
49
- default: '{}',
50
- }),
51
- ],
52
- })
53
- }
54
-
55
- export function QueryOptions({
56
- name,
57
- clientName,
58
- dataReturnType,
59
- node,
60
- tsResolver,
61
- paramsCasing,
62
- paramsType,
63
- pathParamsType,
64
- queryKeyName,
65
- }: Props): KubbReactNode {
66
- const responseName = tsResolver.resolveResponseName(node)
67
- const errorNames = resolveErrorNames(node, tsResolver)
68
-
69
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
70
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
71
-
72
- const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
73
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
74
- const rawParamsCall = callPrinter.print(paramsNode) ?? ''
75
- const clientCallStr = rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }')
76
-
77
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
78
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
79
-
80
- const enabledSource = buildEnabledCheck(queryKeyParamsNode)
81
- const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : ''
82
-
83
- return (
84
- <File.Source name={name} isExportable isIndexable>
85
- <Function name={name} export params={paramsSignature}>
86
- {`
87
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
88
- return queryOptions<${TData}, ${TError}, ${TData}, typeof queryKey>({
89
- ${enabledText}
90
- queryKey,
91
- queryFn: async ({ signal }) => {
92
- return ${clientName}(${clientCallStr})
93
- },
94
- })
95
- `}
96
- </Function>
97
- </File.Source>
98
- )
99
- }
@@ -1,161 +0,0 @@
1
- import { getOperationParameters } from '@internals/shared'
2
- import { ast } from '@kubb/core'
3
- import type { ResolverTs } from '@kubb/plugin-ts'
4
- import { functionPrinter } from '@kubb/plugin-ts'
5
- import { File, Function } from '@kubb/renderer-jsx'
6
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
7
- import type { Infinite, PluginReactQuery } from '../types.ts'
8
- import { buildQueryKeyParams, getComments, resolveErrorNames } 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
- initialPageParam: Infinite['initialPageParam']
24
- queryParam?: Infinite['queryParam']
25
- }
26
-
27
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
28
- const callPrinter = functionPrinter({ mode: 'call' })
29
-
30
- function buildSuspenseInfiniteQueryParamsNode(
31
- node: ast.OperationNode,
32
- options: {
33
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
34
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
35
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
36
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
37
- resolver: ResolverTs
38
- pageParamGeneric: string
39
- },
40
- ): ast.FunctionParametersNode {
41
- const { paramsType, paramsCasing, pathParamsType, resolver, pageParamGeneric } = options
42
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
43
-
44
- const optionsParam = ast.createFunctionParameter({
45
- name: 'options',
46
- type: ast.createParamsType({
47
- variant: 'reference',
48
- name: `{
49
- query?: Partial<UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient },
50
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
51
- }`,
52
- }),
53
- default: '{}',
54
- })
55
-
56
- return ast.createOperationParams(node, {
57
- paramsType,
58
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
59
- paramsCasing,
60
- resolver,
61
- extraParams: [optionsParam],
62
- })
63
- }
64
-
65
- export function SuspenseInfiniteQuery({
66
- name,
67
- queryKeyTypeName,
68
- queryOptionsName,
69
- queryKeyName,
70
- paramsType,
71
- paramsCasing,
72
- pathParamsType,
73
- dataReturnType,
74
- node,
75
- tsResolver,
76
- customOptions,
77
- initialPageParam,
78
- queryParam,
79
- }: Props): KubbReactNode {
80
- const responseName = tsResolver.resolveResponseName(node)
81
- const errorNames = resolveErrorNames(node, tsResolver)
82
-
83
- const responseType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
84
- const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
85
-
86
- const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
87
- const fallbackPageParamType =
88
- typeof initialPageParam === 'number'
89
- ? 'number'
90
- : typeof initialPageParam === 'string'
91
- ? initialPageParam.includes(' as ')
92
- ? (() => {
93
- const parts = initialPageParam.split(' as ')
94
- return parts[parts.length - 1] ?? 'unknown'
95
- })()
96
- : 'string'
97
- : typeof initialPageParam === 'boolean'
98
- ? 'boolean'
99
- : 'unknown'
100
-
101
- const rawQueryParams = getOperationParameters(node).query
102
- const queryParamsTypeName =
103
- rawQueryParams.length > 0
104
- ? (() => {
105
- const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
106
- const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
107
- return groupName !== individualName ? groupName : undefined
108
- })()
109
- : undefined
110
-
111
- const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : undefined
112
- const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
113
-
114
- const returnType = 'UseSuspenseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }'
115
- const generics = [
116
- `TQueryFnData = ${responseType}`,
117
- `TError = ${errorType}`,
118
- 'TData = InfiniteData<TQueryFnData>',
119
- `TQueryKey extends QueryKey = ${queryKeyTypeName}`,
120
- `TPageParam = ${pageParamType}`,
121
- ]
122
-
123
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
124
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
125
-
126
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
127
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
128
-
129
- const paramsNode = buildSuspenseInfiniteQueryParamsNode(node, {
130
- paramsType,
131
- paramsCasing,
132
- pathParamsType,
133
- dataReturnType,
134
- resolver: tsResolver,
135
- pageParamGeneric: 'TPageParam',
136
- })
137
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
138
-
139
- return (
140
- <File.Source name={name} isExportable isIndexable>
141
- <Function name={name} export generics={generics.join(', ')} params={paramsSignature} returnType={undefined} JSDoc={{ comments: getComments(node) }}>
142
- {`
143
- const { query: queryConfig = {}, client: config = {} } = options ?? {}
144
- const { client: queryClient, ...resolvedOptions } = queryConfig
145
- const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})
146
- ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
147
-
148
- const query = useSuspenseInfiniteQuery({
149
- ...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
150
- ...resolvedOptions,
151
- queryKey,
152
- } as unknown as UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType}
153
-
154
- query.queryKey = queryKey as TQueryKey
155
-
156
- return query
157
- `}
158
- </Function>
159
- </File.Source>
160
- )
161
- }
@@ -1,178 +0,0 @@
1
- import { getOperationParameters } from '@internals/shared'
2
- import { getNestedAccessor } from '@internals/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, resolveErrorNames } from '../utils.ts'
10
- import { buildEnabledCheck } 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 SuspenseInfiniteQueryOptions({
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 responseName = tsResolver.resolveResponseName(node)
50
- const queryFnDataType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
51
- const errorNames = resolveErrorNames(node, tsResolver)
52
- const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
53
-
54
- const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
55
- const fallbackPageParamType =
56
- typeof initialPageParam === 'number'
57
- ? 'number'
58
- : typeof initialPageParam === 'string'
59
- ? initialPageParam.includes(' as ')
60
- ? (() => {
61
- const parts = initialPageParam.split(' as ')
62
- return parts[parts.length - 1] ?? 'unknown'
63
- })()
64
- : 'string'
65
- : typeof initialPageParam === 'boolean'
66
- ? 'boolean'
67
- : 'unknown'
68
-
69
- const rawQueryParams = getOperationParameters(node).query
70
- const queryParamsTypeName =
71
- rawQueryParams.length > 0
72
- ? (() => {
73
- const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
74
- const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
75
- return groupName !== individualName ? groupName : undefined
76
- })()
77
- : undefined
78
-
79
- const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : undefined
80
- const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
81
-
82
- const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
83
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
84
- const rawParamsCall = callPrinter.print(paramsNode) ?? ''
85
- const clientCallStr = rawParamsCall.replace(/\bconfig\b(?=[^,]*$)/, '{ ...config, signal: config.signal ?? signal }')
86
-
87
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
88
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
89
-
90
- const enabledSource = buildEnabledCheck(queryKeyParamsNode)
91
- const enabledText = enabledSource ? `enabled: !!(${enabledSource}),` : ''
92
-
93
- const hasNewParams = nextParam !== undefined || previousParam !== undefined
94
-
95
- let getNextPageParamExpr: string | undefined
96
- let getPreviousPageParamExpr: string | undefined
97
-
98
- if (hasNewParams) {
99
- if (nextParam) {
100
- const accessor = getNestedAccessor(nextParam, 'lastPage')
101
- if (accessor) {
102
- getNextPageParamExpr = `getNextPageParam: (lastPage) => ${accessor}`
103
- }
104
- }
105
- if (previousParam) {
106
- const accessor = getNestedAccessor(previousParam, 'firstPage')
107
- if (accessor) {
108
- getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => ${accessor}`
109
- }
110
- }
111
- } else if (cursorParam) {
112
- getNextPageParamExpr = `getNextPageParam: (lastPage) => lastPage['${cursorParam}']`
113
- getPreviousPageParamExpr = `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']`
114
- } else {
115
- if (dataReturnType === 'full') {
116
- getNextPageParamExpr =
117
- 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
118
- } else {
119
- getNextPageParamExpr =
120
- 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
121
- }
122
- getPreviousPageParamExpr = 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1'
123
- }
124
-
125
- const queryOptionsArr = [
126
- `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
127
- getNextPageParamExpr,
128
- getPreviousPageParamExpr,
129
- ].filter(Boolean)
130
-
131
- const infiniteOverrideParams =
132
- queryParam && queryParamsTypeName
133
- ? `
134
- params = {
135
- ...(params ?? {}),
136
- ['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
137
- } as ${queryParamsTypeName}`
138
- : ''
139
-
140
- if (infiniteOverrideParams) {
141
- return (
142
- <File.Source name={name} isExportable isIndexable>
143
- <Function name={name} export params={paramsSignature}>
144
- {`
145
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
146
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
147
- ${enabledText}
148
- queryKey,
149
- queryFn: async ({ signal, pageParam }) => {
150
- ${infiniteOverrideParams}
151
- return ${clientName}(${clientCallStr})
152
- },
153
- ${queryOptionsArr.join(',\n')}
154
- })
155
- `}
156
- </Function>
157
- </File.Source>
158
- )
159
- }
160
-
161
- return (
162
- <File.Source name={name} isExportable isIndexable>
163
- <Function name={name} export params={paramsSignature}>
164
- {`
165
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
166
- return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
167
- ${enabledText}
168
- queryKey,
169
- queryFn: async ({ signal }) => {
170
- return ${clientName}(${clientCallStr})
171
- },
172
- ${queryOptionsArr.join(',\n')}
173
- })
174
- `}
175
- </Function>
176
- </File.Source>
177
- )
178
- }
@@ -1,118 +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 { buildQueryKeyParams, getComments, resolveErrorNames } from '../utils.ts'
8
- import { getQueryOptionsParams } from './QueryOptions.tsx'
9
-
10
- type Props = {
11
- name: string
12
- queryOptionsName: string
13
- queryKeyName: string
14
- queryKeyTypeName: string
15
- node: ast.OperationNode
16
- tsResolver: ResolverTs
17
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
18
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
19
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
20
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
21
- customOptions: PluginReactQuery['resolvedOptions']['customOptions']
22
- }
23
-
24
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
25
- const callPrinter = functionPrinter({ mode: 'call' })
26
-
27
- function buildSuspenseQueryParamsNode(
28
- node: ast.OperationNode,
29
- options: {
30
- paramsType: PluginReactQuery['resolvedOptions']['paramsType']
31
- paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
32
- pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
33
- dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
34
- resolver: ResolverTs
35
- },
36
- ): ast.FunctionParametersNode {
37
- const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
38
- const responseName = resolver.resolveResponseName(node)
39
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
40
- const errorNames = resolveErrorNames(node, resolver)
41
-
42
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
43
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
44
-
45
- const optionsParam = ast.createFunctionParameter({
46
- name: 'options',
47
- type: ast.createParamsType({
48
- variant: 'reference',
49
- name: `{
50
- query?: Partial<UseSuspenseQueryOptions<${[TData, TError, 'TData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
51
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
52
- }`,
53
- }),
54
- default: '{}',
55
- })
56
-
57
- return ast.createOperationParams(node, {
58
- paramsType,
59
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
60
- paramsCasing,
61
- resolver,
62
- extraParams: [optionsParam],
63
- })
64
- }
65
-
66
- export function SuspenseQuery({
67
- name,
68
- queryKeyTypeName,
69
- queryOptionsName,
70
- queryKeyName,
71
- paramsType,
72
- paramsCasing,
73
- pathParamsType,
74
- dataReturnType,
75
- node,
76
- tsResolver,
77
- customOptions,
78
- }: Props): KubbReactNode {
79
- const responseName = tsResolver.resolveResponseName(node)
80
- const errorNames = resolveErrorNames(node, tsResolver)
81
-
82
- const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
83
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
84
- const returnType = `UseSuspenseQueryResult<${'TData'}, ${TError}> & { queryKey: TQueryKey }`
85
- const generics = [`TData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
86
-
87
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
88
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
89
-
90
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
91
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
92
-
93
- const paramsNode = buildSuspenseQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
94
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
95
-
96
- return (
97
- <File.Source name={name} isExportable isIndexable>
98
- <Function name={name} export generics={generics.join(', ')} params={paramsSignature} returnType={undefined} JSDoc={{ comments: getComments(node) }}>
99
- {`
100
- const { query: queryConfig = {}, client: config = {} } = options ?? {}
101
- const { client: queryClient, ...resolvedOptions } = queryConfig
102
- const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})
103
- ${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
104
-
105
- const query = useSuspenseQuery({
106
- ...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
107
- ...resolvedOptions,
108
- queryKey,
109
- } as unknown as UseSuspenseQueryOptions, queryClient) as ${returnType}
110
-
111
- query.queryKey = queryKey as TQueryKey
112
-
113
- return query
114
- `}
115
- </Function>
116
- </File.Source>
117
- )
118
- }
@@ -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,84 +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, jsxRenderer } from '@kubb/renderer-jsx'
6
- import type { PluginReactQuery } from '../types'
7
-
8
- export const customHookOptionsFileGenerator = defineGenerator<PluginReactQuery>({
9
- name: 'react-query-custom-hook-options-file',
10
- renderer: jsxRenderer,
11
- operations(nodes, ctx) {
12
- const { resolver, config, root } = ctx
13
- const { output, customOptions, query, group } = ctx.options
14
-
15
- if (!customOptions) return null
16
-
17
- const override = output.override ?? config.output.override ?? false
18
- const { importPath, name } = customOptions
19
- const hookOptionsName = resolver.resolveHookOptionsName()
20
- const customHookOptionsName = resolver.resolveCustomHookOptionsName()
21
-
22
- const reactQueryImportPath = query ? query.importPath : '@tanstack/react-query'
23
-
24
- let hookFilePath: string
25
- const firstNode = nodes[0]
26
- if (firstNode) {
27
- const hookName = resolver.resolveQueryName(firstNode)
28
- const hookFile = resolver.resolveFile(
29
- { name: hookName, extname: '.ts', tag: firstNode.tags[0] ?? 'default', path: firstNode.path },
30
- { root, output, group },
31
- )
32
- hookFilePath = hookFile.path
33
- } else {
34
- hookFilePath = path.resolve(root, 'index.ts')
35
- }
36
-
37
- const ensureExtension = (filePath: string, extname: string) => {
38
- if (path.extname(filePath) === '') return filePath + extname
39
- return filePath
40
- }
41
-
42
- const basePath = path.dirname(hookFilePath)
43
- const actualFilePath = ensureExtension(importPath, '.ts')
44
- const file = {
45
- baseName: path.basename(actualFilePath) as `${string}.${string}`,
46
- name: path.basename(actualFilePath, path.extname(actualFilePath)),
47
- path: path.resolve(basePath, actualFilePath),
48
- }
49
-
50
- if (fs.existsSync(file.path) && !override) return null
51
-
52
- return (
53
- <File baseName={file.baseName} path={file.path}>
54
- <File.Import name={['QueryClient']} path={reactQueryImportPath} isTypeOnly />
55
- <File.Import name={['useQueryClient']} path={reactQueryImportPath} />
56
- <File.Import name={[hookOptionsName]} root={file.path} path={path.resolve(root, './index.ts')} />
57
- <File.Source name={file.name} isExportable isIndexable>
58
- <Function name={customHookOptionsName} params="{ queryClient }: { queryClient: QueryClient }" returnType={`Partial<${hookOptionsName}>`}>
59
- {`return {
60
- // TODO: Define custom hook options here
61
- // Example:
62
- // useUpdatePetHook: {
63
- // onSuccess: () => {
64
- // void queryClient.invalidateQueries({ queryKey: ['pet'] })
65
- // }
66
- // }
67
- }`}
68
- </Function>
69
- <Function
70
- name={name}
71
- generics={`T extends keyof ${hookOptionsName}`}
72
- params="{ hookName, operationId }: { hookName: T, operationId: string }"
73
- returnType={`${hookOptionsName}[T]`}
74
- export
75
- >
76
- {`const queryClient = useQueryClient()
77
- const customOptions = ${customHookOptionsName}({ queryClient })
78
- return customOptions[hookName] ?? {}`}
79
- </Function>
80
- </File.Source>
81
- </File>
82
- )
83
- },
84
- })