@kubb/plugin-react-query 5.0.0-beta.10 → 5.0.0-beta.100
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/README.md +22 -26
- package/dist/index.cjs +2288 -191
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +494 -6
- package/dist/index.js +2258 -185
- package/dist/index.js.map +1 -1
- package/package.json +11 -34
- package/dist/components-Dow6tde8.js +0 -1459
- package/dist/components-Dow6tde8.js.map +0 -1
- package/dist/components-HwdCDefj.cjs +0 -1603
- package/dist/components-HwdCDefj.cjs.map +0 -1
- package/dist/components.cjs +0 -13
- package/dist/components.d.ts +0 -268
- package/dist/components.js +0 -2
- package/dist/generators-CcOmnTPa.cjs +0 -1454
- package/dist/generators-CcOmnTPa.cjs.map +0 -1
- package/dist/generators-yfZr_qfT.js +0 -1412
- package/dist/generators-yfZr_qfT.js.map +0 -1
- package/dist/generators.cjs +0 -9
- package/dist/generators.d.ts +0 -27
- package/dist/generators.js +0 -2
- package/dist/types-DG_OxOym.d.ts +0 -363
- package/extension.yaml +0 -911
- package/src/components/InfiniteQuery.tsx +0 -161
- package/src/components/InfiniteQueryOptions.tsx +0 -178
- package/src/components/Mutation.tsx +0 -121
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/MutationOptions.tsx +0 -103
- package/src/components/Query.tsx +0 -118
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -99
- package/src/components/SuspenseInfiniteQuery.tsx +0 -161
- package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -178
- package/src/components/SuspenseQuery.tsx +0 -118
- package/src/components/index.ts +0 -11
- package/src/generators/customHookOptionsFileGenerator.tsx +0 -84
- package/src/generators/hookOptionsGenerator.tsx +0 -133
- package/src/generators/index.ts +0 -7
- package/src/generators/infiniteQueryGenerator.tsx +0 -194
- package/src/generators/mutationGenerator.tsx +0 -168
- package/src/generators/queryGenerator.tsx +0 -180
- package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -203
- package/src/generators/suspenseQueryGenerator.tsx +0 -181
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -186
- package/src/resolvers/resolverReactQuery.ts +0 -107
- package/src/types.ts +0 -328
- package/src/utils.ts +0 -10
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
|
@@ -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
|
-
initialPageParam: Infinite['initialPageParam']
|
|
23
|
-
queryParam?: Infinite['queryParam']
|
|
24
|
-
customOptions: PluginReactQuery['resolvedOptions']['customOptions']
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
28
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
29
|
-
|
|
30
|
-
function buildInfiniteQueryParamsNode(
|
|
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<InfiniteQueryObserverOptions<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 InfiniteQuery({
|
|
66
|
-
name,
|
|
67
|
-
queryKeyTypeName,
|
|
68
|
-
queryOptionsName,
|
|
69
|
-
queryKeyName,
|
|
70
|
-
paramsType,
|
|
71
|
-
paramsCasing,
|
|
72
|
-
pathParamsType,
|
|
73
|
-
dataReturnType,
|
|
74
|
-
node,
|
|
75
|
-
tsResolver,
|
|
76
|
-
initialPageParam,
|
|
77
|
-
queryParam,
|
|
78
|
-
customOptions,
|
|
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 = 'UseInfiniteQueryResult<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 = buildInfiniteQueryParamsNode(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 = useInfiniteQuery({
|
|
149
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
|
|
150
|
-
...resolvedOptions,
|
|
151
|
-
queryKey,
|
|
152
|
-
} as unknown as InfiniteQueryObserverOptions<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 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 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,121 +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 } 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 responseName = resolver.resolveResponseName(node)
|
|
51
|
-
const errorNames = resolveErrorNames(node, resolver)
|
|
52
|
-
|
|
53
|
-
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
54
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
55
|
-
|
|
56
|
-
const mutationArgParamsNode = createMutationArgParams(node, {
|
|
57
|
-
paramsCasing,
|
|
58
|
-
resolver,
|
|
59
|
-
})
|
|
60
|
-
const TRequest = mutationArgParamsNode.params.length > 0 ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
61
|
-
const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
|
|
62
|
-
|
|
63
|
-
return ast.createFunctionParameters({
|
|
64
|
-
params: [
|
|
65
|
-
ast.createFunctionParameter({
|
|
66
|
-
name: 'options',
|
|
67
|
-
type: ast.createParamsType({
|
|
68
|
-
variant: 'reference',
|
|
69
|
-
name: `{
|
|
70
|
-
mutation?: UseMutationOptions<${generics}> & { client?: QueryClient },
|
|
71
|
-
client?: ${buildRequestConfigType(node, resolver)},
|
|
72
|
-
}`,
|
|
73
|
-
}),
|
|
74
|
-
default: '{}',
|
|
75
|
-
}),
|
|
76
|
-
],
|
|
77
|
-
})
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export function Mutation({ name, mutationOptionsName, paramsCasing, dataReturnType, node, tsResolver, mutationKeyName, customOptions }: Props): KubbReactNode {
|
|
81
|
-
const responseName = 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
|
-
|
|
87
|
-
const mutationArgParamsNode = createMutationArgParams(node, {
|
|
88
|
-
paramsCasing,
|
|
89
|
-
resolver: tsResolver,
|
|
90
|
-
})
|
|
91
|
-
const TRequest = mutationArgParamsNode.params.length > 0 ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
92
|
-
const generics = [TData, TError, TRequest ? `{${TRequest}}` : 'void', 'TContext'].join(', ')
|
|
93
|
-
const returnType = `UseMutationResult<${generics}>`
|
|
94
|
-
|
|
95
|
-
const mutationOptionsConfigNode = buildMutationConfigParamsNode(node, tsResolver)
|
|
96
|
-
const mutationOptionsParamsCall = callPrinter.print(mutationOptionsConfigNode) ?? ''
|
|
97
|
-
|
|
98
|
-
const paramsNode = buildMutationParamsNode(node, { paramsCasing, dataReturnType, resolver: tsResolver })
|
|
99
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
100
|
-
|
|
101
|
-
return (
|
|
102
|
-
<File.Source name={name} isExportable isIndexable>
|
|
103
|
-
<Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }} generics={['TContext']}>
|
|
104
|
-
{`
|
|
105
|
-
const { mutation = {}, client: config = {} } = options ?? {}
|
|
106
|
-
const { client: queryClient, ...mutationOptions } = mutation;
|
|
107
|
-
const mutationKey = mutationOptions.mutationKey ?? ${mutationKeyName}()
|
|
108
|
-
|
|
109
|
-
const baseOptions = ${mutationOptionsName}(${mutationOptionsParamsCall}) as UseMutationOptions<${generics}>
|
|
110
|
-
${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' }) as UseMutationOptions<${generics}>` : ''}
|
|
111
|
-
|
|
112
|
-
return useMutation<${generics}>({
|
|
113
|
-
...baseOptions,${customOptions ? '\n...customOptions,' : ''}
|
|
114
|
-
mutationKey,
|
|
115
|
-
...mutationOptions,
|
|
116
|
-
}, queryClient) as ${returnType}
|
|
117
|
-
`}
|
|
118
|
-
</Function>
|
|
119
|
-
</File.Source>
|
|
120
|
-
)
|
|
121
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { MutationKey } from '@internals/tanstack-query'
|
|
@@ -1,103 +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 } 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 responseName = tsResolver.resolveResponseName(node)
|
|
52
|
-
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
53
|
-
const errorNames = resolveErrorNames(node, tsResolver)
|
|
54
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
55
|
-
|
|
56
|
-
const configParamsNode = buildMutationConfigParamsNode(node, tsResolver)
|
|
57
|
-
const paramsSignature = declarationPrinter.print(configParamsNode) ?? ''
|
|
58
|
-
|
|
59
|
-
const mutationArgParamsNode = ast.createOperationParams(node, {
|
|
60
|
-
paramsType: 'inline',
|
|
61
|
-
pathParamsType: 'inline',
|
|
62
|
-
paramsCasing,
|
|
63
|
-
resolver: tsResolver,
|
|
64
|
-
})
|
|
65
|
-
const hasMutationParams = mutationArgParamsNode.params.length > 0
|
|
66
|
-
|
|
67
|
-
const TRequest = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
68
|
-
const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
69
|
-
|
|
70
|
-
const clientCallParamsNode = ast.createOperationParams(node, {
|
|
71
|
-
paramsType,
|
|
72
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
73
|
-
paramsCasing,
|
|
74
|
-
resolver: tsResolver,
|
|
75
|
-
extraParams: [
|
|
76
|
-
ast.createFunctionParameter({
|
|
77
|
-
name: 'config',
|
|
78
|
-
type: ast.createParamsType({
|
|
79
|
-
variant: 'reference',
|
|
80
|
-
name: buildRequestConfigType(node, tsResolver),
|
|
81
|
-
}),
|
|
82
|
-
default: '{}',
|
|
83
|
-
}),
|
|
84
|
-
],
|
|
85
|
-
})
|
|
86
|
-
const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
|
|
87
|
-
|
|
88
|
-
return (
|
|
89
|
-
<File.Source name={name} isExportable isIndexable>
|
|
90
|
-
<Function name={name} export params={paramsSignature} generics={['TContext = unknown']}>
|
|
91
|
-
{`
|
|
92
|
-
const mutationKey = ${mutationKeyName}()
|
|
93
|
-
return mutationOptions<${TData}, ${TError}, ${TRequest ? `{${TRequest}}` : 'void'}, TContext>({
|
|
94
|
-
mutationKey,
|
|
95
|
-
mutationFn: async(${hasMutationParams ? `{ ${argKeysStr} }` : '_'}) => {
|
|
96
|
-
return ${clientName}(${clientCallStr})
|
|
97
|
-
},
|
|
98
|
-
})
|
|
99
|
-
`}
|
|
100
|
-
</Function>
|
|
101
|
-
</File.Source>
|
|
102
|
-
)
|
|
103
|
-
}
|
package/src/components/Query.tsx
DELETED
|
@@ -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 buildQueryParamsNode(
|
|
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<QueryObserverOptions<${[TData, TError, 'TData', 'TQueryData', '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 Query({
|
|
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 = `UseQueryResult<${'TData'}, ${TError}> & { queryKey: TQueryKey }`
|
|
85
|
-
const generics = [`TData = ${TData}`, `TQueryData = ${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 = buildQueryParamsNode(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 = useQuery({
|
|
106
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n...customOptions,' : ''}
|
|
107
|
-
...resolvedOptions,
|
|
108
|
-
queryKey,
|
|
109
|
-
} as unknown as QueryObserverOptions, queryClient) as ${returnType}
|
|
110
|
-
|
|
111
|
-
query.queryKey = queryKey as TQueryKey
|
|
112
|
-
|
|
113
|
-
return query
|
|
114
|
-
`}
|
|
115
|
-
</Function>
|
|
116
|
-
</File.Source>
|
|
117
|
-
)
|
|
118
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { QueryKey } from '@internals/tanstack-query'
|