@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
package/src/plugin.ts ADDED
@@ -0,0 +1,183 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase, pascalCase } from '@kubb/core/transformers'
5
+ import { renderTemplate } from '@kubb/core/utils'
6
+ import { pluginOasName } from '@kubb/plugin-oas'
7
+ import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
8
+ import { pluginTsName } from '@kubb/plugin-ts'
9
+ import { pluginZodName } from '@kubb/plugin-zod'
10
+
11
+ import { OperationGenerator } from './OperationGenerator.tsx'
12
+ import { Mutation, Operations, Query, QueryKey, QueryOptions } from './components/index.ts'
13
+
14
+ import type { Plugin } from '@kubb/core'
15
+ import type { PluginOas } from '@kubb/plugin-oas'
16
+ import { QueryImports } from './components/QueryImports.tsx'
17
+ import type { PluginReactQuery } from './types.ts'
18
+
19
+ export const pluginReactQueryName = 'plugin-react-query' satisfies PluginReactQuery['name']
20
+
21
+ export const pluginReactQuery = createPlugin<PluginReactQuery>((options) => {
22
+ const {
23
+ output = { path: 'hooks' },
24
+ group,
25
+ exclude = [],
26
+ include,
27
+ override = [],
28
+ parser,
29
+ suspense = {},
30
+ infinite,
31
+ transformers = {},
32
+ dataReturnType = 'data',
33
+ pathParamsType = 'inline',
34
+ mutate = {},
35
+ query = {},
36
+ queryOptions = {},
37
+ templates,
38
+ } = options
39
+ const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
40
+
41
+ return {
42
+ name: pluginReactQueryName,
43
+ options: {
44
+ client: {
45
+ importPath: '@kubb/plugin-client/client',
46
+ ...options.client,
47
+ },
48
+ dataReturnType,
49
+ pathParamsType,
50
+ infinite: infinite
51
+ ? {
52
+ queryParam: 'id',
53
+ initialPageParam: 0,
54
+ cursorParam: undefined,
55
+ ...infinite,
56
+ }
57
+ : false,
58
+ suspense,
59
+ query: query
60
+ ? {
61
+ queryKey: (key: unknown[]) => key,
62
+ methods: ['get'],
63
+ importPath: '@tanstack/react-query',
64
+ ...query,
65
+ }
66
+ : false,
67
+ queryOptions,
68
+ mutate: mutate
69
+ ? {
70
+ variablesType: 'hook',
71
+ methods: ['post', 'put', 'patch', 'delete'],
72
+ ...mutate,
73
+ }
74
+ : false,
75
+ templates: {
76
+ mutation: Mutation.templates,
77
+ query: Query.templates,
78
+ queryOptions: QueryOptions.templates,
79
+ queryKey: QueryKey.templates,
80
+ queryImports: QueryImports.templates,
81
+ operations: Operations.templates,
82
+ ...templates,
83
+ },
84
+ parser,
85
+ },
86
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
87
+ resolvePath(baseName, pathMode, options) {
88
+ const root = path.resolve(this.config.root, this.config.output.path)
89
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
90
+
91
+ if (mode === 'single') {
92
+ /**
93
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
94
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
95
+ */
96
+ return path.resolve(root, output.path)
97
+ }
98
+
99
+ if (options?.tag && group?.type === 'tag') {
100
+ const tag = camelCase(options.tag)
101
+
102
+ return path.resolve(root, renderTemplate(template, { tag }), baseName)
103
+ }
104
+
105
+ return path.resolve(root, output.path, baseName)
106
+ },
107
+ resolveName(name, type) {
108
+ let resolvedName = camelCase(name)
109
+
110
+ if (type === 'file' || type === 'function') {
111
+ resolvedName = camelCase(name, {
112
+ prefix: 'use',
113
+ isFile: type === 'file',
114
+ })
115
+ }
116
+ if (type === 'type') {
117
+ resolvedName = pascalCase(name)
118
+ }
119
+
120
+ if (type) {
121
+ return transformers?.name?.(resolvedName, type) || resolvedName
122
+ }
123
+
124
+ return resolvedName
125
+ },
126
+ async buildStart() {
127
+ const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
128
+
129
+ const oas = await swaggerPlugin.api.getOas()
130
+ const root = path.resolve(this.config.root, this.config.output.path)
131
+ const mode = FileManager.getMode(path.resolve(root, output.path))
132
+
133
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
134
+ oas,
135
+ pluginManager: this.pluginManager,
136
+ plugin: this.plugin,
137
+ contentType: swaggerPlugin.api.contentType,
138
+ exclude,
139
+ include,
140
+ override,
141
+ mode,
142
+ })
143
+
144
+ const files = await operationGenerator.build()
145
+ await this.addFile(...files)
146
+ },
147
+ async writeFile(path, source) {
148
+ if (!path.endsWith('.ts') || !source) {
149
+ return
150
+ }
151
+
152
+ return this.fileManager.write(path, source, { sanity: false })
153
+ },
154
+ async buildEnd() {
155
+ if (this.config.output.write === false) {
156
+ return
157
+ }
158
+
159
+ const root = path.resolve(this.config.root, this.config.output.path)
160
+
161
+ if (group?.type === 'tag') {
162
+ const rootFiles = await getGroupedByTagFiles({
163
+ logger: this.logger,
164
+ files: this.fileManager.files,
165
+ plugin: this.plugin,
166
+ template,
167
+ exportAs: group.exportAs || '{{tag}}Hooks',
168
+ root,
169
+ output,
170
+ })
171
+
172
+ await this.addFile(...rootFiles)
173
+ }
174
+
175
+ await this.fileManager.addIndexes({
176
+ root,
177
+ output,
178
+ meta: { pluginKey: this.plugin.key },
179
+ logger: this.logger,
180
+ })
181
+ },
182
+ }
183
+ })
package/src/types.ts ADDED
@@ -0,0 +1,240 @@
1
+ import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+ import type * as KubbFile from '@kubb/fs/types'
3
+
4
+ import type { HttpMethod } from '@kubb/oas'
5
+ import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
6
+ import type { Mutation } from './components/Mutation.tsx'
7
+ import type { Operations } from './components/Operations.tsx'
8
+ import type { Query as QueryTemplate } from './components/Query.tsx'
9
+ import type { QueryImports } from './components/QueryImports.tsx'
10
+ import type { QueryKey } from './components/QueryKey.tsx'
11
+ import type { QueryOptions as QueryOptionsTemplate } from './components/QueryOptions.tsx'
12
+
13
+ type Templates = {
14
+ operations?: typeof Operations.templates | false
15
+ mutation?: typeof Mutation.templates | false
16
+ query?: typeof QueryTemplate.templates | false
17
+ queryOptions?: typeof QueryOptionsTemplate.templates | false
18
+ queryKey?: typeof QueryKey.templates | false
19
+ }
20
+
21
+ export type Suspense = object
22
+
23
+ export type Query = {
24
+ /**
25
+ * Customize the queryKey, here you can specify a suffix.
26
+ */
27
+ queryKey: (key: unknown[]) => unknown[]
28
+ /**
29
+ * Define which HttpMethods can be used for queries
30
+ * @default ['get']
31
+ */
32
+ methods: Array<HttpMethod>
33
+ /**
34
+ * Path to the useQuery that will be used to do the useQuery functionality.
35
+ * It will be used as `import { useQuery } from '${importPath}'`.
36
+ * It allows both relative and absolute path.
37
+ * the path will be applied as is, so relative path should be based on the file being generated.
38
+ * @default '@tanstack/react-query'
39
+ */
40
+ importPath?: string
41
+ }
42
+
43
+ export type QueryOptions = object
44
+
45
+ export type Mutate = {
46
+ /**
47
+ * Define the way of passing through the queryParams, headerParams and data.
48
+ * @default `'hook'`
49
+ */
50
+ variablesType: 'mutate' | 'hook'
51
+ /**
52
+ * Define which HttpMethods can be used for mutations
53
+ * @default ['post', 'put', 'delete']
54
+ */
55
+ methods: Array<HttpMethod>
56
+ /**
57
+ * Path to the useQuery that will be used to do the useQuery functionality.
58
+ * It will be used as `import { useQuery } from '${importPath}'`.
59
+ * It allows both relative and absolute path.
60
+ * the path will be applied as is, so relative path should be based on the file being generated.
61
+ * @default '@tanstack/react-query'
62
+ */
63
+ importPath?: string
64
+ }
65
+
66
+ export type Infinite = {
67
+ /**
68
+ * Specify the params key used for `pageParam`.
69
+ * Used inside `useInfiniteQuery`, `createInfiniteQueries`, `createInfiniteQuery`
70
+ * @default `'id'`
71
+ */
72
+ queryParam: string
73
+ /**
74
+ * Which field of the data will be used, set it to undefined when no cursor is known.
75
+ */
76
+ cursorParam?: string | undefined
77
+ /**
78
+ * The initial value, the value of the first page.
79
+ * @default `0`
80
+ */
81
+ initialPageParam: unknown
82
+ }
83
+
84
+ export type Options = {
85
+ output?: {
86
+ /**
87
+ * Output to save the @tanstack/query hooks.
88
+ * @default `"hooks"`
89
+ */
90
+ path: string
91
+ /**
92
+ * Name to be used for the `export * as {{exportAs}} from './'`
93
+ */
94
+ exportAs?: string
95
+ /**
96
+ * Add an extension to the generated imports and exports, default it will not use an extension
97
+ */
98
+ extName?: KubbFile.Extname
99
+ /**
100
+ * Define what needs to exported, here you can also disable the export of barrel files
101
+ * @default `'barrel'`
102
+ */
103
+ exportType?: 'barrel' | 'barrelNamed' | false
104
+ }
105
+ /**
106
+ * Group the @tanstack/query hooks based on the provided name.
107
+ */
108
+ group?: {
109
+ /**
110
+ * Tag will group based on the operation tag inside the Swagger file
111
+ */
112
+ type: 'tag'
113
+ /**
114
+ * Relative path to save the grouped @tanstack/query hooks.
115
+ *
116
+ * `{{tag}}` will be replaced by the current tagName.
117
+ * @example `${output}/{{tag}}Controller` => `hooks/PetController`
118
+ * @default `${output}/{{tag}}Controller`
119
+ */
120
+ output?: string
121
+ /**
122
+ * Name to be used for the `export * as {{exportAs}} from './`
123
+ * @default `"{{tag}}Hooks"`
124
+ */
125
+ exportAs?: string
126
+ }
127
+
128
+ client?: {
129
+ /**
130
+ * Path to the client that will be used to do the API calls.
131
+ * It will be used as `import client from '${client.importPath}'`.
132
+ * It allows both relative and absolute path.
133
+ * the path will be applied as is, so relative path should be based on the file being generated.
134
+ * @default '@kubb/plugin-client/client'
135
+ */
136
+ importPath?: string
137
+ }
138
+ /**
139
+ * ReturnType that needs to be used when calling client().
140
+ *
141
+ * `Data` will return ResponseConfig[data].
142
+ *
143
+ * `Full` will return ResponseConfig.
144
+ * @default `'data'`
145
+ * @private
146
+ */
147
+ /**
148
+ * ReturnType that needs to be used when calling client().
149
+ *
150
+ * `Data` will return ResponseConfig[data].
151
+ *
152
+ * `Full` will return ResponseConfig.
153
+ * @default `'data'`
154
+ * @private
155
+ */
156
+ dataReturnType?: 'data' | 'full'
157
+ /**
158
+ * How to pass your pathParams.
159
+ *
160
+ * `object` will return the pathParams as an object.
161
+ *
162
+ * `inline` will return the pathParams as comma separated params.
163
+ * @default `'inline'`
164
+ * @private
165
+ */
166
+ pathParamsType?: 'object' | 'inline'
167
+ /**
168
+ * Which parser can be used before returning the data to `@tanstack/query`.
169
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
170
+ */
171
+ parser?: 'zod'
172
+ /**
173
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
174
+ */
175
+ exclude?: Array<Exclude>
176
+ /**
177
+ * Array containing include parameters to include tags/operations/methods/paths.
178
+ */
179
+ include?: Array<Include>
180
+ /**
181
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
182
+ */
183
+ override?: Array<Override<ResolvedOptions>>
184
+ /**
185
+ * When set, an infiniteQuery hooks will be added.
186
+ */
187
+ infinite?: Partial<Infinite> | false
188
+ /**
189
+ * When set, a suspenseQuery hooks will be added.
190
+ */
191
+ suspense?: Partial<Suspense> | false
192
+ /**
193
+ * Override some useQuery behaviours.
194
+ */
195
+ query?: Partial<Query> | false
196
+ queryOptions?: Partial<QueryOptions> | false
197
+ /**
198
+ * Override some useMutation behaviours.
199
+ */
200
+ mutate?: Mutate | false
201
+ transformers?: {
202
+ /**
203
+ * Customize the names based on the type that is provided by the plugin.
204
+ */
205
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
206
+ }
207
+ /**
208
+ * Make it possible to override one of the templates
209
+ */
210
+ templates?: Partial<Templates>
211
+ }
212
+
213
+ type ResolvedOptions = {
214
+ client: Required<NonNullable<PluginReactQuery['options']['client']>>
215
+ dataReturnType: NonNullable<PluginReactQuery['options']['dataReturnType']>
216
+ pathParamsType: NonNullable<PluginReactQuery['options']['pathParamsType']>
217
+ parser: PluginReactQuery['options']['parser']
218
+ /**
219
+ * Only used of infinite
220
+ */
221
+ infinite: Infinite | false
222
+ suspense: Suspense | false
223
+ query: Query | false
224
+ queryOptions: QueryOptions | false
225
+ mutate: Mutate | false
226
+ templates: NonNullable<Templates>
227
+ }
228
+
229
+ export type FileMeta = {
230
+ pluginKey?: Plugin['key']
231
+ tag?: string
232
+ }
233
+
234
+ export type PluginReactQuery = PluginFactoryOptions<'plugin-react-query', Options, ResolvedOptions, never, ResolvePathOptions>
235
+
236
+ declare module '@kubb/core' {
237
+ export interface _Register {
238
+ ['@kubb/plugin-react-query']: PluginReactQuery
239
+ }
240
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,96 @@
1
+ import { PackageManager } from '@kubb/core'
2
+
3
+ export const reactQueryDepRegex = /@tanstack\/(react|solid|vue|svelte)-query/
4
+
5
+ export function getImportNames() {
6
+ const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
7
+
8
+ return {
9
+ mutation: {
10
+ react: {
11
+ path: '@tanstack/react-query',
12
+ hookName: 'useMutation',
13
+ optionsType: 'UseMutationOptions',
14
+ resultType: 'UseMutationResult',
15
+ },
16
+ solid: {
17
+ path: '@tanstack/solid-query',
18
+ hookName: 'createMutation',
19
+ optionsType: 'CreateMutationOptions',
20
+ resultType: 'CreateMutationResult',
21
+ },
22
+ svelte: {
23
+ path: '@tanstack/svelte-query',
24
+ hookName: 'createMutation',
25
+ optionsType: 'CreateMutationOptions',
26
+ resultType: 'CreateMutationResult',
27
+ },
28
+ vue: {
29
+ path: '@tanstack/vue-query',
30
+ hookName: 'useMutation',
31
+ optionsType: isV5 ? 'UseMutationOptions' : 'VueMutationObserverOptions',
32
+ resultType: 'UseMutationReturnType',
33
+ },
34
+ },
35
+ query: {
36
+ react: {
37
+ path: '@tanstack/react-query',
38
+ hookName: 'useQuery',
39
+ optionsType: isV5 ? 'QueryObserverOptions' : 'UseBaseQueryOptions',
40
+ resultType: 'UseQueryResult',
41
+ },
42
+ solid: {
43
+ path: '@tanstack/solid-query',
44
+ hookName: 'createQuery',
45
+ optionsType: 'CreateBaseQueryOptions',
46
+ resultType: 'CreateQueryResult',
47
+ },
48
+ svelte: {
49
+ path: '@tanstack/svelte-query',
50
+ hookName: 'createQuery',
51
+ optionsType: 'CreateBaseQueryOptions',
52
+ resultType: 'CreateQueryResult',
53
+ },
54
+ vue: {
55
+ path: '@tanstack/vue-query',
56
+ hookName: 'useQuery',
57
+ optionsType: isV5 ? 'QueryObserverOptions' : 'VueQueryObserverOptions',
58
+ resultType: isV5 ? 'UseQueryReturnType' : 'UseQueryReturnType',
59
+ },
60
+ },
61
+ queryInfinite: {
62
+ react: {
63
+ path: '@tanstack/react-query',
64
+ hookName: 'useInfiniteQuery',
65
+ optionsType: isV5 ? 'InfiniteQueryObserverOptions' : 'UseInfiniteQueryOptions',
66
+ resultType: 'UseInfiniteQueryResult',
67
+ },
68
+ solid: {
69
+ path: '@tanstack/solid-query',
70
+ hookName: 'createInfiniteQuery',
71
+ optionsType: 'CreateInfiniteQueryOptions',
72
+ resultType: 'CreateInfiniteQueryResult',
73
+ },
74
+ svelte: {
75
+ path: '@tanstack/svelte-query',
76
+ hookName: 'createInfiniteQuery',
77
+ optionsType: 'CreateInfiniteQueryOptions',
78
+ resultType: 'CreateInfiniteQueryResult',
79
+ },
80
+ vue: {
81
+ path: '@tanstack/vue-query',
82
+ hookName: 'useInfiniteQuery',
83
+ optionsType: isV5 ? 'UseInfiniteQueryOptions' : 'VueInfiniteQueryObserverOptions',
84
+ resultType: isV5 ? 'UseInfiniteQueryReturnType' : 'VueInfiniteQueryObserverOptions',
85
+ },
86
+ },
87
+ querySuspense: {
88
+ react: {
89
+ path: '@tanstack/react-query',
90
+ hookName: 'useSuspenseQuery',
91
+ optionsType: 'UseSuspenseQueryOptions',
92
+ resultType: 'UseSuspenseQueryResult',
93
+ },
94
+ },
95
+ } as const
96
+ }