@kubb/plugin-svelte-query 3.0.0-alpha.20

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 (52) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/chunk-QPJYCH77.js +376 -0
  4. package/dist/chunk-QPJYCH77.js.map +1 -0
  5. package/dist/chunk-RE36BJQW.cjs +384 -0
  6. package/dist/chunk-RE36BJQW.cjs.map +1 -0
  7. package/dist/chunk-TD7H4OF4.cjs +376 -0
  8. package/dist/chunk-TD7H4OF4.cjs.map +1 -0
  9. package/dist/chunk-V5DEG2OQ.js +369 -0
  10. package/dist/chunk-V5DEG2OQ.js.map +1 -0
  11. package/dist/components.cjs +24 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +78 -0
  14. package/dist/components.d.ts +78 -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 +131 -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 +124 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-64YHTP7S.d.cts +148 -0
  30. package/dist/types-64YHTP7S.d.ts +148 -0
  31. package/package.json +102 -0
  32. package/src/components/Mutation.tsx +133 -0
  33. package/src/components/Query.tsx +120 -0
  34. package/src/components/QueryKey.tsx +73 -0
  35. package/src/components/QueryOptions.tsx +84 -0
  36. package/src/components/index.ts +4 -0
  37. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +50 -0
  38. package/src/generators/__snapshots__/clientGetImportPath.ts +50 -0
  39. package/src/generators/__snapshots__/clientPostImportPath.ts +38 -0
  40. package/src/generators/__snapshots__/findByTags.ts +50 -0
  41. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +50 -0
  42. package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +50 -0
  43. package/src/generators/__snapshots__/findByTagsWithZod.ts +50 -0
  44. package/src/generators/__snapshots__/postAsQuery.ts +48 -0
  45. package/src/generators/__snapshots__/updatePetById.ts +38 -0
  46. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +40 -0
  47. package/src/generators/index.ts +2 -0
  48. package/src/generators/mutationGenerator.tsx +96 -0
  49. package/src/generators/queryGenerator.tsx +122 -0
  50. package/src/index.ts +2 -0
  51. package/src/plugin.ts +143 -0
  52. package/src/types.ts +153 -0
