@kubb/plugin-swr 5.0.0-beta.56 → 5.0.0-beta.73

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.
@@ -1,166 +0,0 @@
1
- import path from 'node:path'
2
- import { resolveOperationTypeNames } from '@internals/shared'
3
- import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
- import { ast, defineGenerator } from '@kubb/core'
5
- import { Client, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
9
- import { Mutation, MutationKey } from '../components'
10
- import type { PluginSwr } from '../types'
11
-
12
- /**
13
- * Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
14
- * per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
15
- * the matching `fooMutationKey` helper.
16
- */
17
- export const mutationGenerator = defineGenerator<PluginSwr>({
18
- name: 'swr-mutation',
19
- renderer: jsxRenderer,
20
- operation(node, ctx) {
21
- if (!ast.isHttpOperationNode(node)) return null
22
- const { config, driver, resolver, root } = ctx
23
- const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group } = ctx.options
24
-
25
- const pluginTs = driver.getPlugin(pluginTsName)
26
- if (!pluginTs) return null
27
- const tsResolver = driver.getResolver(pluginTsName)
28
-
29
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
30
- const queryMethods = new Set(query ? query.methods : [])
31
- const isMutation =
32
- mutation !== false &&
33
- !isQuery &&
34
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
35
-
36
- if (!isMutation) return null
37
-
38
- const importPath = mutation ? mutation.importPath : 'swr/mutation'
39
-
40
- const mutationHookName = resolver.resolveMutationName(node)
41
- const mutationKeyName = resolver.resolveMutationKeyName(node)
42
- const mutationKeyTypeName = resolver.resolveMutationKeyTypeName(node)
43
- const mutationArgTypeName = resolver.resolveMutationArgTypeName(node)
44
- const clientName = resolver.resolveClientName(node)
45
-
46
- const meta = {
47
- file: resolver.resolveFile({ name: mutationHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group }),
48
- fileTs: tsResolver.resolveFile(
49
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
50
- { root, output: pluginTs.options?.output ?? output, group: pluginTs.options?.group },
51
- ),
52
- }
53
-
54
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, order: 'body-response-first' })
55
-
56
- const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : undefined
57
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
58
- const fileZod = zodResolver
59
- ? zodResolver.resolveFile(
60
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
61
- { root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
62
- )
63
- : undefined
64
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
65
-
66
- const clientPlugin = driver.getPlugin(pluginClientName)
67
- const hasClientPlugin = clientPlugin?.name === pluginClientName
68
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
69
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : undefined
70
-
71
- const clientFile = shouldUseClientPlugin
72
- ? clientResolver?.resolveFile(
73
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
74
- {
75
- root,
76
- output: clientPlugin?.options?.output ?? output,
77
- group: clientPlugin?.options?.group,
78
- },
79
- )
80
- : undefined
81
-
82
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
83
-
84
- return (
85
- <File
86
- baseName={meta.file.baseName}
87
- path={meta.file.path}
88
- meta={meta.file.meta}
89
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
90
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
91
- >
92
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
93
- {clientOptions.importPath ? (
94
- <>
95
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
96
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
97
- </>
98
- ) : (
99
- <>
100
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
101
- <File.Import
102
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
103
- root={meta.file.path}
104
- path={path.resolve(root, '.kubb/client.ts')}
105
- isTypeOnly
106
- />
107
- </>
108
- )}
109
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
110
- {!shouldUseClientPlugin && node.requestBody?.content?.some((e) => e.contentType === 'multipart/form-data') && (
111
- <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />
112
- )}
113
- {!shouldUseClientPlugin && parser === 'zod' && zodResolver && node.requestBody?.content?.[0]?.schema && (
114
- <File.Import name={['z']} path="zod" isTypeOnly />
115
- )}
116
- {meta.fileTs && importedTypeNames.length > 0 && (
117
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
118
- )}
119
-
120
- <MutationKey
121
- name={mutationKeyName}
122
- typeName={mutationKeyTypeName}
123
- node={node}
124
- pathParamsType={pathParamsType}
125
- paramsCasing={paramsCasing}
126
- transformer={ctx.options.mutationKey}
127
- />
128
-
129
- {!shouldUseClientPlugin && (
130
- <Client
131
- name={resolvedClientName}
132
- baseURL={clientOptions.baseURL}
133
- dataReturnType={clientOptions.dataReturnType || 'data'}
134
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
135
- paramsType={paramsType}
136
- pathParamsType={pathParamsType}
137
- parser={parser}
138
- node={node}
139
- tsResolver={tsResolver}
140
- zodResolver={zodResolver}
141
- />
142
- )}
143
-
144
- {mutation && (
145
- <>
146
- <File.Import name={'useSWRMutation'} path={importPath} />
147
- <File.Import name={['SWRMutationConfiguration']} path={importPath} isTypeOnly />
148
- <Mutation
149
- name={mutationHookName}
150
- clientName={resolvedClientName}
151
- mutationKeyName={mutationKeyName}
152
- mutationKeyTypeName={mutationKeyTypeName}
153
- mutationArgTypeName={mutationArgTypeName}
154
- node={node}
155
- tsResolver={tsResolver}
156
- dataReturnType={clientOptions.dataReturnType || 'data'}
157
- paramsCasing={paramsCasing}
158
- paramsType={paramsType}
159
- pathParamsType={pathParamsType}
160
- />
161
- </>
162
- )}
163
- </File>
164
- )
165
- },
166
- })
@@ -1,170 +0,0 @@
1
- import path from 'node:path'
2
- import { resolveOperationTypeNames } from '@internals/shared'
3
- import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
- import { ast, defineGenerator } from '@kubb/core'
5
- import { Client, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
9
- import { Query, QueryKey, QueryOptions } from '../components'
10
- import type { PluginSwr } from '../types'
11
-
12
- export const queryGenerator = defineGenerator<PluginSwr>({
13
- name: 'swr-query',
14
- renderer: jsxRenderer,
15
- operation(node, ctx) {
16
- if (!ast.isHttpOperationNode(node)) return null
17
- const { config, driver, resolver, root } = ctx
18
- const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group } = ctx.options
19
-
20
- const pluginTs = driver.getPlugin(pluginTsName)
21
- if (!pluginTs) return null
22
- const tsResolver = driver.getResolver(pluginTsName)
23
-
24
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
25
- const queryMethods = new Set(query ? query.methods : [])
26
- const isMutation =
27
- mutation !== false &&
28
- !isQuery &&
29
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
30
-
31
- if (!isQuery || isMutation) return null
32
-
33
- const importPath = query ? query.importPath : 'swr'
34
-
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)
40
-
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
- ),
47
- }
48
-
49
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, {
50
- paramsCasing,
51
- exclude: [queryKeyTypeName],
52
- order: 'body-response-first',
53
- })
54
-
55
- const pluginZod = isParserEnabled(parser) ? 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, parser)
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
- return (
84
- <File
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
- >
91
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
92
- {clientOptions.importPath ? (
93
- <>
94
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
95
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
96
- </>
97
- ) : (
98
- <>
99
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
100
- <File.Import
101
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
102
- root={meta.file.path}
103
- path={path.resolve(root, '.kubb/client.ts')}
104
- isTypeOnly
105
- />
106
- </>
107
- )}
108
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
109
- {!shouldUseClientPlugin && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
110
- {meta.fileTs && importedTypeNames.length > 0 && (
111
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
112
- )}
113
-
114
- <QueryKey
115
- name={queryKeyName}
116
- typeName={queryKeyTypeName}
117
- node={node}
118
- tsResolver={tsResolver}
119
- pathParamsType={pathParamsType}
120
- paramsCasing={paramsCasing}
121
- transformer={ctx.options.queryKey}
122
- />
123
-
124
- {!shouldUseClientPlugin && (
125
- <Client
126
- name={resolvedClientName}
127
- baseURL={clientOptions.baseURL}
128
- dataReturnType={clientOptions.dataReturnType || 'data'}
129
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
130
- paramsType={paramsType}
131
- pathParamsType={pathParamsType}
132
- parser={parser}
133
- node={node}
134
- tsResolver={tsResolver}
135
- zodResolver={zodResolver}
136
- />
137
- )}
138
-
139
- <QueryOptions
140
- name={queryOptionsName}
141
- clientName={resolvedClientName}
142
- node={node}
143
- tsResolver={tsResolver}
144
- paramsCasing={paramsCasing}
145
- paramsType={paramsType}
146
- pathParamsType={pathParamsType}
147
- />
148
-
149
- {query && (
150
- <>
151
- <File.Import name={'useSWR'} path={importPath} />
152
- <File.Import name={['SWRConfiguration']} path={importPath} isTypeOnly />
153
- <Query
154
- name={queryName}
155
- queryOptionsName={queryOptionsName}
156
- queryKeyName={queryKeyName}
157
- queryKeyTypeName={queryKeyTypeName}
158
- node={node}
159
- tsResolver={tsResolver}
160
- paramsCasing={paramsCasing}
161
- paramsType={paramsType}
162
- pathParamsType={pathParamsType}
163
- dataReturnType={clientOptions.dataReturnType || 'data'}
164
- />
165
- </>
166
- )}
167
- </File>
168
- )
169
- },
170
- })
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { default, pluginSwr, pluginSwrName } from './plugin.ts'
2
- export type { PluginSwr, Transformer } from './types.ts'
package/src/plugin.ts DELETED
@@ -1,142 +0,0 @@
1
- import path from 'node:path'
2
- import { createGroupConfig } from '@internals/shared'
3
- import { ast, definePlugin } from '@kubb/core'
4
- import { isParserEnabled, pluginClientName } from '@kubb/plugin-client'
5
- import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
6
- import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
7
- import { source as configSource } from '@kubb/plugin-client/templates/config.source'
8
- import { pluginTsName } from '@kubb/plugin-ts'
9
- import { pluginZodName } from '@kubb/plugin-zod'
10
- import { mutationKeyTransformer, queryKeyTransformer } from '@internals/tanstack-query'
11
- import { mutationGenerator, queryGenerator } from './generators'
12
- import { resolverSwr } from './resolvers/resolverSwr.ts'
13
- import type { PluginSwr } from './types.ts'
14
-
15
- export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
16
-
17
- export const pluginSwr = definePlugin<PluginSwr>((options) => {
18
- const {
19
- output = { path: 'hooks', barrel: { type: 'named' } },
20
- group,
21
- exclude = [],
22
- include,
23
- override = [],
24
- parser = false,
25
- paramsType = 'inline',
26
- pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
27
- mutation = {},
28
- query = {},
29
- mutationKey = mutationKeyTransformer,
30
- queryKey = queryKeyTransformer,
31
- paramsCasing,
32
- client,
33
- resolver: userResolver,
34
- transformer: userTransformer,
35
- generators: userGenerators = [],
36
- } = options
37
-
38
- const clientName = client?.client ?? 'axios'
39
- const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
40
-
41
- const selectedGenerators =
42
- options.generators ?? [queryGenerator, mutationGenerator].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))
43
-
44
- const groupConfig = createGroupConfig(group) ?? undefined
45
-
46
- return {
47
- name: pluginSwrName,
48
- options,
49
- dependencies: [pluginTsName, isParserEnabled(parser) ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),
50
- hooks: {
51
- 'kubb:plugin:setup'(ctx) {
52
- const resolver = userResolver ? { ...resolverSwr, ...userResolver } : resolverSwr
53
-
54
- ctx.setOptions({
55
- output,
56
- client: {
57
- bundle: client?.bundle,
58
- baseURL: client?.baseURL,
59
- client: clientName,
60
- clientType: client?.clientType ?? 'function',
61
- importPath: clientImportPath,
62
- dataReturnType: client?.dataReturnType ?? 'data',
63
- paramsCasing,
64
- },
65
- queryKey,
66
- query:
67
- query === false
68
- ? false
69
- : {
70
- importPath: 'swr',
71
- methods: ['get'],
72
- ...query,
73
- },
74
- mutationKey,
75
- mutation:
76
- mutation === false
77
- ? false
78
- : {
79
- importPath: 'swr/mutation',
80
- methods: ['post', 'put', 'patch', 'delete'],
81
- ...mutation,
82
- },
83
- parser,
84
- paramsType,
85
- pathParamsType,
86
- paramsCasing,
87
- group: groupConfig,
88
- exclude,
89
- include,
90
- override,
91
- resolver,
92
- })
93
- ctx.setResolver(resolver)
94
- if (userTransformer) {
95
- ctx.setTransformer(userTransformer)
96
- }
97
-
98
- for (const gen of selectedGenerators) {
99
- ctx.addGenerator(gen)
100
- }
101
- for (const gen of userGenerators) {
102
- ctx.addGenerator(gen)
103
- }
104
-
105
- const root = path.resolve(ctx.config.root, ctx.config.output.path)
106
- const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)
107
-
108
- if (client?.bundle && !hasClientPlugin && !clientImportPath) {
109
- ctx.injectFile({
110
- baseName: 'client.ts',
111
- path: path.resolve(root, '.kubb/client.ts'),
112
- sources: [
113
- ast.createSource({
114
- name: 'client',
115
- nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],
116
- isExportable: true,
117
- isIndexable: true,
118
- }),
119
- ],
120
- })
121
- }
122
-
123
- if (!hasClientPlugin) {
124
- ctx.injectFile({
125
- baseName: 'config.ts',
126
- path: path.resolve(root, '.kubb/config.ts'),
127
- sources: [
128
- ast.createSource({
129
- name: 'config',
130
- nodes: [ast.createText(configSource)],
131
- isExportable: false,
132
- isIndexable: false,
133
- }),
134
- ],
135
- })
136
- }
137
- },
138
- },
139
- }
140
- })
141
-
142
- export default pluginSwr
@@ -1,56 +0,0 @@
1
- import { camelCase, toFilePath } from '@internals/utils'
2
- import { defineResolver } from '@kubb/core'
3
- import type { PluginSwr } from '../types.ts'
4
-
5
- function capitalize(name: string): string {
6
- return `${name.charAt(0).toUpperCase()}${name.slice(1)}`
7
- }
8
-
9
- /**
10
- * Naming convention resolver for the SWR plugin.
11
- *
12
- * Provides default naming helpers using camelCase for functions and file paths.
13
- *
14
- * @example
15
- * `resolverSwr.default('list pets', 'function') // → 'listPets'`
16
- */
17
- export const resolverSwr = defineResolver<PluginSwr>(() => ({
18
- name: 'default',
19
- pluginName: 'plugin-swr',
20
- default(name, type) {
21
- return type === 'file' ? toFilePath(name) : camelCase(name)
22
- },
23
- resolveName(name) {
24
- return this.default(name, 'function')
25
- },
26
- resolvePathName(name, type) {
27
- return this.default(name, type)
28
- },
29
- resolveQueryName(node) {
30
- return `use${capitalize(this.resolveName(node.operationId))}`
31
- },
32
- resolveMutationName(node) {
33
- return `use${capitalize(this.resolveName(node.operationId))}`
34
- },
35
- resolveQueryOptionsName(node) {
36
- return `${this.resolveName(node.operationId)}QueryOptions`
37
- },
38
- resolveQueryKeyName(node) {
39
- return `${this.resolveName(node.operationId)}QueryKey`
40
- },
41
- resolveMutationKeyName(node) {
42
- return `${this.resolveName(node.operationId)}MutationKey`
43
- },
44
- resolveQueryKeyTypeName(node) {
45
- return `${capitalize(this.resolveName(node.operationId))}QueryKey`
46
- },
47
- resolveMutationKeyTypeName(node) {
48
- return `${capitalize(this.resolveName(node.operationId))}MutationKey`
49
- },
50
- resolveMutationArgTypeName(node) {
51
- return `${capitalize(this.resolveName(node.operationId))}MutationArg`
52
- },
53
- resolveClientName(node) {
54
- return this.resolveName(node.operationId)
55
- },
56
- }))