@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.
- package/dist/{components-DQAYLQW0.cjs → components-B0j2b6y8.cjs} +298 -416
- package/dist/components-B0j2b6y8.cjs.map +1 -0
- package/dist/{components-IArDg-DO.js → components-Ds-e_nnK.js} +294 -412
- package/dist/components-Ds-e_nnK.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +1 -1
- package/dist/components.js +1 -1
- package/dist/{generators-BqGaMUH6.cjs → generators-CU6zfet7.cjs} +149 -229
- package/dist/generators-CU6zfet7.cjs.map +1 -0
- package/dist/{generators-B86BJkmW.js → generators-nnZhtlrk.js} +151 -231
- package/dist/generators-nnZhtlrk.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +1 -1
- package/dist/generators.js +1 -1
- package/dist/index.cjs +41 -18
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +42 -19
- package/dist/index.js.map +1 -1
- package/dist/{types-Dh4HNR9K.d.ts → types-ANQXv7Gr.d.ts} +13 -17
- package/package.json +10 -18
- package/dist/components-DQAYLQW0.cjs.map +0 -1
- package/dist/components-IArDg-DO.js.map +0 -1
- package/dist/generators-B86BJkmW.js.map +0 -1
- package/dist/generators-BqGaMUH6.cjs.map +0 -1
- package/extension.yaml +0 -1484
- package/src/components/InfiniteQuery.tsx +0 -167
- package/src/components/InfiniteQueryOptions.tsx +0 -169
- package/src/components/Mutation.tsx +0 -123
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/MutationOptions.tsx +0 -104
- package/src/components/Query.tsx +0 -125
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -104
- package/src/components/SuspenseInfiniteQuery.tsx +0 -162
- package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -163
- package/src/components/SuspenseQuery.tsx +0 -120
- package/src/components/index.ts +0 -11
- package/src/generators/customHookOptionsFileGenerator.tsx +0 -90
- package/src/generators/hookOptionsGenerator.tsx +0 -129
- package/src/generators/index.ts +0 -7
- package/src/generators/infiniteQueryGenerator.tsx +0 -200
- package/src/generators/mutationGenerator.tsx +0 -173
- package/src/generators/queryGenerator.tsx +0 -185
- package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -209
- package/src/generators/suspenseQueryGenerator.tsx +0 -188
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -203
- package/src/resolvers/resolverReactQuery.ts +0 -118
- package/src/types.ts +0 -365
- package/src/utils.ts +0 -17
|
@@ -1,167 +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 { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
|
|
9
|
-
import { buildQueryKeyParams, getComments, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
|
|
10
|
-
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
11
|
-
|
|
12
|
-
type Props = {
|
|
13
|
-
name: string
|
|
14
|
-
queryOptionsName: string
|
|
15
|
-
queryKeyName: string
|
|
16
|
-
queryKeyTypeName: 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
|
-
queryParam?: Infinite['queryParam']
|
|
25
|
-
customOptions: PluginReactQuery['resolvedOptions']['customOptions']
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
29
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
30
|
-
|
|
31
|
-
function buildInfiniteQueryParamsNode(
|
|
32
|
-
node: ast.OperationNode,
|
|
33
|
-
options: {
|
|
34
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
35
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
36
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
37
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
38
|
-
resolver: ResolverTs
|
|
39
|
-
pageParamGeneric: string
|
|
40
|
-
},
|
|
41
|
-
): ast.FunctionParametersNode {
|
|
42
|
-
const { paramsType, paramsCasing, pathParamsType, resolver, pageParamGeneric } = options
|
|
43
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null
|
|
44
|
-
|
|
45
|
-
const optionsParam = ast.createFunctionParameter({
|
|
46
|
-
name: 'options',
|
|
47
|
-
type: ast.createParamsType({
|
|
48
|
-
variant: 'reference',
|
|
49
|
-
name: `{
|
|
50
|
-
query?: Partial<InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { 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 InfiniteQuery({
|
|
67
|
-
name,
|
|
68
|
-
queryKeyTypeName,
|
|
69
|
-
queryOptionsName,
|
|
70
|
-
queryKeyName,
|
|
71
|
-
paramsType,
|
|
72
|
-
paramsCasing,
|
|
73
|
-
pathParamsType,
|
|
74
|
-
dataReturnType,
|
|
75
|
-
node,
|
|
76
|
-
tsResolver,
|
|
77
|
-
initialPageParam,
|
|
78
|
-
queryParam,
|
|
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 responseType = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
86
|
-
const errorType = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
87
|
-
|
|
88
|
-
const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
|
|
89
|
-
const fallbackPageParamType =
|
|
90
|
-
typeof initialPageParam === 'number'
|
|
91
|
-
? 'number'
|
|
92
|
-
: typeof initialPageParam === 'string'
|
|
93
|
-
? initialPageParam.includes(' as ')
|
|
94
|
-
? (() => {
|
|
95
|
-
const parts = initialPageParam.split(' as ')
|
|
96
|
-
return parts[parts.length - 1] ?? 'unknown'
|
|
97
|
-
})()
|
|
98
|
-
: 'string'
|
|
99
|
-
: typeof initialPageParam === 'boolean'
|
|
100
|
-
? 'boolean'
|
|
101
|
-
: 'unknown'
|
|
102
|
-
|
|
103
|
-
const rawQueryParams = getOperationParameters(node).query
|
|
104
|
-
const queryParamsTypeName =
|
|
105
|
-
rawQueryParams.length > 0
|
|
106
|
-
? (() => {
|
|
107
|
-
const groupName = tsResolver.resolveQueryParamsName(node, rawQueryParams[0]!)
|
|
108
|
-
const individualName = tsResolver.resolveParamName(node, rawQueryParams[0]!)
|
|
109
|
-
return groupName !== individualName ? groupName : null
|
|
110
|
-
})()
|
|
111
|
-
: null
|
|
112
|
-
|
|
113
|
-
const queryParamType = queryParam && queryParamsTypeName ? `${queryParamsTypeName}['${queryParam}']` : null
|
|
114
|
-
const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
|
|
115
|
-
|
|
116
|
-
const returnType = 'UseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }'
|
|
117
|
-
const generics = [
|
|
118
|
-
`TQueryFnData = ${responseType}`,
|
|
119
|
-
`TError = ${errorType}`,
|
|
120
|
-
'TData = InfiniteData<TQueryFnData>',
|
|
121
|
-
`TQueryKey extends QueryKey = ${queryKeyTypeName}`,
|
|
122
|
-
`TPageParam = ${pageParamType}`,
|
|
123
|
-
]
|
|
124
|
-
|
|
125
|
-
const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
126
|
-
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
|
|
127
|
-
const enabledNames = getEnabledParamNames(queryKeyParamsNode)
|
|
128
|
-
|
|
129
|
-
const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
130
|
-
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
|
|
131
|
-
|
|
132
|
-
const paramsNode = markParamsOptional(
|
|
133
|
-
buildInfiniteQueryParamsNode(node, {
|
|
134
|
-
paramsType,
|
|
135
|
-
paramsCasing,
|
|
136
|
-
pathParamsType,
|
|
137
|
-
dataReturnType,
|
|
138
|
-
resolver: tsResolver,
|
|
139
|
-
pageParamGeneric: 'TPageParam',
|
|
140
|
-
}),
|
|
141
|
-
enabledNames,
|
|
142
|
-
)
|
|
143
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
144
|
-
|
|
145
|
-
return (
|
|
146
|
-
<File.Source name={name} isExportable isIndexable>
|
|
147
|
-
<Function name={name} export generics={generics.join(', ')} params={paramsSignature} returnType={undefined} JSDoc={{ comments: getComments(node) }}>
|
|
148
|
-
{`
|
|
149
|
-
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
150
|
-
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
151
|
-
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParamsCall})
|
|
152
|
-
${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
|
|
153
|
-
|
|
154
|
-
const query = useInfiniteQuery({
|
|
155
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
|
|
156
|
-
...resolvedOptions,
|
|
157
|
-
queryKey,
|
|
158
|
-
} as unknown as InfiniteQueryObserverOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType}
|
|
159
|
-
|
|
160
|
-
query.queryKey = queryKey as TQueryKey
|
|
161
|
-
|
|
162
|
-
return query
|
|
163
|
-
`}
|
|
164
|
-
</Function>
|
|
165
|
-
</File.Source>
|
|
166
|
-
)
|
|
167
|
-
}
|
|
@@ -1,169 +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 { 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 : `ResponseConfig<${responseName}>`
|
|
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
|
-
? `
|
|
125
|
-
params = {
|
|
126
|
-
...(params ?? {}),
|
|
127
|
-
['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
|
|
128
|
-
} as ${queryParamsTypeName}`
|
|
129
|
-
: ''
|
|
130
|
-
|
|
131
|
-
if (infiniteOverrideParams) {
|
|
132
|
-
return (
|
|
133
|
-
<File.Source name={name} isExportable isIndexable>
|
|
134
|
-
<Function name={name} export params={paramsSignature}>
|
|
135
|
-
{`
|
|
136
|
-
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
137
|
-
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
138
|
-
${enabledText}
|
|
139
|
-
queryKey,
|
|
140
|
-
queryFn: async ({ signal, pageParam }) => {
|
|
141
|
-
${infiniteOverrideParams}
|
|
142
|
-
return ${clientName}(${clientCallStr})
|
|
143
|
-
},
|
|
144
|
-
${queryOptionsArr.join(',\n')}
|
|
145
|
-
})
|
|
146
|
-
`}
|
|
147
|
-
</Function>
|
|
148
|
-
</File.Source>
|
|
149
|
-
)
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return (
|
|
153
|
-
<File.Source name={name} isExportable isIndexable>
|
|
154
|
-
<Function name={name} export params={paramsSignature}>
|
|
155
|
-
{`
|
|
156
|
-
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
157
|
-
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
158
|
-
${enabledText}
|
|
159
|
-
queryKey,
|
|
160
|
-
queryFn: async ({ signal }) => {
|
|
161
|
-
return ${clientName}(${clientCallStr})
|
|
162
|
-
},
|
|
163
|
-
${queryOptionsArr.join(',\n')}
|
|
164
|
-
})
|
|
165
|
-
`}
|
|
166
|
-
</Function>
|
|
167
|
-
</File.Source>
|
|
168
|
-
)
|
|
169
|
-
}
|
|
@@ -1,123 +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, 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 : `ResponseConfig<${responseName}>`
|
|
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 : `ResponseConfig<${responseName}>`
|
|
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}>
|
|
112
|
-
${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' }) as UseMutationOptions<${generics}>` : ''}
|
|
113
|
-
|
|
114
|
-
return useMutation<${generics}>({
|
|
115
|
-
...baseOptions,${customOptions ? '\n...customOptions,' : ''}
|
|
116
|
-
mutationKey,
|
|
117
|
-
...mutationOptions,
|
|
118
|
-
}, queryClient) as ${returnType}
|
|
119
|
-
`}
|
|
120
|
-
</Function>
|
|
121
|
-
</File.Source>
|
|
122
|
-
)
|
|
123
|
-
}
|
|
@@ -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, 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 : `ResponseConfig<${responseName}>`
|
|
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
|
-
}
|