@kubb/plugin-react-query 3.0.0-alpha.3 → 3.0.0-alpha.30

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.
Files changed (81) hide show
  1. package/README.md +13 -4
  2. package/dist/chunk-2D27Y6L5.js +618 -0
  3. package/dist/chunk-2D27Y6L5.js.map +1 -0
  4. package/dist/chunk-C2H3KPHM.cjs +658 -0
  5. package/dist/chunk-C2H3KPHM.cjs.map +1 -0
  6. package/dist/chunk-JW3GZUSC.cjs +627 -0
  7. package/dist/chunk-JW3GZUSC.cjs.map +1 -0
  8. package/dist/chunk-Y3DM2P6L.js +647 -0
  9. package/dist/chunk-Y3DM2P6L.js.map +1 -0
  10. package/dist/components.cjs +39 -14
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +161 -7
  13. package/dist/components.d.ts +161 -7
  14. package/dist/components.js +2 -14
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +25 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +14 -0
  19. package/dist/generators.d.ts +14 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +75 -154
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +1 -5
  25. package/dist/index.d.ts +1 -5
  26. package/dist/index.js +54 -140
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-rJasmoxy.d.cts +187 -0
  29. package/dist/types-rJasmoxy.d.ts +187 -0
  30. package/package.json +22 -17
  31. package/src/components/InfiniteQuery.tsx +129 -0
  32. package/src/components/InfiniteQueryOptions.tsx +130 -0
  33. package/src/components/Mutation.tsx +138 -322
  34. package/src/components/MutationKey.tsx +48 -0
  35. package/src/components/Query.tsx +91 -598
  36. package/src/components/QueryKey.tsx +51 -178
  37. package/src/components/QueryOptions.tsx +72 -466
  38. package/src/components/SuspenseQuery.tsx +129 -0
  39. package/src/components/index.ts +4 -1
  40. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +51 -0
  41. package/src/generators/__snapshots__/clientGetImportPath.ts +51 -0
  42. package/src/generators/__snapshots__/clientPostImportPath.ts +44 -0
  43. package/src/generators/__snapshots__/findByTags.ts +51 -0
  44. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  45. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  46. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  47. package/src/generators/__snapshots__/findInfiniteByTags.ts +57 -0
  48. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +57 -0
  49. package/src/generators/__snapshots__/getAsMutation.ts +31 -0
  50. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  51. package/src/generators/__snapshots__/updatePetById.ts +44 -0
  52. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +46 -0
  53. package/src/generators/index.ts +4 -0
  54. package/src/generators/infiniteQueryGenerator.tsx +124 -0
  55. package/src/generators/mutationGenerator.tsx +108 -0
  56. package/src/generators/queryGenerator.tsx +121 -0
  57. package/src/generators/suspenseQueryGenerator.tsx +120 -0
  58. package/src/plugin.ts +51 -75
  59. package/src/types.ts +49 -88
  60. package/dist/chunk-5IL6M74X.js +0 -1504
  61. package/dist/chunk-5IL6M74X.js.map +0 -1
  62. package/dist/chunk-JFX7DCS7.cjs +0 -1504
  63. package/dist/chunk-JFX7DCS7.cjs.map +0 -1
  64. package/dist/index-C9fwRDH7.d.cts +0 -579
  65. package/dist/index-C9fwRDH7.d.ts +0 -579
  66. package/src/OperationGenerator.tsx +0 -86
  67. package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +0 -64
  68. package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +0 -57
  69. package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +0 -63
  70. package/src/__snapshots__/queryOptions/getPetById.ts +0 -37
  71. package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +0 -47
  72. package/src/__snapshots__/upload/UploadFile.ts +0 -67
  73. package/src/__snapshots__/uploadMutation/UploadFile.ts +0 -44
  74. package/src/__snapshots__/variablesTypeMutate/deletePet.ts +0 -21
  75. package/src/components/Operations.tsx +0 -74
  76. package/src/components/QueryImports.tsx +0 -167
  77. package/src/components/SchemaType.tsx +0 -55
  78. package/src/components/__snapshots__/gen/showPetById.ts +0 -57
  79. package/src/components/__snapshots__/gen/useCreatePets.ts +0 -46
  80. package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +0 -47
  81. package/src/utils.ts +0 -96
@@ -1,627 +1,120 @@
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'
1
+ import { File, Function, FunctionParams } from '@kubb/react'
9
2
 
