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

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 (78) hide show
  1. package/README.md +14 -5
  2. package/dist/chunk-37VO6QXJ.cjs +669 -0
  3. package/dist/chunk-37VO6QXJ.cjs.map +1 -0
  4. package/dist/chunk-C2H3KPHM.cjs +658 -0
  5. package/dist/chunk-C2H3KPHM.cjs.map +1 -0
  6. package/dist/chunk-IRW2Y3EC.js +660 -0
  7. package/dist/chunk-IRW2Y3EC.js.map +1 -0
  8. package/dist/chunk-Y3DM2P6L.js +647 -0
  9. package/dist/chunk-Y3DM2P6L.js.map +1 -0
  10. package/dist/components.cjs +39 -12
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +161 -6
  13. package/dist/components.d.ts +161 -6
  14. package/dist/components.js +2 -12
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +25 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +14 -0
  19. package/dist/generators.d.ts +14 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +78 -130
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +1 -4
  25. package/dist/index.d.ts +1 -4
  26. package/dist/index.js +61 -120
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-5pnOmDmM.d.cts +231 -0
  29. package/dist/types-5pnOmDmM.d.ts +231 -0
  30. package/package.json +22 -17
  31. package/src/components/InfiniteQuery.tsx +129 -0
  32. package/src/components/InfiniteQueryOptions.tsx +130 -0
  33. package/src/components/Mutation.tsx +137 -321
  34. package/src/components/MutationKey.tsx +48 -0
  35. package/src/components/Query.tsx +91 -594
  36. package/src/components/QueryKey.tsx +51 -182
  37. package/src/components/QueryOptions.tsx +71 -473
  38. package/src/components/SuspenseQuery.tsx +129 -0
  39. package/src/components/index.ts +4 -0
  40. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +51 -0
  41. package/src/generators/__snapshots__/clientGetImportPath.ts +51 -0
  42. package/src/generators/__snapshots__/clientPostImportPath.ts +44 -0
  43. package/src/generators/__snapshots__/findByTags.ts +51 -0
  44. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  45. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  46. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  47. package/src/generators/__snapshots__/findInfiniteByTags.ts +57 -0
  48. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +57 -0
  49. package/src/generators/__snapshots__/getAsMutation.ts +31 -0
  50. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  51. package/src/generators/__snapshots__/updatePetById.ts +44 -0
  52. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +46 -0
  53. package/src/generators/index.ts +4 -0
  54. package/src/generators/infiniteQueryGenerator.tsx +124 -0
  55. package/src/generators/mutationGenerator.tsx +108 -0
  56. package/src/generators/queryGenerator.tsx +121 -0
  57. package/src/generators/suspenseQueryGenerator.tsx +120 -0
  58. package/src/plugin.ts +60 -76
  59. package/src/types.ts +42 -115
  60. package/dist/chunk-7ZODZVKP.cjs +0 -1470
  61. package/dist/chunk-7ZODZVKP.cjs.map +0 -1
  62. package/dist/chunk-ZYTZV43V.js +0 -1470
  63. package/dist/chunk-ZYTZV43V.js.map +0 -1
  64. package/dist/index-5kpkk-7M.d.cts +0 -545
  65. package/dist/index-5kpkk-7M.d.ts +0 -545
  66. package/src/OperationGenerator.tsx +0 -57
  67. package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +0 -69
  68. package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +0 -62
  69. package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +0 -68
  70. package/src/__snapshots__/queryOptions/getPetById.ts +0 -41
  71. package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +0 -51
  72. package/src/__snapshots__/variablesTypeMutate/deletePet.ts +0 -24
  73. package/src/components/QueryImports.tsx +0 -167
  74. package/src/components/SchemaType.tsx +0 -59
  75. package/src/components/__snapshots__/gen/showPetById.ts +0 -67
  76. package/src/components/__snapshots__/gen/useCreatePets.ts +0 -46
  77. package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +0 -46
  78. package/src/utils.ts +0 -96
