@kubb/plugin-react-query 5.0.0-alpha.9 → 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/LICENSE +17 -10
- package/README.md +38 -93
- package/dist/index.cjs +2317 -149
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +494 -6
- package/dist/index.js +2283 -143
- package/dist/index.js.map +1 -1
- package/package.json +47 -75
- package/dist/components-BHQT9ZLc.cjs +0 -1634
- package/dist/components-BHQT9ZLc.cjs.map +0 -1
- package/dist/components-CpyHYGOw.js +0 -1520
- package/dist/components-CpyHYGOw.js.map +0 -1
- package/dist/components.cjs +0 -13
- package/dist/components.d.ts +0 -398
- package/dist/components.js +0 -2
- package/dist/generators-DP07m3rH.cjs +0 -1469
- package/dist/generators-DP07m3rH.cjs.map +0 -1
- package/dist/generators-DkQwKTc2.js +0 -1427
- package/dist/generators-DkQwKTc2.js.map +0 -1
- package/dist/generators.cjs +0 -9
- package/dist/generators.d.ts +0 -523
- package/dist/generators.js +0 -2
- package/dist/types-D5S7Ny9r.d.ts +0 -270
- package/src/components/InfiniteQuery.tsx +0 -220
- package/src/components/InfiniteQueryOptions.tsx +0 -289
- package/src/components/Mutation.tsx +0 -158
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/MutationOptions.tsx +0 -122
- package/src/components/Query.tsx +0 -194
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -181
- package/src/components/SuspenseInfiniteQuery.tsx +0 -220
- package/src/components/SuspenseInfiniteQueryOptions.tsx +0 -289
- package/src/components/SuspenseQuery.tsx +0 -204
- package/src/components/index.ts +0 -11
- package/src/generators/customHookOptionsFileGenerator.tsx +0 -98
- package/src/generators/hookOptionsGenerator.tsx +0 -196
- package/src/generators/index.ts +0 -7
- package/src/generators/infiniteQueryGenerator.tsx +0 -214
- package/src/generators/mutationGenerator.tsx +0 -195
- package/src/generators/queryGenerator.tsx +0 -194
- package/src/generators/suspenseInfiniteQueryGenerator.tsx +0 -202
- package/src/generators/suspenseQueryGenerator.tsx +0 -207
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -239
- package/src/types.ts +0 -205
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/src/components/Query.tsx
DELETED
|
@@ -1,194 +0,0 @@
|
|
|
1
|
-
import { isAllOptional, isOptional, type Operation } from '@kubb/oas'
|
|
2
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
3
|
-
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
4
|
-
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
5
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
6
|
-
import type { PluginReactQuery } from '../types.ts'
|
|
7
|
-
import { QueryKey } from './QueryKey.tsx'
|
|
8
|
-
import { QueryOptions } from './QueryOptions.tsx'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
14
|
-
name: string
|
|
15
|
-
queryOptionsName: string
|
|
16
|
-
queryKeyName: string
|
|
17
|
-
queryKeyTypeName: string
|
|
18
|
-
typeSchemas: OperationSchemas
|
|
19
|
-
operation: Operation
|
|
20
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
21
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
22
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
23
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
24
|
-
customOptions: PluginReactQuery['resolvedOptions']['customOptions']
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
type GetParamsProps = {
|
|
28
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
29
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
30
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
31
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
32
|
-
typeSchemas: OperationSchemas
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
36
|
-
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
37
|
-
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
38
|
-
|
|
39
|
-
if (paramsType === 'object') {
|
|
40
|
-
const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
|
|
41
|
-
|
|
42
|
-
const children = {
|
|
43
|
-
...pathParams,
|
|
44
|
-
data: typeSchemas.request?.name
|
|
45
|
-
? {
|
|
46
|
-
type: typeSchemas.request?.name,
|
|
47
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
48
|
-
}
|
|
49
|
-
: undefined,
|
|
50
|
-
params: typeSchemas.queryParams?.name
|
|
51
|
-
? {
|
|
52
|
-
type: typeSchemas.queryParams?.name,
|
|
53
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
54
|
-
}
|
|
55
|
-
: undefined,
|
|
56
|
-
headers: typeSchemas.headerParams?.name
|
|
57
|
-
? {
|
|
58
|
-
type: typeSchemas.headerParams?.name,
|
|
59
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
60
|
-
}
|
|
61
|
-
: undefined,
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Check if all children are optional or undefined
|
|
65
|
-
const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
|
|
66
|
-
|
|
67
|
-
return FunctionParams.factory({
|
|
68
|
-
data: {
|
|
69
|
-
mode: 'object',
|
|
70
|
-
children,
|
|
71
|
-
default: allChildrenAreOptional ? '{}' : undefined,
|
|
72
|
-
},
|
|
73
|
-
options: {
|
|
74
|
-
type: `
|
|
75
|
-
{
|
|
76
|
-
query?: Partial<QueryObserverOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
|
|
77
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
|
|
78
|
-
}
|
|
79
|
-
`,
|
|
80
|
-
default: '{}',
|
|
81
|
-
},
|
|
82
|
-
})
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return FunctionParams.factory({
|
|
86
|
-
pathParams: typeSchemas.pathParams?.name
|
|
87
|
-
? {
|
|
88
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
89
|
-
children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
|
|
90
|
-
default: isAllOptional(typeSchemas.pathParams?.schema) ? '{}' : undefined,
|
|
91
|
-
}
|
|
92
|
-
: undefined,
|
|
93
|
-
data: typeSchemas.request?.name
|
|
94
|
-
? {
|
|
95
|
-
type: typeSchemas.request?.name,
|
|
96
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
97
|
-
}
|
|
98
|
-
: undefined,
|
|
99
|
-
params: typeSchemas.queryParams?.name
|
|
100
|
-
? {
|
|
101
|
-
type: typeSchemas.queryParams?.name,
|
|
102
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
103
|
-
}
|
|
104
|
-
: undefined,
|
|
105
|
-
headers: typeSchemas.headerParams?.name
|
|
106
|
-
? {
|
|
107
|
-
type: typeSchemas.headerParams?.name,
|
|
108
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
109
|
-
}
|
|
110
|
-
: undefined,
|
|
111
|
-
options: {
|
|
112
|
-
type: `
|
|
113
|
-
{
|
|
114
|
-
query?: Partial<QueryObserverOptions<${[TData, TError, 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>> & { client?: QueryClient },
|
|
115
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
|
|
116
|
-
}
|
|
117
|
-
`,
|
|
118
|
-
default: '{}',
|
|
119
|
-
},
|
|
120
|
-
})
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
export function Query({
|
|
124
|
-
name,
|
|
125
|
-
queryKeyTypeName,
|
|
126
|
-
queryOptionsName,
|
|
127
|
-
queryKeyName,
|
|
128
|
-
paramsType,
|
|
129
|
-
paramsCasing,
|
|
130
|
-
pathParamsType,
|
|
131
|
-
dataReturnType,
|
|
132
|
-
typeSchemas,
|
|
133
|
-
operation,
|
|
134
|
-
customOptions,
|
|
135
|
-
}: Props): FabricReactNode {
|
|
136
|
-
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
137
|
-
const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
138
|
-
const returnType = `UseQueryResult<${['TData', TError].join(', ')}> & { queryKey: TQueryKey }`
|
|
139
|
-
const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
|
|
140
|
-
|
|
141
|
-
const queryKeyParams = QueryKey.getParams({
|
|
142
|
-
pathParamsType,
|
|
143
|
-
typeSchemas,
|
|
144
|
-
paramsCasing,
|
|
145
|
-
})
|
|
146
|
-
const queryOptionsParams = QueryOptions.getParams({
|
|
147
|
-
paramsType,
|
|
148
|
-
pathParamsType,
|
|
149
|
-
typeSchemas,
|
|
150
|
-
paramsCasing,
|
|
151
|
-
})
|
|
152
|
-
const params = getParams({
|
|
153
|
-
paramsCasing,
|
|
154
|
-
paramsType,
|
|
155
|
-
pathParamsType,
|
|
156
|
-
dataReturnType,
|
|
157
|
-
typeSchemas,
|
|
158
|
-
})
|
|
159
|
-
|
|
160
|
-
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`
|
|
161
|
-
|
|
162
|
-
return (
|
|
163
|
-
<File.Source name={name} isExportable isIndexable>
|
|
164
|
-
<Function
|
|
165
|
-
name={name}
|
|
166
|
-
export
|
|
167
|
-
generics={generics.join(', ')}
|
|
168
|
-
params={params.toConstructor()}
|
|
169
|
-
JSDoc={{
|
|
170
|
-
comments: getComments(operation),
|
|
171
|
-
}}
|
|
172
|
-
>
|
|
173
|
-
{`
|
|
174
|
-
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
175
|
-
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
176
|
-
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
|
|
177
|
-
${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${operation.getOperationId()}' })` : ''}
|
|
178
|
-
|
|
179
|
-
const query = useQuery({
|
|
180
|
-
...${queryOptions},${customOptions ? '\n...customOptions,' : ''}
|
|
181
|
-
...resolvedOptions,
|
|
182
|
-
queryKey,
|
|
183
|
-
} as unknown as QueryObserverOptions, queryClient) as ${returnType}
|
|
184
|
-
|
|
185
|
-
query.queryKey = queryKey as TQueryKey
|
|
186
|
-
|
|
187
|
-
return query
|
|
188
|
-
`}
|
|
189
|
-
</Function>
|
|
190
|
-
</File.Source>
|
|
191
|
-
)
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
Query.getParams = getParams
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { QueryKey } from '@internals/tanstack-query'
|
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
import { isAllOptional, isOptional } from '@kubb/oas'
|
|
2
|
-
import { Client } from '@kubb/plugin-client/components'
|
|
3
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
4
|
-
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
5
|
-
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
6
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
7
|
-
import type { PluginReactQuery } from '../types.ts'
|
|
8
|
-
import { QueryKey } from './QueryKey.tsx'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
name: string
|
|
12
|
-
clientName: string
|
|
13
|
-
queryKeyName: string
|
|
14
|
-
typeSchemas: OperationSchemas
|
|
15
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
16
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
17
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
18
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
type GetParamsProps = {
|
|
22
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
23
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
24
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
25
|
-
typeSchemas: OperationSchemas
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: GetParamsProps) {
|
|
29
|
-
if (paramsType === 'object') {
|
|
30
|
-
const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
|
|
31
|
-
|
|
32
|
-
const children = {
|
|
33
|
-
...pathParams,
|
|
34
|
-
data: typeSchemas.request?.name
|
|
35
|
-
? {
|
|
36
|
-
type: typeSchemas.request?.name,
|
|
37
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
38
|
-
}
|
|
39
|
-
: undefined,
|
|
40
|
-
params: typeSchemas.queryParams?.name
|
|
41
|
-
? {
|
|
42
|
-
type: typeSchemas.queryParams?.name,
|
|
43
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
44
|
-
}
|
|
45
|
-
: undefined,
|
|
46
|
-
headers: typeSchemas.headerParams?.name
|
|
47
|
-
? {
|
|
48
|
-
type: typeSchemas.headerParams?.name,
|
|
49
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
50
|
-
}
|
|
51
|
-
: undefined,
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Check if all children are optional or undefined
|
|
55
|
-
const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
|
|
56
|
-
|
|
57
|
-
return FunctionParams.factory({
|
|
58
|
-
data: {
|
|
59
|
-
mode: 'object',
|
|
60
|
-
children,
|
|
61
|
-
default: allChildrenAreOptional ? '{}' : undefined,
|
|
62
|
-
},
|
|
63
|
-
config: {
|
|
64
|
-
type: typeSchemas.request?.name
|
|
65
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
66
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
67
|
-
default: '{}',
|
|
68
|
-
},
|
|
69
|
-
})
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return FunctionParams.factory({
|
|
73
|
-
pathParams: typeSchemas.pathParams?.name
|
|
74
|
-
? {
|
|
75
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
76
|
-
children: getPathParams(typeSchemas.pathParams, {
|
|
77
|
-
typed: true,
|
|
78
|
-
casing: paramsCasing,
|
|
79
|
-
}),
|
|
80
|
-
default: isAllOptional(typeSchemas.pathParams?.schema) ? '{}' : undefined,
|
|
81
|
-
}
|
|
82
|
-
: undefined,
|
|
83
|
-
data: typeSchemas.request?.name
|
|
84
|
-
? {
|
|
85
|
-
type: typeSchemas.request?.name,
|
|
86
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
87
|
-
}
|
|
88
|
-
: undefined,
|
|
89
|
-
params: typeSchemas.queryParams?.name
|
|
90
|
-
? {
|
|
91
|
-
type: typeSchemas.queryParams?.name,
|
|
92
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
93
|
-
}
|
|
94
|
-
: undefined,
|
|
95
|
-
headers: typeSchemas.headerParams?.name
|
|
96
|
-
? {
|
|
97
|
-
type: typeSchemas.headerParams?.name,
|
|
98
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
99
|
-
}
|
|
100
|
-
: undefined,
|
|
101
|
-
config: {
|
|
102
|
-
type: typeSchemas.request?.name
|
|
103
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
104
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
105
|
-
default: '{}',
|
|
106
|
-
},
|
|
107
|
-
})
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export function QueryOptions({
|
|
111
|
-
name,
|
|
112
|
-
clientName,
|
|
113
|
-
dataReturnType,
|
|
114
|
-
typeSchemas,
|
|
115
|
-
paramsCasing,
|
|
116
|
-
paramsType,
|
|
117
|
-
pathParamsType,
|
|
118
|
-
queryKeyName,
|
|
119
|
-
}: Props): FabricReactNode {
|
|
120
|
-
const params = getParams({
|
|
121
|
-
paramsType,
|
|
122
|
-
paramsCasing,
|
|
123
|
-
pathParamsType,
|
|
124
|
-
typeSchemas,
|
|
125
|
-
})
|
|
126
|
-
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
127
|
-
const TError = typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'
|
|
128
|
-
|
|
129
|
-
const clientParams = Client.getParams({
|
|
130
|
-
typeSchemas,
|
|
131
|
-
paramsCasing,
|
|
132
|
-
paramsType,
|
|
133
|
-
pathParamsType,
|
|
134
|
-
isConfigurable: true,
|
|
135
|
-
})
|
|
136
|
-
const queryKeyParams = QueryKey.getParams({
|
|
137
|
-
pathParamsType,
|
|
138
|
-
typeSchemas,
|
|
139
|
-
paramsCasing,
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
// Only add enabled check for required (non-optional) parameters
|
|
143
|
-
// Optional parameters with defaults should not prevent query execution
|
|
144
|
-
const enabled = Object.entries(queryKeyParams.flatParams)
|
|
145
|
-
.map(([key, item]) => {
|
|
146
|
-
// Only include if the parameter exists and is NOT optional
|
|
147
|
-
// This ensures we only check required parameters
|
|
148
|
-
return item && !item.optional && !item.default ? key : undefined
|
|
149
|
-
})
|
|
150
|
-
.filter(Boolean)
|
|
151
|
-
.join('&& ')
|
|
152
|
-
|
|
153
|
-
const enabledText = enabled ? `enabled: !!(${enabled}),` : ''
|
|
154
|
-
|
|
155
|
-
return (
|
|
156
|
-
<File.Source name={name} isExportable isIndexable>
|
|
157
|
-
<Function name={name} export params={params.toConstructor()}>
|
|
158
|
-
{`
|
|
159
|
-
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
|
|
160
|
-
return queryOptions<${TData}, ResponseErrorConfig<${TError}>, ${TData}, typeof queryKey>({
|
|
161
|
-
${enabledText}
|
|
162
|
-
queryKey,
|
|
163
|
-
queryFn: async ({ signal }) => {
|
|
164
|
-
return ${clientName}(${clientParams.toCall({
|
|
165
|
-
transformName(name) {
|
|
166
|
-
if (name === 'config') {
|
|
167
|
-
return '{ ...config, signal: config.signal ?? signal }'
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return name
|
|
171
|
-
},
|
|
172
|
-
})})
|
|
173
|
-
},
|
|
174
|
-
})
|
|
175
|
-
`}
|
|
176
|
-
</Function>
|
|
177
|
-
</File.Source>
|
|
178
|
-
)
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
QueryOptions.getParams = getParams
|
|
@@ -1,220 +0,0 @@
|
|
|
1
|
-
import { isAllOptional, isOptional, type Operation } from '@kubb/oas'
|
|
2
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
3
|
-
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
4
|
-
import { File, Function, FunctionParams } from '@kubb/react-fabric'
|
|
5
|
-
import type { FabricReactNode } from '@kubb/react-fabric/types'
|
|
6
|
-
import type { Infinite, PluginReactQuery } from '../types.ts'
|
|
7
|
-
import { QueryKey } from './QueryKey.tsx'
|
|
8
|
-
import { QueryOptions } from './QueryOptions.tsx'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
/**
|
|
12
|
-
* Name of the function
|
|
13
|
-
*/
|
|
14
|
-
name: string
|
|
15
|
-
queryOptionsName: string
|
|
16
|
-
queryKeyName: string
|
|
17
|
-
queryKeyTypeName: string
|
|
18
|
-
typeSchemas: OperationSchemas
|
|
19
|
-
operation: Operation
|
|
20
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
21
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
22
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
23
|
-
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
24
|
-
customOptions: PluginReactQuery['resolvedOptions']['customOptions']
|
|
25
|
-
initialPageParam: Infinite['initialPageParam']
|
|
26
|
-
queryParam?: Infinite['queryParam']
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
type GetParamsProps = {
|
|
30
|
-
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
31
|
-
paramsCasing: PluginReactQuery['resolvedOptions']['paramsCasing']
|
|
32
|
-
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
33
|
-
typeSchemas: OperationSchemas
|
|
34
|
-
pageParamGeneric: string
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas, pageParamGeneric }: GetParamsProps) {
|
|
38
|
-
if (paramsType === 'object') {
|
|
39
|
-
const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
|
|
40
|
-
|
|
41
|
-
const children = {
|
|
42
|
-
...pathParams,
|
|
43
|
-
data: typeSchemas.request?.name
|
|
44
|
-
? {
|
|
45
|
-
type: typeSchemas.request?.name,
|
|
46
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
47
|
-
}
|
|
48
|
-
: undefined,
|
|
49
|
-
params: typeSchemas.queryParams?.name
|
|
50
|
-
? {
|
|
51
|
-
type: typeSchemas.queryParams?.name,
|
|
52
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
53
|
-
}
|
|
54
|
-
: undefined,
|
|
55
|
-
headers: typeSchemas.headerParams?.name
|
|
56
|
-
? {
|
|
57
|
-
type: typeSchemas.headerParams?.name,
|
|
58
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
59
|
-
}
|
|
60
|
-
: undefined,
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
// Check if all children are optional or undefined
|
|
64
|
-
const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
|
|
65
|
-
|
|
66
|
-
return FunctionParams.factory({
|
|
67
|
-
data: {
|
|
68
|
-
mode: 'object',
|
|
69
|
-
children,
|
|
70
|
-
default: allChildrenAreOptional ? '{}' : undefined,
|
|
71
|
-
},
|
|
72
|
-
options: {
|
|
73
|
-
type: `
|
|
74
|
-
{
|
|
75
|
-
query?: Partial<UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient },
|
|
76
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
|
|
77
|
-
}
|
|
78
|
-
`,
|
|
79
|
-
default: '{}',
|
|
80
|
-
},
|
|
81
|
-
})
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return FunctionParams.factory({
|
|
85
|
-
pathParams: typeSchemas.pathParams?.name
|
|
86
|
-
? {
|
|
87
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
88
|
-
children: getPathParams(typeSchemas.pathParams, {
|
|
89
|
-
typed: true,
|
|
90
|
-
casing: paramsCasing,
|
|
91
|
-
}),
|
|
92
|
-
default: isAllOptional(typeSchemas.pathParams?.schema) ? '{}' : undefined,
|
|
93
|
-
}
|
|
94
|
-
: undefined,
|
|
95
|
-
data: typeSchemas.request?.name
|
|
96
|
-
? {
|
|
97
|
-
type: typeSchemas.request?.name,
|
|
98
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
99
|
-
}
|
|
100
|
-
: undefined,
|
|
101
|
-
params: typeSchemas.queryParams?.name
|
|
102
|
-
? {
|
|
103
|
-
type: typeSchemas.queryParams?.name,
|
|
104
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
105
|
-
}
|
|
106
|
-
: undefined,
|
|
107
|
-
headers: typeSchemas.headerParams?.name
|
|
108
|
-
? {
|
|
109
|
-
type: typeSchemas.headerParams?.name,
|
|
110
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
111
|
-
}
|
|
112
|
-
: undefined,
|
|
113
|
-
options: {
|
|
114
|
-
type: `
|
|
115
|
-
{
|
|
116
|
-
query?: Partial<UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, ${pageParamGeneric}>> & { client?: QueryClient },
|
|
117
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'}
|
|
118
|
-
}
|
|
119
|
-
`,
|
|
120
|
-
default: '{}',
|
|
121
|
-
},
|
|
122
|
-
})
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
export function SuspenseInfiniteQuery({
|
|
126
|
-
name,
|
|
127
|
-
queryKeyTypeName,
|
|
128
|
-
queryOptionsName,
|
|
129
|
-
queryKeyName,
|
|
130
|
-
paramsType,
|
|
131
|
-
paramsCasing,
|
|
132
|
-
pathParamsType,
|
|
133
|
-
dataReturnType,
|
|
134
|
-
typeSchemas,
|
|
135
|
-
operation,
|
|
136
|
-
customOptions,
|
|
137
|
-
initialPageParam,
|
|
138
|
-
queryParam,
|
|
139
|
-
}: Props): FabricReactNode {
|
|
140
|
-
const responseType = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
141
|
-
const errorType = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
|
|
142
|
-
const isInitialPageParamDefined = initialPageParam !== undefined && initialPageParam !== null
|
|
143
|
-
const fallbackPageParamType =
|
|
144
|
-
typeof initialPageParam === 'number'
|
|
145
|
-
? 'number'
|
|
146
|
-
: typeof initialPageParam === 'string'
|
|
147
|
-
? initialPageParam.includes(' as ')
|
|
148
|
-
? (() => {
|
|
149
|
-
const parts = initialPageParam.split(' as ')
|
|
150
|
-
return parts[parts.length - 1] ?? 'unknown'
|
|
151
|
-
})()
|
|
152
|
-
: 'string'
|
|
153
|
-
: typeof initialPageParam === 'boolean'
|
|
154
|
-
? 'boolean'
|
|
155
|
-
: 'unknown'
|
|
156
|
-
const queryParamType = queryParam && typeSchemas.queryParams?.name ? `${typeSchemas.queryParams?.name}['${queryParam}']` : undefined
|
|
157
|
-
const pageParamType = queryParamType ? (isInitialPageParamDefined ? `NonNullable<${queryParamType}>` : queryParamType) : fallbackPageParamType
|
|
158
|
-
const returnType = 'UseSuspenseInfiniteQueryResult<TData, TError> & { queryKey: TQueryKey }'
|
|
159
|
-
const generics = [
|
|
160
|
-
`TQueryFnData = ${responseType}`,
|
|
161
|
-
`TError = ${errorType}`,
|
|
162
|
-
'TData = InfiniteData<TQueryFnData>',
|
|
163
|
-
`TQueryKey extends QueryKey = ${queryKeyTypeName}`,
|
|
164
|
-
`TPageParam = ${pageParamType}`,
|
|
165
|
-
]
|
|
166
|
-
|
|
167
|
-
const queryKeyParams = QueryKey.getParams({
|
|
168
|
-
pathParamsType,
|
|
169
|
-
typeSchemas,
|
|
170
|
-
paramsCasing,
|
|
171
|
-
})
|
|
172
|
-
const queryOptionsParams = QueryOptions.getParams({
|
|
173
|
-
paramsType,
|
|
174
|
-
pathParamsType,
|
|
175
|
-
typeSchemas,
|
|
176
|
-
paramsCasing,
|
|
177
|
-
})
|
|
178
|
-
const params = getParams({
|
|
179
|
-
paramsCasing,
|
|
180
|
-
paramsType,
|
|
181
|
-
pathParamsType,
|
|
182
|
-
typeSchemas,
|
|
183
|
-
pageParamGeneric: 'TPageParam',
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()})`
|
|
187
|
-
|
|
188
|
-
return (
|
|
189
|
-
<File.Source name={name} isExportable isIndexable>
|
|
190
|
-
<Function
|
|
191
|
-
name={name}
|
|
192
|
-
export
|
|
193
|
-
generics={generics.join(', ')}
|
|
194
|
-
params={params.toConstructor()}
|
|
195
|
-
JSDoc={{
|
|
196
|
-
comments: getComments(operation),
|
|
197
|
-
}}
|
|
198
|
-
>
|
|
199
|
-
{`
|
|
200
|
-
const { query: queryConfig = {}, client: config = {} } = options ?? {}
|
|
201
|
-
const { client: queryClient, ...resolvedOptions } = queryConfig
|
|
202
|
-
const queryKey = resolvedOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
|
|
203
|
-
${customOptions ? `const customOptions = ${customOptions.name}({ hookName: '${name}', operationId: '${operation.getOperationId()}' })` : ''}
|
|
204
|
-
|
|
205
|
-
const query = useSuspenseInfiniteQuery({
|
|
206
|
-
...${queryOptions},${customOptions ? '\n...customOptions,' : ''}
|
|
207
|
-
...resolvedOptions,
|
|
208
|
-
queryKey,
|
|
209
|
-
} as unknown as UseSuspenseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>, queryClient) as ${returnType}
|
|
210
|
-
|
|
211
|
-
query.queryKey = queryKey as TQueryKey
|
|
212
|
-
|
|
213
|
-
return query
|
|
214
|
-
`}
|
|
215
|
-
</Function>
|
|
216
|
-
</File.Source>
|
|
217
|
-
)
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
SuspenseInfiniteQuery.getParams = getParams
|