10
- import { getImportNames, reactQueryDepRegex } from '../utils.ts'
11
- import { QueryImports } from './QueryImports.tsx'
3
+ import { type Operation, isOptional } from '@kubb/oas'
4
+ import type { OperationSchemas } from '@kubb/plugin-oas'
5
+ import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
6
+ import type { ReactNode } from 'react'
7
+ import type { PluginReactQuery } from '../types.ts'
12
8
  import { QueryKey } from './QueryKey.tsx'
13
9
  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
10
 
21
- type TemplateProps = {
11
+ type Props = {
22
12
  /**
23
13
  * Name of the function
24
14
  */
25
15
  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
16
+ queryOptionsName: string
17
+ queryKeyName: string
18
+ queryKeyTypeName: string
19
+ typeSchemas: OperationSchemas
20
+ operation: Operation
21
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
22
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
51
23
  }
52
24
 
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
- }
25
+ type GetParamsProps = {
26
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
27
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
28
+ typeSchemas: OperationSchemas
111
29
  }
112
30
 
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']`]
31
+ function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
32
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
167
33
 
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>>
34
+ return FunctionParams.factory({
35
+ pathParams: {
36
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
37
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
38
+ },
39
+ data: typeSchemas.request?.name
40
+ ? {
41
+ type: typeSchemas.request?.name,
42
+ optional: isOptional(typeSchemas.request?.schema),
43
+ }
44
+ : undefined,
45
+ params: typeSchemas.queryParams?.name
46
+ ? {
47
+ type: typeSchemas.queryParams?.name,
48
+ optional: isOptional(typeSchemas.queryParams?.schema),
49
+ }
50
+ : undefined,
51
+ headers: typeSchemas.headerParams?.name
52
+ ? {
53
+ type: typeSchemas.headerParams?.name,
54
+ optional: isOptional(typeSchemas.headerParams?.schema),
55
+ }
56
+ : undefined,
57
+ options: {
58
+ type: `
59
+ {
60
+ query?: Partial<QueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
61
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
312
62
  }
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 },
63
+ `,
64
+ default: '{}',
329
65
  },
330
- } = useApp<PluginReactQuery>()
331
-
332
- const operation = useOperation()
333
- const { getSchemas, getName } = useOperationManager()
66
+ })
67
+ }
334
68
 
335
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
336
- const name = getName(operation, { type: 'function' })
337
- const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
69
+ export function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode {
70
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
71
+ const returnType = `UseQueryResult<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'].join(', ')}> & { queryKey: TQueryKey }`
72
+ const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
338
73
 
339
- const queryKey = pluginManager.resolveName({
340
- name: [factory.name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined, 'QueryKey'].filter(Boolean).join(''),
341
- pluginKey,
74
+ const queryKeyParams = QueryKey.getParams({
75
+ pathParamsType,
76
+ typeSchemas,
342
77
  })
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,
78
+ const queryOptionsParams = QueryOptions.getParams({
79
+ pathParamsType,
80
+ typeSchemas,
347
81
  })
348
- const queryOptions = pluginManager.resolveName({
349
- name: [factory.name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined, 'QueryOptions'].filter(Boolean).join(''),
350
- pluginKey,
82
+ const params = getParams({
83
+ pathParamsType,
84
+ dataReturnType,
85
+ typeSchemas,
351
86
  })
352
87
 
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
- }
88
+ const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as QueryObserverOptions`
458
89
 
459
90
  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']
91
+ <File.Source name={name} isExportable isIndexable>
92
+ <Function
93
+ name={name}
94
+ export
95
+ generics={generics.join(', ')}
96
+ params={params.toConstructor()}
97
+ JSDoc={{
98
+ comments: getComments(operation),
99
+ }}
100
+ >
101
+ {`
102
+ const { query: queryOptions, client: config = {} } = options ?? {}
103
+ const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
544
104
 
545
- const factory = {
546
- name: factoryName,
547
- }
105
+ const query = useQuery({
106
+ ...${queryOptions},
107
+ queryKey,
108
+ ...queryOptions as unknown as Omit<QueryObserverOptions, "queryKey">
109
+ }) as ${returnType}
548
110
 
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
- />
111
+ query.queryKey = queryKey as TQueryKey
568
112
 
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>
113
+ return query
114
+ `}
115
+ </Function>
116
+ </File.Source>
624
117
  )
625
118
  }
626
119
 
627
- Query.templates = defaultTemplates
120
+ Query.getParams = getParams