@kubb/plugin-react-query 0.0.0-canary-20241104172400

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 (62) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-EOG7AHFO.cjs +890 -0
  4. package/dist/chunk-EOG7AHFO.cjs.map +1 -0
  5. package/dist/chunk-EY5KE7R7.js +879 -0
  6. package/dist/chunk-EY5KE7R7.js.map +1 -0
  7. package/dist/chunk-NBC6BPMV.cjs +700 -0
  8. package/dist/chunk-NBC6BPMV.cjs.map +1 -0
  9. package/dist/chunk-NZKAIPYC.js +691 -0
  10. package/dist/chunk-NZKAIPYC.js.map +1 -0
  11. package/dist/components.cjs +40 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +175 -0
  14. package/dist/components.d.ts +175 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +25 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +14 -0
  20. package/dist/generators.d.ts +14 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +135 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +9 -0
  26. package/dist/index.d.ts +9 -0
  27. package/dist/index.js +128 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-IuxCCG1K.d.cts +244 -0
  30. package/dist/types-IuxCCG1K.d.ts +244 -0
  31. package/package.json +102 -0
  32. package/src/components/InfiniteQuery.tsx +176 -0
  33. package/src/components/InfiniteQueryOptions.tsx +185 -0
  34. package/src/components/Mutation.tsx +163 -0
  35. package/src/components/MutationKey.tsx +54 -0
  36. package/src/components/Query.tsx +176 -0
  37. package/src/components/QueryKey.tsx +83 -0
  38. package/src/components/QueryOptions.tsx +133 -0
  39. package/src/components/SuspenseQuery.tsx +176 -0
  40. package/src/components/index.ts +8 -0
  41. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +51 -0
  42. package/src/generators/__snapshots__/clientGetImportPath.ts +51 -0
  43. package/src/generators/__snapshots__/clientPostImportPath.ts +44 -0
  44. package/src/generators/__snapshots__/findByTags.ts +51 -0
  45. package/src/generators/__snapshots__/findByTagsObject.ts +60 -0
  46. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  47. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  48. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  49. package/src/generators/__snapshots__/findInfiniteByTags.ts +57 -0
  50. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +57 -0
  51. package/src/generators/__snapshots__/getAsMutation.ts +31 -0
  52. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  53. package/src/generators/__snapshots__/updatePetById.ts +44 -0
  54. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +44 -0
  55. package/src/generators/index.ts +4 -0
  56. package/src/generators/infiniteQueryGenerator.tsx +136 -0
  57. package/src/generators/mutationGenerator.tsx +117 -0
  58. package/src/generators/queryGenerator.tsx +127 -0
  59. package/src/generators/suspenseQueryGenerator.tsx +130 -0
  60. package/src/index.ts +2 -0
  61. package/src/plugin.ts +152 -0
  62. package/src/types.ts +166 -0
