@kubb/plugin-react-query 5.0.0-beta.56 → 5.0.0-beta.73
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/index.cjs +2521 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +521 -3
- package/dist/index.js +2488 -49
- package/dist/index.js.map +1 -1
- package/package.json +10 -20
- package/dist/components-DL0Cai7l.js +0 -1507
- package/dist/components-DL0Cai7l.js.map +0 -1
- package/dist/components-yMQOuFmI.cjs +0 -1657
- package/dist/components-yMQOuFmI.cjs.map +0 -1
- package/dist/components.cjs +0 -13
- package/dist/components.d.ts +0 -270
- package/dist/components.js +0 -2
- package/dist/generators-BG-Vcvfg.js +0 -1307
- package/dist/generators-BG-Vcvfg.js.map +0 -1
- package/dist/generators-zGKP8yII.cjs +0 -1349
- package/dist/generators-zGKP8yII.cjs.map +0 -1
- package/dist/generators.cjs +0 -9
- package/dist/generators.d.ts +0 -66
- package/dist/generators.js +0 -2
- package/dist/types-X7D0NSvJ.d.ts +0 -396
- package/src/components/InfiniteQuery.tsx +0 -166
- package/src/components/InfiniteQueryOptions.tsx +0 -166
- package/src/components/Mutation.tsx +0 -122
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/MutationOptions.tsx +0 -104
- package/src/components/Query.tsx +0 -124
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -85
- package/src/components/SuspenseInfiniteQuery.tsx +0 -161
- package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -162
- package/src/components/SuspenseQuery.tsx +0 -119
- 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 -196
- package/src/generators/mutationGenerator.tsx +0 -172
- package/src/generators/queryGenerator.tsx +0 -181
- package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -205
- package/src/generators/suspenseQueryGenerator.tsx +0 -184
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -203
- package/src/resolvers/resolverReactQuery.ts +0 -118
- package/src/types.ts +0 -361
- package/src/utils.ts +0 -18
- /package/dist/{chunk-C0LytTxp.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, buildStatusUnionType, getComments, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
|
|
9
|
-
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
10
|
-
|
|
11
|
-
type Props = {
|
|
12
|
-
name: string
|
|
13
|
-
queryOptionsName: string
|
|
14
|
-
queryKeyName: string
|
|
15
|
-
queryKeyTypeName: string
|
|
16
|
-
node: ast.OperationNode
|
|
17
|
-
tsResolver: ResolverTs
|
|
18
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
19
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
20
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
21
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
22
|
-
customOptions: PluginReactQuery['resolvedOptions']['customOptions']
|
|
23
|
-
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 : buildStatusUnionType(node, tsResolver)
|
|
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})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
|
|
147
|
-
|
|
148
|
-
const query = useSuspenseInfiniteQuery({
|
|
149
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n ...customOptions,' : ''}
|
|
150
|
-
...resolvedOptions,
|
|
151
|
-
queryKey,
|
|
152
|
-
} as unknown as UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType}
|
|
153
|
-
|
|
154
|
-
query.queryKey = queryKey as TQueryKey
|
|
155
|
-
|
|
156
|
-
return query
|
|
157
|
-
`}
|
|
158
|
-
</Function>
|
|
159
|
-
</File.Source>
|
|
160
|
-
)
|
|
161
|
-
}
|
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
import { getOperationParameters } from '@internals/shared'
|
|
2
|
-
import { getNestedAccessor } from '@kubb/ast/utils'
|
|
3
|
-
import type { ast } from '@kubb/core'
|
|
4
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
5
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
6
|
-
import { File, Function } from '@kubb/renderer-jsx'
|
|
7
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
8
|
-
import type { Infinite, PluginReactQuery } from '../types.ts'
|
|
9
|
-
import { buildQueryKeyParams, buildStatusUnionType, resolveErrorNames, resolveSuccessNames } from '../utils.ts'
|
|
10
|
-
import { 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 : buildStatusUnionType(node, tsResolver)
|
|
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
|
-
? `params = {
|
|
121
|
-
...(params ?? {}),
|
|
122
|
-
['${queryParam}']: pageParam as unknown as ${queryParamsTypeName}['${queryParam}'],
|
|
123
|
-
} as ${queryParamsTypeName}`
|
|
124
|
-
: ''
|
|
125
|
-
|
|
126
|
-
if (infiniteOverrideParams) {
|
|
127
|
-
return (
|
|
128
|
-
<File.Source name={name} isExportable isIndexable>
|
|
129
|
-
<Function name={name} export params={paramsSignature}>
|
|
130
|
-
{`
|
|
131
|
-
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
132
|
-
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
133
|
-
queryKey,
|
|
134
|
-
queryFn: async ({ signal, pageParam }) => {
|
|
135
|
-
${infiniteOverrideParams}
|
|
136
|
-
return ${clientName}(${clientCallStr})
|
|
137
|
-
},
|
|
138
|
-
${queryOptionsArr.join(',\n ')}
|
|
139
|
-
})
|
|
140
|
-
`}
|
|
141
|
-
</Function>
|
|
142
|
-
</File.Source>
|
|
143
|
-
)
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return (
|
|
147
|
-
<File.Source name={name} isExportable isIndexable>
|
|
148
|
-
<Function name={name} export params={paramsSignature}>
|
|
149
|
-
{`
|
|
150
|
-
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
151
|
-
return infiniteQueryOptions<${queryFnDataType}, ${errorType}, InfiniteData<${queryFnDataType}>, typeof queryKey, ${pageParamType}>({
|
|
152
|
-
queryKey,
|
|
153
|
-
queryFn: async ({ signal }) => {
|
|
154
|
-
return ${clientName}(${clientCallStr})
|
|
155
|
-
},
|
|
156
|
-
${queryOptionsArr.join(',\n ')}
|
|
157
|
-
})
|
|
158
|
-
`}
|
|
159
|
-
</Function>
|
|
160
|
-
</File.Source>
|
|
161
|
-
)
|
|
162
|
-
}
|
|
@@ -1,119 +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, buildStatusUnionType, 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 : buildStatusUnionType(node, resolver)
|
|
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 : buildStatusUnionType(node, tsResolver)
|
|
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})${customOptions ? `\n const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${node.operationId}' })` : ''}
|
|
105
|
-
|
|
106
|
-
const query = useSuspenseQuery({
|
|
107
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),${customOptions ? '\n ...customOptions,' : ''}
|
|
108
|
-
...resolvedOptions,
|
|
109
|
-
queryKey,
|
|
110
|
-
} as unknown as UseSuspenseQueryOptions, queryClient) as ${returnType}
|
|
111
|
-
|
|
112
|
-
query.queryKey = queryKey as TQueryKey
|
|
113
|
-
|
|
114
|
-
return query
|
|
115
|
-
`}
|
|
116
|
-
</Function>
|
|
117
|
-
</File.Source>
|
|
118
|
-
)
|
|
119
|
-
}
|
package/src/components/index.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
export { InfiniteQuery } from './InfiniteQuery.tsx'
|
|
2
|
-
export { InfiniteQueryOptions } from './InfiniteQueryOptions.tsx'
|
|
3
|
-
export { Mutation } from './Mutation.tsx'
|
|
4
|
-
export { MutationKey } from './MutationKey.tsx'
|
|
5
|
-
export { MutationOptions } from './MutationOptions.tsx'
|
|
6
|
-
export { Query } from './Query.tsx'
|
|
7
|
-
export { QueryKey } from './QueryKey.tsx'
|
|
8
|
-
export { QueryOptions } from './QueryOptions.tsx'
|
|
9
|
-
export { SuspenseInfiniteQuery } from './SuspenseInfiniteQuery.tsx'
|
|
10
|
-
export { SuspenseInfiniteQueryOptions } from './SuspenseInfiniteQueryOptions.tsx'
|
|
11
|
-
export { SuspenseQuery } from './SuspenseQuery.tsx'
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs'
|
|
2
|
-
import path from 'node:path'
|
|
3
|
-
|
|
4
|
-
import { defineGenerator } from '@kubb/core'
|
|
5
|
-
import { File, Function, jsxRenderer } from '@kubb/renderer-jsx'
|
|
6
|
-
import type { PluginReactQuery } from '../types'
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Scaffolds the user-editable `useCustomHookOptions` file when
|
|
10
|
-
* `pluginReactQuery({ customOptions: { ... } })` is configured. The file is
|
|
11
|
-
* only created when it does not already exist, so user edits persist across
|
|
12
|
-
* regeneration.
|
|
13
|
-
*/
|
|
14
|
-
export const customHookOptionsFileGenerator = defineGenerator<PluginReactQuery>({
|
|
15
|
-
name: 'react-query-custom-hook-options-file',
|
|
16
|
-
renderer: jsxRenderer,
|
|
17
|
-
operations(nodes, ctx) {
|
|
18
|
-
const { resolver, config, root } = ctx
|
|
19
|
-
const { output, customOptions, query, group } = ctx.options
|
|
20
|
-
|
|
21
|
-
if (!customOptions) return null
|
|
22
|
-
|
|
23
|
-
const override = output.override ?? config.output.override ?? false
|
|
24
|
-
const { importPath, name } = customOptions
|
|
25
|
-
const hookOptionsName = resolver.resolveHookOptionsName()
|
|
26
|
-
const customHookOptionsName = resolver.resolveCustomHookOptionsName()
|
|
27
|
-
|
|
28
|
-
const reactQueryImportPath = query ? query.importPath : '@tanstack/react-query'
|
|
29
|
-
|
|
30
|
-
let hookFilePath: string
|
|
31
|
-
const firstNode = nodes[0]
|
|
32
|
-
if (firstNode) {
|
|
33
|
-
const hookName = resolver.resolveQueryName(firstNode)
|
|
34
|
-
const hookFile = resolver.resolveFile(
|
|
35
|
-
{ name: hookName, extname: '.ts', tag: firstNode.tags[0] ?? 'default', path: firstNode.path },
|
|
36
|
-
{ root, output, group: group ?? undefined },
|
|
37
|
-
)
|
|
38
|
-
hookFilePath = hookFile.path
|
|
39
|
-
} else {
|
|
40
|
-
hookFilePath = path.resolve(root, 'index.ts')
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const ensureExtension = (filePath: string, extname: string) => {
|
|
44
|
-
if (path.extname(filePath) === '') return filePath + extname
|
|
45
|
-
return filePath
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const basePath = path.dirname(hookFilePath)
|
|
49
|
-
const actualFilePath = ensureExtension(importPath, '.ts')
|
|
50
|
-
const file = {
|
|
51
|
-
baseName: path.basename(actualFilePath) as `${string}.${string}`,
|
|
52
|
-
name: path.basename(actualFilePath, path.extname(actualFilePath)),
|
|
53
|
-
path: path.resolve(basePath, actualFilePath),
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (fs.existsSync(file.path) && !override) return null
|
|
57
|
-
|
|
58
|
-
return (
|
|
59
|
-
<File baseName={file.baseName} path={file.path}>
|
|
60
|
-
<File.Import name={['QueryClient']} path={reactQueryImportPath} isTypeOnly />
|
|
61
|
-
<File.Import name={['useQueryClient']} path={reactQueryImportPath} />
|
|
62
|
-
<File.Import name={[hookOptionsName]} root={file.path} path={path.resolve(root, './index.ts')} />
|
|
63
|
-
<File.Source name={file.name} isExportable isIndexable>
|
|
64
|
-
<Function name={customHookOptionsName} params="{ queryClient }: { queryClient: QueryClient }" returnType={`Partial<${hookOptionsName}>`}>
|
|
65
|
-
{`return {
|
|
66
|
-
// TODO: Define custom hook options here
|
|
67
|
-
// Example:
|
|
68
|
-
// useUpdatePetHook: {
|
|
69
|
-
// onSuccess: () => {
|
|
70
|
-
// void queryClient.invalidateQueries({ queryKey: ['pet'] })
|
|
71
|
-
// }
|
|
72
|
-
// }
|
|
73
|
-
}`}
|
|
74
|
-
</Function>
|
|
75
|
-
<Function
|
|
76
|
-
name={name}
|
|
77
|
-
generics={`T extends keyof ${hookOptionsName}`}
|
|
78
|
-
params="{ hookName, operationId }: { hookName: T, operationId: string }"
|
|
79
|
-
returnType={`${hookOptionsName}[T]`}
|
|
80
|
-
export
|
|
81
|
-
>
|
|
82
|
-
{`const queryClient = useQueryClient()
|
|
83
|
-
const customOptions = ${customHookOptionsName}({ queryClient })
|
|
84
|
-
return customOptions[hookName] ?? {}`}
|
|
85
|
-
</Function>
|
|
86
|
-
</File.Source>
|
|
87
|
-
</File>
|
|
88
|
-
)
|
|
89
|
-
},
|
|
90
|
-
})
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { getOperationParameters, operationFileEntry } from '@internals/shared'
|
|
2
|
-
import { ast, defineGenerator } from '@kubb/core'
|
|
3
|
-
import { File, jsxRenderer, Type } from '@kubb/renderer-jsx'
|
|
4
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
5
|
-
import type { PluginReactQuery } from '../types'
|
|
6
|
-
import { resolveOperationOverrides } from '../utils.ts'
|
|
7
|
-
|
|
8
|
-
type QueryOption = PluginReactQuery['resolvedOptions']['query']
|
|
9
|
-
type MutationOption = PluginReactQuery['resolvedOptions']['mutation']
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Emits the `HookOptions` type used by `customOptions`. Enabled when
|
|
13
|
-
* `pluginReactQuery({ customOptions: { ... } })`. The generated type lists
|
|
14
|
-
* every hook keyed by name so user-supplied options stay in sync with the
|
|
15
|
-
* generated hooks at compile time.
|
|
16
|
-
*/
|
|
17
|
-
export const hookOptionsGenerator = defineGenerator<PluginReactQuery>({
|
|
18
|
-
name: 'react-query-hook-options',
|
|
19
|
-
renderer: jsxRenderer,
|
|
20
|
-
operations(nodes, ctx) {
|
|
21
|
-
const { resolver, config, root } = ctx
|
|
22
|
-
const { output, customOptions, query, mutation, suspense, infinite, group, override } = ctx.options
|
|
23
|
-
|
|
24
|
-
if (!customOptions) return null
|
|
25
|
-
|
|
26
|
-
const name = resolver.resolveHookOptionsName()
|
|
27
|
-
const resolvedFile = resolver.resolveFile({ name, extname: '.ts' }, { root, output, group: group ?? undefined })
|
|
28
|
-
const hookOptionsFile = {
|
|
29
|
-
...resolvedFile,
|
|
30
|
-
baseName: `${name}.ts` as const,
|
|
31
|
-
path: resolvedFile.path.replace(/[^/\\]+\.ts$/, `${name}.ts`),
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const imports: Array<KubbReactNode> = []
|
|
35
|
-
const hookOptions: Record<string, string> = {}
|
|
36
|
-
|
|
37
|
-
for (const node of nodes) {
|
|
38
|
-
if (!ast.isHttpOperationNode(node)) continue
|
|
39
|
-
const opOverrides = resolveOperationOverrides(node, override)
|
|
40
|
-
const nodeQuery: QueryOption = 'query' in opOverrides ? (opOverrides.query as QueryOption) : query
|
|
41
|
-
const nodeMutation: MutationOption = 'mutation' in opOverrides ? (opOverrides.mutation as MutationOption) : mutation
|
|
42
|
-
const nodeInfinite = 'infinite' in opOverrides ? opOverrides.infinite : infinite
|
|
43
|
-
const nodeInfiniteOptions = nodeInfinite && typeof nodeInfinite === 'object' ? nodeInfinite : null
|
|
44
|
-
|
|
45
|
-
// query: false means "still a query but skip the useQuery hook"
|
|
46
|
-
const isQueryOp =
|
|
47
|
-
nodeQuery === false
|
|
48
|
-
? !!query && query.methods.some((m) => node.method.toLowerCase() === m.toLowerCase())
|
|
49
|
-
: !!nodeQuery && nodeQuery.methods.some((m) => node.method.toLowerCase() === m.toLowerCase())
|
|
50
|
-
const nodeQueryMethods = new Set(nodeQuery ? nodeQuery.methods : [])
|
|
51
|
-
const isMutationOp =
|
|
52
|
-
nodeMutation !== false &&
|
|
53
|
-
!isQueryOp &&
|
|
54
|
-
(nodeMutation ? nodeMutation.methods : []).some((m) => !nodeQueryMethods.has(m) && node.method.toLowerCase() === m.toLowerCase())
|
|
55
|
-
const isSuspenseOp = !!suspense
|
|
56
|
-
const isInfiniteOp = !!nodeInfiniteOptions
|
|
57
|
-
|
|
58
|
-
if (isQueryOp) {
|
|
59
|
-
const queryOptionsName = resolver.resolveQueryOptionsName(node)
|
|
60
|
-
const queryHookName = resolver.resolveQueryName(node)
|
|
61
|
-
const queryHookFile = resolver.resolveFile(operationFileEntry(node, queryHookName), { root, output, group: group ?? undefined })
|
|
62
|
-
imports.push(<File.Import name={[queryOptionsName]} root={hookOptionsFile.path} path={queryHookFile.path} />)
|
|
63
|
-
hookOptions[queryHookName] = `Partial<ReturnType<typeof ${queryOptionsName}>>`
|
|
64
|
-
|
|
65
|
-
if (isSuspenseOp) {
|
|
66
|
-
const suspenseOptionsName = resolver.resolveSuspenseQueryOptionsName(node)
|
|
67
|
-
const suspenseHookName = resolver.resolveSuspenseQueryName(node)
|
|
68
|
-
const suspenseHookFile = resolver.resolveFile(operationFileEntry(node, suspenseHookName), { root, output, group: group ?? undefined })
|
|
69
|
-
imports.push(<File.Import name={[suspenseOptionsName]} root={hookOptionsFile.path} path={suspenseHookFile.path} />)
|
|
70
|
-
hookOptions[suspenseHookName] = `Partial<ReturnType<typeof ${suspenseOptionsName}>>`
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (isInfiniteOp) {
|
|
74
|
-
// Validate queryParam
|
|
75
|
-
const normalizeKey = (key: string) => key.replace(/\?$/, '')
|
|
76
|
-
const queryParamKeys = getOperationParameters(node).query.map((p) => p.name)
|
|
77
|
-
const hasQueryParam = nodeInfiniteOptions!.queryParam ? queryParamKeys.some((k) => normalizeKey(k) === nodeInfiniteOptions!.queryParam) : false
|
|
78
|
-
|
|
79
|
-
if (hasQueryParam) {
|
|
80
|
-
const infiniteOptionsName = resolver.resolveInfiniteQueryOptionsName(node)
|
|
81
|
-
const infiniteHookName = resolver.resolveInfiniteQueryName(node)
|
|
82
|
-
const infiniteHookFile = resolver.resolveFile(operationFileEntry(node, infiniteHookName), { root, output, group: group ?? undefined })
|
|
83
|
-
imports.push(<File.Import name={[infiniteOptionsName]} root={hookOptionsFile.path} path={infiniteHookFile.path} />)
|
|
84
|
-
hookOptions[infiniteHookName] = `Partial<ReturnType<typeof ${infiniteOptionsName}>>`
|
|
85
|
-
|
|
86
|
-
if (isSuspenseOp) {
|
|
87
|
-
const suspenseInfiniteOptionsName = resolver.resolveSuspenseInfiniteQueryOptionsName(node)
|
|
88
|
-
const suspenseInfiniteHookName = resolver.resolveSuspenseInfiniteQueryName(node)
|
|
89
|
-
const suspenseInfiniteHookFile = resolver.resolveFile(operationFileEntry(node, suspenseInfiniteHookName), {
|
|
90
|
-
root,
|
|
91
|
-
output,
|
|
92
|
-
group: group ?? undefined,
|
|
93
|
-
})
|
|
94
|
-
imports.push(<File.Import name={[suspenseInfiniteOptionsName]} root={hookOptionsFile.path} path={suspenseInfiniteHookFile.path} />)
|
|
95
|
-
hookOptions[suspenseInfiniteHookName] = `Partial<ReturnType<typeof ${suspenseInfiniteOptionsName}>>`
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
if (isMutationOp) {
|
|
102
|
-
const mutationOptionsName = resolver.resolveMutationOptionsName(node)
|
|
103
|
-
const mutationHookName = resolver.resolveMutationName(node)
|
|
104
|
-
const mutationHookFile = resolver.resolveFile(operationFileEntry(node, mutationHookName), { root, output, group: group ?? undefined })
|
|
105
|
-
imports.push(<File.Import name={[mutationOptionsName]} root={hookOptionsFile.path} path={mutationHookFile.path} />)
|
|
106
|
-
hookOptions[mutationHookName] = `Partial<ReturnType<typeof ${mutationOptionsName}>>`
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
return (
|
|
111
|
-
<File
|
|
112
|
-
baseName={hookOptionsFile.baseName}
|
|
113
|
-
path={hookOptionsFile.path}
|
|
114
|
-
meta={hookOptionsFile.meta}
|
|
115
|
-
banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: hookOptionsFile.path, baseName: hookOptionsFile.baseName } })}
|
|
116
|
-
footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: hookOptionsFile.path, baseName: hookOptionsFile.baseName } })}
|
|
117
|
-
>
|
|
118
|
-
{imports}
|
|
119
|
-
<File.Source name={name} isExportable isIndexable isTypeOnly>
|
|
120
|
-
<Type export name={name}>
|
|
121
|
-
{`{ ${Object.keys(hookOptions)
|
|
122
|
-
.map((key) => `${JSON.stringify(key)}: ${hookOptions[key]}`)
|
|
123
|
-
.join(', ')} }`}
|
|
124
|
-
</Type>
|
|
125
|
-
</File.Source>
|
|
126
|
-
</File>
|
|
127
|
-
)
|
|
128
|
-
},
|
|
129
|
-
})
|