package/src/plugin.ts ADDED
@@ -0,0 +1,143 @@
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 { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
7
+
8
+ import { pluginTsName } from '@kubb/plugin-ts'
9
+ import { pluginZodName } from '@kubb/plugin-zod'
10
+
11
+ import type { Plugin } from '@kubb/core'
12
+ import type { PluginOas } from '@kubb/plugin-oas'
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' },
21
+ group,
22
+ exclude = [],
23
+ include,
24
+ override = [],
25
+ parser = 'client',
26
+ transformers = {},
27
+ pathParamsType = 'inline',
28
+ mutation = {},
29
+ query = {},
30
+ } = options
31
+ const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
32
+
33
+ return {
34
+ name: pluginSvelteQueryName,
35
+ options: {
36
+ output: {
37
+ exportType: 'barrelNamed',
38
+ ...output,
39
+ },
40
+ baseURL: undefined,
41
+ client: {
42
+ importPath: '@kubb/plugin-client/client',
43
+ dataReturnType: 'data',
44
+ pathParamsType: 'inline',
45
+ ...options.client,
46
+ },
47
+ query: {
48
+ key: (key: unknown[]) => key,
49
+ methods: ['get'],
50
+ importPath: '@tanstack/svelte-query',
51
+ ...query,
52
+ },
53
+ mutation: {
54
+ methods: ['post', 'put', 'patch', 'delete'],
55
+ importPath: '@tanstack/svelte-query',
56
+ ...mutation,
57
+ },
58
+ pathParamsType,
59
+ parser,
60
+ },
61
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
62
+ resolvePath(baseName, pathMode, options) {
63
+ const root = path.resolve(this.config.root, this.config.output.path)
64
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
65
+
66
+ if (mode === 'single') {
67
+ /**
68
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
69
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
70
+ */
71
+ return path.resolve(root, output.path)
72
+ }
73
+
74
+ if (options?.tag && group?.type === 'tag') {
75
+ const tag = camelCase(options.tag)
76
+
77
+ return path.resolve(root, renderTemplate(template, { tag }), baseName)
78
+ }
79
+
80
+ return path.resolve(root, output.path, baseName)
81
+ },
82
+ resolveName(name, type) {
83
+ let resolvedName = camelCase(name)
84
+
85
+ if (type === 'file' || type === 'function') {
86
+ resolvedName = camelCase(name, {
87
+ isFile: type === 'file',
88
+ })
89
+ }
90
+ if (type === 'type') {
91
+ resolvedName = pascalCase(name)
92
+ }
93
+
94
+ if (type) {
95
+ return transformers?.name?.(resolvedName, type) || resolvedName
96
+ }
97
+
98
+ return resolvedName
99
+ },
100
+ async buildStart() {
101
+ const [swaggerPlugin]: [Plugin<PluginOas>] = PluginManager.getDependedPlugins<PluginOas>(this.plugins, [pluginOasName])
102
+
103
+ const oas = await swaggerPlugin.context.getOas()
104
+ const root = path.resolve(this.config.root, this.config.output.path)
105
+ const mode = FileManager.getMode(path.resolve(root, output.path))
106
+ const baseURL = await swaggerPlugin.context.getBaseURL()
107
+
108
+ const operationGenerator = new OperationGenerator(
109
+ {
110
+ ...this.plugin.options,
111
+ baseURL,
112
+ },
113
+ {
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
+
125
+ const files = await operationGenerator.build(queryGenerator, mutationGenerator)
126
+ await this.addFile(...files)
127
+
128
+ if (this.config.output.exportType) {
129
+ const barrelFiles = await this.fileManager.getBarrelFiles({
130
+ root,
131
+ output,
132
+ files: this.fileManager.files,
133
+ meta: {
134
+ pluginKey: this.plugin.key,
135
+ },
136
+ logger: this.logger,
137
+ })
138
+
139
+ await this.addFile(...barrelFiles)
140
+ }
141
+ },
142
+ }
143
+ })
package/src/types.ts ADDED
@@ -0,0 +1,153 @@
1
+ import type { Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+
3
+ import type { HttpMethod } from '@kubb/oas'
4
+ import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
5
+
6
+ type Query = {
7
+ /**
8
+ * Customize the queryKey, here you can specify a suffix.
9
+ */
10
+ key: (key: unknown[]) => unknown[]
11
+ /**
12
+ * Define which HttpMethods can be used for queries
13
+ * @default ['get']
14
+ */
15
+ methods: Array<HttpMethod>
16
+ /**
17
+ * Path to the useQuery that will be used to do the useQuery functionality.
18
+ * It will be used as `import { useQuery } from '${importPath}'`.
19
+ * It allows both relative and absolute path.
20
+ * the path will be applied as is, so relative path should be based on the file being generated.
21
+ * @default '@tanstack/svelte-query'
22
+ */
23
+ importPath?: string
24
+ }
25
+
26
+ type Mutation = {
27
+ /**
28
+ * Define which HttpMethods can be used for mutations
29
+ * @default ['post', 'put', 'delete']
30
+ */
31
+ methods: Array<HttpMethod>
32
+ /**
33
+ * Path to the useQuery that will be used to do the useQuery functionality.
34
+ * It will be used as `import { useQuery } from '${importPath}'`.
35
+ * It allows both relative and absolute path.
36
+ * the path will be applied as is, so relative path should be based on the file being generated.
37
+ * @default '@tanstack/svelte-query'
38
+ */
39
+ importPath?: string
40
+ }
41
+
42
+ export type Options = {
43
+ /**
44
+ * @default 'hooks'
45
+ */
46
+ output?: Output
47
+ /**
48
+ * Group the @tanstack/query hooks based on the provided name.
49
+ */
50
+ group?: {
51
+ /**
52
+ * Tag will group based on the operation tag inside the Swagger file
53
+ */
54
+ type: 'tag'
55
+ /**
56
+ * Relative path to save the grouped @tanstack/query hooks.
57
+ *
58
+ * `{{tag}}` will be replaced by the current tagName.
59
+ * @example `${output}/{{tag}}Controller` => `hooks/PetController`
60
+ * @default `${output}/{{tag}}Controller`
61
+ */
62
+ output?: string
63
+ /**
64
+ * Name to be used for the `export * as {{exportAs}} from './`
65
+ * @default `"{{tag}}Hooks"`
66
+ */
67
+ exportAs?: string
68
+ }
69
+
70
+ client?: {
71
+ /**
72
+ * Path to the client that will be used to do the API calls.
73
+ * It will be used as `import client from '${client.importPath}'`.
74
+ * It allows both relative and absolute path.
75
+ * the path will be applied as is, so relative path should be based on the file being generated.
76
+ * @default '@kubb/plugin-client/client'
77
+ */
78
+ importPath?: string
79
+ /**
80
+ * ReturnType that needs to be used when calling client().
81
+ *
82
+ * `Data` will return ResponseConfig[data].
83
+ *
84
+ * `Full` will return ResponseConfig.
85
+ * @default `'data'`
86
+ * @private
87
+ */
88
+ dataReturnType?: 'data' | 'full'
89
+ }
90
+ /**
91
+ * ReturnType that needs to be used when calling client().
92
+ *
93
+ * `Data` will return ResponseConfig[data].
94
+ *
95
+ * `Full` will return ResponseConfig.
96
+ * @default `'data'`
97
+ * @private
98
+ */
99
+
100
+ /**
101
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
102
+ */
103
+ exclude?: Array<Exclude>
104
+ /**
105
+ * Array containing include parameters to include tags/operations/methods/paths.
106
+ */
107
+ include?: Array<Include>
108
+ /**
109
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
110
+ */
111
+ override?: Array<Override<ResolvedOptions>>
112
+ /**
113
+ * How to pass your pathParams.
114
+ *
115
+ * `object` will return the pathParams as an object.
116
+ *
117
+ * `inline` will return the pathParams as comma separated params.
118
+ * @default `'inline'`
119
+ * @private
120
+ */
121
+ pathParamsType?: 'object' | 'inline'
122
+ /**
123
+ * Override some useQuery behaviours.
124
+ */
125
+ query?: Partial<Query> | false
126
+ /**
127
+ * Override some useMutation behaviours.
128
+ */
129
+ mutation?: Mutation | false
130
+ /**
131
+ * Which parser can be used before returning the data to `@tanstack/query`.
132
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
133
+ */
134
+ parser?: 'client' | 'zod'
135
+ transformers?: {
136
+ /**
137
+ * Customize the names based on the type that is provided by the plugin.
138
+ */
139
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
140
+ }
141
+ }
142
+
143
+ type ResolvedOptions = {
144
+ output: Output
145
+ baseURL: string | undefined
146
+ client: Required<NonNullable<PluginSvelteQuery['options']['client']>>
147
+ parser: Required<NonNullable<Options['parser']>>
148
+ pathParamsType: NonNullable<Options['pathParamsType']>
149
+ query: NonNullable<Required<Query>> | false
150
+ mutation: NonNullable<Required<Mutation>> | false
151
+ }
152
+
153
+ export type PluginSvelteQuery = PluginFactoryOptions<'plugin-svelte-query', Options, ResolvedOptions, never, ResolvePathOptions>