@@ -1,204 +1,73 @@
1
- import { FunctionParams, URLPath } from '@kubb/core/utils'
2
- import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
3
- import { getASTParams } from '@kubb/plugin-oas/utils'
4
- import { File, Function, Type, useApp } from '@kubb/react'
1
+ import { URLPath } from '@kubb/core/utils'
2
+ import { getPathParams } from '@kubb/plugin-oas/utils'
3
+ import { File, Function, FunctionParams, Type } from '@kubb/react'
5
4
 
6
- import { isRequired } from '@kubb/oas'
7
- import { pluginTsName } from '@kubb/plugin-ts'
5
+ import { type Operation, isOptional } from '@kubb/oas'
6
+ import type { OperationSchemas } from '@kubb/plugin-oas'
8
7
  import type { ReactNode } from 'react'
9
8
  import type { PluginReactQuery } from '../types'
10
9
 
11
- type TemplateProps = {
12
- /**
13
- * Name of the function
14
- */
10
+ type Props = {
15
11
  name: string
16
- /**
17
- * TypeName of the function in PascalCase
18
- */
19
12
  typeName: string
20
- /**
21
- * Parameters/options/props that need to be used
22
- */
23
- params: string
24
- /**
25
- * Generics that needs to be added for TypeScript
26
- */
27
- generics?: string
28
- /**
29
- * ReturnType(see async for adding Promise type)
30
- */
31
- returnType?: string
32
- /**
33
- * Options for JSdocs
34
- */
35
- JSDoc?: {
36
- comments: string[]
37
- }
38
- keys?: string
39
- }
40
-
41
- function Template({ name, typeName, params, generics, returnType, JSDoc, keys }: TemplateProps): ReactNode {
42
- return (
43
- <>
44
- <File.Source name={name} isExportable isIndexable>
45
- <Function.Arrow name={name} export generics={generics} params={params} returnType={returnType} singleLine JSDoc={JSDoc}>
46
- {`[${keys}] as const`}
47
- </Function.Arrow>
48
- </File.Source>
49
-
50
- <File.Source name={typeName} isExportable isIndexable isTypeOnly>
51
- <Type name={typeName} export>
52
- {`ReturnType<typeof ${name}>`}
53
- </Type>
54
- </File.Source>
55
- </>
56
- )
13
+ typeSchemas: OperationSchemas
14
+ operation: Operation
15
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
16
+ keysFn: ((keys: unknown[]) => unknown[]) | undefined
57
17
  }
58
18
 
59
- type FrameworkProps = TemplateProps & {
60
- context: {
61
- factory: {
62
- name: string
63
- }
64
- }
65
- }
66
-
67
- const defaultTemplates = {
68
- get react() {
69
- return function (props: FrameworkProps): ReactNode {
70
- return <Template {...props} />
71
- }
72
- },
73
- get solid() {
74
- return function (props: FrameworkProps): ReactNode {
75
- return <Template {...props} />
76
- }
77
- },
78
- get svelte() {
79
- return function (props: FrameworkProps): ReactNode {
80
- return <Template {...props} />
81
- }
82
- },
83
- get vue() {
84
- return function ({ context, ...rest }: FrameworkProps): ReactNode {
85
- const { factory } = context
86
-
87
- const {
88
- plugin: {
89
- options: { pathParamsType, query },
90
- },
91
- } = useApp<PluginReactQuery>()
92
- const { getSchemas } = useOperationManager()
93
- const operation = useOperation()
94
-
95
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
96
- const path = new URLPath(operation.path)
97
- const params = new FunctionParams()
98
- const withQueryParams = !!schemas.queryParams?.name
99
- const withRequest = !!schemas.request?.name
100
-
101
- params.add([
102
- ...(pathParamsType === 'object'
103
- ? [
104
- getASTParams(schemas.pathParams, {
105
- typed: true,
106
- override: (item) => ({
107
- ...item,
108
- type: `MaybeRef<${item.type}>`,
109
- }),
110
- }),
111
- ]
112
- : getASTParams(schemas.pathParams, {
113
- typed: true,
114
- override: (item) => ({
115
- ...item,
116
- type: `MaybeRef<${item.type}>`,
117
- }),
118
- })),
119
- {
120
- name: 'params',
121
- type: `MaybeRef<${`${factory.name}["queryParams"]`}>`,
122
- enabled: withQueryParams,
123
- required: isRequired(schemas.queryParams?.schema),
124
- },
125
- {
126
- name: 'request',
127
- type: `MaybeRef<${`${factory.name}["request"]`}>`,
128
- enabled: withRequest,
129
- required: isRequired(schemas.request?.schema),
130
- },
131
- ])
132
-
133
- const keys = [
134
- path.toObject({
135
- type: 'path',
136
- stringify: true,
137
- replacer: (pathParam) => `unref(${pathParam})`,
138
- }),
139
- withQueryParams ? '...(params ? [params] : [])' : undefined,
140
- withRequest ? '...(request ? [request] : [])' : undefined,
141
- ].filter(Boolean)
142
-
143
- return <Template {...rest} params={params.toString()} keys={keys.join(', ')} />
144
- }
145
- },
146
- } as const
147
-
148
- type Props = {
149
- name: string
150
- typeName: string
151
- keysFn: (keys: unknown[]) => unknown[]
152
- factory: {
153
- name: string
154
- }
155
- /**
156
- * This will make it possible to override the default behaviour.
157
- */
158
- Template?: React.ComponentType<FrameworkProps>
19
+ type GetParamsProps = {
20
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
21
+ typeSchemas: OperationSchemas
159
22
  }
160
23
 
161
- export function QueryKey({ name, typeName, factory, keysFn, Template = defaultTemplates.react }: Props): ReactNode {
162
- const {
163
- plugin: {
164
- options: { pathParamsType },
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 }),
165
29
  },
166
- } = useApp<PluginReactQuery>()
167
- const { getSchemas } = useOperationManager()
168
- const operation = useOperation()
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
+ }
169
44
 
