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