@kubb/plugin-react-query 3.0.0-alpha.9 → 3.0.0-beta.10

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 (79) hide show
  1. package/README.md +14 -5
  2. package/dist/chunk-24SL7I66.cjs +683 -0
  3. package/dist/chunk-24SL7I66.cjs.map +1 -0
  4. package/dist/chunk-BML6BZ4F.cjs +878 -0
  5. package/dist/chunk-BML6BZ4F.cjs.map +1 -0
  6. package/dist/chunk-JFIGHRBM.js +867 -0
  7. package/dist/chunk-JFIGHRBM.js.map +1 -0
  8. package/dist/chunk-LBVGJA4Q.js +674 -0
  9. package/dist/chunk-LBVGJA4Q.js.map +1 -0
  10. package/dist/components.cjs +39 -12
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +172 -6
  13. package/dist/components.d.ts +172 -6
  14. package/dist/components.js +2 -12
  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 +80 -130
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +1 -4
  25. package/dist/index.d.ts +1 -4
  26. package/dist/index.js +63 -120
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-Dlwxp28D.d.cts +230 -0
  29. package/dist/types-Dlwxp28D.d.ts +230 -0
  30. package/package.json +22 -17
  31. package/src/components/InfiniteQuery.tsx +176 -0
  32. package/src/components/InfiniteQueryOptions.tsx +185 -0
  33. package/src/components/Mutation.tsx +142 -320
  34. package/src/components/MutationKey.tsx +48 -0
  35. package/src/components/Query.tsx +145 -592
  36. package/src/components/QueryKey.tsx +51 -182
  37. package/src/components/QueryOptions.tsx +110 -472
  38. package/src/components/SuspenseQuery.tsx +176 -0
  39. package/src/components/index.ts +4 -0
  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__/findByTagsObject.ts +60 -0
  45. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  46. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  47. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  48. package/src/generators/__snapshots__/findInfiniteByTags.ts +57 -0
  49. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +57 -0
  50. package/src/generators/__snapshots__/getAsMutation.ts +31 -0
  51. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  52. package/src/generators/__snapshots__/updatePetById.ts +44 -0
  53. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +44 -0
  54. package/src/generators/index.ts +4 -0
  55. package/src/generators/infiniteQueryGenerator.tsx +126 -0
  56. package/src/generators/mutationGenerator.tsx +109 -0
  57. package/src/generators/queryGenerator.tsx +123 -0
  58. package/src/generators/suspenseQueryGenerator.tsx +123 -0
  59. package/src/plugin.ts +62 -76
  60. package/src/types.ts +49 -126
  61. package/dist/chunk-7ZODZVKP.cjs +0 -1470
  62. package/dist/chunk-7ZODZVKP.cjs.map +0 -1
  63. package/dist/chunk-ZYTZV43V.js +0 -1470
  64. package/dist/chunk-ZYTZV43V.js.map +0 -1
  65. package/dist/index-5kpkk-7M.d.cts +0 -545
  66. package/dist/index-5kpkk-7M.d.ts +0 -545
  67. package/src/OperationGenerator.tsx +0 -57
  68. package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +0 -69
  69. package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +0 -62
  70. package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +0 -68
  71. package/src/__snapshots__/queryOptions/getPetById.ts +0 -41
  72. package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +0 -51
  73. package/src/__snapshots__/variablesTypeMutate/deletePet.ts +0 -24
  74. package/src/components/QueryImports.tsx +0 -167
  75. package/src/components/SchemaType.tsx +0 -59
  76. package/src/components/__snapshots__/gen/showPetById.ts +0 -67
  77. package/src/components/__snapshots__/gen/useCreatePets.ts +0 -46
  78. package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +0 -46
  79. package/src/utils.ts +0 -96
