@kubb/plugin-react-query 3.0.0-alpha.0

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 (45) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +114 -0
  3. package/dist/chunk-5IL6M74X.js +1504 -0
  4. package/dist/chunk-5IL6M74X.js.map +1 -0
  5. package/dist/chunk-JFX7DCS7.cjs +1504 -0
  6. package/dist/chunk-JFX7DCS7.cjs.map +1 -0
  7. package/dist/components.cjs +15 -0
  8. package/dist/components.cjs.map +1 -0
  9. package/dist/components.d.cts +8 -0
  10. package/dist/components.d.ts +8 -0
  11. package/dist/components.js +15 -0
  12. package/dist/components.js.map +1 -0
  13. package/dist/index-yXskx3Td.d.cts +584 -0
  14. package/dist/index-yXskx3Td.d.ts +584 -0
  15. package/dist/index.cjs +223 -0
  16. package/dist/index.cjs.map +1 -0
  17. package/dist/index.d.cts +13 -0
  18. package/dist/index.d.ts +13 -0
  19. package/dist/index.js +223 -0
  20. package/dist/index.js.map +1 -0
  21. package/package.json +97 -0
  22. package/src/OperationGenerator.tsx +86 -0
  23. package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +64 -0
  24. package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +57 -0
  25. package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +63 -0
  26. package/src/__snapshots__/queryOptions/getPetById.ts +37 -0
  27. package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +47 -0
  28. package/src/__snapshots__/upload/UploadFile.ts +67 -0
  29. package/src/__snapshots__/uploadMutation/UploadFile.ts +44 -0
  30. package/src/__snapshots__/variablesTypeMutate/deletePet.ts +21 -0
  31. package/src/components/Mutation.tsx +341 -0
  32. package/src/components/Operations.tsx +74 -0
  33. package/src/components/Query.tsx +627 -0
  34. package/src/components/QueryImports.tsx +167 -0
  35. package/src/components/QueryKey.tsx +200 -0
  36. package/src/components/QueryOptions.tsx +487 -0
  37. package/src/components/SchemaType.tsx +55 -0
  38. package/src/components/__snapshots__/gen/showPetById.ts +57 -0
  39. package/src/components/__snapshots__/gen/useCreatePets.ts +46 -0
  40. package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +47 -0
  41. package/src/components/index.ts +5 -0
  42. package/src/index.ts +2 -0
  43. package/src/plugin.ts +183 -0
  44. package/src/types.ts +240 -0
  45. package/src/utils.ts +96 -0
