@kubb/plugin-react-query 3.0.0-alpha.3 → 3.0.0-alpha.30

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 (81) hide show
  1. package/README.md +13 -4
  2. package/dist/chunk-2D27Y6L5.js +618 -0
  3. package/dist/chunk-2D27Y6L5.js.map +1 -0
  4. package/dist/chunk-C2H3KPHM.cjs +658 -0
  5. package/dist/chunk-C2H3KPHM.cjs.map +1 -0
  6. package/dist/chunk-JW3GZUSC.cjs +627 -0
  7. package/dist/chunk-JW3GZUSC.cjs.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 -14
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +161 -7
  13. package/dist/components.d.ts +161 -7
  14. package/dist/components.js +2 -14
  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 +75 -154
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +1 -5
  25. package/dist/index.d.ts +1 -5
  26. package/dist/index.js +54 -140
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-rJasmoxy.d.cts +187 -0
  29. package/dist/types-rJasmoxy.d.ts +187 -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 +138 -322
  34. package/src/components/MutationKey.tsx +48 -0
  35. package/src/components/Query.tsx +91 -598
  36. package/src/components/QueryKey.tsx +51 -178
  37. package/src/components/QueryOptions.tsx +72 -466
  38. package/src/components/SuspenseQuery.tsx +129 -0
  39. package/src/components/index.ts +4 -1
  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 +51 -75
  59. package/src/types.ts +49 -88
  60. package/dist/chunk-5IL6M74X.js +0 -1504
  61. package/dist/chunk-5IL6M74X.js.map +0 -1
  62. package/dist/chunk-JFX7DCS7.cjs +0 -1504
  63. package/dist/chunk-JFX7DCS7.cjs.map +0 -1
  64. package/dist/index-C9fwRDH7.d.cts +0 -579
  65. package/dist/index-C9fwRDH7.d.ts +0 -579
  66. package/src/OperationGenerator.tsx +0 -86
  67. package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +0 -64
  68. package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +0 -57
  69. package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +0 -63
  70. package/src/__snapshots__/queryOptions/getPetById.ts +0 -37
  71. package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +0 -47
  72. package/src/__snapshots__/upload/UploadFile.ts +0 -67
  73. package/src/__snapshots__/uploadMutation/UploadFile.ts +0 -44
  74. package/src/__snapshots__/variablesTypeMutate/deletePet.ts +0 -21
  75. package/src/components/Operations.tsx +0 -74
  76. package/src/components/QueryImports.tsx +0 -167
  77. package/src/components/SchemaType.tsx +0 -55
  78. package/src/components/__snapshots__/gen/showPetById.ts +0 -57
  79. package/src/components/__snapshots__/gen/useCreatePets.ts +0 -46
  80. package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +0 -47
  81. package/src/utils.ts +0 -96
@@ -1,200 +1,73 @@
1
- import { FunctionParams, URLPath } from '@kubb/core/utils'
2
- import { Function, Type, useApp } from '@kubb/react'
3
- import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
4
- import { getASTParams } from '@kubb/plugin-oas/utils'
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'
5
+ import { type Operation, isOptional } from '@kubb/oas'
6
+ import type { OperationSchemas } from '@kubb/plugin-oas'
7
7
  import type { ReactNode } from 'react'
8
8
  import type { PluginReactQuery } from '../types'
9
- import { pluginTsName } from '@kubb/plugin-ts'
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
- <Function.Arrow name={name} export generics={generics} params={params} returnType={returnType} singleLine JSDoc={JSDoc}>
45
- {`[${keys}] as const`}
46
- </Function.Arrow>
47
-
48
- <Type name={typeName} export>
49
- {`ReturnType<typeof ${name}>`}
50
- </Type>
51
- </>
52
- )
53
- }
54
-
55
- type FrameworkProps = TemplateProps & {
56
- context: {
57
- factory: {
58
- name: string
59
- }
60
- }
13
+ typeSchemas: OperationSchemas
14
+ operation: Operation
15
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
16
+ keysFn: ((keys: unknown[]) => unknown[]) | undefined
61
17
  }
62
18
 
