@kubb/plugin-swr 3.0.0-alpha.2 → 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 (58) hide show
  1. package/README.md +13 -4
  2. package/dist/chunk-7WG6LPZJ.js +185 -0
  3. package/dist/chunk-7WG6LPZJ.js.map +1 -0
  4. package/dist/chunk-DGY6XSP2.js +187 -0
  5. package/dist/chunk-DGY6XSP2.js.map +1 -0
  6. package/dist/chunk-RMG5RYPG.cjs +189 -0
  7. package/dist/chunk-RMG5RYPG.cjs.map +1 -0
  8. package/dist/chunk-WI3XUYBA.cjs +194 -0
  9. package/dist/chunk-WI3XUYBA.cjs.map +1 -0
  10. package/dist/components.cjs +15 -6
  11. package/dist/components.cjs.map +1 -1
  12. package/dist/components.d.cts +49 -6
  13. package/dist/components.d.ts +49 -6
  14. package/dist/components.js +2 -10
  15. package/dist/components.js.map +1 -1
  16. package/dist/generators.cjs +17 -0
  17. package/dist/generators.cjs.map +1 -0
  18. package/dist/generators.d.cts +10 -0
  19. package/dist/generators.d.ts +10 -0
  20. package/dist/generators.js +4 -0
  21. package/dist/generators.js.map +1 -0
  22. package/dist/index.cjs +90 -127
  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 +70 -114
  27. package/dist/index.js.map +1 -1
  28. package/dist/types-BCd4p4VZ.d.cts +127 -0
  29. package/dist/types-BCd4p4VZ.d.ts +127 -0
  30. package/package.json +22 -16
  31. package/src/components/Mutation.tsx +82 -227
  32. package/src/components/Query.tsx +86 -264
  33. package/src/components/QueryOptions.tsx +58 -179
  34. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +40 -0
  35. package/src/generators/__snapshots__/clientGetImportPath.ts +40 -0
  36. package/src/generators/__snapshots__/clientPostImportPath.ts +30 -0
  37. package/src/generators/__snapshots__/findByTags.ts +40 -0
  38. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +40 -0
  39. package/src/generators/__snapshots__/findByTagsWithZod.ts +40 -0
  40. package/src/generators/__snapshots__/getAsMutation.ts +32 -0
  41. package/src/generators/__snapshots__/postAsQuery.ts +39 -0
  42. package/src/generators/__snapshots__/updatePetById.ts +30 -0
  43. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +34 -0
  44. package/src/generators/index.ts +2 -0
  45. package/src/generators/mutationGenerator.tsx +96 -0
  46. package/src/generators/queryGenerator.tsx +101 -0
  47. package/src/plugin.ts +61 -50
  48. package/src/types.ts +63 -55
  49. package/dist/chunk-ECJ346AA.js +0 -542
  50. package/dist/chunk-ECJ346AA.js.map +0 -1
  51. package/dist/chunk-H4LHXYRJ.cjs +0 -542
  52. package/dist/chunk-H4LHXYRJ.cjs.map +0 -1
  53. package/dist/index-B3C-JOIU.d.cts +0 -299
  54. package/dist/index-B3C-JOIU.d.ts +0 -299
  55. package/src/OperationGenerator.tsx +0 -75
  56. package/src/components/SchemaType.tsx +0 -59
  57. package/src/components/__snapshots__/Mutation/Pets.ts +0 -42
  58. package/src/components/__snapshots__/Query/showPetById.ts +0 -55
package/src/plugin.ts CHANGED
@@ -3,40 +3,59 @@ import path from 'node:path'
3
3
  import { FileManager, PluginManager, createPlugin } from '@kubb/core'
4
4
  import { camelCase, pascalCase } from '@kubb/core/transformers'
5
5
  import { renderTemplate } from '@kubb/core/utils'
6
- import { pluginOasName } from '@kubb/plugin-oas'
7
- import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
6
+ import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
7
+
8
8
  import { pluginTsName } from '@kubb/plugin-ts'
9
9
  import { pluginZodName } from '@kubb/plugin-zod'
10
10
 
11
- import { OperationGenerator } from './OperationGenerator.tsx'
12
- import { Mutation, Query, QueryOptions } from './components/index.ts'
13
-
14
11
  import type { Plugin } from '@kubb/core'
15
12
  import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
13
+ import { mutationGenerator, queryGenerator } from './generators'
16
14
  import type { PluginSwr } from './types.ts'
17
15
 
18
16
  export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
19
17
 
