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