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