@kubb/plugin-swr 5.0.0-alpha.34 → 5.0.0-alpha.35

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 (41) hide show
  1. package/dist/components-BJSzUg7M.cjs +955 -0
  2. package/dist/components-BJSzUg7M.cjs.map +1 -0
  3. package/dist/components-JQ2KRFCa.js +877 -0
  4. package/dist/components-JQ2KRFCa.js.map +1 -0
  5. package/dist/components.cjs +1 -1
  6. package/dist/components.d.ts +77 -37
  7. package/dist/components.js +1 -1
  8. package/dist/generators-17ulS9mu.cjs +537 -0
  9. package/dist/generators-17ulS9mu.cjs.map +1 -0
  10. package/dist/generators-Cl7nr-FB.js +526 -0
  11. package/dist/generators-Cl7nr-FB.js.map +1 -0
  12. package/dist/generators.cjs +1 -1
  13. package/dist/generators.d.ts +4 -4
  14. package/dist/generators.js +1 -1
  15. package/dist/index.cjs +132 -110
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.ts +22 -2
  18. package/dist/index.js +132 -110
  19. package/dist/index.js.map +1 -1
  20. package/dist/{types-BVDtH9S7.d.ts → types-FA5mH9Ch.d.ts} +46 -90
  21. package/package.json +7 -11
  22. package/src/components/Mutation.tsx +165 -170
  23. package/src/components/MutationKey.tsx +50 -1
  24. package/src/components/Query.tsx +122 -126
  25. package/src/components/QueryKey.tsx +65 -1
  26. package/src/components/QueryOptions.tsx +38 -93
  27. package/src/generators/mutationGenerator.tsx +194 -117
  28. package/src/generators/queryGenerator.tsx +205 -139
  29. package/src/plugin.ts +117 -152
  30. package/src/resolvers/resolverSwr.ts +26 -0
  31. package/src/resolvers/resolverSwrLegacy.ts +17 -0
  32. package/src/types.ts +55 -18
  33. package/src/utils.ts +209 -0
  34. package/dist/components-DaCTPplv.js +0 -756
  35. package/dist/components-DaCTPplv.js.map +0 -1
  36. package/dist/components-Qs8_faOt.cjs +0 -834
  37. package/dist/components-Qs8_faOt.cjs.map +0 -1
  38. package/dist/generators-0YayIrse.js +0 -400
  39. package/dist/generators-0YayIrse.js.map +0 -1
  40. package/dist/generators-Bd4rCa3l.cjs +0 -411
  41. package/dist/generators-Bd4rCa3l.cjs.map +0 -1
package/src/plugin.ts CHANGED
@@ -1,23 +1,42 @@
1
1
  import path from 'node:path'
2
- import { camelCase, pascalCase } from '@internals/utils'
3
- import { createFile, createSource, createText } from '@kubb/ast'
4
- import type { FileNode } from '@kubb/ast/types'
5
- import { createPlugin, getBarrelFiles, type UserGroup } from '@kubb/core'
2
+ import { camelCase } from '@internals/utils'
3
+
4
+ import { ast, definePlugin, type Group } from '@kubb/core'
6
5
  import { pluginClientName } from '@kubb/plugin-client'
7
6
  import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
8
7
  import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
9
8
  import { source as configSource } from '@kubb/plugin-client/templates/config.source'
10
- import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
11
9
  import { pluginTsName } from '@kubb/plugin-ts'
12
10
  import { pluginZodName } from '@kubb/plugin-zod'
13
- import { version } from '../package.json'
14
11
  import { MutationKey, QueryKey } from './components'
15
12
  import { mutationGenerator, queryGenerator } from './generators'
13
+ import { resolverSwr } from './resolvers/resolverSwr.ts'
14
+ import { resolverSwrLegacy } from './resolvers/resolverSwrLegacy.ts'
16
15
  import type { PluginSwr } from './types.ts'
17
16
 
17
+ /**
18
+ * Canonical plugin name for `@kubb/plugin-swr`, used to identify the plugin
19
+ * in driver lookups and warnings.
20
+ */
18
21
  export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