@@ -0,0 +1,185 @@
1
+ import { getPathParams } from '@kubb/plugin-oas/utils'
2
+ import { File, Function, FunctionParams } from '@kubb/react'
3
+
4
+ import type { ReactNode } from 'react'
5
+
6
+ import { isOptional } from '@kubb/oas'
7
+ import { Client } from '@kubb/plugin-client/components'
8
+ import type { OperationSchemas } from '@kubb/plugin-oas'
9
+ import type { Infinite, PluginReactQuery } from '../types.ts'
10
+ import { QueryKey } from './QueryKey.tsx'
11
+
12
+ type Props = {
13
+ name: string
14
+ clientName: string
15
+ queryKeyName: string
16
+ typeSchemas: OperationSchemas
17
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
18
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
19
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
20
+ initialPageParam: Infinite['initialPageParam']
21
+ cursorParam: Infinite['cursorParam']
22
+ queryParam: Infinite['queryParam']
23
+ }
24
+
25
+ type GetParamsProps = {
26
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
27
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
28
+ typeSchemas: OperationSchemas
29
+ }
30
+
31
+ function getParams({ paramsType, pathParamsType, typeSchemas }: GetParamsProps) {
32
+ if (paramsType === 'object') {
33
+ return FunctionParams.factory({
34
+ data: {
35
+ mode: 'object',
36
+ children: {
37
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
38
+ data: typeSchemas.request?.name
39
+ ? {
40
+ type: typeSchemas.request?.name,
41
+ optional: isOptional(typeSchemas.request?.schema),
42
+ }
43
+ : undefined,
44
+ params: typeSchemas.queryParams?.name
45
+ ? {
46
+ type: typeSchemas.queryParams?.name,
47
+ optional: isOptional(typeSchemas.queryParams?.schema),
48
+ }
49
+ : undefined,
50
+ headers: typeSchemas.headerParams?.name
51
+ ? {
52
+ type: typeSchemas.headerParams?.name,
53
+ optional: isOptional(typeSchemas.headerParams?.schema),
54
+ }
55
+ : undefined,
56
+ },
57
+ },
58
+ config: {
59
+ type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
60
+ default: '{}',
61
+ },
62
+ })
63
+ }
64
+
65
+ return FunctionParams.factory({
66
+ pathParams: typeSchemas.pathParams?.name
67
+ ? {
68
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
69
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
70
+ type: typeSchemas.pathParams?.name,
71
+ optional: isOptional(typeSchemas.pathParams?.schema),
72
+ }
73
+ : undefined,
74
+ data: typeSchemas.request?.name
75
+ ? {
76
+ type: typeSchemas.request?.name,
77
+ optional: isOptional(typeSchemas.request?.schema),
78
+ }
79
+ : undefined,
80
+ params: typeSchemas.queryParams?.name
81
+ ? {
82
+ type: typeSchemas.queryParams?.name,
83
+ optional: isOptional(typeSchemas.queryParams?.schema),
84
+ }
85
+ : undefined,
86
+ headers: typeSchemas.headerParams?.name
87
+ ? {
88
+ type: typeSchemas.headerParams?.name,
89
+ optional: isOptional(typeSchemas.headerParams?.schema),
90
+ }
91
+ : undefined,
92
+ config: {
93
+ type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
94
+ default: '{}',
95
+ },
96
+ })
97
+ }
98
+
99
+ export function InfiniteQueryOptions({
100
+ name,
101
+ clientName,
102
+ initialPageParam,
103
+ cursorParam,
104
+ typeSchemas,
105
+ paramsType,
106
+ dataReturnType,
107
+ pathParamsType,
108
+ queryParam,
109
+ queryKeyName,
110
+ }: Props): ReactNode {
111
+ const params = getParams({ paramsType, pathParamsType, typeSchemas })
112
+ const clientParams = Client.getParams({
113
+ typeSchemas,
114
+ paramsType,
115
+ pathParamsType,
116
+ })
117
+ const queryKeyParams = QueryKey.getParams({
118
+ pathParamsType,
119
+ typeSchemas,
120
+ })
121
+
122
+ const queryOptions = [
123
+ `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
124
+ cursorParam ? `getNextPageParam: (lastPage) => lastPage['${cursorParam}']` : undefined,
125
+ cursorParam ? `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']` : undefined,
126
+ !cursorParam && dataReturnType === 'full'
127
+ ? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
128
+ : undefined,
129
+ !cursorParam && dataReturnType === 'data'
130
+ ? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
131
+ : undefined,
132
+ !cursorParam ? 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1' : undefined,
133
+ ].filter(Boolean)
134
+
135
+ const infiniteOverrideParams =
136
+ queryParam && typeSchemas.queryParams?.name
137
+ ? `
138
+ if(params) {
139
+ params['${queryParam}'] = pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}']
140
+ }`
141
+ : ''
142
+
143
+ const enabled = Object.entries(queryKeyParams.flatParams)
144
+ .map(([key, item]) => (item && !item.optional ? key : undefined))
145
+ .filter(Boolean)
146
+ .join('&& ')
147
+
148
+ const enabledText = enabled ? `enabled: !!(${enabled})` : ''
149
+
150
+ return (
151
+ <File.Source name={name} isExportable isIndexable>
152
+ <Function name={name} export params={params.toConstructor()}>
153
+ {infiniteOverrideParams &&
154
+ `
155
+ const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
156
+ return infiniteQueryOptions({
157
+ ${enabledText}
158
+ queryKey,
159
+ queryFn: async ({ signal, pageParam }) => {
160
+ config.signal = signal
161
+ ${infiniteOverrideParams}
162
+ return ${clientName}(${clientParams.toCall()})
163
+ },
164
+ ${queryOptions.join('\n')}
165
+ })
166
+ `}
167
+ {!infiniteOverrideParams &&
168
+ `
169
+ const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
170
+ return infiniteQueryOptions({
171
+ ${enabledText}
172
+ queryKey,
173
+ queryFn: async ({ signal }) => {
174
+ config.signal = signal
175
+ return ${clientName}(${clientParams.toCall()})
176
+ },
177
+ ${queryOptions.join('\n')}
178
+ })
179
+ `}
180
+ </Function>
181
+ </File.Source>
182
+ )
183
+ }
184
+
185
+ InfiniteQueryOptions.getParams = getParams
@@ -0,0 +1,163 @@
1
+ import { File, Function, FunctionParams } from '@kubb/react'
2
+
3
+ import { type Operation, isOptional } from '@kubb/oas'
4
+ import { Client } from '@kubb/plugin-client/components'
5
+ import type { OperationSchemas } from '@kubb/plugin-oas'
6
+ import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
7
+ import type { Params } from '@kubb/react/types'
8
+ import type { ReactNode } from 'react'
9
+ import type { PluginReactQuery } from '../types.ts'
10
+ import { MutationKey } from './MutationKey.tsx'
11
+
12
+ type Props = {
13
+ /**
14
+ * Name of the function
15
+ */
16
+ name: string
17
+ typeName: string
18
+ clientName: string
19
+ mutationKeyName: string
20
+ typeSchemas: OperationSchemas
21
+ operation: Operation
22
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
23
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
24
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
25
+ }
26
+
27
+ type GetParamsProps = {
28
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
29
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
30
+ typeSchemas: OperationSchemas
31
+ }
32
+
33
+ function getParams({ dataReturnType, typeSchemas }: GetParamsProps) {
34
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
35
+ const mutationParams = FunctionParams.factory({
36
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
37
+ data: typeSchemas.request?.name
38
+ ? {
39
+ type: typeSchemas.request?.name,
40
+ optional: isOptional(typeSchemas.request?.schema),
41
+ }
42
+ : undefined,
43
+ params: typeSchemas.queryParams?.name
44
+ ? {
45
+ type: typeSchemas.queryParams?.name,
46
+ optional: isOptional(typeSchemas.queryParams?.schema),
47
+ }
48
+ : undefined,
49
+ headers: typeSchemas.headerParams?.name
50
+ ? {
51
+ type: typeSchemas.headerParams?.name,
52
+ optional: isOptional(typeSchemas.headerParams?.schema),
53
+ }
54
+ : undefined,
55
+ })
56
+ const TRequest = mutationParams.toConstructor({ valueAsType: true })
57
+ const generics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', TRequest ? `{${TRequest}}` : undefined]
58
+ .filter(Boolean)
59
+ .join(', ')
60
+
61
+ return FunctionParams.factory({
62
+ options: {
63
+ type: `
64
+ {
65
+ mutation?: UseMutationOptions<${generics}>,
66
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
67
+ }
68
+ `,
69
+ default: '{}',
70
+ },
71
+ })
72
+ }
73
+
74
+ export function Mutation({ name, clientName, paramsType, pathParamsType, dataReturnType, typeSchemas, operation, mutationKeyName }: Props): ReactNode {
75
+ const mutationKeyParams = MutationKey.getParams({
76
+ pathParamsType,
77
+ typeSchemas,
78
+ })
79
+
80
+ const params = getParams({
81
+ pathParamsType,
82
+ dataReturnType,
83
+ typeSchemas,
84
+ })
85
+
86
+ const clientParams = Client.getParams({
87
+ paramsType,
88
+ typeSchemas,
89
+ pathParamsType,
90
+ })
91
+
92
+ const mutationParams = FunctionParams.factory({
93
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
94
+ data: typeSchemas.request?.name
95
+ ? {
96
+ type: typeSchemas.request?.name,
97
+ optional: isOptional(typeSchemas.request?.schema),
98
+ }
99
+ : undefined,
100
+ params: typeSchemas.queryParams?.name
101
+ ? {
102
+ type: typeSchemas.queryParams?.name,
103
+ optional: isOptional(typeSchemas.queryParams?.schema),
104
+ }
105
+ : undefined,
106
+ headers: typeSchemas.headerParams?.name
107
+ ? {
108
+ type: typeSchemas.headerParams?.name,
109
+ optional: isOptional(typeSchemas.headerParams?.schema),
110
+ }
111
+ : undefined,
112
+ })
113
+
114
+ const dataParams = FunctionParams.factory({
115
+ data: {
116
+ // No use of pathParams because useMutation can only take one argument in object form,
117
+ // see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
118
+ mode: 'object',
119
+ children: Object.entries(mutationParams.params).reduce((acc, [key, value]) => {
120
+ if (value) {
121
+ acc[key] = {
122
+ ...value,
123
+ type: undefined,
124
+ }
125
+ }
126
+
127
+ return acc
128
+ }, {} as Params),
129
+ },
130
+ })
131
+
132
+ const TRequest = mutationParams.toConstructor({ valueAsType: true })
133
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
134
+ const generics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', TRequest ? `{${TRequest}}` : undefined]
135
+ .filter(Boolean)
136
+ .join(', ')
137
+
138
+ return (
139
+ <File.Source name={name} isExportable isIndexable>
140
+ <Function
141
+ name={name}
142
+ export
143
+ params={params.toConstructor()}
144
+ JSDoc={{
145
+ comments: getComments(operation),
146
+ }}
147
+ >
148
+ {`
149
+ const { mutation: mutationOptions, client: config = {} } = options ?? {}
150
+ const mutationKey = mutationOptions?.mutationKey ?? ${mutationKeyName}(${mutationKeyParams.toCall()})
151
+
152
+ return useMutation<${generics}>({
153
+ mutationFn: async(${dataParams.toConstructor()}) => {
154
+ return ${clientName}(${clientParams.toCall()})
155
+ },
156
+ mutationKey,
157
+ ...mutationOptions
158
+ })
159
+ `}
160
+ </Function>
161
+ </File.Source>
162
+ )
163
+ }
@@ -0,0 +1,54 @@
1
+ import { URLPath } from '@kubb/core/utils'
2
+ import { File, Function, FunctionParams, Type } from '@kubb/react'
3
+
4
+ import type { Operation } from '@kubb/oas'
5
+ import type { OperationSchemas } from '@kubb/plugin-oas'
6
+ import type { ReactNode } from 'react'
7
+ import type { PluginReactQuery, Transformer } from '../types'
8
+
9
+ type Props = {
10
+ name: string
11
+ typeName: string
12
+ typeSchemas: OperationSchemas
13
+ operation: Operation
14
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
15
+ transformer: Transformer | undefined
16
+ }
17
+
18
+ type GetParamsProps = {
19
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
20
+ typeSchemas: OperationSchemas
21
+ }
22
+
23
+ function getParams({}: GetParamsProps) {
24
+ return FunctionParams.factory({})
25
+ }
26
+
27
+ const getTransformer: Transformer = ({ operation }) => {
28
+ const path = new URLPath(operation.path)
29
+
30
+ return [JSON.stringify({ url: path.path })].filter(Boolean)
31
+ }
32
+
33
+ export function MutationKey({ name, typeSchemas, pathParamsType, operation, typeName, transformer = getTransformer }: Props): ReactNode {
34
+ const params = getParams({ pathParamsType, typeSchemas })
35
+ const keys = transformer({ operation, schemas: typeSchemas })
36
+
37
+ return (
38
+ <>
39
+ <File.Source name={name} isExportable isIndexable>
40
+ <Function.Arrow name={name} export params={params.toConstructor()} singleLine>
41
+ {`[${keys.join(', ')}] as const`}
42
+ </Function.Arrow>
43
+ </File.Source>
44
+ <File.Source name={typeName} isExportable isIndexable isTypeOnly>
45
+ <Type name={typeName} export>
46
+ {`ReturnType<typeof ${name}>`}
47
+ </Type>
48
+ </File.Source>
49
+ </>
50
+ )
51
+ }
52
+
53
+ MutationKey.getParams = getParams
54
+ MutationKey.getTransformer = getTransformer
@@ -0,0 +1,176 @@
1
+ import { File, Function, FunctionParams } from '@kubb/react'
2
+
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'
8
+ import { QueryKey } from './QueryKey.tsx'
9
+ import { QueryOptions } from './QueryOptions.tsx'
10
+
11
+ type Props = {
12
+ /**
13
+ * Name of the function
14
+ */
15
+ name: string
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']
24
+ }
25
+
26
+ type GetParamsProps = {
27
+ paramsType: PluginReactQuery['resolvedOptions']['paramsType']
28
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
29
+ dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
30
+ typeSchemas: OperationSchemas
31
+ }
32
+
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,
60
+ },
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
+ })
72
+ }
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
+ })
111
+ }
112
+
113
+ export function Query({
114
+ name,
115
+ queryKeyTypeName,
116
+ queryOptionsName,
117
+ queryKeyName,
118
+ paramsType,
119
+ pathParamsType,
120
+ dataReturnType,
121
+ typeSchemas,
122
+ operation,
123
+ }: Props): ReactNode {
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}`]
127
+
128
+ const queryKeyParams = QueryKey.getParams({
129
+ pathParamsType,
130
+ typeSchemas,
131
+ })
132
+ const queryOptionsParams = QueryOptions.getParams({
133
+ paramsType,
134
+ pathParamsType,
135
+ typeSchemas,
136
+ })
137
+ const params = getParams({
138
+ paramsType,
139
+ pathParamsType,
140
+ dataReturnType,
141
+ typeSchemas,
142
+ })
143
+
144
+ const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as QueryObserverOptions`
145
+
146
+ return (
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()})
160
+
161
+ const query = useQuery({
162
+ ...${queryOptions},
163
+ queryKey,
164
+ ...queryOptions as unknown as Omit<QueryObserverOptions, "queryKey">
165
+ }) as ${returnType}
166
+
167
+ query.queryKey = queryKey as TQueryKey
168
+
169
+ return query
170
+ `}
171
+ </Function>
172
+ </File.Source>
173
+ )
174
+ }
175
+
176
+ Query.getParams = getParams
@@ -0,0 +1,83 @@
1
+ import { URLPath } from '@kubb/core/utils'
2
+ import { getPathParams } from '@kubb/plugin-oas/utils'
3
+ import { File, Function, FunctionParams, Type } from '@kubb/react'
4
+
5
+ import { type Operation, isOptional } from '@kubb/oas'
6
+ import type { OperationSchemas } from '@kubb/plugin-oas'
7
+ import type { ReactNode } from 'react'
8
+ import type { PluginReactQuery, Transformer } from '../types'
9
+
10
+ type Props = {
11
+ name: string
12
+ typeName: string
13
+ typeSchemas: OperationSchemas
14
+ operation: Operation
15
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
16
+ transformer: Transformer | undefined
17
+ }
18
+
19
+ type GetParamsProps = {
20
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
21
+ typeSchemas: OperationSchemas
22
+ }
23
+
24
+ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
25
+ return FunctionParams.factory({
26
+ pathParams: {
27
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
28
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
29
+ },
30
+ data: typeSchemas.request?.name
31
+ ? {
32
+ type: typeSchemas.request?.name,
33
+ optional: isOptional(typeSchemas.request?.schema),
34
+ }
35
+ : undefined,
36
+ params: typeSchemas.queryParams?.name
37
+ ? {
38
+ type: typeSchemas.queryParams?.name,
39
+ optional: isOptional(typeSchemas.queryParams?.schema),
40
+ }
41
+ : undefined,
42
+ })
43
+ }
44
+
45
+ const getTransformer: Transformer = ({ operation, schemas }) => {
46
+ const path = new URLPath(operation.path)
47
+ const keys = [
48
+ path.toObject({
49
+ type: 'path',
50
+ stringify: true,
51
+ }),
52
+ schemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
53
+ schemas.request?.name ? '...(data ? [data] : [])' : undefined,
54
+ ].filter(Boolean)
55
+
56
+ return keys
57
+ }
58
+
59
+ export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, transformer = getTransformer }: Props): ReactNode {
60
+ const params = getParams({ pathParamsType, typeSchemas })
61
+ const keys = transformer({
62
+ operation,
63
+ schemas: typeSchemas,
64
+ })
65
+
66
+ return (
67
+ <>
68
+ <File.Source name={name} isExportable isIndexable>
69
+ <Function.Arrow name={name} export params={params.toConstructor()} singleLine>
70
+ {`[${keys.join(', ')}] as const`}
71
+ </Function.Arrow>
72
+ </File.Source>
73
+ <File.Source name={typeName} isExportable isIndexable isTypeOnly>
74
+ <Type name={typeName} export>
75
+ {`ReturnType<typeof ${name}>`}
76
+ </Type>
77
+ </File.Source>
78
+ </>
79
+ )
80
+ }
81
+
82
+ QueryKey.getParams = getParams
83
+ QueryKey.getTransformer = getTransformer