63
- const defaultTemplates = {
64
- get react() {
65
- return function (props: FrameworkProps): ReactNode {
66
- return <Template {...props} />
67
- }
68
- },
69
- get solid() {
70
- return function (props: FrameworkProps): ReactNode {
71
- return <Template {...props} />
72
- }
73
- },
74
- get svelte() {
75
- return function (props: FrameworkProps): ReactNode {
76
- return <Template {...props} />
77
- }
78
- },
79
- get vue() {
80
- return function ({ context, ...rest }: FrameworkProps): ReactNode {
81
- const { factory } = context
82
-
83
- const {
84
- plugin: {
85
- options: { pathParamsType, query },
86
- },
87
- } = useApp<PluginReactQuery>()
88
- const { getSchemas } = useOperationManager()
89
- const operation = useOperation()
90
-
91
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
92
- const path = new URLPath(operation.path)
93
- const params = new FunctionParams()
94
- const withQueryParams = !!schemas.queryParams?.name
95
- const withRequest = !!schemas.request?.name
96
-
97
- params.add([
98
- ...(pathParamsType === 'object'
99
- ? [
100
- getASTParams(schemas.pathParams, {
101
- typed: true,
102
- override: (item) => ({
103
- ...item,
104
- type: `MaybeRef<${item.type}>`,
105
- }),
106
- }),
107
- ]
108
- : getASTParams(schemas.pathParams, {
109
- typed: true,
110
- override: (item) => ({
111
- ...item,
112
- type: `MaybeRef<${item.type}>`,
113
- }),
114
- })),
115
- {
116
- name: 'params',
117
- type: `MaybeRef<${`${factory.name}["queryParams"]`}>`,
118
- enabled: withQueryParams,
119
- required: isRequired(schemas.queryParams?.schema),
120
- },
121
- {
122
- name: 'request',
123
- type: `MaybeRef<${`${factory.name}["request"]`}>`,
124
- enabled: withRequest,
125
- required: isRequired(schemas.request?.schema),
126
- },
127
- ])
128
-
129
- const keys = [
130
- path.toObject({
131
- type: 'path',
132
- stringify: true,
133
- replacer: (pathParam) => `unref(${pathParam})`,
134
- }),
135
- withQueryParams ? '...(params ? [params] : [])' : undefined,
136
- withRequest ? '...(request ? [request] : [])' : undefined,
137
- ].filter(Boolean)
138
-
139
- return <Template {...rest} params={params.toString()} keys={keys.join(', ')} />
140
- }
141
- },
142
- } as const
143
-
144
- type Props = {
145
- name: string
146
- typeName: string
147
- keysFn: (keys: unknown[]) => unknown[]
148
- factory: {
149
- name: string
150
- }
151
- /**
152
- * This will make it possible to override the default behaviour.
153
- */
154
- Template?: React.ComponentType<FrameworkProps>
19
+ type GetParamsProps = {
20
+ pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
21
+ typeSchemas: OperationSchemas
155
22
  }
156
23
 
157
- export function QueryKey({ name, typeName, factory, keysFn, Template = defaultTemplates.react }: Props): ReactNode {
158
- const {
159
- plugin: {
160
- 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 }),
161
29
  },
162
- } = useApp<PluginReactQuery>()
163
- const { getSchemas } = useOperationManager()
164
- 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
+ }
165
44
 
166
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
45
+ export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, keysFn = (name) => name }: Props): ReactNode {
167
46
  const path = new URLPath(operation.path)
168
- const params = new FunctionParams()
169
- const withQueryParams = !!schemas.queryParams?.name
170
- const withRequest = !!schemas.request?.name
171
-
172
- params.add([
173
- ...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams, { typed: true })] : getASTParams(schemas.pathParams, { typed: true })),
174
- {
175
- name: 'params',
176
- type: `${factory.name}["queryParams"]`,
177
- enabled: withQueryParams,
178
- required: isRequired(schemas.queryParams?.schema),
179
- },
180
- {
181
- name: 'data',
182
- type: `${factory.name}["request"]`,
183
- enabled: withRequest,
184
- required: isRequired(schemas.request?.schema),
185
- },
186
- ])
187
-
47
+ const params = getParams({ pathParamsType, typeSchemas })
188
48
  const keys = [
189
49
  path.toObject({
190
50
  type: 'path',
191
51
  stringify: true,
192
52
  }),
193
- withQueryParams ? '...(params ? [params] : [])' : undefined,
194
- withRequest ? '...(data ? [data] : [])' : undefined,
53
+ typeSchemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
54
+ typeSchemas.request?.name ? '...(data ? [data] : [])' : undefined,
195
55
  ].filter(Boolean)
196
56
 
197
- 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
+ )
198
71
  }
199
72
 
200
- QueryKey.templates = defaultTemplates
73
+ QueryKey.getParams = getParams