20
18
  export const pluginSwr = createPlugin<PluginSwr>((options) => {
21
- const { output = { path: 'hooks' }, group, exclude = [], include, override = [], parser, transformers = {}, templates, dataReturnType = 'data' } = options
19
+ const {
20
+ output = { path: 'hooks' },
21
+ group,
22
+ exclude = [],
23
+ include,
24
+ override = [],
25
+ parser = 'client',
26
+ transformers = {},
27
+ query,
28
+ mutation,
29
+ client,
30
+ pathParamsType = 'inline',
31
+ } = options
22
32
  const template = group?.output ? group.output : `${output.path}/{{tag}}SWRController`
23
33
 
24
34
  return {
25
35
  name: pluginSwrName,
26
36
  options: {
27
- extName: output.extName,
28
- templates: {
29
- mutation: Mutation.templates,
30
- query: Query.templates,
31
- queryOptions: QueryOptions.templates,
32
- ...templates,
37
+ output: {
38
+ exportType: 'barrelNamed',
39
+ ...output,
33
40
  },
41
+ pathParamsType,
34
42
  client: {
35
43
  importPath: '@kubb/plugin-client/client',
36
- ...options.client,
44
+ dataReturnType: 'data',
45
+ ...client,
46
+ },
47
+ query: {
48
+ importPath: 'swr',
49
+ methods: ['get'],
50
+ ...query,
51
+ },
52
+ mutation: {
53
+ importPath: 'swr/mutation',
54
+ methods: ['post', 'put', 'delete', 'patch'],
55
+ ...mutation,
37
56
  },
38
- dataReturnType,
39
57
  parser,
58
+ baseURL: undefined,
40
59
  },
41
60
  pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
42
61
  resolvePath(baseName, pathMode, options) {
@@ -64,7 +83,6 @@ export const pluginSwr = createPlugin<PluginSwr>((options) => {
64
83
 
65
84
  if (type === 'file' || type === 'function') {
66
85
  resolvedName = camelCase(name, {
67
- prefix: 'use',
68
86
  isFile: type === 'file',
69
87
  })
70
88
  }
@@ -85,48 +103,41 @@ export const pluginSwr = createPlugin<PluginSwr>((options) => {
85
103
  const oas = await swaggerPlugin.context.getOas()
86
104
  const root = path.resolve(this.config.root, this.config.output.path)
87
105
  const mode = FileManager.getMode(path.resolve(root, output.path))
88
-
89
- const operationGenerator = new OperationGenerator(this.plugin.options, {
90
- oas,
91
- pluginManager: this.pluginManager,
92
- plugin: this.plugin,
93
- contentType: swaggerPlugin.context.contentType,
94
- exclude,
95
- include,
96
- override,
97
- mode,
98
- })
99
-
100
- const files = await operationGenerator.build()
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)
101
126
  await this.addFile(...files)
102
- },
103
- async buildEnd() {
104
- if (this.config.output.write === false) {
105
- return
106
- }
107
127
 
108
- const root = path.resolve(this.config.root, this.config.output.path)
109
-
110
- if (group?.type === 'tag') {
111
- const rootFiles = await getGroupedByTagFiles({
112
- logger: this.logger,
113
- files: this.fileManager.files,
114
- plugin: this.plugin,
115
- template,
116
- exportAs: group.exportAs || '{{tag}}SWRHooks',
128
+ if (this.config.output.exportType) {
129
+ const barrelFiles = await this.fileManager.getBarrelFiles({
117
130
  root,
118
131
  output,
132
+ files: this.fileManager.files,
133
+ meta: {
134
+ pluginKey: this.plugin.key,
135
+ },
136
+ logger: this.logger,
119
137
  })
120
138
 
121
- await this.addFile(...rootFiles)
139
+ await this.addFile(...barrelFiles)
122
140
  }
123
-
124
- await this.fileManager.addIndexes({
125
- root,
126
- output,
127
- meta: { pluginKey: this.plugin.key },
128
- logger: this.logger,
129
- })
130
141
  },
131
142
  }
132
143
  })
package/src/types.ts CHANGED
@@ -1,37 +1,12 @@
1
- import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
- import type * as KubbFile from '@kubb/fs/types'
1
+ import type { Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+ import type { HttpMethod } from '@kubb/oas'
3
3
  import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
4
- import type { Mutation } from './components/Mutation.tsx'
5
- import type { Query } from './components/Query.tsx'
6
- import type { QueryOptions } from './components/QueryOptions.tsx'
7
-
8
- type Templates = {
9
- mutation?: typeof Mutation.templates | false
10
- query?: typeof Query.templates | false
11
- queryOptions?: typeof QueryOptions.templates | false
12
- }
13
4
 
14
5
  export type Options = {
15
- output?: {
16
- /**
17
- * Output to save the SWR hooks.
18
- * @default `"hooks"`
19
- */
20
- path: string
21
- /**
22
- * Name to be used for the `export * as {{exportAs}} from './'`
23
- */
24
- exportAs?: string
25
- /**
26
- * Add an extension to the generated imports and exports, default it will not use an extension
27
- */
28
- extName?: KubbFile.Extname
29
- /**
30
- * Define what needs to exported, here you can also disable the export of barrel files
31
- * @default `'barrel'`
32
- */
33
- exportType?: 'barrel' | 'barrelNamed' | false
34
- }
6
+ /**
7
+ * @default 'hooks'
8
+ */
9
+ output?: Output
35
10
  /**
36
11
  * Group the SWR hooks based on the provided name.
37
12
  */
@@ -70,50 +45,83 @@ export type Options = {
70
45
  /**
71
46
  * Path to the client import path that will be used to do the API calls.
72
47
  * It will be used as `import client from '${client.importPath}'`.
73
- * It allow both relative and absolute path.
48
+ * It allows both relative and absolute path.
74
49
  * the path will be applied as is, so relative path shoule be based on the file being generated.
75
50
  * @default '@kubb/plugin-client/client'
76
51
  */
77
52
  importPath?: string
53
+ /**
54
+ * ReturnType that needs to be used when calling client().
55
+ *
56
+ * `Data` will return ResponseConfig[data].
57
+ *
58
+ * `Full` will return ResponseConfig.
59
+ * @default `'data'`
60
+ * @private
61
+ */
62
+ dataReturnType?: 'data' | 'full'
63
+ }
64
+ query?: {
65
+ /**
66
+ * Define which HttpMethods can be used for queries
67
+ * @default ['get']
68
+ */
69
+ methods?: Array<HttpMethod>
70
+ /**
71
+ * Path to the useQuery that will be used to do the useQuery functionality.
72
+ * It will be used as `import { useQuery } from '${importPath}'`.
73
+ * It allows both relative and absolute path.
74
+ * the path will be applied as is, so relative path should be based on the file being generated.
75
+ * @default '@tanstack/react-query' if 'framework' is set to 'react'
76
+ */
77
+ importPath?: string
78
+ }
79
+ mutation?: {
80
+ /**
81
+ * Define which HttpMethods can be used for queries
82
+ * @default ['post', 'put', 'delete', 'patch']
83
+ */
84
+ methods?: Array<HttpMethod>
85
+ /**
86
+ * Path to the useQuery that will be used to do the useQuery functionality.
87
+ * It will be used as `import { useQuery } from '${importPath}'`.
88
+ * It allows both relative and absolute path.
89
+ * the path will be applied as is, so relative path should be based on the file being generated.
90
+ * @default '@tanstack/react-query' if 'framework' is set to 'react'
91
+ */
92
+ importPath?: string
78
93
  }
79
94
  /**
80
- * ReturnType that needs to be used when calling client().
95
+ * How to pass your pathParams.
81
96
  *
82
- * `Data` will return ResponseConfig[data].
97
+ * `object` will return the pathParams as an object.
83
98
  *
84
- * `Full` will return ResponseConfig.
85
- * @default `'data'`
99
+ * `inline` will return the pathParams as comma separated params.
100
+ * @default `'inline'`
86
101
  * @private
87
102
  */
88
- dataReturnType?: 'data' | 'full'
103
+ pathParamsType?: 'object' | 'inline'
104
+ /**
105
+ * Which parser can be used before returning the data to `swr`.
106
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
107
+ */
108
+ parser?: 'client' | 'zod'
89
109
  transformers?: {
90
110
  /**
91
111
  * Customize the names based on the type that is provided by the plugin.
92
112
  */
93
113
  name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
94
114
  }
95
- /**
96
- * Which parser can be used before returning the data to `@tanstack/query`.
97
- * `'zod'` will use `@kubb/plugin-zod` to parse the data.
98
- */
99
- parser?: 'zod'
100
- /**
101
- * Make it possible to override one of the templates
102
- */
103
- templates?: Partial<Templates>
104
115
  }
105
116
 
106
117
  type ResolvedOptions = {
107
- extName: KubbFile.Extname | undefined
118
+ output: Output
119
+ baseURL: string | undefined
108
120
  client: Required<NonNullable<Options['client']>>
109
- dataReturnType: NonNullable<Options['dataReturnType']>
110
- templates: NonNullable<Templates>
111
- parser: Options['parser']
112
- }
113
-
114
- export type FileMeta = {
115
- pluginKey?: Plugin['key']
116
- tag?: string
121
+ parser: Required<NonNullable<Options['parser']>>
122
+ mutation: Required<NonNullable<Options['mutation']>>
123
+ query: Required<NonNullable<Options['query']>>
124
+ pathParamsType: NonNullable<Options['pathParamsType']>
117
125
  }
118
126
 
119
127
  export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>