@@ -0,0 +1,167 @@
1
+ import { PackageManager } from '@kubb/core'
2
+ import { File } from '@kubb/react'
3
+
4
+ import { getImportNames, reactQueryDepRegex } from '../utils.ts'
5
+
6
+ import type { ReactNode } from 'react'
7
+
8
+ type TemplateProps = {
9
+ /**
10
+ * Path to @tanstack-query
11
+ */
12
+ path: string
13
+ /**
14
+ * Override the path of 'useQuery'
15
+ * @default 'path'
16
+ */
17
+ hookPath: string | undefined
18
+ optionsType: string
19
+ queryOptions: string | undefined
20
+ resultType: string
21
+ hookName: string
22
+ isInfinite: boolean
23
+ }
24
+
25
+ function Template({ path, hookPath = path, isInfinite, hookName, queryOptions, optionsType, resultType }: TemplateProps): ReactNode {
26
+ return (
27
+ <>
28
+ <File.Import name={[optionsType, resultType]} path={path} isTypeOnly />
29
+ <File.Import name={[hookName]} path={hookPath} />
30
+
31
+ {queryOptions && <File.Import name={[queryOptions].filter(Boolean)} path={path} />}
32
+ <File.Import name={['QueryKey', 'WithRequired', isInfinite ? 'InfiniteData' : undefined].filter(Boolean)} path={path} isTypeOnly />
33
+ </>
34
+ )
35
+ }
36
+
37
+ type FrameworkProps = Partial<TemplateProps> & {
38
+ context: {
39
+ isInfinite: boolean
40
+ isSuspense: boolean
41
+ }
42
+ }
43
+
44
+ const defaultTemplates = {
45
+ get react() {
46
+ return function ({ context, hookPath, ...rest }: FrameworkProps): ReactNode {
47
+ const importNames = getImportNames()
48
+ const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
49
+ const { isInfinite, isSuspense } = context
50
+
51
+ return (
52
+ <Template
53
+ isInfinite={isInfinite}
54
+ {...(isSuspense ? importNames.querySuspense.react : isInfinite ? importNames.queryInfinite.react : importNames.query.react)}
55
+ queryOptions={isV5 ? (isInfinite ? 'infiniteQueryOptions' : 'queryOptions') : undefined}
56
+ hookPath={hookPath}
57
+ {...rest}
58
+ />
59
+ )
60
+ }
61
+ },
62
+ get solid() {
63
+ return function ({ context, hookPath, ...rest }: FrameworkProps): ReactNode {
64
+ const importNames = getImportNames()
65
+ const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
66
+ const { isInfinite } = context
67
+
68
+ return (
69
+ <Template
70
+ isInfinite={isInfinite}
71
+ {...(isInfinite ? importNames.queryInfinite.solid : importNames.query.solid)}
72
+ queryOptions={isV5 ? (isInfinite ? 'infiniteQueryOptions' : 'queryOptions') : undefined}
73
+ hookPath={hookPath}
74
+ {...rest}
75
+ />
76
+ )
77
+ }
78
+ },
79
+ get svelte() {
80
+ return function ({ context, hookPath, ...rest }: FrameworkProps): ReactNode {
81
+ const importNames = getImportNames()
82
+ const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
83
+ const { isInfinite } = context
84
+
85
+ return (
86
+ <Template
87
+ isInfinite={isInfinite}
88
+ {...(isInfinite ? importNames.queryInfinite.svelte : importNames.query.svelte)}
89
+ queryOptions={isV5 ? (isInfinite ? 'infiniteQueryOptions' : 'queryOptions') : undefined}
90
+ hookPath={hookPath}
91
+ {...rest}
92
+ />
93
+ )
94
+ }
95
+ },
96
+ get vue() {
97
+ return function ({ context, hookPath, ...rest }: FrameworkProps): ReactNode {
98
+ const importNames = getImportNames()
99
+ const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
100
+ const { isInfinite } = context
101
+ const path = '@tanstack/vue-query'
102
+
103
+ return (
104
+ <>
105
+ {isV5 && (
106
+ <>
107
+ <Template
108
+ isInfinite={isInfinite}
109
+ {...(isInfinite ? importNames.queryInfinite.vue : importNames.query.vue)}
110
+ queryOptions={isInfinite ? 'infiniteQueryOptions' : 'queryOptions'}
111
+ hookPath={hookPath}
112
+ {...rest}
113
+ />
114
+ <File.Import name={['QueryObserverOptions']} path={path} isTypeOnly />
115
+ </>
116
+ )}
117
+
118
+ {!isV5 && isInfinite && (
119
+ <>
120
+ <File.Import name={[importNames.queryInfinite.vue.resultType]} path={path} isTypeOnly />
121
+ <File.Import name={[importNames.queryInfinite.vue.optionsType]} path={'@tanstack/vue-query/build/lib/types'} isTypeOnly />
122
+ <File.Import name={[importNames.queryInfinite.vue.hookName]} path={path} />
123
+ </>
124
+ )}
125
+
126
+ {!isV5 && !isInfinite && (
127
+ <>
128
+ <File.Import name={[importNames.query.vue.resultType]} path={path} isTypeOnly />
129
+ <File.Import name={[importNames.query.vue.optionsType]} path={'@tanstack/vue-query/build/lib/types'} isTypeOnly />
130
+ <File.Import name={[importNames.query.vue.hookName]} path={path} />
131
+ </>
132
+ )}
133
+ <File.Import name={['unref']} path={'vue'} />
134
+ <File.Import name={['MaybeRef']} path={'vue'} isTypeOnly />
135
+ <File.Import name={['QueryKey', 'WithRequired']} path={path} isTypeOnly />
136
+ </>
137
+ )
138
+ }
139
+ },
140
+ } as const
141
+
142
+ type Props = {
143
+ hookPath: string | undefined
144
+ isInfinite: boolean
145
+ /**
146
+ * Only for React and v5
147
+ */
148
+ isSuspense: boolean
149
+ /**
150
+ * This will make it possible to override the default behaviour.
151
+ */
152
+ Template?: React.ComponentType<FrameworkProps>
153
+ }
154
+
155
+ export function QueryImports({ hookPath, isInfinite, isSuspense, Template = defaultTemplates.react }: Props): ReactNode {
156
+ return (
157
+ <Template
158
+ hookPath={hookPath}
159
+ context={{
160
+ isInfinite,
161
+ isSuspense,
162
+ }}
163
+ />
164
+ )
165
+ }
166
+
167
+ QueryImports.templates = defaultTemplates
@@ -0,0 +1,200 @@
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'
5
+
6
+ import { isRequired } from '@kubb/oas'
7
+ import type { ReactNode } from 'react'
8
+ import type { PluginReactQuery } from '../types'
9
+ import { pluginTsName } from '@kubb/plugin-ts'
10
+
11
+ type TemplateProps = {
12
+ /**
13
+ * Name of the function
14
+ */
15
+ name: string
16
+ /**
17
+ * TypeName of the function in PascalCase
18
+ */
19
+ 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
+ }
61
+ }
62
+
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>
155
+ }
156
+
157
+ export function QueryKey({ name, typeName, factory, keysFn, Template = defaultTemplates.react }: Props): ReactNode {
158
+ const {
159
+ plugin: {
160
+ options: { pathParamsType },
161
+ },
162
+ } = useApp<PluginReactQuery>()
163
+ const { getSchemas } = useOperationManager()
164
+ const operation = useOperation()
165
+
166
+ const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
167
+ 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
+
188
+ const keys = [
189
+ path.toObject({
190
+ type: 'path',
191
+ stringify: true,
192
+ }),
193
+ withQueryParams ? '...(params ? [params] : [])' : undefined,
194
+ withRequest ? '...(data ? [data] : [])' : undefined,
195
+ ].filter(Boolean)
196
+
197
+ return <Template typeName={typeName} name={name} params={params.toString()} keys={keysFn(keys).join(', ')} context={{ factory }} />
198
+ }
199
+
200
+ QueryKey.templates = defaultTemplates