@kubb/plugin-vue-query 0.0.0-canary-20241104172400

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 (59) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-A7SD37VK.cjs +584 -0
  4. package/dist/chunk-A7SD37VK.cjs.map +1 -0
  5. package/dist/chunk-DHJLKFYS.js +827 -0
  6. package/dist/chunk-DHJLKFYS.js.map +1 -0
  7. package/dist/chunk-J4RZRRHQ.cjs +837 -0
  8. package/dist/chunk-J4RZRRHQ.cjs.map +1 -0
  9. package/dist/chunk-O4EGNKUX.js +576 -0
  10. package/dist/chunk-O4EGNKUX.js.map +1 -0
  11. package/dist/components.cjs +36 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +150 -0
  14. package/dist/components.d.ts +150 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +21 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +12 -0
  20. package/dist/generators.d.ts +12 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +134 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +9 -0
  26. package/dist/index.d.ts +9 -0
  27. package/dist/index.js +127 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-C8LfCZUP.d.cts +389 -0
  30. package/dist/types-C8LfCZUP.d.ts +389 -0
  31. package/package.json +102 -0
  32. package/src/components/InfiniteQuery.tsx +190 -0
  33. package/src/components/InfiniteQueryOptions.tsx +185 -0
  34. package/src/components/Mutation.tsx +167 -0
  35. package/src/components/MutationKey.tsx +54 -0
  36. package/src/components/Query.tsx +191 -0
  37. package/src/components/QueryKey.tsx +91 -0
  38. package/src/components/QueryOptions.tsx +152 -0
  39. package/src/components/index.ts +7 -0
  40. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +53 -0
  41. package/src/generators/__snapshots__/clientGetImportPath.ts +53 -0
  42. package/src/generators/__snapshots__/clientPostImportPath.ts +45 -0
  43. package/src/generators/__snapshots__/findByTags.ts +53 -0
  44. package/src/generators/__snapshots__/findByTagsObject.ts +62 -0
  45. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +53 -0
  46. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +53 -0
  47. package/src/generators/__snapshots__/findByTagsWithZod.ts +53 -0
  48. package/src/generators/__snapshots__/findInfiniteByTags.ts +58 -0
  49. package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +58 -0
  50. package/src/generators/__snapshots__/postAsQuery.ts +52 -0
  51. package/src/generators/__snapshots__/updatePetById.ts +45 -0
  52. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +45 -0
  53. package/src/generators/index.ts +3 -0
  54. package/src/generators/infiniteQueryGenerator.tsx +137 -0
  55. package/src/generators/mutationGenerator.tsx +116 -0
  56. package/src/generators/queryGenerator.tsx +129 -0
  57. package/src/index.ts +2 -0
  58. package/src/plugin.ts +149 -0
  59. package/src/types.ts +159 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,149 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase, pascalCase } from '@kubb/core/transformers'
