@kubb/plugin-react-query 3.0.0-alpha.14 → 3.0.0-alpha.16

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 (73) hide show
  1. package/dist/chunk-ESENIDWJ.js +608 -0
  2. package/dist/chunk-ESENIDWJ.js.map +1 -0
  3. package/dist/chunk-JQEH3UHY.cjs +618 -0
  4. package/dist/chunk-JQEH3UHY.cjs.map +1 -0
  5. package/dist/chunk-KRG2LWHM.cjs +606 -0
  6. package/dist/chunk-KRG2LWHM.cjs.map +1 -0
  7. package/dist/chunk-ME7ZFS5H.js +596 -0
  8. package/dist/chunk-ME7ZFS5H.js.map +1 -0
  9. package/dist/components.cjs +17 -5
  10. package/dist/components.d.cts +143 -5
  11. package/dist/components.d.ts +143 -5
  12. package/dist/components.js +1 -1
  13. package/dist/generators.cjs +25 -0
  14. package/dist/generators.cjs.map +1 -0
  15. package/dist/generators.d.cts +15 -0
  16. package/dist/generators.d.ts +15 -0
  17. package/dist/generators.js +4 -0
  18. package/dist/generators.js.map +1 -0
  19. package/dist/index.cjs +33 -76
  20. package/dist/index.cjs.map +1 -1
  21. package/dist/index.d.cts +2 -4
  22. package/dist/index.d.ts +2 -4
  23. package/dist/index.js +34 -77
  24. package/dist/index.js.map +1 -1
  25. package/dist/types-DJxL-JeY.d.cts +195 -0
  26. package/dist/types-DJxL-JeY.d.ts +195 -0
  27. package/package.json +23 -15
  28. package/src/components/InfiniteQuery.tsx +129 -0
  29. package/src/components/InfiniteQueryOptions.tsx +121 -0
  30. package/src/components/Mutation.tsx +112 -304
  31. package/src/components/Query.tsx +91 -593
  32. package/src/components/QueryKey.tsx +51 -182
  33. package/src/components/QueryOptions.tsx +64 -465
  34. package/src/components/SuspenseQuery.tsx +129 -0
  35. package/src/components/index.ts +3 -0
  36. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +64 -0
  37. package/src/generators/__snapshots__/clientGetImportPath.ts +64 -0
  38. package/src/generators/__snapshots__/clientPostImportPath.ts +59 -0
  39. package/src/generators/__snapshots__/findByTags.ts +64 -0
  40. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +64 -0
  41. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +64 -0
  42. package/src/generators/__snapshots__/findByTagsWithZod.ts +64 -0
  43. package/src/generators/__snapshots__/findInfiniteByTags.ts +74 -0
  44. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +74 -0
  45. package/src/generators/__snapshots__/getAsMutation.ts +31 -0
  46. package/src/generators/__snapshots__/postAsQuery.ts +77 -0
  47. package/src/generators/__snapshots__/updatePetById.ts +59 -0
  48. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +63 -0
  49. package/src/generators/index.ts +4 -0
  50. package/src/generators/infiniteQueryGenerator.tsx +124 -0
  51. package/src/generators/mutationGenerator.tsx +94 -0
  52. package/src/generators/queryGenerator.tsx +121 -0
  53. package/src/generators/suspenseQueryGenerator.tsx +120 -0
  54. package/src/plugin.ts +38 -49
  55. package/src/types.ts +35 -59
  56. package/dist/chunk-AGLJPONA.cjs +0 -1423
  57. package/dist/chunk-AGLJPONA.cjs.map +0 -1
  58. package/dist/chunk-I7X4HNDR.js +0 -1413
  59. package/dist/chunk-I7X4HNDR.js.map +0 -1
  60. package/dist/index-BzoLlZve.d.cts +0 -537
  61. package/dist/index-BzoLlZve.d.ts +0 -537
  62. package/src/OperationGenerator.tsx +0 -57
  63. package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +0 -69
  64. package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +0 -62
  65. package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +0 -68
  66. package/src/__snapshots__/queryOptions/getPetById.ts +0 -41
  67. package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +0 -51
  68. package/src/__snapshots__/variablesTypeMutate/deletePet.ts +0 -24
  69. package/src/components/QueryImports.tsx +0 -167
  70. package/src/components/SchemaType.tsx +0 -74
  71. package/src/components/__snapshots__/gen/showPetById.ts +0 -67
  72. package/src/components/__snapshots__/gen/useCreatePets.ts +0 -46
  73. package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +0 -46
