@kubb/plugin-swr 5.0.0-alpha.8 → 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.
- package/LICENSE +17 -10
- package/README.md +33 -26
- package/dist/components-ByADeLZO.cjs +1029 -0
- package/dist/components-ByADeLZO.cjs.map +1 -0
- package/dist/components-CBdpiiay.js +933 -0
- package/dist/components-CBdpiiay.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +45 -51
- package/dist/components.js +1 -1
- package/dist/generators-Bb5wNYig.cjs +445 -0
- package/dist/generators-Bb5wNYig.cjs.map +1 -0
- package/dist/generators-D5kJZsB1.js +434 -0
- package/dist/generators-D5kJZsB1.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +4 -471
- package/dist/generators.js +1 -1
- package/dist/index.cjs +134 -113
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +130 -113
- package/dist/index.js.map +1 -1
- package/dist/types-D9jeG3fE.d.ts +220 -0
- package/extension.yaml +199 -0
- package/package.json +57 -67
- package/src/components/Mutation.tsx +106 -227
- package/src/components/MutationKey.tsx +25 -1
- package/src/components/Query.tsx +75 -140
- package/src/components/QueryOptions.tsx +46 -95
- package/src/generators/mutationGenerator.tsx +113 -128
- package/src/generators/queryGenerator.tsx +125 -137
- package/src/index.ts +2 -2
- package/src/plugin.ts +117 -170
- package/src/resolvers/resolverSwr.ts +56 -0
- package/src/types.ts +115 -59
- package/src/utils.ts +10 -0
- package/dist/components-DRDGvgXG.js +0 -702
- package/dist/components-DRDGvgXG.js.map +0 -1
- package/dist/components-jd0l9XKn.cjs +0 -780
- package/dist/components-jd0l9XKn.cjs.map +0 -1
- package/dist/generators-CRSl6u2M.js +0 -399
- package/dist/generators-CRSl6u2M.js.map +0 -1
- package/dist/generators-D062obA7.cjs +0 -410
- package/dist/generators-D062obA7.cjs.map +0 -1
- package/dist/types-BIaGRPjD.d.ts +0 -210
|
@@ -1,182 +1,170 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
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/
|
|
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 =
|
|
13
|
+
export const queryGenerator = defineGenerator<PluginSwr>({
|
|
16
14
|
name: 'swr-query',
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
} =
|
|
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
|
|
34
|
-
|
|
35
|
-
|
|
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
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
80
|
-
|
|
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={
|
|
86
|
-
path={
|
|
87
|
-
meta={
|
|
88
|
-
banner={
|
|
89
|
-
footer={
|
|
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
|
-
{
|
|
92
|
-
|
|
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={'
|
|
97
|
-
<File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={
|
|
98
|
-
{
|
|
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={
|
|
108
|
-
path={path.resolve(
|
|
103
|
+
root={meta.file.path}
|
|
104
|
+
path={path.resolve(root, '.kubb/client.ts')}
|
|
109
105
|
isTypeOnly
|
|
110
106
|
/>
|
|
111
|
-
{
|
|
112
|
-
<File.Import name={['ResponseConfig']} root={
|
|
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={[
|
|
117
|
-
{!shouldUseClientPlugin && (
|
|
118
|
-
|
|
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={
|
|
136
|
-
typeName={
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
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={
|
|
146
|
-
baseURL={
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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={
|
|
159
|
-
clientName={
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
paramsCasing={
|
|
163
|
-
|
|
144
|
+
name={queryOptionsName}
|
|
145
|
+
clientName={resolvedClientName}
|
|
146
|
+
node={node}
|
|
147
|
+
tsResolver={tsResolver}
|
|
148
|
+
paramsCasing={paramsCasing}
|
|
149
|
+
paramsType={paramsType}
|
|
150
|
+
pathParamsType={pathParamsType}
|
|
164
151
|
/>
|
|
165
|
-
|
|
152
|
+
|
|
153
|
+
{query && (
|
|
166
154
|
<>
|
|
167
|
-
<File.Import name=
|
|
168
|
-
<File.Import name={['
|
|
155
|
+
<File.Import name={'useSWR'} path={importPath} />
|
|
156
|
+
<File.Import name={['SWRConfiguration']} path={importPath} isTypeOnly />
|
|
169
157
|
<Query
|
|
170
|
-
name={
|
|
171
|
-
queryOptionsName={
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
|
3
|
-
import {
|
|
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 {
|
|
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 =
|
|
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 =
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
27
|
+
mutation = {},
|
|
28
|
+
query = {},
|
|
29
|
+
mutationKey = mutationKeyTransformer,
|
|
30
|
+
queryKey = queryKeyTransformer,
|
|
34
31
|
paramsCasing,
|
|
35
|
-
|
|
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
|
-
|
|
42
|
-
|
|
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
|
-
|
|
92
|
-
|
|
44
|
+
const groupConfig = group
|
|
45
|
+
? ({
|
|
46
|
+
...group,
|
|
47
|
+
name: group.name
|
|
93
48
|
? group.name
|
|
94
|
-
: (ctx) => {
|
|
95
|
-
if (group
|
|
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
|
-
|
|
158
|
-
exports: [],
|
|
159
|
-
})
|
|
160
|
-
}
|
|
55
|
+
} satisfies Group)
|
|
56
|
+
: undefined
|
|
161
57
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
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
|