5
+ import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
6
+
7
+ import { pluginTsName } from '@kubb/plugin-ts'
8
+ import { pluginZodName } from '@kubb/plugin-zod'
9
+
10
+ import type { Plugin } from '@kubb/core'
11
+ import type { PluginOas } from '@kubb/plugin-oas'
12
+ import { MutationKey, QueryKey } from './components'
13
+ import { infiniteQueryGenerator, mutationGenerator, queryGenerator } from './generators'
14
+ import type { PluginVueQuery } from './types.ts'
15
+
16
+ export const pluginVueQueryName = 'plugin-vue-query' satisfies PluginVueQuery['name']
17
+
18
+ export const pluginVueQuery = createPlugin<PluginVueQuery>((options) => {
19
+ const {
20
+ output = { path: 'hooks', barrelType: 'named' },
21
+ group,
22
+ exclude = [],
23
+ include,
24
+ override = [],
25
+ parser = 'client',
26
+ infinite,
27
+ transformers = {},
28
+ paramsType = 'inline',
29
+ pathParamsType = 'inline',
30
+ mutation = {},
31
+ query = {},
32
+ mutationKey = MutationKey.getTransformer,
33
+ queryKey = QueryKey.getTransformer,
34
+ generators = [queryGenerator, infiniteQueryGenerator, mutationGenerator].filter(Boolean),
35
+ } = options
36
+
37
+ return {
38
+ name: pluginVueQueryName,
39
+ options: {
40
+ output,
41
+ client: {
42
+ importPath: '@kubb/plugin-client/client',
43
+ dataReturnType: 'data',
44
+ pathParamsType: 'inline',
45
+ ...options.client,
46
+ },
47
+ infinite: infinite
48
+ ? {
49
+ queryParam: 'id',
50
+ initialPageParam: 0,
51
+ cursorParam: undefined,
52
+ ...infinite,
53
+ }
54
+ : false,
55
+ queryKey,
56
+ query: {
57
+ methods: ['get'],
58
+ importPath: '@tanstack/vue-query',
59
+ ...query,
60
+ },
61
+ mutationKey,
62
+ mutation: {
63
+ methods: ['post', 'put', 'patch', 'delete'],
64
+ importPath: '@tanstack/vue-query',
65
+ ...mutation,
66
+ },
67
+ paramsType,
68
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType,
69
+ parser,
70
+ },
71
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
72
+ resolvePath(baseName, pathMode, options) {
73
+ const root = path.resolve(this.config.root, this.config.output.path)
74
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
75
+
76
+ if (options?.tag && group?.type === 'tag') {
77
+ const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
78
+
79
+ return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
80
+ }
81
+
82
+ if (mode === 'single') {
83
+ /**
84
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
85
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
86
+ */
87
+ return path.resolve(root, output.path)
88
+ }
89
+
90
+ return path.resolve(root, output.path, baseName)
91
+ },
92
+ resolveName(name, type) {
93
+ let resolvedName = camelCase(name)
94
+
95
+ if (type === 'file' || type === 'function') {
96
+ resolvedName = camelCase(name, {
97
+ isFile: type === 'file',
98
+ })
99
+ }
100
+ if (type === 'type') {
101
+ resolvedName = pascalCase(name)
102
+ }
103
+
104
+ if (type) {
105
+ return transformers?.name?.(resolvedName, type) || resolvedName
106
+ }
107
+
108
+ return resolvedName
109
+ },
110
+ async buildStart() {
111
+ const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
112
+
113
+ const oas = await swaggerPlugin.context.getOas()
114
+ const root = path.resolve(this.config.root, this.config.output.path)
115
+ const mode = FileManager.getMode(path.resolve(root, output.path))
116
+ const baseURL = await swaggerPlugin.context.getBaseURL()
117
+
118
+ if (baseURL) {
119
+ this.plugin.options.client.baseURL = baseURL
120
+ }
121
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
122
+ oas,
123
+ pluginManager: this.pluginManager,
124
+ plugin: this.plugin,
125
+ contentType: swaggerPlugin.context.contentType,
126
+ exclude,
127
+ include,
128
+ override,
129
+ mode,
130
+ })
131
+
132
+ const files = await operationGenerator.build(...generators)
133
+ await this.addFile(...files)
134
+
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
+ })
145
+
146
+ await this.addFile(...barrelFiles)
147
+ },
148
+ }
149
+ })
package/src/types.ts ADDED
@@ -0,0 +1,159 @@
1
+ import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+
3
+ import type { HttpMethod, Operation } from '@kubb/oas'
4
+ import type { PluginClient } from '@kubb/plugin-client'
5
+ import type { Exclude, Generator, Include, OperationSchemas, Override, ResolvePathOptions } from '@kubb/plugin-oas'
6
+ import type { PluginReactQuery } from '@kubb/plugin-react-query'
7
+
8
+ type TransformerProps = {
9
+ operation: Operation
10
+ schemas: OperationSchemas
11
+ }
12
+
13
+ export type Transformer = (props: TransformerProps) => unknown[]
14
+
15
+ /**
16
+ * Customize the queryKey
17
+ */
18
+ type QueryKey = Transformer
19
+
20
+ /**
21
+ * Customize the mutationKey
22
+ */
23
+ type MutationKey = Transformer
24
+
25
+ type Query = {
26
+ /**
27
+ * Define which HttpMethods can be used for queries
28
+ * @default ['get']
29
+ */
30
+ methods: Array<HttpMethod>
31
+ /**
32
+ * Path to the useQuery that will be used to do the useQuery functionality.
33
+ * It will be used as `import { useQuery } from '${importPath}'`.
34
+ * It allows both relative and absolute path.
35
+ * the path will be applied as is, so relative path should be based on the file being generated.
36
+ * @default '@tanstack/react-query'
37
+ */
38
+ importPath?: string
39
+ }
40
+
41
+ type Mutation = {
42
+ /**
43
+ * Define which HttpMethods can be used for mutations
44
+ * @default ['post', 'put', 'delete']
45
+ */
46
+ methods: Array<HttpMethod>
47
+ /**
48
+ * Path to the useQuery that will be used to do the useQuery functionality.
49
+ * It will be used as `import { useQuery } from '${importPath}'`.
50
+ * It allows both relative and absolute path.
51
+ * the path will be applied as is, so relative path should be based on the file being generated.
52
+ * @default '@tanstack/react-query'
53
+ */
54
+ importPath?: string
55
+ }
56
+
57
+ export type Infinite = {
58
+ /**
59
+ * Specify the params key used for `pageParam`.
60
+ * @default `'id'`
61
+ */
62
+ queryParam: string
63
+ /**
64
+ * Which field of the data will be used, set it to undefined when no cursor is known.
65
+ */
66
+ cursorParam?: string | undefined
67
+ /**
68
+ * The initial value, the value of the first page.
69
+ * @default `0`
70
+ */
71
+ initialPageParam: unknown
72
+ }
73
+
74
+ export type Options = {
75
+ /**
76
+ * Specify the export location for the files and define the behavior of the output
77
+ * @default { path: 'hooks', barrelType: 'named' }
78
+ */
79
+ output?: Output
80
+ /**
81
+ * Group the @tanstack/query hooks based on the provided name.
82
+ */
83
+ group?: Group
84
+ client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath' | 'baseURL'>
85
+
86
+ /**
87
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
88
+ */
89
+ exclude?: Array<Exclude>
90
+ /**
91
+ * Array containing include parameters to include tags/operations/methods/paths.
92
+ */
93
+ include?: Array<Include>
94
+ /**
95
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
96
+ */
97
+ override?: Array<Override<ResolvedOptions>>
98
+ /**
99
+ * How to pass your params
100
+ * - 'object' will return the params and pathParams as an object.
101
+ * - 'inline' will return the params as comma separated params.
102
+ * @default 'inline'
103
+ */
104
+ paramsType?: 'object' | 'inline'
105
+ /**
106
+ * How to pass your pathParams.
107
+ * - 'object' will return the pathParams as an object.
108
+ * - 'inline' will return the pathParams as comma separated params.
109
+ * @default 'inline'
110
+ */
111
+ pathParamsType?: PluginClient['options']['pathParamsType']
112
+ /**
113
+ * When set, an infiniteQuery hooks will be added.
114
+ */
115
+ infinite?: Partial<Infinite> | false
116
+ queryKey?: QueryKey
117
+ /**
118
+ * Override some useQuery behaviours.
119
+ */
120
+ query?: Partial<Query> | false
121
+ mutationKey?: MutationKey
122
+ /**
123
+ * Override some useMutation behaviours.
124
+ */
125
+ mutation?: Mutation | false
126
+ /**
127
+ * Which parser should 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']
131
+ transformers?: {
132
+ /**
133
+ * Customize the names based on the type that is provided by the plugin.
134
+ */
135
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
136
+ }
137
+ /**
138
+ * Define some generators next to the vue-query generators
139
+ */
140
+ generators?: Array<Generator<PluginVueQuery>>
141
+ }
142
+
143
+ type ResolvedOptions = {
144
+ output: Output
145
+ client: Required<Omit<NonNullable<PluginReactQuery['options']['client']>, 'baseURL'>> & { baseURL?: string }
146
+ parser: Required<NonNullable<Options['parser']>>
147
+ paramsType: NonNullable<Options['paramsType']>
148
+ pathParamsType: NonNullable<Options['pathParamsType']>
149
+ /**
150
+ * Only used of infinite
151
+ */
152
+ infinite: NonNullable<Infinite> | false
153
+ queryKey: QueryKey | undefined
154
+ query: NonNullable<Required<Query>> | false
155
+ mutationKey: MutationKey | undefined
156
+ mutation: NonNullable<Required<Mutation>> | false
157
+ }
158
+
159
+ export type PluginVueQuery = PluginFactoryOptions<'plugin-vue-query', Options, ResolvedOptions, never, ResolvePathOptions>