@@ -0,0 +1,77 @@
1
+ import client from '@kubb/plugin-client/client'
2
+ import type { RequestConfig } from '@kubb/plugin-client/client'
3
+ import type { QueryKey, QueryObserverOptions, UseQueryResult } from 'custom-query'
4
+ import { useQuery, queryOptions } from 'custom-query'
5
+
6
+ export const updatePetWithFormQueryKey = (
7
+ petId: UpdatePetWithFormPathParams['petId'],
8
+ data?: UpdatePetWithFormMutationRequest,
9
+ params?: UpdatePetWithFormQueryParams,
10
+ ) => [{ url: '/pet/:petId', params: { petId: petId } }, ...(params ? [params] : []), ...(data ? [data] : [])] as const
11
+
12
+ export type UpdatePetWithFormQueryKey = ReturnType<typeof updatePetWithFormQueryKey>
13
+
14
+ /**
15
+ * @summary Updates a pet in the store with form data
16
+ * @link /pet/:petId
17
+ */
18
+ async function updatePetWithForm(
19
+ petId: UpdatePetWithFormPathParams['petId'],
20
+ data?: UpdatePetWithFormMutationRequest,
21
+ params?: UpdatePetWithFormQueryParams,
22
+ config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {},
23
+ ) {
24
+ const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationRequest>({
25
+ method: 'post',
26
+ url: `/pet/${petId}`,
27
+ params,
28
+ data,
29
+ ...config,
30
+ })
31
+ return updatePetWithFormMutationResponse.parse(res.data)
32
+ }
33
+
34
+ export function updatePetWithFormQueryOptions(
35
+ petId: UpdatePetWithFormPathParams['petId'],
36
+ data?: UpdatePetWithFormMutationRequest,
37
+ params?: UpdatePetWithFormQueryParams,
38
+ config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {},
39
+ ) {
40
+ const queryKey = updatePetWithFormQueryKey(petId, data, params)
41
+ return queryOptions({
42
+ queryKey,
43
+ queryFn: async () => {
44
+ return updatePetWithForm(petId, data, params, config)
45
+ },
46
+ })
47
+ }
48
+
49
+ /**
50
+ * @summary Updates a pet in the store with form data
51
+ * @link /pet/:petId
52
+ */
53
+ export function useUpdatePetWithForm<
54
+ TData = UpdatePetWithFormMutationResponse,
55
+ TQueryData = UpdatePetWithFormMutationResponse,
56
+ TQueryKey extends QueryKey = UpdatePetWithFormQueryKey,
57
+ >(
58
+ petId: UpdatePetWithFormPathParams['petId'],
59
+ data?: UpdatePetWithFormMutationRequest,
60
+ params?: UpdatePetWithFormQueryParams,
61
+ options: {
62
+ query?: Partial<QueryObserverOptions<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, TData, TQueryData, TQueryKey>>
63
+ client?: Partial<RequestConfig<UpdatePetWithFormMutationRequest>>
64
+ } = {},
65
+ ) {
66
+ const { query: queryOptions, client: config = {} } = options ?? {}
67
+ const queryKey = queryOptions?.queryKey ?? updatePetWithFormQueryKey(petId, data, params)
68
+ const query = useQuery({
69
+ ...(updatePetWithFormQueryOptions(petId, data, params, config) as unknown as QueryObserverOptions),
70
+ queryKey,
71
+ ...(queryOptions as unknown as Omit<QueryObserverOptions, 'queryKey'>),
72
+ }) as UseQueryResult<TData, UpdatePetWithForm405> & {
73
+ queryKey: TQueryKey
74
+ }
75
+ query.queryKey = queryKey as TQueryKey
76
+ return query
77
+ }
@@ -0,0 +1,59 @@
1
+ import client from '@kubb/plugin-client/client'
2
+ import type { RequestConfig } from '@kubb/plugin-client/client'
3
+ import type { UseMutationOptions } from '@tanstack/react-query'
4
+ import { useMutation } from '@tanstack/react-query'
5
+
6
+ /**
7
+ * @summary Updates a pet in the store with form data
8
+ * @link /pet/:petId
9
+ */
10
+ async function updatePetWithForm(
11
+ petId: UpdatePetWithFormPathParams['petId'],
12
+ data?: UpdatePetWithFormMutationRequest,
13
+ params?: UpdatePetWithFormQueryParams,
14
+ config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {},
15
+ ) {
16
+ const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationRequest>({
17
+ method: 'post',
18
+ url: `/pet/${petId}`,
19
+ params,
20
+ data,
21
+ ...config,
22
+ })
23
+ return updatePetWithFormMutationResponse.parse(res.data)
24
+ }
25
+
26
+ /**
27
+ * @summary Updates a pet in the store with form data
28
+ * @link /pet/:petId
29
+ */
30
+ export function useUpdatePetWithForm(
31
+ options: {
32
+ mutation?: UseMutationOptions<
33
+ UpdatePetWithFormMutationResponse,
34
+ UpdatePetWithForm405,
35
+ {
36
+ petId: UpdatePetWithFormPathParams['petId']
37
+ data?: UpdatePetWithFormMutationRequest
38
+ params?: UpdatePetWithFormQueryParams
39
+ }
40
+ >
41
+ client?: Partial<RequestConfig<UpdatePetWithFormMutationRequest>>
42
+ } = {},
43
+ ) {
44
+ const { mutation: mutationOptions, client: config = {} } = options ?? {}
45
+ return useMutation({
46
+ mutationFn: async ({
47
+ petId,
48
+ data,
49
+ params,
50
+ }: {
51
+ petId: UpdatePetWithFormPathParams['petId']
52
+ data?: UpdatePetWithFormMutationRequest
53
+ params?: UpdatePetWithFormQueryParams
54
+ }) => {
55
+ return updatePetWithForm(petId, data, params, config)
56
+ },
57
+ ...mutationOptions,
58
+ })
59
+ }
@@ -0,0 +1,63 @@
1
+ import client from '@kubb/plugin-client/client'
2
+ import type { RequestConfig } from '@kubb/plugin-client/client'
3
+ import type { UseMutationOptions } from '@tanstack/react-query'
4
+ import { useMutation } from '@tanstack/react-query'
5
+
6
+ /**
7
+ * @summary Updates a pet in the store with form data
8
+ * @link /pet/:petId
9
+ */
10
+ async function updatePetWithForm(
11
+ {
12
+ petId,
13
+ }: {
14
+ petId: UpdatePetWithFormPathParams['petId']
15
+ },
16
+ data?: UpdatePetWithFormMutationRequest,
17
+ params?: UpdatePetWithFormQueryParams,
18
+ config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {},
19
+ ) {
20
+ const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationRequest>({
21
+ method: 'post',
22
+ url: `/pet/${petId}`,
23
+ params,
24
+ data,
25
+ ...config,
26
+ })
27
+ return updatePetWithFormMutationResponse.parse(res.data)
28
+ }
29
+
30
+ /**
31
+ * @summary Updates a pet in the store with form data
32
+ * @link /pet/:petId
33
+ */
34
+ export function useUpdatePetWithForm(
35
+ options: {
36
+ mutation?: UseMutationOptions<
37
+ UpdatePetWithFormMutationResponse,
38
+ UpdatePetWithForm405,
39
+ {
40
+ petId: UpdatePetWithFormPathParams['petId']
41
+ data?: UpdatePetWithFormMutationRequest
42
+ params?: UpdatePetWithFormQueryParams
43
+ }
44
+ >
45
+ client?: Partial<RequestConfig<UpdatePetWithFormMutationRequest>>
46
+ } = {},
47
+ ) {
48
+ const { mutation: mutationOptions, client: config = {} } = options ?? {}
49
+ return useMutation({
50
+ mutationFn: async ({
51
+ petId,
52
+ data,
53
+ params,
54
+ }: {
55
+ petId: UpdatePetWithFormPathParams['petId']
56
+ data?: UpdatePetWithFormMutationRequest
57
+ params?: UpdatePetWithFormQueryParams
58
+ }) => {
59
+ return updatePetWithForm({ petId }, data, params, config)
60
+ },
61
+ ...mutationOptions,
62
+ })
63
+ }
@@ -0,0 +1,4 @@
1
+ export { queryGenerator } from './queryGenerator.tsx'
2
+ export { mutationGenerator } from './mutationGenerator.tsx'
3
+ export { infiniteQueryGenerator } from './infiniteQueryGenerator.tsx'
4
+ export { suspenseQueryGenerator } from './suspenseQueryGenerator.tsx'
@@ -0,0 +1,124 @@
1
+ import transformers from '@kubb/core/transformers'
2
+ import { pluginClientName } from '@kubb/plugin-client'
3
+ import { Client } from '@kubb/plugin-client/components'
4
+ import { createReactGenerator } from '@kubb/plugin-oas'
5
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
6
+ import { pluginTsName } from '@kubb/plugin-ts'
7
+ import { pluginZodName } from '@kubb/plugin-zod'
8
+ import { File, useApp } from '@kubb/react'
9
+ import { InfiniteQuery, InfiniteQueryOptions, Query, QueryKey, QueryOptions } from '../components'
10
+ import type { PluginReactQuery } from '../types'
11
+
12
+ export const infiniteQueryGenerator = createReactGenerator<PluginReactQuery>({
13
+ name: 'react-infinite-query',
14
+ Operation({ options, operation }) {
15
+ const {
16
+ plugin: { output },
17
+ } = useApp<PluginReactQuery>()
18
+ const { getSchemas, getName, getFile } = useOperationManager()
19
+ const isQuery = typeof options.query === 'boolean' ? options.query : !!options.query.methods?.some((method) => operation.method === method)
20
+ const isInfinite = isQuery && !!options.infinite
21
+
22
+ const query = {
23
+ name: getName(operation, { type: 'function', prefix: 'use', suffix: 'infinite' }),
24
+ typeName: getName(operation, { type: 'type' }),
25
+ file: getFile(operation, { prefix: 'use', suffix: 'infinite' }),
26
+ }
27
+
28
+ const client = {
29
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
30
+ }
31
+
32
+ const queryOptions = {
33
+ name: transformers.camelCase(`${operation.getOperationId()} InfiniteQueryOptions`),
34
+ }
35
+
36
+ const queryKey = {
37
+ name: transformers.camelCase(`${operation.getOperationId()} InfiniteQueryKey`),
38
+ typeName: transformers.pascalCase(`${operation.getOperationId()} InfiniteQueryKey`),
39
+ }
40
+
41
+ const type = {
42
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
43
+ //todo remove type?
44
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
45
+ }
46
+
47
+ const zod = {
48
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
49
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
50
+ }
51
+
52
+ if (!isQuery || !isInfinite || typeof options.query === 'boolean' || typeof options.infinite === 'boolean') {
53
+ return null
54
+ }
55
+
56
+ return (
57
+ <File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta}>
58
+ {options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
59
+ <File.Import name={['useInfiniteQuery', 'infiniteQueryOptions']} path={options.query.importPath} />
60
+ <File.Import name={['QueryKey', 'WithRequired']} path={options.query.importPath} isTypeOnly />
61
+ <File.Import name={['InfiniteQueryObserverOptions', 'UseInfiniteQueryResult']} path={options.query.importPath} isTypeOnly />
62
+ <File.Import name={'client'} path={options.client.importPath} />
63
+ <File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
64
+ {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
65
+ <File.Import
66
+ extName={output?.extName}
67
+ name={[
68
+ type.schemas.request?.name,
69
+ type.schemas.response.name,
70
+ type.schemas.pathParams?.name,
71
+ type.schemas.queryParams?.name,
72
+ type.schemas.headerParams?.name,
73
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
74
+ ].filter(Boolean)}
75
+ root={query.file.path}
76
+ path={type.file.path}
77
+ isTypeOnly
78
+ />
79
+
80
+ <QueryKey
81
+ name={queryKey.name}
82
+ typeName={queryKey.typeName}
83
+ operation={operation}
84
+ pathParamsType={options.pathParamsType}
85
+ typeSchemas={type.schemas}
86
+ keysFn={options.query.key}
87
+ />
88
+ <Client
89
+ name={client.name}
90
+ isExportable={false}
91
+ isIndexable={false}
92
+ baseURL={options.baseURL}
93
+ operation={operation}
94
+ typeSchemas={type.schemas}
95
+ zodSchemas={zod.schemas}
96
+ dataReturnType={options.client.dataReturnType}
97
+ pathParamsType={options.pathParamsType}
98
+ parser={options.parser}
99
+ />
100
+ <InfiniteQueryOptions
101
+ name={queryOptions.name}
102
+ clientName={client.name}
103
+ queryKeyName={queryKey.name}
104
+ typeSchemas={type.schemas}
105
+ pathParamsType={options.pathParamsType}
106
+ dataReturnType={options.client.dataReturnType}
107
+ cursorParam={options.infinite.cursorParam}
108
+ initialPageParam={options.infinite.initialPageParam}
109
+ queryParam={options.infinite.queryParam}
110
+ />
111
+ <InfiniteQuery
112
+ name={query.name}
113
+ queryOptionsName={queryOptions.name}
114
+ typeSchemas={type.schemas}
115
+ pathParamsType={options.pathParamsType}
116
+ operation={operation}
117
+ dataReturnType={options.client.dataReturnType}
118
+ queryKeyName={queryKey.name}
119
+ queryKeyTypeName={queryKey.typeName}
120
+ />
121
+ </File>
122
+ )
123
+ },
124
+ })
@@ -0,0 +1,94 @@
1
+ import { pluginClientName } from '@kubb/plugin-client'
2
+ import { Client } from '@kubb/plugin-client/components'
3
+ import { createReactGenerator } from '@kubb/plugin-oas'
4
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
5
+ import { pluginTsName } from '@kubb/plugin-ts'
6
+ import { pluginZodName } from '@kubb/plugin-zod'
7
+ import { File, useApp } from '@kubb/react'
8
+ import { Mutation } from '../components'
9
+ import type { PluginReactQuery } from '../types'
10
+
11
+ export const mutationGenerator = createReactGenerator<PluginReactQuery>({
12
+ name: 'react-query',
13
+ Operation({ options, operation }) {
14
+ const {
15
+ plugin: { output },
16
+ } = useApp<PluginReactQuery>()
17
+ const { getSchemas, getName, getFile } = useOperationManager()
18
+
19
+ const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
20
+ const isMutation = !isQuery && options.mutation && options.mutation.methods.some((method) => operation.method === method)
21
+
22
+ const mutation = {
23
+ name: getName(operation, { type: 'function', prefix: 'use' }),
24
+ typeName: getName(operation, { type: 'type' }),
25
+ file: getFile(operation, { prefix: 'use' }),
26
+ }
27
+
28
+ const type = {
29
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
30
+ //todo remove type?
31
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
32
+ }
33
+
34
+ const zod = {
35
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
36
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
37
+ }
38
+
39
+ const client = {
40
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
41
+ }
42
+
43
+ if (!isMutation || typeof options.mutation === 'boolean') {
44
+ return null
45
+ }
46
+
47
+ return (
48
+ <File baseName={mutation.file.baseName} path={mutation.file.path} meta={mutation.file.meta}>
49
+ {options.parser === 'zod' && (
50
+ <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={mutation.file.path} path={zod.file.path} />
51
+ )}
52
+ <File.Import name={['useMutation']} path={options.mutation.importPath} />
53
+ <File.Import name={['UseMutationOptions', 'UseMutationResult']} path={options.mutation.importPath} isTypeOnly />
54
+ <File.Import name={'client'} path={options.client.importPath} />
55
+ <File.Import name={['RequestConfig', 'ResponseConfig']} path={options.client.importPath} isTypeOnly />
56
+ <File.Import
57
+ extName={output?.extName}
58
+ name={[
59
+ type.schemas.request?.name,
60
+ type.schemas.response.name,
61
+ type.schemas.pathParams?.name,
62
+ type.schemas.queryParams?.name,
63
+ type.schemas.headerParams?.name,
64
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
65
+ ].filter(Boolean)}
66
+ root={mutation.file.path}
67
+ path={type.file.path}
68
+ isTypeOnly
69
+ />
70
+ <Client
71
+ name={client.name}
72
+ isExportable={false}
73
+ isIndexable={false}
74
+ baseURL={options.baseURL}
75
+ operation={operation}
76
+ typeSchemas={type.schemas}
77
+ zodSchemas={zod.schemas}
78
+ dataReturnType={options.client.dataReturnType}
79
+ pathParamsType={options.pathParamsType}
80
+ parser={options.parser}
81
+ />
82
+ <Mutation
83
+ name={mutation.name}
84
+ clientName={client.name}
85
+ typeName={mutation.typeName}
86
+ typeSchemas={type.schemas}
87
+ operation={operation}
88
+ dataReturnType={options.client.dataReturnType}
89
+ pathParamsType={options.pathParamsType}
90
+ />
91
+ </File>
92
+ )
93
+ },
94
+ })
@@ -0,0 +1,121 @@
1
+ import transformers from '@kubb/core/transformers'
2
+ import { pluginClientName } from '@kubb/plugin-client'
3
+ import { Client } from '@kubb/plugin-client/components'
4
+ import { createReactGenerator } from '@kubb/plugin-oas'
5
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
6
+ import { pluginTsName } from '@kubb/plugin-ts'
7
+ import { pluginZodName } from '@kubb/plugin-zod'
8
+ import { File, useApp } from '@kubb/react'
9
+ import { Query, QueryKey, QueryOptions } from '../components'
10
+ import type { PluginReactQuery } from '../types'
11
+
12
+ export const queryGenerator = createReactGenerator<PluginReactQuery>({
13
+ name: 'react-query',
14
+ Operation({ options, operation }) {
15
+ const {
16
+ plugin: { output },
17
+ } = useApp<PluginReactQuery>()
18
+ const { getSchemas, getName, getFile } = useOperationManager()
19
+
20
+ const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
21
+ const isMutation = !isQuery && options.mutation && options.mutation.methods.some((method) => operation.method === method)
22
+
23
+ const query = {
24
+ name: getName(operation, { type: 'function', prefix: 'use' }),
25
+ typeName: getName(operation, { type: 'type' }),
26
+ file: getFile(operation, { prefix: 'use' }),
27
+ }
28
+
29
+ const client = {
30
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
31
+ }
32
+
33
+ const queryOptions = {
34
+ name: transformers.camelCase(`${operation.getOperationId()} QueryOptions`),
35
+ }
36
+
37
+ const queryKey = {
38
+ name: transformers.camelCase(`${operation.getOperationId()} QueryKey`),
39
+ typeName: transformers.pascalCase(`${operation.getOperationId()} QueryKey`),
40
+ }
41
+
42
+ const type = {
43
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
44
+ //todo remove type?
45
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
46
+ }
47
+
48
+ const zod = {
49
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
50
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
51
+ }
52
+
53
+ if (!isQuery || typeof options.query === 'boolean') {
54
+ return null
55
+ }
56
+
57
+ return (
58
+ <File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta}>
59
+ {options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
60
+ <File.Import name={['useQuery', 'queryOptions']} path={options.query.importPath} />
61
+ <File.Import name={['QueryKey', 'WithRequired']} path={options.query.importPath} isTypeOnly />
62
+ <File.Import name={['QueryObserverOptions', 'UseQueryResult']} path={options.query.importPath} isTypeOnly />
63
+ <File.Import name={'client'} path={options.client.importPath} />
64
+ <File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
65
+ {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
66
+ <File.Import
67
+ extName={output?.extName}
68
+ name={[
69
+ type.schemas.request?.name,
70
+ type.schemas.response.name,
71
+ type.schemas.pathParams?.name,
72
+ type.schemas.queryParams?.name,
73
+ type.schemas.headerParams?.name,
74
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
75
+ ].filter(Boolean)}
76
+ root={query.file.path}
77
+ path={type.file.path}
78
+ isTypeOnly
79
+ />
80
+
81
+ <QueryKey
82
+ name={queryKey.name}
83
+ typeName={queryKey.typeName}
84
+ operation={operation}
85
+ pathParamsType={options.pathParamsType}
86
+ typeSchemas={type.schemas}
87
+ keysFn={options.query.key}
88
+ />
89
+ <Client
90
+ name={client.name}
91
+ isExportable={false}
92
+ isIndexable={false}
93
+ baseURL={options.baseURL}
94
+ operation={operation}
95
+ typeSchemas={type.schemas}
96
+ zodSchemas={zod.schemas}
97
+ dataReturnType={options.client.dataReturnType}
98
+ pathParamsType={options.pathParamsType}
99
+ parser={options.parser}
100
+ />
101
+ <QueryOptions
102
+ name={queryOptions.name}
103
+ clientName={client.name}
104
+ queryKeyName={queryKey.name}
105
+ typeSchemas={type.schemas}
106
+ pathParamsType={options.pathParamsType}
107
+ />
108
+ <Query
109
+ name={query.name}
110
+ queryOptionsName={queryOptions.name}
111
+ typeSchemas={type.schemas}
112
+ pathParamsType={options.pathParamsType}
113
+ operation={operation}
114
+ dataReturnType={options.client.dataReturnType}
115
+ queryKeyName={queryKey.name}
116
+ queryKeyTypeName={queryKey.typeName}
117
+ />
118
+ </File>
119
+ )
120
+ },
121
+ })
@@ -0,0 +1,120 @@
1
+ import transformers from '@kubb/core/transformers'
2
+ import { pluginClientName } from '@kubb/plugin-client'
3
+ import { Client } from '@kubb/plugin-client/components'
4
+ import { createReactGenerator } from '@kubb/plugin-oas'
5
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
6
+ import { pluginTsName } from '@kubb/plugin-ts'
7
+ import { pluginZodName } from '@kubb/plugin-zod'
8
+ import { File, useApp } from '@kubb/react'
9
+ import { InfiniteQuery, InfiniteQueryOptions, Query, QueryKey, QueryOptions, SuspenseQuery } from '../components'
10
+ import type { PluginReactQuery } from '../types'
11
+
12
+ export const suspenseQueryGenerator = createReactGenerator<PluginReactQuery>({
13
+ name: 'react-suspense-query',
14
+ Operation({ options, operation }) {
15
+ const {
16
+ plugin: { output },
17
+ } = useApp<PluginReactQuery>()
18
+ const { getSchemas, getName, getFile } = useOperationManager()
19
+ const isQuery = typeof options.query === 'boolean' ? options.query : !!options.query.methods?.some((method) => operation.method === method)
20
+ const isSuspense = isQuery && !!options.suspense
21
+
22
+ const query = {
23
+ name: getName(operation, { type: 'function', prefix: 'use', suffix: 'suspense' }),
24
+ typeName: getName(operation, { type: 'type' }),
25
+ file: getFile(operation, { prefix: 'use', suffix: 'suspense' }),
26
+ }
27
+
28
+ const client = {
29
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
30
+ }
31
+
32
+ const queryOptions = {
33
+ name: transformers.camelCase(`${operation.getOperationId()} SuspenseQueryOptions`),
34
+ }
35
+
36
+ const queryKey = {
37
+ name: transformers.camelCase(`${operation.getOperationId()} SuspenseQueryKey`),
38
+ typeName: transformers.pascalCase(`${operation.getOperationId()} SuspenseQueryKey`),
39
+ }
40
+
41
+ const type = {
42
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
43
+ //todo remove type?
44
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
45
+ }
46
+
47
+ const zod = {
48
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
49
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
50
+ }
51
+
52
+ if (!isQuery || !isSuspense || typeof options.query === 'boolean') {
53
+ return null
54
+ }
55
+
56
+ return (
57
+ <File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta}>
58
+ {options.parser === 'zod' && <File.Import extName={output?.extName} name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
59
+ <File.Import name={['useSuspenseQuery', 'queryOptions']} path={options.query.importPath} />
60
+ <File.Import name={['QueryKey', 'WithRequired']} path={options.query.importPath} isTypeOnly />
61
+ <File.Import name={['UseSuspenseQueryOptions', 'UseSuspenseQueryResult']} path={options.query.importPath} isTypeOnly />
62
+ <File.Import name={'client'} path={options.client.importPath} />
63
+ <File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
64
+ {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
65
+ <File.Import
66
+ extName={output?.extName}
67
+ name={[
68
+ type.schemas.request?.name,
69
+ type.schemas.response.name,
70
+ type.schemas.pathParams?.name,
71
+ type.schemas.queryParams?.name,
72
+ type.schemas.headerParams?.name,
73
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
74
+ ].filter(Boolean)}
75
+ root={query.file.path}
76
+ path={type.file.path}
77
+ isTypeOnly
78
+ />
79
+
80
+ <QueryKey
81
+ name={queryKey.name}
82
+ typeName={queryKey.typeName}
83
+ operation={operation}
84
+ pathParamsType={options.pathParamsType}
85
+ typeSchemas={type.schemas}
86
+ keysFn={options.query.key}
87
+ />
88
+ <Client
89
+ name={client.name}
90
+ isExportable={false}
91
+ isIndexable={false}
92
+ baseURL={options.baseURL}
93
+ operation={operation}
94
+ typeSchemas={type.schemas}
95
+ zodSchemas={zod.schemas}
96
+ dataReturnType={options.client.dataReturnType}
97
+ pathParamsType={options.pathParamsType}
98
+ parser={options.parser}
99
+ />
100
+ <QueryOptions
101
+ name={queryOptions.name}
102
+ clientName={client.name}
103
+ queryKeyName={queryKey.name}
104
+ typeSchemas={type.schemas}
105
+ pathParamsType={options.pathParamsType}
106
+ />
107
+ <SuspenseQuery
108
+ name={query.name}
109
+ queryOptionsName={queryOptions.name}
110
+ typeSchemas={type.schemas}
111
+ pathParamsType={options.pathParamsType}
112
+ operation={operation}
113
+ dataReturnType={options.client.dataReturnType}
114
+ queryKeyName={queryKey.name}
115
+ queryKeyTypeName={queryKey.typeName}
116
+ />
117
+ </File>
118
+ )
119
+ },
120
+ })