@kubb/plugin-swr 3.0.0-alpha.0

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.
package/src/plugin.ts ADDED
@@ -0,0 +1,139 @@
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 { pluginOasName } from '@kubb/plugin-oas'
7
+ import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
8
+ import { pluginTsName } from '@kubb/plugin-ts'
9
+ import { pluginZodName } from '@kubb/plugin-zod'
10
+
11
+ import { OperationGenerator } from './OperationGenerator.tsx'
12
+ import { Mutation, Query, QueryOptions } from './components/index.ts'
13
+
14
+ import type { Plugin } from '@kubb/core'
15
+ import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
16
+ import type { PluginSwr } from './types.ts'
17
+
18
+ export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
19
+
20
+ export const pluginSwr = createPlugin<PluginSwr>((options) => {
21
+ const { output = { path: 'hooks' }, group, exclude = [], include, override = [], parser, transformers = {}, templates, dataReturnType = 'data' } = options
22
+ const template = group?.output ? group.output : `${output.path}/{{tag}}SWRController`
23
+
24
+ return {
25
+ name: pluginSwrName,
26
+ options: {
27
+ extName: output.extName,
28
+ templates: {
29
+ mutation: Mutation.templates,
30
+ query: Query.templates,
31
+ queryOptions: QueryOptions.templates,
32
+ ...templates,
33
+ },
34
+ client: {
35
+ importPath: '@kubb/plugin-client/client',
36
+ ...options.client,
37
+ },
38
+ dataReturnType,
39
+ parser,
40
+ },
41
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
42
+ resolvePath(baseName, pathMode, options) {
43
+ const root = path.resolve(this.config.root, this.config.output.path)
44
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
45
+
46
+ if (mode === 'single') {
47
+ /**
48
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
49
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
50
+ */
51
+ return path.resolve(root, output.path)
52
+ }
53
+
54
+ if (options?.tag && group?.type === 'tag') {
55
+ const tag = camelCase(options.tag)
56
+
57
+ return path.resolve(root, renderTemplate(template, { tag }), baseName)
58
+ }
59
+
60
+ return path.resolve(root, output.path, baseName)
61
+ },
62
+ resolveName(name, type) {
63
+ let resolvedName = camelCase(name)
64
+
65
+ if (type === 'file' || type === 'function') {
66
+ resolvedName = camelCase(name, {
67
+ prefix: 'use',
68
+ isFile: type === 'file',
69
+ })
70
+ }
71
+
72
+ if (type === 'type') {
73
+ resolvedName = pascalCase(name)
74
+ }
75
+
76
+ if (type) {
77
+ return transformers?.name?.(resolvedName, type) || resolvedName
78
+ }
79
+
80
+ return resolvedName
81
+ },
82
+ async buildStart() {
83
+ const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
84
+
85
+ const oas = await swaggerPlugin.api.getOas()
86
+ const root = path.resolve(this.config.root, this.config.output.path)
87
+ 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.api.contentType,
94
+ exclude,
95
+ include,
96
+ override,
97
+ mode,
98
+ })
99
+
100
+ const files = await operationGenerator.build()
101
+ await this.addFile(...files)
102
+ },
103
+ async writeFile(path, source) {
104
+ if (!path.endsWith('.ts') || !source) {
105
+ return
106
+ }
107
+
108
+ return this.fileManager.write(path, source, { sanity: false })
109
+ },
110
+ async buildEnd() {
111
+ if (this.config.output.write === false) {
112
+ return
113
+ }
114
+
115
+ const root = path.resolve(this.config.root, this.config.output.path)
116
+
117
+ if (group?.type === 'tag') {
118
+ const rootFiles = await getGroupedByTagFiles({
119
+ logger: this.logger,
120
+ files: this.fileManager.files,
121
+ plugin: this.plugin,
122
+ template,
123
+ exportAs: group.exportAs || '{{tag}}SWRHooks',
124
+ root,
125
+ output,
126
+ })
127
+
128
+ await this.addFile(...rootFiles)
129
+ }
130
+
131
+ await this.fileManager.addIndexes({
132
+ root,
133
+ output,
134
+ meta: { pluginKey: this.plugin.key },
135
+ logger: this.logger,
136
+ })
137
+ },
138
+ }
139
+ })
package/src/types.ts ADDED
@@ -0,0 +1,125 @@
1
+ import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
+ import type * as KubbFile from '@kubb/fs/types'
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
+
14
+ 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
+ }
35
+ /**
36
+ * Group the SWR hooks based on the provided name.
37
+ */
38
+ group?: {
39
+ /**
40
+ * Tag will group based on the operation tag inside the Swagger file
41
+ */
42
+ type: 'tag'
43
+ /**
44
+ * Relative path to save the grouped SWR hooks.
45
+ *
46
+ * `{{tag}}` will be replaced by the current tagName.
47
+ * @example `${output}/{{tag}}Controller` => `hooks/PetController`
48
+ * @default `${output}/{{tag}}Controller`
49
+ */
50
+ output?: string
51
+ /**
52
+ * Name to be used for the `export * as {{exportAs}} from './`
53
+ * @default `"{{tag}}SWRHooks"`
54
+ */
55
+ exportAs?: string
56
+ }
57
+ /**
58
+ * Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
59
+ */
60
+ exclude?: Array<Exclude>
61
+ /**
62
+ * Array containing include parameters to include tags/operations/methods/paths.
63
+ */
64
+ include?: Array<Include>
65
+ /**
66
+ * Array containing override parameters to override `options` based on tags/operations/methods/paths.
67
+ */
68
+ override?: Array<Override<ResolvedOptions>>
69
+ client?: {
70
+ /**
71
+ * Path to the client import path that will be used to do the API calls.
72
+ * It will be used as `import client from '${client.importPath}'`.
73
+ * It allow both relative and absolute path.
74
+ * the path will be applied as is, so relative path shoule be based on the file being generated.
75
+ * @default '@kubb/plugin-client/client'
76
+ */
77
+ importPath?: string
78
+ }
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
+ transformers?: {
90
+ /**
91
+ * Customize the names based on the type that is provided by the plugin.
92
+ */
93
+ name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
94
+ }
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
+ }
105
+
106
+ type ResolvedOptions = {
107
+ extName: KubbFile.Extname | undefined
108
+ 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
117
+ }
118
+
119
+ export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>
120
+
121
+ declare module '@kubb/core' {
122
+ export interface _Register {
123
+ ['@kubb/plugin-swr']: PluginSwr
124
+ }
125
+ }