@kubb/plugin-swr 5.0.0-alpha.9 → 5.0.0-beta.33

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 (44) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +33 -26
  3. package/dist/components-ByADeLZO.cjs +1029 -0
  4. package/dist/components-ByADeLZO.cjs.map +1 -0
  5. package/dist/components-CBdpiiay.js +933 -0
  6. package/dist/components-CBdpiiay.js.map +1 -0
  7. package/dist/components.cjs +1 -1
  8. package/dist/components.d.ts +45 -51
  9. package/dist/components.js +1 -1
  10. package/dist/generators-Bb5wNYig.cjs +445 -0
  11. package/dist/generators-Bb5wNYig.cjs.map +1 -0
  12. package/dist/generators-D5kJZsB1.js +434 -0
  13. package/dist/generators-D5kJZsB1.js.map +1 -0
  14. package/dist/generators.cjs +1 -1
  15. package/dist/generators.d.ts +4 -500
  16. package/dist/generators.js +1 -1
  17. package/dist/index.cjs +134 -113
  18. package/dist/index.cjs.map +1 -1
  19. package/dist/index.d.ts +4 -4
  20. package/dist/index.js +130 -113
  21. package/dist/index.js.map +1 -1
  22. package/dist/types-D9jeG3fE.d.ts +220 -0
  23. package/extension.yaml +199 -0
  24. package/package.json +57 -67
  25. package/src/components/Mutation.tsx +106 -227
  26. package/src/components/MutationKey.tsx +25 -1
  27. package/src/components/Query.tsx +75 -140
  28. package/src/components/QueryOptions.tsx +46 -95
  29. package/src/generators/mutationGenerator.tsx +113 -128
  30. package/src/generators/queryGenerator.tsx +125 -137
  31. package/src/index.ts +2 -2
  32. package/src/plugin.ts +117 -170
  33. package/src/resolvers/resolverSwr.ts +56 -0
  34. package/src/types.ts +115 -59
  35. package/src/utils.ts +10 -0
  36. package/dist/components-DRDGvgXG.js +0 -702
  37. package/dist/components-DRDGvgXG.js.map +0 -1
  38. package/dist/components-jd0l9XKn.cjs +0 -780
  39. package/dist/components-jd0l9XKn.cjs.map +0 -1
  40. package/dist/generators-CRSl6u2M.js +0 -399
  41. package/dist/generators-CRSl6u2M.js.map +0 -1
  42. package/dist/generators-D062obA7.cjs +0 -410
  43. package/dist/generators-D062obA7.cjs.map +0 -1
  44. package/dist/types-BIaGRPjD.d.ts +0 -210
@@ -1,182 +1,170 @@
1
1
  import path from 'node:path'
2
- import { usePluginDriver } from '@kubb/core/hooks'
3
- import { pluginClientName } from '@kubb/plugin-client'
4
- import { Client } from '@kubb/plugin-client/components'
5
- import { createReactGenerator } from '@kubb/plugin-oas/generators'
6
- import { useOas, useOperationManager } from '@kubb/plugin-oas/hooks'
7
- import { getBanner, getFooter } from '@kubb/plugin-oas/utils'
2
+ import { resolveOperationTypeNames } from '@internals/shared'
3
+ import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
+ import { ast, defineGenerator } from '@kubb/core'
5
+ import { Client, pluginClientName } from '@kubb/plugin-client'
8
6
  import { pluginTsName } from '@kubb/plugin-ts'
9
7
  import { pluginZodName } from '@kubb/plugin-zod'
10
- import { File } from '@kubb/react-fabric'
8
+ import { File, jsxRenderer } from '@kubb/renderer-jsx'
11
9
  import { difference } from 'remeda'
12
10
  import { Query, QueryKey, QueryOptions } from '../components'
13
11
  import type { PluginSwr } from '../types'
14
12
 