170
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
45
+ export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, keysFn = (name) => name }: Props): ReactNode {
171
46
  const path = new URLPath(operation.path)
172
- const params = new FunctionParams()
173
- const withQueryParams = !!schemas.queryParams?.name
174
- const withRequest = !!schemas.request?.name
175
-
176
- params.add([
177
- ...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams, { typed: true })] : getASTParams(schemas.pathParams, { typed: true })),
178
- {
179
- name: 'params',
180
- type: `${factory.name}["queryParams"]`,
181
- enabled: withQueryParams,
182
- required: isRequired(schemas.queryParams?.schema),
183
- },
184
- {
185
- name: 'data',
186
- type: `${factory.name}["request"]`,
187
- enabled: withRequest,
188
- required: isRequired(schemas.request?.schema),
189
- },
190
- ])
191
-
47
+ const params = getParams({ pathParamsType, typeSchemas })
192
48
  const keys = [
193
49
  path.toObject({
194
50
  type: 'path',
195
51
  stringify: true,
196
52
  }),
197
- withQueryParams ? '...(params ? [params] : [])' : undefined,
198
- withRequest ? '...(data ? [data] : [])' : undefined,
53
+ typeSchemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
54
+ typeSchemas.request?.name ? '...(data ? [data] : [])' : undefined,
199
55
  ].filter(Boolean)
200
56
 
201
- return <Template typeName={typeName} name={name} params={params.toString()} keys={keysFn(keys).join(', ')} context={{ factory }} />
57
+ return (
58
+ <>
59
+ <File.Source name={name} isExportable isIndexable>
60
+ <Function.Arrow name={name} export params={params.toConstructor()} singleLine>
61
+ {`[${keysFn(keys).join(', ')}] as const`}
62
+ </Function.Arrow>
63
+ </File.Source>
64
+ <File.Source name={typeName} isExportable isIndexable isTypeOnly>
65
+ <Type name={typeName} export>
66
+ {`ReturnType<typeof ${name}>`}
67
+ </Type>
68
+ </File.Source>
69
+ </>
70
+ )
202
71
  }
203
72
 
204
- QueryKey.templates = defaultTemplates
73
+ QueryKey.getParams = getParams