@kubb/plugin-vue-query 3.0.0-alpha.20

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-3HD4DCDR.js +505 -0
  4. package/dist/chunk-3HD4DCDR.js.map +1 -0
  5. package/dist/chunk-4YY5DUPV.cjs +514 -0
  6. package/dist/chunk-4YY5DUPV.cjs.map +1 -0
  7. package/dist/chunk-BKYBSBUA.js +567 -0
  8. package/dist/chunk-BKYBSBUA.js.map +1 -0
  9. package/dist/chunk-FRKJLBA5.cjs +576 -0
  10. package/dist/chunk-FRKJLBA5.cjs.map +1 -0
  11. package/dist/components.cjs +32 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +121 -0
  14. package/dist/components.d.ts +121 -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 +138 -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 +131 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-CmetQDTc.d.cts +173 -0
  30. package/dist/types-CmetQDTc.d.ts +173 -0
  31. package/package.json +102 -0
  32. package/src/components/InfiniteQuery.tsx +137 -0
  33. package/src/components/InfiniteQueryOptions.tsx +129 -0
  34. package/src/components/Mutation.tsx +141 -0
  35. package/src/components/Query.tsx +128 -0
  36. package/src/components/QueryKey.tsx +81 -0
  37. package/src/components/QueryOptions.tsx +88 -0
  38. package/src/components/index.ts +6 -0
  39. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +52 -0
  40. package/src/generators/__snapshots__/clientGetImportPath.ts +52 -0
  41. package/src/generators/__snapshots__/clientPostImportPath.ts +38 -0
  42. package/src/generators/__snapshots__/findByTags.ts +52 -0
  43. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +52 -0
  44. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +52 -0
  45. package/src/generators/__snapshots__/findByTagsWithZod.ts +52 -0
  46. package/src/generators/__snapshots__/findInfiniteByTags.ts +57 -0
  47. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +57 -0
  48. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  49. package/src/generators/__snapshots__/updatePetById.ts +38 -0
  50. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +40 -0
  51. package/src/generators/index.ts +3 -0
  52. package/src/generators/infiniteQueryGenerator.tsx +131 -0
  53. package/src/generators/mutationGenerator.tsx +96 -0
  54. package/src/generators/queryGenerator.tsx +124 -0
  55. package/src/index.ts +2 -0
  56. package/src/plugin.ts +152 -0
  57. package/src/types.ts +179 -0