15
- export const queryGenerator = createReactGenerator<PluginSwr>({
13
+ export const queryGenerator = defineGenerator<PluginSwr>({
16
14
  name: 'swr-query',
17
- Operation({ config, operation, generator, plugin }) {
18
- const {
19
- options,
20
- options: { output },
21
- } = plugin
22
- const driver = usePluginDriver()
23
-
24
- const oas = useOas()
25
- const { getSchemas, getName, getFile } = useOperationManager(generator)
26
-
27
- const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
28
- const isMutation = difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some(
29
- (method) => operation.method === method,
30
- )
31
- const importPath = options.query ? options.query.importPath : 'swr/mutation'
15
+ renderer: jsxRenderer,
16
+ operation(node, ctx) {
17
+ if (!ast.isHttpOperationNode(node)) return null
18
+ const { config, driver, resolver, root } = ctx
19
+ const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group } = ctx.options
32
20
 
33
- const query = {
34
- name: getName(operation, { type: 'function', prefix: 'use' }),
35
- typeName: getName(operation, { type: 'type' }),
36
- file: getFile(operation, { prefix: 'use' }),
37
- }
21
+ const pluginTs = driver.getPlugin(pluginTsName)
22
+ if (!pluginTs) return null
23
+ const tsResolver = driver.getResolver(pluginTsName)
38
24
 
39
- const hasClientPlugin = !!driver.getPluginByName(pluginClientName)
40
- // Class-based clients are not compatible with query hooks, so we generate inline clients
41
- const shouldUseClientPlugin = hasClientPlugin && options.client.clientType !== 'class'
42
- const client = {
43
- name: shouldUseClientPlugin
44
- ? getName(operation, {
45
- type: 'function',
46
- pluginName: pluginClientName,
47
- })
48
- : getName(operation, {
49
- type: 'function',
50
- }),
51
- file: getFile(operation, { pluginName: pluginClientName }),
52
- }
25
+ const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
26
+ const isMutation =
27
+ mutation !== false &&
28
+ !isQuery &&
29
+ difference(mutation ? mutation.methods : [], query ? query.methods : []).some((method) => node.method.toLowerCase() === method.toLowerCase())
53
30
 
54
- const queryOptions = {
55
- name: getName(operation, { type: 'function', suffix: 'QueryOptions' }),
56
- }
57
- const queryKey = {
58
- name: getName(operation, { type: 'const', suffix: 'QueryKey' }),
59
- typeName: getName(operation, { type: 'type', suffix: 'QueryKey' }),
60
- }
31
+ if (!isQuery || isMutation) return null
61
32
 
62
- const type = {
63
- file: getFile(operation, { pluginName: pluginTsName }),
64
- //todo remove type?
65
- schemas: getSchemas(operation, {
66
- pluginName: pluginTsName,
67
- type: 'type',
68
- }),
69
- }
33
+ const importPath = query ? query.importPath : 'swr'
70
34
 
71
- const zod = {
72
- file: getFile(operation, { pluginName: pluginZodName }),
73
- schemas: getSchemas(operation, {
74
- pluginName: pluginZodName,
75
- type: 'function',
76
- }),
77
- }
35
+ const queryName = resolver.resolveQueryName(node)
36
+ const queryOptionsName = resolver.resolveQueryOptionsName(node)
37
+ const queryKeyName = resolver.resolveQueryKeyName(node)
38
+ const queryKeyTypeName = resolver.resolveQueryKeyTypeName(node)
39
+ const clientName = resolver.resolveClientName(node)
78
40
 
79
- if (!isQuery || isMutation) {
80
- return null
41
+ const meta = {
42
+ file: resolver.resolveFile({ name: queryName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group }),
43
+ fileTs: tsResolver.resolveFile(
44
+ { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
45
+ { root, output: pluginTs.options?.output ?? output, group: pluginTs.options?.group },
46
+ ),
81
47
  }
82
48
 
49
+ const importedTypeNames = resolveOperationTypeNames(node, tsResolver, {
50
+ paramsCasing,
51
+ exclude: [queryKeyTypeName],
52
+ order: 'body-response-first',
53
+ })
54
+
55
+ const pluginZod = parser === 'zod' ? driver.getPlugin(pluginZodName) : undefined
56
+ const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
57
+ const fileZod = zodResolver
58
+ ? zodResolver.resolveFile(
59
+ { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
60
+ { root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
61
+ )
62
+ : undefined
63
+ const zodSchemaNames = resolveZodSchemaNames(node, zodResolver)
64
+
65
+ const clientPlugin = driver.getPlugin(pluginClientName)
66
+ const hasClientPlugin = clientPlugin?.name === pluginClientName
67
+ const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
68
+ const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : undefined
69
+
70
+ const clientFile = shouldUseClientPlugin
71
+ ? clientResolver?.resolveFile(
72
+ { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
73
+ {
74
+ root,
75
+ output: clientPlugin?.options?.output ?? output,
76
+ group: clientPlugin?.options?.group,
77
+ },
78
+ )
79
+ : undefined
80
+
81
+ const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
82
+
83
83
  return (
84
84
  <File
85
- baseName={query.file.baseName}
86
- path={query.file.path}
87
- meta={query.file.meta}
88
- banner={getBanner({ oas, output, config: driver.config })}
89
- footer={getFooter({ oas, output })}
85
+ baseName={meta.file.baseName}
86
+ path={meta.file.path}
87
+ meta={meta.file.meta}
88
+ banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
89
+ footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
90
90
  >
91
- {options.parser === 'zod' && (
92
- <File.Import name={[zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean)} root={query.file.path} path={zod.file.path} />
93
- )}
94
- {options.client.importPath ? (
91
+ {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
92
+ {clientOptions.importPath ? (
95
93
  <>
96
- {!shouldUseClientPlugin && <File.Import name={'fetch'} path={options.client.importPath} />}
97
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={options.client.importPath} isTypeOnly />
98
- {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
94
+ {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
95
+ <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
96
+ {clientOptions.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={clientOptions.importPath} isTypeOnly />}
99
97
  </>
100
98
  ) : (
101
99
  <>
102
- {!shouldUseClientPlugin && (
103
- <File.Import name={['fetch']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} />
104
- )}
100
+ {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
105
101
  <File.Import
106
102
  name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
107
- root={query.file.path}
108
- path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
103
+ root={meta.file.path}
104
+ path={path.resolve(root, '.kubb/client.ts')}
109
105
  isTypeOnly
110
106
  />
111
- {options.client.dataReturnType === 'full' && (
112
- <File.Import name={['ResponseConfig']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} isTypeOnly />
107
+ {clientOptions.dataReturnType === 'full' && (
108
+ <File.Import name={['ResponseConfig']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} isTypeOnly />
113
109
  )}
114
110
  </>
115
111
  )}
116
- {shouldUseClientPlugin && <File.Import name={[client.name]} root={query.file.path} path={client.file.path} />}
117
- {!shouldUseClientPlugin && (
118
- <File.Import name={['buildFormData']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/config.ts')} />
112
+ {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
113
+ {!shouldUseClientPlugin && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
114
+ {meta.fileTs && importedTypeNames.length > 0 && (
115
+ <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
119
116
  )}
120
117
 
121
- <File.Import
122
- name={[
123
- type.schemas.request?.name,
124
- type.schemas.response.name,
125
- type.schemas.pathParams?.name,
126
- type.schemas.queryParams?.name,
127
- type.schemas.headerParams?.name,
128
- ...(type.schemas.statusCodes?.map((item) => item.name) || []),
129
- ].filter(Boolean)}
130
- root={query.file.path}
131
- path={type.file.path}
132
- isTypeOnly
133
- />
134
118
  <QueryKey
135
- name={queryKey.name}
136
- typeName={queryKey.typeName}
137
- operation={operation}
138
- pathParamsType={options.pathParamsType}
139
- typeSchemas={type.schemas}
140
- paramsCasing={options.paramsCasing}
141
- transformer={options.queryKey}
119
+ name={queryKeyName}
120
+ typeName={queryKeyTypeName}
121
+ node={node}
122
+ tsResolver={tsResolver}
123
+ pathParamsType={pathParamsType}
124
+ paramsCasing={paramsCasing}
125
+ transformer={ctx.options.queryKey}
142
126
  />
127
+
143
128
  {!shouldUseClientPlugin && (
144
129
  <Client
145
- name={client.name}
146
- baseURL={options.client.baseURL}
147
- operation={operation}
148
- typeSchemas={type.schemas}
149
- zodSchemas={zod.schemas}
150
- dataReturnType={options.client.dataReturnType || 'data'}
151
- paramsCasing={options.client.paramsCasing || options.paramsCasing}
152
- paramsType={options.paramsType}
153
- pathParamsType={options.pathParamsType}
154
- parser={options.parser}
130
+ name={resolvedClientName}
131
+ baseURL={clientOptions.baseURL}
132
+ dataReturnType={clientOptions.dataReturnType || 'data'}
133
+ paramsCasing={clientOptions.paramsCasing || paramsCasing}
134
+ paramsType={paramsType}
135
+ pathParamsType={pathParamsType}
136
+ parser={parser}
137
+ node={node}
138
+ tsResolver={tsResolver}
139
+ zodResolver={zodResolver}
155
140
  />
156
141
  )}
142
+
157
143
  <QueryOptions
158
- name={queryOptions.name}
159
- clientName={client.name}
160
- typeSchemas={type.schemas}
161
- paramsType={options.paramsType}
162
- paramsCasing={options.paramsCasing}
163
- pathParamsType={options.pathParamsType}
144
+ name={queryOptionsName}
145
+ clientName={resolvedClientName}
146
+ node={node}
147
+ tsResolver={tsResolver}
148
+ paramsCasing={paramsCasing}
149
+ paramsType={paramsType}
150
+ pathParamsType={pathParamsType}
164
151
  />
165
- {options.query && (
152
+
153
+ {query && (
166
154
  <>
167
- <File.Import name="useSWR" path={importPath} />
168
- <File.Import name={['SWRResponse']} path={importPath} isTypeOnly />
155
+ <File.Import name={'useSWR'} path={importPath} />
156
+ <File.Import name={['SWRConfiguration']} path={importPath} isTypeOnly />
169
157
  <Query
170
- name={query.name}
171
- queryOptionsName={queryOptions.name}
172
- typeSchemas={type.schemas}
173
- paramsType={options.paramsType}
174
- pathParamsType={options.pathParamsType}
175
- operation={operation}
176
- dataReturnType={options.client.dataReturnType || 'data'}
177
- queryKeyName={queryKey.name}
178
- paramsCasing={options.paramsCasing}
179
- queryKeyTypeName={queryKey.typeName}
158
+ name={queryName}
159
+ queryOptionsName={queryOptionsName}
160
+ queryKeyName={queryKeyName}
161
+ queryKeyTypeName={queryKeyTypeName}
162
+ node={node}
163
+ tsResolver={tsResolver}
164
+ paramsCasing={paramsCasing}
165
+ paramsType={paramsType}
166
+ pathParamsType={pathParamsType}
167
+ dataReturnType={clientOptions.dataReturnType || 'data'}
180
168
  />
181
169
  </>
182
170
  )}
package/src/index.ts CHANGED
@@ -1,2 +1,2 @@
1
- export { pluginSwr, pluginSwrName } from './plugin.ts'
2
- export type { PluginSwr } from './types.ts'
1
+ export { default, pluginSwr, pluginSwrName } from './plugin.ts'
2
+ export type { PluginSwr, Transformer } from './types.ts'
package/src/plugin.ts CHANGED
@@ -1,207 +1,154 @@
1
1
  import path from 'node:path'
2
- import { camelCase, pascalCase } from '@internals/utils'
3
- import { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'
2
+ import { camelCase } from '@internals/utils'
3
+ import { ast, definePlugin, type Group } from '@kubb/core'
4
4
  import { pluginClientName } from '@kubb/plugin-client'
5
5
  import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
6
6
  import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
7
7
  import { source as configSource } from '@kubb/plugin-client/templates/config.source'
8
- import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
9
8
  import { pluginTsName } from '@kubb/plugin-ts'
10
9
  import { pluginZodName } from '@kubb/plugin-zod'
11
- import { MutationKey, QueryKey } from './components'
10
+ import { mutationKeyTransformer, queryKeyTransformer } from '@internals/tanstack-query'
12
11
  import { mutationGenerator, queryGenerator } from './generators'
12
+ import { resolverSwr } from './resolvers/resolverSwr.ts'
13
13
  import type { PluginSwr } from './types.ts'
14
14
 
15
15
  export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
16
16
 
17
- export const pluginSwr = createPlugin<PluginSwr>((options) => {
17
+ export const pluginSwr = definePlugin<PluginSwr>((options) => {
18
18
  const {
19
19
  output = { path: 'hooks', barrelType: 'named' },
20
20
  group,
21
21
  exclude = [],
22
22
  include,
23
23
  override = [],
24
- parser = 'client',
25
- transformers = {},
26
- query,
27
- mutation,
28
- client,
24
+ parser = false,
29
25
  paramsType = 'inline',
30
26
  pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
31
- mutationKey = MutationKey.getTransformer,
32
- queryKey = QueryKey.getTransformer,
33
- generators = [queryGenerator, mutationGenerator].filter(Boolean),
27
+ mutation = {},
28
+ query = {},
29
+ mutationKey = mutationKeyTransformer,
30
+ queryKey = queryKeyTransformer,
34
31
  paramsCasing,
35
- contentType,
32
+ client,
33
+ resolver: userResolver,
34
+ transformer: userTransformer,
35
+ generators: userGenerators = [],
36
36
  } = options
37
37
 
38
38
  const clientName = client?.client ?? 'axios'
39
39
  const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
40
40
 
41
- return {
42
- name: pluginSwrName,
43
- options: {
44
- output,
45
- client: {
46
- bundle: client?.bundle,
47
- baseURL: client?.baseURL,
48
- client: clientName,
49
- clientType: client?.clientType ?? 'function',
50
- importPath: clientImportPath,
51
- dataReturnType: client?.dataReturnType ?? 'data',
52
- paramsCasing,
53
- },
54
- queryKey,
55
- query:
56
- query === false
57
- ? false
58
- : {
59
- importPath: 'swr',
60
- methods: ['get'],
61
- ...query,
62
- },
63
- mutationKey,
64
- mutation:
65
- mutation === false
66
- ? false
67
- : {
68
- importPath: 'swr/mutation',
69
- methods: ['post', 'put', 'delete', 'patch'],
70
- ...mutation,
71
- },
72
- parser,
73
- paramsType,
74
- pathParamsType,
75
- paramsCasing,
76
- group,
77
- },
78
- pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
79
- resolvePath(baseName, pathMode, options) {
80
- const root = path.resolve(this.config.root, this.config.output.path)
81
- const mode = pathMode ?? getMode(path.resolve(root, output.path))
82
-
83
- if (mode === 'single') {
84
- /**
85
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
86
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
87
- */
88
- return path.resolve(root, output.path)
89
- }
41
+ const selectedGenerators =
42
+ options.generators ?? [queryGenerator, mutationGenerator].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))
90
43
 
91
- if (group && (options?.group?.path || options?.group?.tag)) {
92
- const groupName: Group['name'] = group?.name
44
+ const groupConfig = group
45
+ ? ({
46
+ ...group,
47
+ name: group.name
93
48
  ? group.name
94
- : (ctx) => {
95
- if (group?.type === 'path') {
49
+ : (ctx: { group: string }) => {
50
+ if (group.type === 'path') {
96
51
  return `${ctx.group.split('/')[1]}`
97
52
  }
98
53
  return `${camelCase(ctx.group)}Controller`
99
- }
100
-
101
- return path.resolve(
102
- root,
103
- output.path,
104
- groupName({
105
- group: group.type === 'path' ? options.group.path! : options.group.tag!,
106
- }),
107
- baseName,
108
- )
109
- }
110
-
111
- return path.resolve(root, output.path, baseName)
112
- },
113
- resolveName(name, type) {
114
- let resolvedName = camelCase(name)
115
-
116
- if (type === 'file' || type === 'function') {
117
- resolvedName = camelCase(name, {
118
- isFile: type === 'file',
119
- })
120
- }
121
-
122
- if (type === 'type') {
123
- resolvedName = pascalCase(name)
124
- }
125
-
126
- if (type) {
127
- return transformers?.name?.(resolvedName, type) || resolvedName
128
- }
129
-
130
- return resolvedName
131
- },
132
- async install() {
133
- const root = path.resolve(this.config.root, this.config.output.path)
134
- const mode = getMode(path.resolve(root, output.path))
135
- const oas = await this.getOas()
136
- const baseURL = await this.getBaseURL()
137
-
138
- if (baseURL) {
139
- this.plugin.options.client.baseURL = baseURL
140
- }
141
-
142
- const hasClientPlugin = !!this.driver.getPluginByName(pluginClientName)
143
-
144
- if (this.plugin.options.client.bundle && !hasClientPlugin && !this.plugin.options.client.importPath) {
145
- // pre add bundled fetch
146
- await this.upsertFile({
147
- baseName: 'fetch.ts',
148
- path: path.resolve(root, '.kubb/fetch.ts'),
149
- sources: [
150
- {
151
- name: 'fetch',
152
- value: this.plugin.options.client.client === 'fetch' ? fetchClientSource : axiosClientSource,
153
- isExportable: true,
154
- isIndexable: true,
155
54
  },
156
- ],
157
- imports: [],
158
- exports: [],
159
- })
160
- }
55
+ } satisfies Group)
56
+ : undefined
161
57
 
162
- if (!hasClientPlugin) {
163
- await this.addFile({
164
- baseName: 'config.ts',
165
- path: path.resolve(root, '.kubb/config.ts'),
166
- sources: [
167
- {
168
- name: 'config',
169
- value: configSource,
170
- isExportable: false,
171
- isIndexable: false,
172
- },
173
- ],
174
- imports: [],
175
- exports: [],
58
+ return {
59
+ name: pluginSwrName,
60
+ options,
61
+ dependencies: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),
62
+ hooks: {
63
+ 'kubb:plugin:setup'(ctx) {
64
+ const resolver = userResolver ? { ...resolverSwr, ...userResolver } : resolverSwr
65
+
66
+ ctx.setOptions({
67
+ output,
68
+ client: {
69
+ bundle: client?.bundle,
70
+ baseURL: client?.baseURL,
71
+ client: clientName,
72
+ clientType: client?.clientType ?? 'function',
73
+ importPath: clientImportPath,
74
+ dataReturnType: client?.dataReturnType ?? 'data',
75
+ paramsCasing,
76
+ },
77
+ queryKey,
78
+ query:
79
+ query === false
80
+ ? false
81
+ : {
82
+ importPath: 'swr',
83
+ methods: ['get'],
84
+ ...query,
85
+ },
86
+ mutationKey,
87
+ mutation:
88
+ mutation === false
89
+ ? false
90
+ : {
91
+ importPath: 'swr/mutation',
92
+ methods: ['post', 'put', 'patch', 'delete'],
93
+ ...mutation,
94
+ },
95
+ parser,
96
+ paramsType,
97
+ pathParamsType,
98
+ paramsCasing,
99
+ group: groupConfig,
100
+ exclude,
101
+ include,
102
+ override,
103
+ resolver,
176
104
  })
177
- }
178
-
179
- const operationGenerator = new OperationGenerator(this.plugin.options, {
180
- fabric: this.fabric,
181
- oas,
182
- driver: this.driver,
183
- events: this.events,
184
- plugin: this.plugin,
185
- contentType,
186
- exclude,
187
- include,
188
- override,
189
- mode,
190
- })
191
-
192
- const files = await operationGenerator.build(...generators)
193
- await this.upsertFile(...files)
194
-
195
- const barrelFiles = await getBarrelFiles(this.fabric.files, {
196
- type: output.barrelType ?? 'named',
197
- root,
198
- output,
199
- meta: {
200
- pluginName: this.plugin.name,
201
- },
202
- })
203
-
204
- await this.upsertFile(...barrelFiles)
105
+ ctx.setResolver(resolver)
106
+ if (userTransformer) {
107
+ ctx.setTransformer(userTransformer)
108
+ }
109
+
110
+ for (const gen of selectedGenerators) {
111
+ ctx.addGenerator(gen)
112
+ }
113
+ for (const gen of userGenerators) {
114
+ ctx.addGenerator(gen)
115
+ }
116
+
117
+ const root = path.resolve(ctx.config.root, ctx.config.output.path)
118
+ const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)
119
+
120
+ if (client?.bundle && !hasClientPlugin && !clientImportPath) {
121
+ ctx.injectFile({
122
+ baseName: 'client.ts',
123
+ path: path.resolve(root, '.kubb/client.ts'),
124
+ sources: [
125
+ ast.createSource({
126
+ name: 'client',
127
+ nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],
128
+ isExportable: true,
129
+ isIndexable: true,
130
+ }),
131
+ ],
132
+ })
133
+ }
134
+
135
+ if (!hasClientPlugin) {
136
+ ctx.injectFile({
137
+ baseName: 'config.ts',
138
+ path: path.resolve(root, '.kubb/config.ts'),
139
+ sources: [
140
+ ast.createSource({
141
+ name: 'config',
142
+ nodes: [ast.createText(configSource)],
143
+ isExportable: false,
144
+ isIndexable: false,
145
+ }),
146
+ ],
147
+ })
148
+ }
149
+ },
205
150
  },
206
151
  }
207
152
  })
153
+
154
+ export default pluginSwr