@@ -1,623 +1,176 @@
1
- import { PackageManager } from '@kubb/core'
2
- import transformers from '@kubb/core/transformers'
3
- import { FunctionParams, URLPath } from '@kubb/core/utils'
4
- import { 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
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
- <File.Source name={name} isExportable isIndexable>
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
- </File.Source>
78
- )
79
- }
80
-
81
- return (
82
- <File.Source name={name} isExportable isIndexable>
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
- </File.Source>
101
- )
16
+ queryOptionsName: string
17
+ queryKeyName: string
18
+ queryKeyTypeName: string
19
+ typeSchemas: OperationSchemas
20
+ operation: Operation
21
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
22
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
23
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
102
24
  }
103
25
 
104
- type FrameworkProps = TemplateProps & {
105
- context: {
106
- factory: {
107
- name: string
108
- }
109
- queryKey: string
110
- }
26
+ type GetParamsProps = {
27
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
28
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
29
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
30
+ typeSchemas: OperationSchemas
111
31
  }
112
32
 
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),
33
+ function getParams({ paramsType, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
34
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
35
+
36
+ if (paramsType === 'object') {
37
+ return FunctionParams.factory({
38
+ data: {
39
+ mode: 'object',
40
+ children: {
41
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
42
+ data: typeSchemas.request?.name
43
+ ? {
44
+ type: typeSchemas.request?.name,
45
+ optional: isOptional(typeSchemas.request?.schema),
46
+ }
47
+ : undefined,
48
+ params: typeSchemas.queryParams?.name
49
+ ? {
50
+ type: typeSchemas.queryParams?.name,
51
+ optional: isOptional(typeSchemas.queryParams?.schema),
52
+ }
53
+ : undefined,
54
+ headers: typeSchemas.headerParams?.name
55
+ ? {
56
+ type: typeSchemas.headerParams?.name,
57
+ optional: isOptional(typeSchemas.headerParams?.schema),
58
+ }
59
+ : undefined,
207
60
  },
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
61
+ },
62
+ options: {
63
+ type: `
64
+ {
65
+ query?: Partial<QueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
66
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
67
+ }
68
+ `,
69
+ default: '{}',
70
+ },
71
+ })
292
72
  }
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>>
73
+
74
+ return FunctionParams.factory({
75
+ pathParams: typeSchemas.pathParams?.name
76
+ ? {
77
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
78
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
79
+ type: typeSchemas.pathParams?.name,
80
+ optional: isOptional(typeSchemas.pathParams?.schema),
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
+ options: {
102
+ type: `
103
+ {
104
+ query?: Partial<QueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
105
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
106
+ }
107
+ `,
108
+ default: '{}',
109
+ },
110
+ })
312
111
  }
313
112
 
314
113
  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
114
+ name,
115
+ queryKeyTypeName,
116
+ queryOptionsName,
117
+ queryKeyName,
118
+ paramsType,
119
+ pathParamsType,
120
+ dataReturnType,
121
+ typeSchemas,
122
+ operation,
323
123
  }: Props): ReactNode {
324
- const {
325
- pluginManager,
326
- plugin: {
327
- key: pluginKey,
328
- options: { dataReturnType, pathParamsType },
329
- },
330
- } = useApp<PluginReactQuery>()
124
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
125
+ const returnType = `UseQueryResult<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'].join(', ')}> & { queryKey: TQueryKey }`
126
+ const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
331
127
 
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,
128
+ const queryKeyParams = QueryKey.getParams({
129
+ pathParamsType,
130
+ typeSchemas,
342
131
  })
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,
132
+ const queryOptionsParams = QueryOptions.getParams({
133
+ paramsType,
134
+ pathParamsType,
135
+ typeSchemas,
347
136
  })
348
- const queryOptions = pluginManager.resolveName({
349
- name: [factory.name, props.infinite ? 'Infinite' : undefined, props.suspense ? 'Suspense' : undefined, 'QueryOptions'].filter(Boolean).join(''),
350
- pluginKey,
137
+ const params = getParams({
138
+ paramsType,
139
+ pathParamsType,
140
+ dataReturnType,
141
+ typeSchemas,
351
142
  })
352
143
 
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
- }
144
+ const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as QueryObserverOptions`
458
145
 
459
146
  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']
147
+ <File.Source name={name} isExportable isIndexable>
148
+ <Function
149
+ name={name}
150
+ export
151
+ generics={generics.join(', ')}
152
+ params={params.toConstructor()}
153
+ JSDoc={{
154
+ comments: getComments(operation),
155
+ }}
156
+ >
157
+ {`
158
+ const { query: queryOptions, client: config = {} } = options ?? {}
159
+ const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
544
160
 
545
- const factory = {
546
- name: factoryName,
547
- }
161
+ const query = useQuery({
162
+ ...${queryOptions},
163
+ queryKey,
164
+ ...queryOptions as unknown as Omit<QueryObserverOptions, "queryKey">
165
+ }) as ${returnType}
548
166
 
549
- return (
550
- <File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
551
- {parser === 'zod' && <File.Import name={[zodSchemas.response?.name]} root={file.path} path={fileZodSchemas.path} />}
552
- <File.Import name={'client'} path={importPath} />
553
- <File.Import name={['ResponseConfig']} path={importPath} isTypeOnly />
554
- <File.Import
555
- name={[
556
- schemas.request?.name,
557
- schemas.response.name,
558
- schemas.pathParams?.name,
559
- schemas.queryParams?.name,
560
- schemas.headerParams?.name,
561
- ...(schemas.errors?.map((error) => error.name) || []),
562
- ].filter(Boolean)}
563
- root={file.path}
564
- path={fileType.path}
565
- isTypeOnly
566
- />
167
+ query.queryKey = queryKey as TQueryKey
567
168
 
568
- <QueryImports hookPath={typeof query !== 'boolean' ? query.importPath : undefined} Template={Import} isInfinite={false} isSuspense={false} />
569
- {!!infinite && (
570
- <QueryImports hookPath={typeof query !== 'boolean' ? query.importPath : undefined} Template={Import} isInfinite={true} isSuspense={false} />
571
- )}
572
- {!!suspense && isV5 && (
573
- <QueryImports hookPath={typeof query !== 'boolean' ? query.importPath : undefined} Template={Import} isInfinite={false} isSuspense={true} />
574
- )}
575
- <SchemaType />
576
- <Query
577
- factory={factory}
578
- Template={Template}
579
- QueryKeyTemplate={QueryKeyTemplate}
580
- QueryOptionsTemplate={QueryOptionsTemplate}
581
- infinite={false}
582
- suspense={false}
583
- query={query}
584
- queryOptions={queryOptions}
585
- hookName={importNames.query['react'].hookName}
586
- resultType={importNames.query['react'].resultType}
587
- optionsType={importNames.query['react'].optionsType}
588
- />
589
- {!!infinite && (
590
- <Query
591
- factory={factory}
592
- Template={Template}
593
- QueryKeyTemplate={QueryKeyTemplate}
594
- QueryOptionsTemplate={QueryOptionsTemplate}
595
- infinite={infinite}
596
- suspense={false}
597
- query={query}
598
- queryOptions={queryOptions}
599
- hookName={importNames.queryInfinite['react'].hookName}
600
- resultType={importNames.queryInfinite['react'].resultType}
601
- optionsType={importNames.queryInfinite['react'].optionsType}
602
- />
603
- )}
604
- {!!suspense && isV5 && (
605
- <Query
606
- factory={factory}
607
- Template={Template}
608
- QueryKeyTemplate={QueryKeyTemplate}
609
- QueryOptionsTemplate={QueryOptionsTemplate}
610
- infinite={false}
611
- suspense={suspense}
612
- query={query}
613
- queryOptions={queryOptions}
614
- hookName={importNames.querySuspense['react'].hookName}
615
- resultType={importNames.querySuspense['react'].resultType}
616
- optionsType={importNames.querySuspense['react'].optionsType}
617
- />
618
- )}
619
- </File>
169
+ return query
170
+ `}
171
+ </Function>
172
+ </File.Source>
620
173
  )
621
174
  }
622
175
 
623
- Query.templates = defaultTemplates
176
+ Query.getParams = getParams