@kubb/plugin-react-query 3.0.0-alpha.0
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 +21 -0
- package/README.md +114 -0
- package/dist/chunk-5IL6M74X.js +1504 -0
- package/dist/chunk-5IL6M74X.js.map +1 -0
- package/dist/chunk-JFX7DCS7.cjs +1504 -0
- package/dist/chunk-JFX7DCS7.cjs.map +1 -0
- package/dist/components.cjs +15 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +8 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.js +15 -0
- package/dist/components.js.map +1 -0
- package/dist/index-yXskx3Td.d.cts +584 -0
- package/dist/index-yXskx3Td.d.ts +584 -0
- package/dist/index.cjs +223 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +223 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -0
- package/src/OperationGenerator.tsx +86 -0
- package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +64 -0
- package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +57 -0
- package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +63 -0
- package/src/__snapshots__/queryOptions/getPetById.ts +37 -0
- package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +47 -0
- package/src/__snapshots__/upload/UploadFile.ts +67 -0
- package/src/__snapshots__/uploadMutation/UploadFile.ts +44 -0
- package/src/__snapshots__/variablesTypeMutate/deletePet.ts +21 -0
- package/src/components/Mutation.tsx +341 -0
- package/src/components/Operations.tsx +74 -0
- package/src/components/Query.tsx +627 -0
- package/src/components/QueryImports.tsx +167 -0
- package/src/components/QueryKey.tsx +200 -0
- package/src/components/QueryOptions.tsx +487 -0
- package/src/components/SchemaType.tsx +55 -0
- package/src/components/__snapshots__/gen/showPetById.ts +57 -0
- package/src/components/__snapshots__/gen/useCreatePets.ts +46 -0
- package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +47 -0
- package/src/components/index.ts +5 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +183 -0
- package/src/types.ts +240 -0
- package/src/utils.ts +96 -0
|
@@ -0,0 +1,627 @@
|
|
|
1
|
+
import { PackageManager } from '@kubb/core'
|
|
2
|
+
import transformers from '@kubb/core/transformers'
|
|
3
|
+
import { FunctionParams, URLPath } from '@kubb/core/utils'
|
|
4
|
+
import { Parser, File, Function, useApp } from '@kubb/react'
|
|
5
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
|
+
import { pluginZodName } from '@kubb/plugin-zod'
|
|
7
|
+
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
8
|
+
import { getASTParams, getComments } from '@kubb/plugin-oas/utils'
|
|
9
|
+
|
|
10
|
+
import { getImportNames, reactQueryDepRegex } from '../utils.ts'
|
|
11
|
+
import { QueryImports } from './QueryImports.tsx'
|
|
12
|
+
import { QueryKey } from './QueryKey.tsx'
|
|
13
|
+
import { QueryOptions } from './QueryOptions.tsx'
|
|
14
|
+
import { SchemaType } from './SchemaType.tsx'
|
|
15
|
+
|
|
16
|
+
import { isRequired } from '@kubb/oas'
|
|
17
|
+
import type { ReactNode } from 'react'
|
|
18
|
+
import type { QueryOptions as QueryOptionsPluginOptions, Query as QueryPluginOptions } from '../types.ts'
|
|
19
|
+
import type { FileMeta, Infinite, PluginReactQuery, Suspense } from '../types.ts'
|
|
20
|
+
|
|
21
|
+
type TemplateProps = {
|
|
22
|
+
/**
|
|
23
|
+
* Name of the function
|
|
24
|
+
*/
|
|
25
|
+
name: string
|
|
26
|
+
/**
|
|
27
|
+
* Parameters/options/props that need to be used
|
|
28
|
+
*/
|
|
29
|
+
params: string
|
|
30
|
+
/**
|
|
31
|
+
* Generics that needs to be added for TypeScript
|
|
32
|
+
*/
|
|
33
|
+
generics?: string
|
|
34
|
+
/**
|
|
35
|
+
* ReturnType(see async for adding Promise type)
|
|
36
|
+
*/
|
|
37
|
+
returnType?: string
|
|
38
|
+
/**
|
|
39
|
+
* Options for JSdocs
|
|
40
|
+
*/
|
|
41
|
+
JSDoc?: {
|
|
42
|
+
comments: string[]
|
|
43
|
+
}
|
|
44
|
+
hook: {
|
|
45
|
+
name: string
|
|
46
|
+
generics?: string
|
|
47
|
+
queryKey: string
|
|
48
|
+
queryOptions: string
|
|
49
|
+
}
|
|
50
|
+
infinite: Infinite | false
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function Template({ name, generics, returnType, params, JSDoc, hook, infinite }: TemplateProps): ReactNode {
|
|
54
|
+
const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
|
|
55
|
+
const resolvedReturnType = `${returnType} & { queryKey: TQueryKey }`
|
|
56
|
+
|
|
57
|
+
if (isV5) {
|
|
58
|
+
return (
|
|
59
|
+
<>
|
|
60
|
+
<Function name={name} export generics={generics} returnType={resolvedReturnType} params={params} JSDoc={JSDoc}>
|
|
61
|
+
{`
|
|
62
|
+
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
|
|
63
|
+
const queryKey = queryOptions?.queryKey ?? ${hook.queryKey}
|
|
64
|
+
|
|
65
|
+
const query = ${hook.name}({
|
|
66
|
+
...${hook.queryOptions} as unknown as ${infinite ? 'InfiniteQueryObserverOptions' : 'QueryObserverOptions'},
|
|
67
|
+
queryKey,
|
|
68
|
+
...queryOptions as unknown as ${infinite ? 'Omit<InfiniteQueryObserverOptions, "queryKey">' : 'Omit<QueryObserverOptions, "queryKey">'}
|
|
69
|
+
}) as ${resolvedReturnType}
|
|
70
|
+
|
|
71
|
+
query.queryKey = queryKey as TQueryKey
|
|
72
|
+
|
|
73
|
+
return query
|
|
74
|
+
|
|
75
|
+
`}
|
|
76
|
+
</Function>
|
|
77
|
+
</>
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<>
|
|
83
|
+
<Function name={name} export generics={generics} returnType={resolvedReturnType} params={params} JSDoc={JSDoc}>
|
|
84
|
+
{`
|
|
85
|
+
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
|
|
86
|
+
const queryKey = queryOptions?.queryKey ?? ${hook.queryKey}
|
|
87
|
+
|
|
88
|
+
const query = ${hook.name}<${hook.generics}>({
|
|
89
|
+
...${hook.queryOptions},
|
|
90
|
+
queryKey,
|
|
91
|
+
...queryOptions
|
|
92
|
+
}) as ${resolvedReturnType}
|
|
93
|
+
|
|
94
|
+
query.queryKey = queryKey as TQueryKey
|
|
95
|
+
|
|
96
|
+
return query
|
|
97
|
+
|
|
98
|
+
`}
|
|
99
|
+
</Function>
|
|
100
|
+
</>
|
|
101
|
+
)
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
type FrameworkProps = TemplateProps & {
|
|
105
|
+
context: {
|
|
106
|
+
factory: {
|
|
107
|
+
name: string
|
|
108
|
+
}
|
|
109
|
+
queryKey: string
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const defaultTemplates = {
|
|
114
|
+
get react() {
|
|
115
|
+
return function (props: FrameworkProps): ReactNode {
|
|
116
|
+
return <Template {...props} />
|
|
117
|
+
}
|
|
118
|
+
},
|
|
119
|
+
get solid() {
|
|
120
|
+
return function (props: FrameworkProps): ReactNode {
|
|
121
|
+
return <Template {...props} />
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
get svelte() {
|
|
125
|
+
return function (props: FrameworkProps): ReactNode {
|
|
126
|
+
return <Template {...props} />
|
|
127
|
+
}
|
|
128
|
+
},
|
|
129
|
+
get vue() {
|
|
130
|
+
return function ({ context, hook, ...rest }: FrameworkProps): ReactNode {
|
|
131
|
+
const { factory, queryKey } = context
|
|
132
|
+
|
|
133
|
+
const {
|
|
134
|
+
pluginManager,
|
|
135
|
+
plugin: {
|
|
136
|
+
key: pluginKey,
|
|
137
|
+
options: { pathParamsType },
|
|
138
|
+
},
|
|
139
|
+
} = useApp<PluginReactQuery>()
|
|
140
|
+
const operation = useOperation()
|
|
141
|
+
const { getSchemas } = useOperationManager()
|
|
142
|
+
|
|
143
|
+
const importNames = getImportNames()
|
|
144
|
+
|
|
145
|
+
const queryOptions = pluginManager.resolveName({
|
|
146
|
+
name: `${factory.name}QueryOptions`,
|
|
147
|
+
pluginKey,
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
const hookName = rest.infinite ? importNames.queryInfinite.vue.hookName : importNames.query.vue.hookName
|
|
151
|
+
const resultType = rest.infinite ? importNames.queryInfinite.vue.resultType : importNames.query.vue.resultType
|
|
152
|
+
const optionsType = rest.infinite ? importNames.queryInfinite.vue.optionsType : importNames.query.vue.optionsType
|
|
153
|
+
|
|
154
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
155
|
+
const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
|
|
156
|
+
const params = new FunctionParams()
|
|
157
|
+
const queryParams = new FunctionParams()
|
|
158
|
+
const queryKeyParams = new FunctionParams()
|
|
159
|
+
const client = {
|
|
160
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
161
|
+
withData: !!schemas.request?.name,
|
|
162
|
+
withPathParams: !!schemas.pathParams?.name,
|
|
163
|
+
withHeaders: !!schemas.headerParams?.name,
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const resultGenerics = ['TData', `${factory.name}['error']`]
|
|
167
|
+
|
|
168
|
+
// only needed for the options to override the useQuery options/params
|
|
169
|
+
const queryOptionsOverrideGenerics = [`${factory.name}['response']`, `${factory.name}['error']`, 'TData', 'TQueryKey']
|
|
170
|
+
const queryOptionsGenerics = ['TData', 'TQueryData']
|
|
171
|
+
|
|
172
|
+
params.add([
|
|
173
|
+
...(pathParamsType === 'object'
|
|
174
|
+
? [
|
|
175
|
+
getASTParams(schemas.pathParams, {
|
|
176
|
+
typed: true,
|
|
177
|
+
override: (item) => ({
|
|
178
|
+
...item,
|
|
179
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
180
|
+
}),
|
|
181
|
+
}),
|
|
182
|
+
]
|
|
183
|
+
: getASTParams(schemas.pathParams, {
|
|
184
|
+
typed: true,
|
|
185
|
+
override: (item) => ({
|
|
186
|
+
...item,
|
|
187
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
188
|
+
}),
|
|
189
|
+
})),
|
|
190
|
+
{
|
|
191
|
+
name: 'refParams',
|
|
192
|
+
type: `MaybeRef<${schemas.queryParams?.name}>`,
|
|
193
|
+
enabled: client.withQueryParams,
|
|
194
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
name: 'refHeaders',
|
|
198
|
+
type: `MaybeRef<${schemas.headerParams?.name}>`,
|
|
199
|
+
enabled: client.withHeaders,
|
|
200
|
+
required: isRequired(schemas.headerParams?.schema),
|
|
201
|
+
},
|
|
202
|
+
{
|
|
203
|
+
name: 'refData',
|
|
204
|
+
type: `MaybeRef<${schemas.request?.name}>`,
|
|
205
|
+
enabled: client.withData,
|
|
206
|
+
required: isRequired(schemas.request?.schema),
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
name: 'options',
|
|
210
|
+
type: `{
|
|
211
|
+
query?: Partial<${optionsType}<${queryOptionsOverrideGenerics.join(', ')}>>,
|
|
212
|
+
client?: ${factory.name}['client']['parameters']
|
|
213
|
+
}`,
|
|
214
|
+
default: '{}',
|
|
215
|
+
},
|
|
216
|
+
])
|
|
217
|
+
|
|
218
|
+
queryParams.add([
|
|
219
|
+
...getASTParams(schemas.pathParams, {
|
|
220
|
+
typed: false,
|
|
221
|
+
override: (item) => ({
|
|
222
|
+
...item,
|
|
223
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
224
|
+
}),
|
|
225
|
+
}),
|
|
226
|
+
{
|
|
227
|
+
name: 'refParams',
|
|
228
|
+
enabled: client.withQueryParams,
|
|
229
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
230
|
+
},
|
|
231
|
+
{
|
|
232
|
+
name: 'refHeaders',
|
|
233
|
+
enabled: client.withHeaders,
|
|
234
|
+
required: isRequired(schemas.headerParams?.schema),
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
name: 'clientOptions',
|
|
238
|
+
required: false,
|
|
239
|
+
},
|
|
240
|
+
])
|
|
241
|
+
|
|
242
|
+
queryKeyParams.add([
|
|
243
|
+
...(pathParamsType === 'object'
|
|
244
|
+
? [
|
|
245
|
+
getASTParams(schemas.pathParams, {
|
|
246
|
+
override: (item) => ({
|
|
247
|
+
...item,
|
|
248
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
249
|
+
}),
|
|
250
|
+
}),
|
|
251
|
+
]
|
|
252
|
+
: getASTParams(schemas.pathParams, {
|
|
253
|
+
override: (item) => ({
|
|
254
|
+
...item,
|
|
255
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
256
|
+
}),
|
|
257
|
+
})),
|
|
258
|
+
{
|
|
259
|
+
name: 'refParams',
|
|
260
|
+
enabled: client.withQueryParams,
|
|
261
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
262
|
+
},
|
|
263
|
+
{
|
|
264
|
+
name: 'refData',
|
|
265
|
+
enabled: client.withData,
|
|
266
|
+
required: isRequired(schemas.request?.schema),
|
|
267
|
+
},
|
|
268
|
+
])
|
|
269
|
+
|
|
270
|
+
return (
|
|
271
|
+
<Template
|
|
272
|
+
{...rest}
|
|
273
|
+
params={params.toString()}
|
|
274
|
+
returnType={`${resultType}<${resultGenerics.join(', ')}>`}
|
|
275
|
+
hook={{
|
|
276
|
+
...hook,
|
|
277
|
+
name: hookName,
|
|
278
|
+
queryOptions: isV5
|
|
279
|
+
? `${queryOptions}(${queryParams.toString()})`
|
|
280
|
+
: `${queryOptions}<${queryOptionsGenerics.join(', ')}>(${queryParams.toString()})`,
|
|
281
|
+
queryKey: `${queryKey}(${queryKeyParams.toString()})`,
|
|
282
|
+
}}
|
|
283
|
+
/>
|
|
284
|
+
)
|
|
285
|
+
}
|
|
286
|
+
},
|
|
287
|
+
} as const
|
|
288
|
+
|
|
289
|
+
type Props = {
|
|
290
|
+
factory: {
|
|
291
|
+
name: string
|
|
292
|
+
}
|
|
293
|
+
resultType: string
|
|
294
|
+
hookName: string
|
|
295
|
+
optionsType: string
|
|
296
|
+
infinite: Infinite | false
|
|
297
|
+
query: QueryPluginOptions | false
|
|
298
|
+
queryOptions: QueryOptionsPluginOptions | false
|
|
299
|
+
suspense: Suspense | false
|
|
300
|
+
/**
|
|
301
|
+
* This will make it possible to override the default behaviour.
|
|
302
|
+
*/
|
|
303
|
+
Template?: React.ComponentType<FrameworkProps>
|
|
304
|
+
/**
|
|
305
|
+
* This will make it possible to override the default behaviour.
|
|
306
|
+
*/
|
|
307
|
+
QueryKeyTemplate?: React.ComponentType<React.ComponentProps<typeof QueryKey.templates.react>>
|
|
308
|
+
/**
|
|
309
|
+
* This will make it possible to override the default behaviour.
|
|
310
|
+
*/
|
|
311
|
+
QueryOptionsTemplate?: React.ComponentType<React.ComponentProps<typeof QueryOptions.templates.react>>
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function Query({
|
|
315
|
+
factory,
|
|
316
|
+
optionsType,
|
|
317
|
+
hookName,
|
|
318
|
+
resultType,
|
|
319
|
+
Template = defaultTemplates.react,
|
|
320
|
+
QueryKeyTemplate = QueryKey.templates.react,
|
|
321
|
+
QueryOptionsTemplate = QueryOptions.templates.react,
|
|
322
|
+
...props
|
|
323
|
+
}: Props): ReactNode {
|
|
324
|
+
const {
|
|
325
|
+
pluginManager,
|
|
326
|
+
plugin: {
|
|
327
|
+
key: pluginKey,
|
|
328
|
+
options: { dataReturnType, pathParamsType },
|
|
329
|
+
},
|
|
330
|
+
} = useApp<PluginReactQuery>()
|
|
331
|
+
|
|
332
|
+
const operation = useOperation()
|
|
333
|
+
const { getSchemas, getName } = useOperationManager()
|
|
334
|
+
|
|
335
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
336
|
+
const name = getName(operation, { type: 'function' })
|
|
337
|
+
const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
|
|
338
|
+
|
|
339
|
+
const queryKey = pluginManager.resolveName({
|
|
340
|
+
name: [factory.name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined, 'QueryKey'].filter(Boolean).join(''),
|
|
341
|
+
pluginKey,
|
|
342
|
+
})
|
|
343
|
+
const queryKeyType = pluginManager.resolveName({
|
|
344
|
+
name: [factory.name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined, 'QueryKey'].filter(Boolean).join(''),
|
|
345
|
+
type: 'type',
|
|
346
|
+
pluginKey,
|
|
347
|
+
})
|
|
348
|
+
const queryOptions = pluginManager.resolveName({
|
|
349
|
+
name: [factory.name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined, 'QueryOptions'].filter(Boolean).join(''),
|
|
350
|
+
pluginKey,
|
|
351
|
+
})
|
|
352
|
+
|
|
353
|
+
const generics = new FunctionParams()
|
|
354
|
+
const params = new FunctionParams()
|
|
355
|
+
const queryParams = new FunctionParams()
|
|
356
|
+
const queryKeyParams = new FunctionParams()
|
|
357
|
+
//TODO operationManager.getCleitn
|
|
358
|
+
const client = {
|
|
359
|
+
method: operation.method,
|
|
360
|
+
path: new URLPath(operation.path),
|
|
361
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
362
|
+
withData: !!schemas.request?.name,
|
|
363
|
+
withPathParams: !!schemas.pathParams?.name,
|
|
364
|
+
withHeaders: !!schemas.headerParams?.name,
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
generics.add([
|
|
368
|
+
{
|
|
369
|
+
type: 'TData',
|
|
370
|
+
default: props.infinite ? `InfiniteData<${factory.name}["response"]>` : `${factory.name}["response"]`,
|
|
371
|
+
},
|
|
372
|
+
props.suspense ? undefined : { type: 'TQueryData', default: `${factory.name}["response"]` },
|
|
373
|
+
{ type: 'TQueryKey extends QueryKey', default: queryKeyType },
|
|
374
|
+
])
|
|
375
|
+
|
|
376
|
+
const resultGenerics = ['TData', `${factory.name}['error']`]
|
|
377
|
+
// only needed for the options to override the useQuery options/params
|
|
378
|
+
// suspense is having 4 generics instead of 5, TQueryData is not needed because data will always be defined
|
|
379
|
+
const queryOptionsOverrideGenerics = props.suspense
|
|
380
|
+
? [`${factory.name}['response']`, `${factory.name}['error']`, 'TData', 'TQueryKey']
|
|
381
|
+
: [`${factory.name}['response']`, `${factory.name}['error']`, 'TData', 'TQueryData', 'TQueryKey']
|
|
382
|
+
|
|
383
|
+
const queryOptionsGenerics = props.suspense ? ['TData'] : ['TData', 'TQueryData']
|
|
384
|
+
|
|
385
|
+
params.add([
|
|
386
|
+
...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams, { typed: true })] : getASTParams(schemas.pathParams, { typed: true })),
|
|
387
|
+
{
|
|
388
|
+
name: 'params',
|
|
389
|
+
type: `${factory.name}['queryParams']`,
|
|
390
|
+
enabled: client.withQueryParams,
|
|
391
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
name: 'headers',
|
|
395
|
+
type: `${factory.name}['headerParams']`,
|
|
396
|
+
enabled: client.withHeaders,
|
|
397
|
+
required: isRequired(schemas.headerParams?.schema),
|
|
398
|
+
},
|
|
399
|
+
{
|
|
400
|
+
name: 'data',
|
|
401
|
+
type: `${factory.name}['request']`,
|
|
402
|
+
enabled: client.withData,
|
|
403
|
+
required: isRequired(schemas.request?.schema),
|
|
404
|
+
},
|
|
405
|
+
{
|
|
406
|
+
name: 'options',
|
|
407
|
+
type: `{
|
|
408
|
+
query?: Partial<${optionsType}<${queryOptionsOverrideGenerics.join(', ')}>>,
|
|
409
|
+
client?: ${factory.name}['client']['parameters']
|
|
410
|
+
}`,
|
|
411
|
+
default: '{}',
|
|
412
|
+
},
|
|
413
|
+
])
|
|
414
|
+
|
|
415
|
+
queryParams.add([
|
|
416
|
+
...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams)] : getASTParams(schemas.pathParams)),
|
|
417
|
+
{
|
|
418
|
+
name: 'params',
|
|
419
|
+
enabled: client.withQueryParams,
|
|
420
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
name: 'headers',
|
|
424
|
+
enabled: client.withHeaders,
|
|
425
|
+
required: isRequired(schemas.headerParams?.schema),
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
name: 'data',
|
|
429
|
+
enabled: client.withData,
|
|
430
|
+
required: isRequired(schemas.request?.schema),
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
name: 'clientOptions',
|
|
434
|
+
required: false,
|
|
435
|
+
},
|
|
436
|
+
])
|
|
437
|
+
|
|
438
|
+
queryKeyParams.add([
|
|
439
|
+
...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams)] : getASTParams(schemas.pathParams)),
|
|
440
|
+
{
|
|
441
|
+
name: 'params',
|
|
442
|
+
enabled: client.withQueryParams,
|
|
443
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
444
|
+
},
|
|
445
|
+
{
|
|
446
|
+
name: 'data',
|
|
447
|
+
enabled: client.withData,
|
|
448
|
+
required: isRequired(schemas.request?.schema),
|
|
449
|
+
},
|
|
450
|
+
])
|
|
451
|
+
|
|
452
|
+
const hook = {
|
|
453
|
+
name: hookName,
|
|
454
|
+
generics: [isV5 ? 'any' : `${factory.name}['data']`, `${factory.name}['error']`, 'TData', 'any'].join(', '),
|
|
455
|
+
queryOptions: isV5 ? `${queryOptions}(${queryParams.toString()})` : `${queryOptions}<${queryOptionsGenerics.join(', ')}>(${queryParams.toString()})`,
|
|
456
|
+
queryKey: `${queryKey}(${queryKeyParams.toString()})`,
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
return (
|
|
460
|
+
<>
|
|
461
|
+
<QueryKey
|
|
462
|
+
keysFn={props.query ? props.query.queryKey : (keys: unknown[]) => keys}
|
|
463
|
+
Template={QueryKeyTemplate}
|
|
464
|
+
factory={factory}
|
|
465
|
+
name={queryKey}
|
|
466
|
+
typeName={queryKeyType}
|
|
467
|
+
/>
|
|
468
|
+
|
|
469
|
+
{props.queryOptions && (
|
|
470
|
+
<QueryOptions
|
|
471
|
+
Template={QueryOptionsTemplate}
|
|
472
|
+
factory={factory}
|
|
473
|
+
resultType={optionsType}
|
|
474
|
+
dataReturnType={dataReturnType}
|
|
475
|
+
infinite={props.infinite}
|
|
476
|
+
suspense={props.suspense}
|
|
477
|
+
/>
|
|
478
|
+
)}
|
|
479
|
+
|
|
480
|
+
{props.query && (
|
|
481
|
+
<Template
|
|
482
|
+
name={[name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined].filter(Boolean).join('')}
|
|
483
|
+
generics={generics.toString()}
|
|
484
|
+
JSDoc={{ comments: getComments(operation) }}
|
|
485
|
+
params={params.toString()}
|
|
486
|
+
returnType={`${resultType}<${resultGenerics.join(', ')}>`}
|
|
487
|
+
hook={hook}
|
|
488
|
+
infinite={props.infinite}
|
|
489
|
+
context={{
|
|
490
|
+
factory,
|
|
491
|
+
queryKey,
|
|
492
|
+
}}
|
|
493
|
+
/>
|
|
494
|
+
)}
|
|
495
|
+
</>
|
|
496
|
+
)
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
type FileProps = {
|
|
500
|
+
/**
|
|
501
|
+
* This will make it possible to override the default behaviour.
|
|
502
|
+
*/
|
|
503
|
+
templates?: {
|
|
504
|
+
query: typeof defaultTemplates
|
|
505
|
+
queryKey: typeof QueryKey.templates
|
|
506
|
+
queryOptions: typeof QueryOptions.templates
|
|
507
|
+
queryImports: typeof QueryImports.templates
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
Query.File = function ({ templates }: FileProps): ReactNode {
|
|
512
|
+
const {
|
|
513
|
+
plugin: {
|
|
514
|
+
options: {
|
|
515
|
+
client: { importPath },
|
|
516
|
+
infinite,
|
|
517
|
+
suspense,
|
|
518
|
+
query,
|
|
519
|
+
queryOptions,
|
|
520
|
+
parser,
|
|
521
|
+
},
|
|
522
|
+
},
|
|
523
|
+
} = useApp<PluginReactQuery>()
|
|
524
|
+
|
|
525
|
+
const { getSchemas, getFile, getName } = useOperationManager()
|
|
526
|
+
const operation = useOperation()
|
|
527
|
+
|
|
528
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
529
|
+
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
|
|
530
|
+
const file = getFile(operation)
|
|
531
|
+
const fileType = getFile(operation, { pluginKey: [pluginTsName] })
|
|
532
|
+
const fileZodSchemas = getFile(operation, {
|
|
533
|
+
pluginKey: [pluginZodName],
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
const factoryName = getName(operation, { type: 'type' })
|
|
537
|
+
|
|
538
|
+
const importNames = getImportNames()
|
|
539
|
+
const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
|
|
540
|
+
const Template = templates?.query['react'] || defaultTemplates['react']
|
|
541
|
+
const QueryOptionsTemplate = templates?.queryOptions['react'] || QueryOptions.templates['react']
|
|
542
|
+
const QueryKeyTemplate = templates?.queryKey['react'] || QueryKey.templates['react']
|
|
543
|
+
const Import = templates?.queryImports['react'] || QueryImports.templates['react']
|
|
544
|
+
|
|
545
|
+
const factory = {
|
|
546
|
+
name: factoryName,
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return (
|
|
550
|
+
<Parser language="typescript">
|
|
551
|
+
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
552
|
+
{parser === 'zod' && <File.Import name={[zodSchemas.response?.name]} root={file.path} path={fileZodSchemas.path} />}
|
|
553
|
+
<File.Import name={'client'} path={importPath} />
|
|
554
|
+
<File.Import name={['ResponseConfig']} path={importPath} isTypeOnly />
|
|
555
|
+
<File.Import
|
|
556
|
+
name={[
|
|
557
|
+
schemas.request?.name,
|
|
558
|
+
schemas.response.name,
|
|
559
|
+
schemas.pathParams?.name,
|
|
560
|
+
schemas.queryParams?.name,
|
|
561
|
+
schemas.headerParams?.name,
|
|
562
|
+
...(schemas.errors?.map((error) => error.name) || []),
|
|
563
|
+
].filter(Boolean)}
|
|
564
|
+
root={file.path}
|
|
565
|
+
path={fileType.path}
|
|
566
|
+
isTypeOnly
|
|
567
|
+
/>
|
|
568
|
+
|
|
569
|
+
<QueryImports hookPath={typeof query !== 'boolean' ? query.importPath : undefined} Template={Import} isInfinite={false} isSuspense={false} />
|
|
570
|
+
{!!infinite && (
|
|
571
|
+
<QueryImports hookPath={typeof query !== 'boolean' ? query.importPath : undefined} Template={Import} isInfinite={true} isSuspense={false} />
|
|
572
|
+
)}
|
|
573
|
+
{!!suspense && isV5 && (
|
|
574
|
+
<QueryImports hookPath={typeof query !== 'boolean' ? query.importPath : undefined} Template={Import} isInfinite={false} isSuspense={true} />
|
|
575
|
+
)}
|
|
576
|
+
<File.Source>
|
|
577
|
+
<SchemaType />
|
|
578
|
+
<Query
|
|
579
|
+
factory={factory}
|
|
580
|
+
Template={Template}
|
|
581
|
+
QueryKeyTemplate={QueryKeyTemplate}
|
|
582
|
+
QueryOptionsTemplate={QueryOptionsTemplate}
|
|
583
|
+
infinite={false}
|
|
584
|
+
suspense={false}
|
|
585
|
+
query={query}
|
|
586
|
+
queryOptions={queryOptions}
|
|
587
|
+
hookName={importNames.query['react'].hookName}
|
|
588
|
+
resultType={importNames.query['react'].resultType}
|
|
589
|
+
optionsType={importNames.query['react'].optionsType}
|
|
590
|
+
/>
|
|
591
|
+
{!!infinite && (
|
|
592
|
+
<Query
|
|
593
|
+
factory={factory}
|
|
594
|
+
Template={Template}
|
|
595
|
+
QueryKeyTemplate={QueryKeyTemplate}
|
|
596
|
+
QueryOptionsTemplate={QueryOptionsTemplate}
|
|
597
|
+
infinite={infinite}
|
|
598
|
+
suspense={false}
|
|
599
|
+
query={query}
|
|
600
|
+
queryOptions={queryOptions}
|
|
601
|
+
hookName={importNames.queryInfinite['react'].hookName}
|
|
602
|
+
resultType={importNames.queryInfinite['react'].resultType}
|
|
603
|
+
optionsType={importNames.queryInfinite['react'].optionsType}
|
|
604
|
+
/>
|
|
605
|
+
)}
|
|
606
|
+
{!!suspense && isV5 && (
|
|
607
|
+
<Query
|
|
608
|
+
factory={factory}
|
|
609
|
+
Template={Template}
|
|
610
|
+
QueryKeyTemplate={QueryKeyTemplate}
|
|
611
|
+
QueryOptionsTemplate={QueryOptionsTemplate}
|
|
612
|
+
infinite={false}
|
|
613
|
+
suspense={suspense}
|
|
614
|
+
query={query}
|
|
615
|
+
queryOptions={queryOptions}
|
|
616
|
+
hookName={importNames.querySuspense['react'].hookName}
|
|
617
|
+
resultType={importNames.querySuspense['react'].resultType}
|
|
618
|
+
optionsType={importNames.querySuspense['react'].optionsType}
|
|
619
|
+
/>
|
|
620
|
+
)}
|
|
621
|
+
</File.Source>
|
|
622
|
+
</File>
|
|
623
|
+
</Parser>
|
|
624
|
+
)
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
Query.templates = defaultTemplates
|