@kubb/plugin-svelte-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 (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-CDRGJAED.cjs +452 -0
  4. package/dist/chunk-CDRGJAED.cjs.map +1 -0
  5. package/dist/chunk-JGITTOE5.js +445 -0
  6. package/dist/chunk-JGITTOE5.js.map +1 -0
  7. package/dist/chunk-KRGCKSGR.cjs +527 -0
  8. package/dist/chunk-KRGCKSGR.cjs.map +1 -0
  9. package/dist/chunk-R6ZVBNG7.js +519 -0
  10. package/dist/chunk-R6ZVBNG7.js.map +1 -0
  11. package/dist/components.cjs +28 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +103 -0
  14. package/dist/components.d.ts +103 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +17 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +10 -0
  20. package/dist/generators.d.ts +10 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +127 -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 +120 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-B9W9aYra.d.cts +365 -0
  30. package/dist/types-B9W9aYra.d.ts +365 -0
  31. package/package.json +102 -0
  32. package/src/components/Mutation.tsx +158 -0
  33. package/src/components/MutationKey.tsx +54 -0
  34. package/src/components/Query.tsx +176 -0
  35. package/src/components/QueryKey.tsx +83 -0
  36. package/src/components/QueryOptions.tsx +133 -0
  37. package/src/components/index.ts +5 -0
  38. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +51 -0
  39. package/src/generators/__snapshots__/clientGetImportPath.ts +51 -0
  40. package/src/generators/__snapshots__/clientPostImportPath.ts +44 -0
  41. package/src/generators/__snapshots__/findByTags.ts +51 -0
  42. package/src/generators/__snapshots__/findByTagsObject.ts +60 -0
  43. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +51 -0
  44. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +51 -0
  45. package/src/generators/__snapshots__/findByTagsWithZod.ts +51 -0
  46. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  47. package/src/generators/__snapshots__/updatePetById.ts +44 -0
  48. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +44 -0
  49. package/src/generators/index.ts +2 -0
  50. package/src/generators/mutationGenerator.tsx +117 -0
  51. package/src/generators/queryGenerator.tsx +129 -0
  52. package/src/index.ts +2 -0
  53. package/src/plugin.ts +141 -0
  54. package/src/types.ts +135 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,141 @@
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 { mutationGenerator, queryGenerator } from './generators'
14
+ import type { PluginSvelteQuery } from './types.ts'
15
+
16
+ export const pluginSvelteQueryName = 'plugin-svelte-query' satisfies PluginSvelteQuery['name']
17
+
18
+ export const pluginSvelteQuery = createPlugin<PluginSvelteQuery>((options) => {
19
+ const {
20
+ output = { path: 'hooks', barrelType: 'named' },
21
+ group,
22
+ exclude = [],
23
+ include,
24
+ override = [],
25
+ parser = 'client',
26
+ transformers = {},
27
+ paramsType = 'inline',
28
+ pathParamsType = 'inline',
29
+ mutation = {},
30
+ query = {},
31
+ mutationKey = MutationKey.getTransformer,
32
+ queryKey = QueryKey.getTransformer,
33
+ generators = [queryGenerator, mutationGenerator].filter(Boolean),
34
+ } = options
35
+
36
+ return {
37
+ name: pluginSvelteQueryName,
38
+ options: {
39
+ output,
40
+ client: {
41
+ importPath: '@kubb/plugin-client/client',
42
+ dataReturnType: 'data',
43
+ pathParamsType: 'inline',
44
+ ...options.client,
45
+ },
46
+ queryKey,
47
+ query: {
48
+ methods: ['get'],
49
+ importPath: '@tanstack/svelte-query',
50
+ ...query,
51
+ },
52
+ mutationKey,
53
+ mutation: {
54
+ methods: ['post', 'put', 'patch', 'delete'],
55
+ importPath: '@tanstack/svelte-query',
56
+ ...mutation,
57
+ },
58
+ paramsType,
59
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType,
60
+ parser,
61
+ },
62
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
63
+ resolvePath(baseName, pathMode, options) {
64
+ const root = path.resolve(this.config.root, this.config.output.path)
65
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
66
+
67
+ if (options?.tag && group?.type === 'tag') {
68
+ const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
69
+
70
+ return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
71
+ }
72
+
73
+ if (mode === 'single') {
74
+ /**
75
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
76
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
77
+ */
78
+ return path.resolve(root, output.path)
79
+ }
80
+
81
+ return path.resolve(root, output.path, baseName)
82
+ },
83
+ resolveName(name, type) {
84
+ let resolvedName = camelCase(name)
85
+
86
+ if (type === 'file' || type === 'function') {
87
+ resolvedName = camelCase(name, {
88
+ isFile: type === 'file',
89
+ })
90
+ }
91
+ if (type === 'type') {
92
+ resolvedName = pascalCase(name)
93
+ }
94
+
95
+ if (type) {
96
+ return transformers?.name?.(resolvedName, type) || resolvedName
97
+ }
98
+
99
+ return resolvedName
100
+ },
101
+ async buildStart() {
102
+ const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
103
+
104
+ const oas = await swaggerPlugin.context.getOas()
105
+ const root = path.resolve(this.config.root, this.config.output.path)
106
+ const mode = FileManager.getMode(path.resolve(root, output.path))
107
+ const baseURL = await swaggerPlugin.context.getBaseURL()
108
+
109
+ if (baseURL) {
110
+ this.plugin.options.client.baseURL = baseURL
111
+ }
112
+
113
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
114
+ oas,
115
+ pluginManager: this.pluginManager,
116
+ plugin: this.plugin,
117
+ contentType: swaggerPlugin.context.contentType,
118
+ exclude,
119
+ include,
120
+ override,
121
+ mode,
122
+ })
123
+
124
+ const files = await operationGenerator.build(...generators)
125
+ await this.addFile(...files)
126
+
127
+ const barrelFiles = await this.fileManager.getBarrelFiles({
128
+ type: output.barrelType ?? 'named',
129
+ root,
130
+ output,
131
+ files: this.fileManager.files,
132
+ meta: {
133
+ pluginKey: this.plugin.key,
134
+ },
135
+ logger: this.logger,
136
+ })
137
+
138
+ await this.addFile(...barrelFiles)
139
+ },
140
+ }
141
+ })
package/src/types.ts ADDED
@@ -0,0 +1,135 @@
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/svelte-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/svelte-query'
53
+ */
54
+ importPath?: string
55
+ }
56
+
57
+ export type Options = {
58
+ /**
59
+ * Specify the export location for the files and define the behavior of the output
60
+ * @default { path: 'hooks', barrelType: 'named' }
61
+ */
62
+ output?: Output
63
+ /**
64
+ * Group the @tanstack/query hooks based on the provided name.
65
+ */
66
+ group?: Group
67
+
68
+ client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath' | 'baseURL'>
69
+
70
+ /**
71
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
72
+ */
73
+ exclude?: Array<Exclude>
74
+ /**
75
+ * Array containing include parameters to include tags/operations/methods/paths.
76
+ */
77
+ include?: Array<Include>
78
+ /**
79
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
80
+ */
81
+ override?: Array<Override<ResolvedOptions>>
82
+ /**
83
+ * How to pass your params
84
+ * - 'object' will return the params and pathParams as an object.
85
+ * - 'inline' will return the params as comma separated params.
86
+ * @default 'inline'
87
+ */
88
+ paramsType?: 'object' | 'inline'
89
+ /**
90
+ * How to pass your pathParams.
91
+ * - 'object' will return the pathParams as an object.
92
+ * - 'inline' will return the pathParams as comma separated params.
93
+ * @default 'inline'
94
+ */
95
+ pathParamsType?: PluginClient['options']['pathParamsType']
96
+ queryKey?: QueryKey
97
+ /**
98
+ * Override some useQuery behaviours.
99
+ */
100
+ query?: Partial<Query> | false
101
+ mutationKey?: MutationKey
102
+ /**
103
+ * Override some useMutation behaviours.
104
+ */
105
+ mutation?: Mutation | false
106
+ /**
107
+ * Which parser should be used before returning the data to `@tanstack/query`.
108
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
109
+ */
110
+ parser?: PluginClient['options']['parser']
111
+ transformers?: {
112
+ /**
113
+ * Customize the names based on the type that is provided by the plugin.
114
+ */
115
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
116
+ }
117
+ /**
118
+ * Define some generators next to the svelte-query generators
119
+ */
120
+ generators?: Array<Generator<PluginSvelteQuery>>
121
+ }
122
+
123
+ type ResolvedOptions = {
124
+ output: Output
125
+ client: Required<Omit<NonNullable<PluginReactQuery['options']['client']>, 'baseURL'>> & { baseURL?: string }
126
+ parser: Required<NonNullable<Options['parser']>>
127
+ paramsType: NonNullable<Options['paramsType']>
128
+ pathParamsType: NonNullable<Options['pathParamsType']>
129
+ queryKey: QueryKey | undefined
130
+ query: NonNullable<Required<Query>> | false
131
+ mutationKey: MutationKey | undefined
132
+ mutation: NonNullable<Required<Mutation>> | false
133
+ }
134
+
135
+ export type PluginSvelteQuery = PluginFactoryOptions<'plugin-svelte-query', Options, ResolvedOptions, never, ResolvePathOptions>