19
22
 
20
- export const pluginSwr = createPlugin<PluginSwr>((options) => {
23
+ /**
24
+ * The `@kubb/plugin-swr` plugin factory.
25
+ *
26
+ * Generates SWR hooks from an OpenAPI/AST `RootNode`.
27
+ * Walks operations, delegates rendering to the active generators,
28
+ * and writes barrel files based on `output.barrelType`.
29
+ *
30
+ * @example
31
+ * ```ts
32
+ * import { pluginSwr } from '@kubb/plugin-swr'
33
+ *
34
+ * export default defineConfig({
35
+ * plugins: [pluginSwr({ output: { path: 'hooks' } })],
36
+ * })
37
+ * ```
38
+ */
39
+ export const pluginSwr = definePlugin<PluginSwr>((options) => {
21
40
  const {
22
41
  output = { path: 'hooks', barrelType: 'named' },
23
42
  group,
@@ -33,181 +52,127 @@ export const pluginSwr = createPlugin<PluginSwr>((options) => {
33
52
  pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
34
53
  mutationKey = MutationKey.getTransformer,
35
54
  queryKey = QueryKey.getTransformer,
36
- generators = [queryGenerator, mutationGenerator].filter(Boolean),
37
55
  paramsCasing,
38
- contentType,
56
+ resolver: userResolver,
57
+ transformer: userTransformer,
58
+ generators: userGenerators = [],
59
+ compatibilityPreset = 'default',
39
60
  } = options
40
61
 
62
+ const defaultResolver = compatibilityPreset === 'kubbV4' ? resolverSwrLegacy : resolverSwr
63
+
41
64
  const clientName = client?.client ?? 'axios'
42
65
  const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
43
66
 
44
- return {
45
- name: pluginSwrName,
46
- version,
47
- options: {
48
- output,
49
- client: {
50
- bundle: client?.bundle,
51
- baseURL: client?.baseURL,
52
- client: clientName,
53
- clientType: client?.clientType ?? 'function',
54
- importPath: clientImportPath,
55
- dataReturnType: client?.dataReturnType ?? 'data',
56
- paramsCasing,
57
- },
58
- queryKey,
59
- query:
60
- query === false
61
- ? false
62
- : {
63
- importPath: 'swr',
64
- methods: ['get'],
65
- ...query,
66
- },
67
- mutationKey,
68
- mutation:
69
- mutation === false
70
- ? false
71
- : {
72
- importPath: 'swr/mutation',
73
- methods: ['post', 'put', 'delete', 'patch'],
74
- ...mutation,
75
- },
76
- parser,
77
- paramsType,
78
- pathParamsType,
79
- paramsCasing,
80
- group,
81
- exclude,
82
- include,
83
- override,
84
- },
85
- pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
86
- resolvePath(baseName, pathMode, options) {
87
- const root = this.root
88
- const mode = pathMode ?? this.getMode(output)
89
-
90
- if (mode === 'single') {
91
- /**
92
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
93
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
94
- */
95
- return path.resolve(root, output.path)
96
- }
67
+ const selectedGenerators = options.generators ?? [queryGenerator, mutationGenerator].filter(Boolean)
97
68
 
98
- if (group && (options?.group?.path || options?.group?.tag)) {
99
- const groupName: UserGroup['name'] = group?.name
69
+ const groupConfig = group
70
+ ? ({
71
+ ...group,
72
+ name: group.name
100
73
  ? group.name
101
- : (ctx) => {
102
- if (group?.type === 'path') {
74
+ : (ctx: { group: string }) => {
75
+ if (group.type === 'path') {
103
76
  return `${ctx.group.split('/')[1]}`
104
77
  }
105
78
  return `${camelCase(ctx.group)}Controller`
106
- }
107
-
108
- return path.resolve(
109
- root,
110
- output.path,
111
- groupName({
112
- group: group.type === 'path' ? options.group.path! : options.group.tag!,
113
- }),
114
- baseName,
115
- )
116
- }
117
-
118
- return path.resolve(root, output.path, baseName)
119
- },
120
- resolveName(name, type) {
121
- let resolvedName = camelCase(name)
79
+ },
80
+ } satisfies Group)
81
+ : undefined
122
82
 
123
- if (type === 'file' || type === 'function') {
124
- resolvedName = camelCase(name, {
125
- isFile: type === 'file',
83
+ return {
84
+ name: pluginSwrName,
85
+ options,
86
+ dependencies: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
87
+ hooks: {
88
+ 'kubb:plugin:setup'(ctx) {
89
+ const resolver = userResolver ? { ...defaultResolver, ...userResolver } : defaultResolver
90
+
91
+ ctx.setOptions({
92
+ output,
93
+ transformers,
94
+ client: {
95
+ bundle: client?.bundle,
96
+ baseURL: client?.baseURL,
97
+ client: clientName,
98
+ clientType: client?.clientType ?? 'function',
99
+ importPath: clientImportPath,
100
+ dataReturnType: client?.dataReturnType ?? 'data',
101
+ paramsCasing,
102
+ },
103
+ queryKey,
104
+ query:
105
+ query === false
106
+ ? false
107
+ : {
108
+ importPath: 'swr',
109
+ methods: ['get'],
110
+ ...query,
111
+ },
112
+ mutationKey,
113
+ mutation:
114
+ mutation === false
115
+ ? false
116
+ : {
117
+ importPath: 'swr/mutation',
118
+ methods: ['post', 'put', 'delete', 'patch'],
119
+ ...mutation,
120
+ },
121
+ parser,
122
+ paramsType,
123
+ pathParamsType,
124
+ paramsCasing,
125
+ group: groupConfig,
126
+ exclude,
127
+ include,
128
+ override,
129
+ resolver,
126
130
  })
127
- }
128
-
129
- if (type === 'type') {
130
- resolvedName = pascalCase(name)
131
- }
132
-
133
- if (type) {
134
- return transformers?.name?.(resolvedName, type) || resolvedName
135
- }
136
-
137
- return resolvedName
138
- },
139
- async buildStart() {
140
- const root = this.root
141
- const mode = this.getMode(output)
142
- const oas = await this.getOas()
143
- const baseURL = await this.getBaseURL()
144
-
145
- if (baseURL) {
146
- this.plugin.options.client.baseURL = baseURL
147
- }
148
-
149
- const hasClientPlugin = !!this.getPlugin(pluginClientName)
150
-
151
- if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) {
152
- // pre add bundled fetch
153
- await this.upsertFile(
154
- createFile({
131
+ ctx.setResolver(resolver)
132
+ if (userTransformer) {
133
+ ctx.setTransformer(userTransformer)
134
+ }
135
+ for (const gen of selectedGenerators) {
136
+ ctx.addGenerator(gen)
137
+ }
138
+ for (const gen of userGenerators) {
139
+ ctx.addGenerator(gen)
140
+ }
141
+
142
+ const root = path.resolve(ctx.config.root, ctx.config.output.path)
143
+
144
+ const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)
145
+
146
+ if (client?.bundle && !hasClientPlugin && !clientImportPath) {
147
+ ctx.injectFile({
155
148
  baseName: 'fetch.ts',
156
149
  path: path.resolve(root, '.kubb/fetch.ts'),
157
150
  sources: [
158
- createSource({
151
+ ast.createSource({
159
152
  name: 'fetch',
160
- nodes: [createText(this.plugin.options.client.client === 'fetch' ? fetchClientSource : axiosClientSource)],
153
+ nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],
161
154
  isExportable: true,
162
155
  isIndexable: true,
163
156
  }),
164
157
  ],
165
- }),
166
- )
167
- }
158
+ })
159
+ }
168
160
 
169
- if (!hasClientPlugin) {
170
- await this.addFile(
171
- createFile({
161
+ if (!hasClientPlugin) {
162
+ ctx.injectFile({
172
163
  baseName: 'config.ts',
173
164
  path: path.resolve(root, '.kubb/config.ts'),
174
165
  sources: [
175
- createSource({
166
+ ast.createSource({
176
167
  name: 'config',
177
- nodes: [createText(configSource)],
168
+ nodes: [ast.createText(configSource)],
178
169
  isExportable: false,
179
170
  isIndexable: false,
180
171
  }),
181
172
  ],
182
- }),
183
- )
184
- }
185
-
186
- const operationGenerator = new OperationGenerator(this.plugin.options, {
187
- oas,
188
- driver: this.driver,
189
- events: this.events,
190
- plugin: this.plugin,
191
- contentType,
192
- exclude,
193
- include,
194
- override,
195
- mode,
196
- })
197
-
198
- const files = await operationGenerator.build(...generators)
199
- await this.upsertFile(...files)
200
-
201
- const barrelFiles = await getBarrelFiles(this.driver.fileManager.files as unknown as FileNode[], {
202
- type: output.barrelType ?? 'named',
203
- root,
204
- output,
205
- meta: {
206
- pluginName: this.plugin.name,
207
- },
208
- })
209
-
210
- await this.upsertFile(...barrelFiles)
173
+ })
174
+ }
175
+ },
211
176
  },
212
177
  }
213
178
  })
@@ -0,0 +1,26 @@
1
+ import { camelCase } from '@internals/utils'
2
+ import { defineResolver } from '@kubb/core'
3
+ import type { PluginSwr } from '../types.ts'
4
+
5
+ /**
6
+ * Resolver for `@kubb/plugin-swr` that provides the default naming
7
+ * and path-resolution helpers used by the plugin.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { resolverSwr } from '@kubb/plugin-swr'
12
+ *
13
+ * resolverSwr.default('list pets', 'function') // -> 'listPets'
14
+ * resolverSwr.resolveName('show pet by id') // -> 'showPetById'
15
+ * ```
16
+ */
17
+ export const resolverSwr = defineResolver<PluginSwr>(() => ({
18
+ name: 'default',
19
+ pluginName: 'plugin-swr',
20
+ default(name, type) {
21
+ return camelCase(name, { isFile: type === 'file' })
22
+ },
23
+ resolveName(name) {
24
+ return this.default(name, 'function')
25
+ },
26
+ }))
@@ -0,0 +1,17 @@
1
+ import { defineResolver } from '@kubb/core'
2
+ import type { PluginSwr } from '../types.ts'
3
+ import { resolverSwr } from './resolverSwr.ts'
4
+
5
+ /**
6
+ * Legacy resolver for `@kubb/plugin-swr` that reproduces the naming conventions
7
+ * used in Kubb v4. Enable via `compatibilityPreset: 'kubbV4'`.
8
+ *
9
+ * The naming logic is identical to the default resolver — the only difference
10
+ * is the `name` field (`'kubbV4'` vs `'default'`) so the driver can
11
+ * distinguish presets.
12
+ */
13
+ export const resolverSwrLegacy = defineResolver<PluginSwr>(() => ({
14
+ ...resolverSwr,
15
+ name: 'kubbV4',
16
+ pluginName: 'plugin-swr',
17
+ }))
package/src/types.ts CHANGED
@@ -1,11 +1,36 @@
1
- import type { Transformer } from '@internals/tanstack-query'
2
- import type { Output, PluginFactoryOptions, ResolveNameParams, UserGroup } from '@kubb/core'
3
- import type { contentType, HttpMethod, Oas } from '@kubb/oas'
1
+ import type {
2
+ ast,
3
+ CompatibilityPreset,
4
+ Exclude,
5
+ Generator,
6
+ Group,
7
+ Include,
8
+ Output,
9
+ Override,
10
+ PluginFactoryOptions,
11
+ ResolvePathOptions,
12
+ Resolver,
13
+ UserGroup,
14
+ } from '@kubb/core'
4
15
  import type { ClientImportPath, PluginClient } from '@kubb/plugin-client'
5
- import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
6
- import type { Generator } from '@kubb/plugin-oas/generators'
7
16
 
8
- export type { Transformer } from '@internals/tanstack-query'
17
+ /**
18
+ * Customizes how queryKey or mutationKey arrays are built.
19
+ */
20
+ export type Transformer = (props: { node: ast.OperationNode; casing: 'camelcase' | undefined }) => unknown[]
21
+
22
+ /**
23
+ * The concrete resolver type for `@kubb/plugin-swr`.
24
+ * Extends the base `Resolver` with a `resolveName` helper for hook function names.
25
+ */
26
+ export type ResolverSwr = Resolver & {
27
+ /**
28
+ * Resolves the hook function name for a given raw operation name.
29
+ * @example
30
+ * resolver.resolveName('show pet by id') // -> 'showPetById'
31
+ */
32
+ resolveName(this: ResolverSwr, name: string): string
33
+ }
9
34
 
10
35
  /**
11
36
  * Customize the queryKey
@@ -22,7 +47,7 @@ type Query = {
22
47
  * Define which HttpMethods can be used for queries
23
48
  * @default ['get']
24
49
  */
25
- methods?: Array<HttpMethod>
50
+ methods?: Array<string>
26
51
  /**
27
52
  * Path to the useQuery hook for useQuery functionality.
28
53
  * Used as `import { useQuery } from '${importPath}'`.
@@ -38,7 +63,7 @@ type Mutation = {
38
63
  * Define which HttpMethods can be used for queries
39
64
  * @default ['post', 'put', 'delete', 'patch']
40
65
  */
41
- methods?: Array<HttpMethod>
66
+ methods?: Array<string>
42
67
  /**
43
68
  * Path to the useQuery hook for useQuery functionality.
44
69
  * Used as `import { useQuery } from '${importPath}'`.
@@ -61,12 +86,7 @@ export type Options = {
61
86
  * Specify the export location for the files and define the behavior of the output
62
87
  * @default { path: 'hooks', barrelType: 'named' }
63
88
  */
64
- output?: Output<Oas>
65
- /**
66
- * Define which contentType should be used.
67
- * By default, the first JSON valid mediaType is used
68
- */
69
- contentType?: contentType
89
+ output?: Output
70
90
  /**
71
91
  * Group the SWR hooks based on the provided name.
72
92
  */
@@ -116,8 +136,23 @@ export type Options = {
116
136
  /**
117
137
  * Customize the names based on the type that is provided by the plugin.
118
138
  */
119
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
139
+ name?: (name: string, type?: string) => string
120
140
  }
141
+ /**
142
+ * Apply a compatibility naming preset.
143
+ * @default 'default'
144
+ */
145
+ compatibilityPreset?: CompatibilityPreset
146
+ /**
147
+ * Override individual resolver methods. Any method you omit falls back to the
148
+ * preset resolver's implementation. Use `this.default(...)` to call it.
149
+ */
150
+ resolver?: Partial<ResolverSwr> & ThisType<ResolverSwr>
151
+ /**
152
+ * Single AST visitor applied to each node before printing.
153
+ * Return `null` or `undefined` from a method to leave the node unchanged.
154
+ */
155
+ transformer?: ast.Visitor
121
156
  /**
122
157
  * Define some generators next to the swr generators
123
158
  */
@@ -125,7 +160,7 @@ export type Options = {
125
160
  }
126
161
 
127
162
  type ResolvedOptions = {
128
- output: Output<Oas>
163
+ output: Output
129
164
  client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>
130
165
  parser: Required<NonNullable<Options['parser']>>
131
166
  queryKey: QueryKey | undefined
@@ -135,13 +170,15 @@ type ResolvedOptions = {
135
170
  paramsCasing: Options['paramsCasing']
136
171
  paramsType: NonNullable<Options['paramsType']>
137
172
  pathParamsType: NonNullable<Options['pathParamsType']>
138
- group: Options['group']
173
+ transformers: NonNullable<Options['transformers']>
174
+ group: Group | undefined
139
175
  exclude: NonNullable<Options['exclude']>
140
176
  include: Options['include']
141
177
  override: NonNullable<Options['override']>
178
+ resolver: ResolverSwr
142
179
  }
143
180
 
144
- export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>
181
+ export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions, ResolverSwr>
145
182
 
146
183
  declare global {
147
184
  namespace Kubb {