@@ -0,0 +1,137 @@
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 { PluginVueQuery } 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
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
22
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
23
+ }
24
+
25
+ type GetParamsProps = {
26
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
27
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
28
+ typeSchemas: OperationSchemas
29
+ }
30
+
31
+ function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
32
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
33
+
34
+ return FunctionParams.factory({
35
+ pathParams: {
36
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
37
+ children: getPathParams(typeSchemas.pathParams, {
38
+ typed: true,
39
+ override(item) {
40
+ return {
41
+ ...item,
42
+ type: `MaybeRef<${item.type}>`,
43
+ }
44
+ },
45
+ }),
46
+ },
47
+ data: typeSchemas.request?.name
48
+ ? {
49
+ type: `MaybeRef<${typeSchemas.request?.name}>`,
50
+ optional: isOptional(typeSchemas.request?.schema),
51
+ }
52
+ : undefined,
53
+ params: typeSchemas.queryParams?.name
54
+ ? {
55
+ type: `MaybeRef<${typeSchemas.queryParams?.name}>`,
56
+ optional: isOptional(typeSchemas.queryParams?.schema),
57
+ }
58
+ : undefined,
59
+ headers: typeSchemas.headerParams?.name
60
+ ? {
61
+ type: `MaybeRef<${typeSchemas.headerParams?.name}>`,
62
+ optional: isOptional(typeSchemas.headerParams?.schema),
63
+ }
64
+ : undefined,
65
+ options: {
66
+ type: `
67
+ {
68
+ query?: Partial<InfiniteQueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
69
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
70
+ }
71
+ `,
72
+ default: '{}',
73
+ },
74
+ })
75
+ }
76
+
77
+ export function InfiniteQuery({
78
+ name,
79
+ queryKeyTypeName,
80
+ queryOptionsName,
81
+ queryKeyName,
82
+ pathParamsType,
83
+ dataReturnType,
84
+ typeSchemas,
85
+ operation,
86
+ }: Props): ReactNode {
87
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
88
+ const returnType = `UseInfiniteQueryReturnType<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}> & { queryKey: TQueryKey }`
89
+ const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
90
+
91
+ const queryKeyParams = QueryKey.getParams({
92
+ pathParamsType,
93
+ typeSchemas,
94
+ })
95
+ const queryOptionsParams = QueryOptions.getParams({
96
+ pathParamsType,
97
+ typeSchemas,
98
+ })
99
+ const params = getParams({
100
+ pathParamsType,
101
+ dataReturnType,
102
+ typeSchemas,
103
+ })
104
+
105
+ const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as InfiniteQueryObserverOptions`
106
+
107
+ return (
108
+ <File.Source name={name} isExportable isIndexable>
109
+ <Function
110
+ name={name}
111
+ export
112
+ generics={generics.join(', ')}
113
+ params={params.toConstructor()}
114
+ JSDoc={{
115
+ comments: getComments(operation),
116
+ }}
117
+ >
118
+ {`
119
+ const { query: queryOptions, client: config = {} } = options ?? {}
120
+ const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
121
+
122
+ const query = useInfiniteQuery({
123
+ ...${queryOptions},
124
+ queryKey,
125
+ ...queryOptions as unknown as Omit<InfiniteQueryObserverOptions, "queryKey">
126
+ }) as ${returnType}
127
+
128
+ query.queryKey = queryKey as TQueryKey
129
+
130
+ return query
131
+ `}
132
+ </Function>
133
+ </File.Source>
134
+ )
135
+ }
136
+
137
+ InfiniteQuery.getParams = getParams
@@ -0,0 +1,129 @@
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
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
18
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
19
+ initialPageParam: Infinite['initialPageParam']
20
+ cursorParam: Infinite['cursorParam']
21
+ queryParam: Infinite['queryParam']
22
+ }
23
+
24
+ type GetParamsProps = {
25
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
26
+ typeSchemas: OperationSchemas
27
+ }
28
+
29
+ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
30
+ return FunctionParams.factory({
31
+ pathParams: {
32
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
33
+ children: getPathParams(typeSchemas.pathParams, {
34
+ typed: true,
35
+ override(item) {
36
+ return {
37
+ ...item,
38
+ type: `MaybeRef<${item.type}>`,
39
+ }
40
+ },
41
+ }),
42
+ },
43
+ data: typeSchemas.request?.name
44
+ ? {
45
+ type: `MaybeRef<${typeSchemas.request?.name}>`,
46
+ optional: isOptional(typeSchemas.request?.schema),
47
+ }
48
+ : undefined,
49
+ params: typeSchemas.queryParams?.name
50
+ ? {
51
+ type: `MaybeRef<${typeSchemas.queryParams?.name}>`,
52
+ optional: isOptional(typeSchemas.queryParams?.schema),
53
+ }
54
+ : undefined,
55
+ headers: typeSchemas.headerParams?.name
56
+ ? {
57
+ type: `MaybeRef<${typeSchemas.headerParams?.name}>`,
58
+ optional: isOptional(typeSchemas.headerParams?.schema),
59
+ }
60
+ : undefined,
61
+ config: {
62
+ type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
63
+ default: '{}',
64
+ },
65
+ })
66
+ }
67
+
68
+ export function InfiniteQueryOptions({
69
+ name,
70
+ clientName,
71
+ initialPageParam,
72
+ cursorParam,
73
+ typeSchemas,
74
+ dataReturnType,
75
+ pathParamsType,
76
+ queryParam,
77
+ queryKeyName,
78
+ }: Props): ReactNode {
79
+ const params = getParams({ pathParamsType, typeSchemas })
80
+ const clientParams = Client.getParams({
81
+ typeSchemas,
82
+ pathParamsType,
83
+ })
84
+ const queryKeyParams = QueryKey.getParams({
85
+ pathParamsType,
86
+ typeSchemas,
87
+ })
88
+
89
+ const queryOptions = [
90
+ `initialPageParam: ${typeof initialPageParam === 'string' ? JSON.stringify(initialPageParam) : initialPageParam}`,
91
+ cursorParam ? `getNextPageParam: (lastPage) => lastPage['${cursorParam}']` : undefined,
92
+ cursorParam ? `getPreviousPageParam: (firstPage) => firstPage['${cursorParam}']` : undefined,
93
+ !cursorParam && dataReturnType === 'full'
94
+ ? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
95
+ : undefined,
96
+ !cursorParam && dataReturnType === 'data'
97
+ ? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
98
+ : undefined,
99
+ !cursorParam ? 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1' : undefined,
100
+ ].filter(Boolean)
101
+
102
+ const infiniteOverrideParams =
103
+ queryParam && typeSchemas.queryParams?.name
104
+ ? `
105
+ if(params) {
106
+ params['${queryParam}'] = pageParam as unknown as ${typeSchemas.queryParams?.name}['${queryParam}']
107
+ }`
108
+ : ''
109
+
110
+ return (
111
+ <File.Source name={name} isExportable isIndexable>
112
+ <Function name={name} export params={params.toConstructor()}>
113
+ {`
114
+ const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
115
+ return infiniteQueryOptions({
116
+ queryKey,
117
+ queryFn: async ({ pageParam }) => {
118
+ ${infiniteOverrideParams}
119
+ return ${clientName}(${clientParams.toCall()})
120
+ },
121
+ ${queryOptions.join('\n')}
122
+ })
123
+ `}
124
+ </Function>
125
+ </File.Source>
126
+ )
127
+ }
128
+
129
+ InfiniteQueryOptions.getParams = getParams
@@ -0,0 +1,141 @@
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 { ReactNode } from 'react'
8
+ import type { PluginVueQuery } from '../types.ts'
9
+
10
+ type Props = {
11
+ /**
12
+ * Name of the function
13
+ */
14
+ name: string
15
+ typeName: string
16
+ clientName: string
17
+ typeSchemas: OperationSchemas
18
+ operation: Operation
19
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
20
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
21
+ }
22
+
23
+ type GetParamsProps = {
24
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
25
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
26
+ typeSchemas: OperationSchemas
27
+ }
28
+
29
+ function getParams({ dataReturnType, typeSchemas }: GetParamsProps) {
30
+ const mutateParams = FunctionParams.factory({
31
+ ...getPathParams(typeSchemas.pathParams, {
32
+ typed: true,
33
+ override(item) {
34
+ return {
35
+ ...item,
36
+ type: `MaybeRef<${item.type}>`,
37
+ }
38
+ },
39
+ }),
40
+ data: typeSchemas.request?.name
41
+ ? {
42
+ type: `MaybeRef<${typeSchemas.request?.name}>`,
43
+ optional: isOptional(typeSchemas.request?.schema),
44
+ }
45
+ : undefined,
46
+ params: typeSchemas.queryParams?.name
47
+ ? {
48
+ type: `MaybeRef<${typeSchemas.queryParams?.name}>`,
49
+ optional: isOptional(typeSchemas.queryParams?.schema),
50
+ }
51
+ : undefined,
52
+ headers: typeSchemas.headerParams?.name
53
+ ? {
54
+ type: `MaybeRef<${typeSchemas.headerParams?.name}>`,
55
+ optional: isOptional(typeSchemas.headerParams?.schema),
56
+ }
57
+ : undefined,
58
+ })
59
+
60
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
61
+ const TRequest = mutateParams.toConstructor({ valueAsType: true })
62
+
63
+ return FunctionParams.factory({
64
+ options: {
65
+ type: `
66
+ {
67
+ mutation?: UseMutationOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', `{${TRequest}`].join(', ')}>,
68
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
69
+ }
70
+ `,
71
+ default: '{}',
72
+ },
73
+ })
74
+ }
75
+
76
+ export function Mutation({ name, clientName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode {
77
+ const params = getParams({
78
+ pathParamsType,
79
+ dataReturnType,
80
+ typeSchemas,
81
+ })
82
+
83
+ const clientParams = Client.getParams({
84
+ typeSchemas,
85
+ pathParamsType,
86
+ })
87
+
88
+ const mutationParams = FunctionParams.factory({
89
+ data: {
90
+ // No use of pathParams because useMutation can only take one argument in object form,
91
+ // see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
92
+ mode: 'object',
93
+ //TODO rename with value
94
+ children: {
95
+ ...getPathParams(typeSchemas.pathParams, { typed: true }),
96
+ data: typeSchemas.request?.name
97
+ ? {
98
+ type: typeSchemas.request?.name,
99
+ optional: isOptional(typeSchemas.request?.schema),
100
+ }
101
+ : undefined,
102
+ params: typeSchemas.queryParams?.name
103
+ ? {
104
+ type: typeSchemas.queryParams?.name,
105
+ optional: isOptional(typeSchemas.queryParams?.schema),
106
+ }
107
+ : undefined,
108
+ headers: typeSchemas.headerParams?.name
109
+ ? {
110
+ type: typeSchemas.headerParams?.name,
111
+ optional: isOptional(typeSchemas.headerParams?.schema),
112
+ }
113
+ : undefined,
114
+ },
115
+ },
116
+ })
117
+
118
+ return (
119
+ <File.Source name={name} isExportable isIndexable>
120
+ <Function
121
+ name={name}
122
+ export
123
+ params={params.toConstructor()}
124
+ JSDoc={{
125
+ comments: getComments(operation),
126
+ }}
127
+ >
128
+ {`
129
+ const { mutation: mutationOptions, client: config = {} } = options ?? {}
130
+
131
+ return useMutation({
132
+ mutationFn: async(${mutationParams.toConstructor()}) => {
133
+ return ${clientName}(${clientParams.toCall()})
134
+ },
135
+ ...mutationOptions
136
+ })
137
+ `}
138
+ </Function>
139
+ </File.Source>
140
+ )
141
+ }
@@ -0,0 +1,128 @@
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 { PluginVueQuery } 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
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
22
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
23
+ }
24
+
25
+ type GetParamsProps = {
26
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
27
+ dataReturnType: PluginVueQuery['resolvedOptions']['client']['dataReturnType']
28
+ typeSchemas: OperationSchemas
29
+ }
30
+
31
+ function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
32
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
33
+
34
+ return FunctionParams.factory({
35
+ pathParams: {
36
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
37
+ children: getPathParams(typeSchemas.pathParams, {
38
+ typed: true,
39
+ override(item) {
40
+ return {
41
+ ...item,
42
+ type: `MaybeRef<${item.type}>`,
43
+ }
44
+ },
45
+ }),
46
+ },
47
+ data: typeSchemas.request?.name
48
+ ? {
49
+ type: `MaybeRef<${typeSchemas.request?.name}>`,
50
+ optional: isOptional(typeSchemas.request?.schema),
51
+ }
52
+ : undefined,
53
+ params: typeSchemas.queryParams?.name
54
+ ? {
55
+ type: `MaybeRef<${typeSchemas.queryParams?.name}>`,
56
+ optional: isOptional(typeSchemas.queryParams?.schema),
57
+ }
58
+ : undefined,
59
+ headers: typeSchemas.headerParams?.name
60
+ ? {
61
+ type: `MaybeRef<${typeSchemas.headerParams?.name}>`,
62
+ optional: isOptional(typeSchemas.headerParams?.schema),
63
+ }
64
+ : undefined,
65
+ options: {
66
+ type: `
67
+ {
68
+ query?: Partial<QueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
69
+ client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
70
+ }
71
+ `,
72
+ default: '{}',
73
+ },
74
+ })
75
+ }
76
+
77
+ export function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode {
78
+ const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
79
+ const returnType = `UseQueryReturnType<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}> & { queryKey: TQueryKey }`
80
+ const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
81
+
82
+ const queryKeyParams = QueryKey.getParams({
83
+ pathParamsType,
84
+ typeSchemas,
85
+ })
86
+ const queryOptionsParams = QueryOptions.getParams({
87
+ pathParamsType,
88
+ typeSchemas,
89
+ })
90
+ const params = getParams({
91
+ pathParamsType,
92
+ dataReturnType,
93
+ typeSchemas,
94
+ })
95
+
96
+ const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as QueryObserverOptions`
97
+
98
+ return (
99
+ <File.Source name={name} isExportable isIndexable>
100
+ <Function
101
+ name={name}
102
+ export
103
+ generics={generics.join(', ')}
104
+ params={params.toConstructor()}
105
+ JSDoc={{
106
+ comments: getComments(operation),
107
+ }}
108
+ >
109
+ {`
110
+ const { query: queryOptions, client: config = {} } = options ?? {}
111
+ const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
112
+
113
+ const query = useQuery({
114
+ ...${queryOptions},
115
+ queryKey,
116
+ ...queryOptions as unknown as Omit<QueryObserverOptions, "queryKey">
117
+ }) as ${returnType}
118
+
119
+ query.queryKey = queryKey as TQueryKey
120
+
121
+ return query
122
+ `}
123
+ </Function>
124
+ </File.Source>
125
+ )
126
+ }
127
+
128
+ Query.getParams = getParams
@@ -0,0 +1,81 @@
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 } from '../types'
9
+
10
+ type Props = {
11
+ name: string
12
+ typeName: string
13
+ typeSchemas: OperationSchemas
14
+ operation: Operation
15
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
16
+ keysFn: ((keys: unknown[]) => unknown[]) | 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
+ export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, keysFn = (name) => name }: Props): ReactNode {
54
+ const path = new URLPath(operation.path)
55
+ const params = getParams({ pathParamsType, typeSchemas })
56
+ const keys = [
57
+ path.toObject({
58
+ type: 'path',
59
+ stringify: true,
60
+ }),
61
+ typeSchemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
62
+ typeSchemas.request?.name ? '...(data ? [data] : [])' : undefined,
63
+ ].filter(Boolean)
64
+
65
+ return (
66
+ <>
67
+ <File.Source name={name} isExportable isIndexable>
68
+ <Function.Arrow name={name} export params={params.toConstructor()} singleLine>
69
+ {`[${keysFn(keys).join(', ')}] as const`}
70
+ </Function.Arrow>
71
+ </File.Source>
72
+ <File.Source name={typeName} isExportable isIndexable isTypeOnly>
73
+ <Type name={typeName} export>
74
+ {`ReturnType<typeof ${name}>`}
75
+ </Type>
76
+ </File.Source>
77
+ </>
78
+ )
79
+ }
80
+
81
+ QueryKey.getParams = getParams
@@ -0,0 +1,88 @@
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 { 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
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
18
+ }
19
+
20
+ type GetParamsProps = {
21
+ pathParamsType: PluginVueQuery['resolvedOptions']['pathParamsType']
22
+ typeSchemas: OperationSchemas
23
+ }
24
+
25
+ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
26
+ return FunctionParams.factory({
27
+ pathParams: {
28
+ mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
29
+ children: getPathParams(typeSchemas.pathParams, { typed: true }),
30
+ },
31
+ data: typeSchemas.request?.name
32
+ ? {
33
+ type: `MaybeRef<${typeSchemas.request?.name}>`,
34
+ optional: isOptional(typeSchemas.request?.schema),
35
+ }
36
+ : undefined,
37
+ params: typeSchemas.queryParams?.name
38
+ ? {
39
+ type: `MaybeRef<${typeSchemas.queryParams?.name}>`,
40
+ optional: isOptional(typeSchemas.queryParams?.schema),
41
+ }
42
+ : undefined,
43
+ headers: typeSchemas.headerParams?.name
44
+ ? {
45
+ type: `MaybeRef<${typeSchemas.queryParams?.name}>`,
46
+ optional: isOptional(typeSchemas.headerParams?.schema),
47
+ }
48
+ : undefined,
49
+ config: {
50
+ type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
51
+ default: '{}',
52
+ },
53
+ })
54
+ }
55
+
56
+ export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, queryKeyName }: Props): ReactNode {
57
+ const params = getParams({ pathParamsType, typeSchemas })
58
+ const clientParams = Client.getParams({
59
+ typeSchemas,
60
+ pathParamsType,
61
+ })
62
+ const queryKeyParams = QueryKey.getParams({
63
+ pathParamsType,
64
+ typeSchemas,
65
+ })
66
+
67
+ return (
68
+ <File.Source name={name} isExportable isIndexable>
69
+ <Function name={name} export params={params.toConstructor()}>
70
+ {`
71
+ const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
72
+ return queryOptions({
73
+ queryKey,
74
+ queryFn: async () => {
75
+ return ${clientName}(${clientParams.toCall({
76
+ transformName(name) {
77
+ return `unref(${name})`
78
+ },
79
+ })})
80
+ },
81
+ })
82
+ `}
83
+ </Function>
84
+ </File.Source>
85
+ )
86
+ }
87
+
88
+ QueryOptions.getParams = getParams