@kubb/plugin-swr 5.0.0-alpha.8 → 5.0.0-beta.100

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,177 +0,0 @@
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'
8
- import { pluginTsName } from '@kubb/plugin-ts'
9
- import { pluginZodName } from '@kubb/plugin-zod'
10
- import { File } from '@kubb/react-fabric'
11
- import { difference } from 'remeda'
12
- import { Mutation, MutationKey } from '../components'
13
- import type { PluginSwr } from '../types'
14
-
15
- export const mutationGenerator = createReactGenerator<PluginSwr>({
16
- name: 'swr-mutation',
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 = !!options.query && options.query?.methods.some((method) => operation.method === method)
28
- const isMutation =
29
- options.mutation !== false &&
30
- !isQuery &&
31
- difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method)
32
-
33
- const importPath = options.mutation ? options.mutation.importPath : 'swr'
34
-
35
- const mutation = {
36
- name: getName(operation, { type: 'function', prefix: 'use' }),
37
- typeName: getName(operation, { type: 'type' }),
38
- file: getFile(operation, { prefix: 'use' }),
39
- }
40
-
41
- const type = {
42
- file: getFile(operation, { pluginName: pluginTsName }),
43
- //todo remove type?
44
- schemas: getSchemas(operation, { pluginName: pluginTsName, type: 'type' }),
45
- }
46
-
47
- const zod = {
48
- file: getFile(operation, { pluginName: pluginZodName }),
49
- schemas: getSchemas(operation, { pluginName: pluginZodName, type: 'function' }),
50
- }
51
-
52
- const hasClientPlugin = !!driver.getPluginByName(pluginClientName)
53
- // Class-based clients are not compatible with query hooks, so we generate inline clients
54
- const shouldUseClientPlugin = hasClientPlugin && options.client.clientType !== 'class'
55
- const client = {
56
- name: shouldUseClientPlugin
57
- ? getName(operation, {
58
- type: 'function',
59
- pluginName: pluginClientName,
60
- })
61
- : getName(operation, {
62
- type: 'function',
63
- }),
64
- file: getFile(operation, { pluginName: pluginClientName }),
65
- }
66
-
67
- const mutationKey = {
68
- name: getName(operation, { type: 'const', suffix: 'MutationKey' }),
69
- typeName: getName(operation, { type: 'type', suffix: 'MutationKey' }),
70
- }
71
-
72
- if (!isMutation) {
73
- return null
74
- }
75
-
76
- return (
77
- <File
78
- baseName={mutation.file.baseName}
79
- path={mutation.file.path}
80
- meta={mutation.file.meta}
81
- banner={getBanner({ oas, output, config: driver.config })}
82
- footer={getFooter({ oas, output })}
83
- >
84
- {options.parser === 'zod' && (
85
- <File.Import name={[zod.schemas.response.name, zod.schemas.request?.name].filter(Boolean)} root={mutation.file.path} path={zod.file.path} />
86
- )}
87
- {options.client.importPath ? (
88
- <>
89
- {!shouldUseClientPlugin && <File.Import name={'fetch'} path={options.client.importPath} />}
90
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={options.client.importPath} isTypeOnly />
91
- {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
92
- </>
93
- ) : (
94
- <>
95
- {!shouldUseClientPlugin && (
96
- <File.Import name={['fetch']} root={mutation.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} />
97
- )}
98
- <File.Import
99
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
100
- root={mutation.file.path}
101
- path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
102
- isTypeOnly
103
- />
104
- {options.client.dataReturnType === 'full' && (
105
- <File.Import
106
- name={['ResponseConfig']}
107
- root={mutation.file.path}
108
- path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
109
- isTypeOnly
110
- />
111
- )}
112
- </>
113
- )}
114
- <File.Import name="useSWRMutation" path={importPath} />
115
- <File.Import name={['SWRMutationConfiguration', 'SWRMutationResponse']} path={importPath} isTypeOnly />
116
- {shouldUseClientPlugin && <File.Import name={[client.name]} root={mutation.file.path} path={client.file.path} />}
117
- {!shouldUseClientPlugin && (
118
- <File.Import name={['buildFormData']} root={mutation.file.path} path={path.resolve(config.root, config.output.path, '.kubb/config.ts')} />
119
- )}
120
- <File.Import
121
- name={[
122
- type.schemas.request?.name,
123
- type.schemas.response.name,
124
- type.schemas.pathParams?.name,
125
- type.schemas.queryParams?.name,
126
- type.schemas.headerParams?.name,
127
- ...(type.schemas.statusCodes?.map((item) => item.name) || []),
128
- ].filter(Boolean)}
129
- root={mutation.file.path}
130
- path={type.file.path}
131
- isTypeOnly
132
- />
133
-
134
- <MutationKey
135
- name={mutationKey.name}
136
- typeName={mutationKey.typeName}
137
- operation={operation}
138
- pathParamsType={options.pathParamsType}
139
- typeSchemas={type.schemas}
140
- paramsCasing={options.paramsCasing}
141
- transformer={options.mutationKey}
142
- />
143
-
144
- {!shouldUseClientPlugin && (
145
- <Client
146
- name={client.name}
147
- baseURL={options.client.baseURL}
148
- operation={operation}
149
- typeSchemas={type.schemas}
150
- zodSchemas={zod.schemas}
151
- dataReturnType={options.client.dataReturnType || 'data'}
152
- paramsCasing={options.client?.paramsCasing || options.paramsCasing}
153
- paramsType={options.paramsType}
154
- pathParamsType={options.pathParamsType}
155
- parser={options.parser}
156
- />
157
- )}
158
- {options.mutation && (
159
- <Mutation
160
- name={mutation.name}
161
- clientName={client.name}
162
- typeName={mutation.typeName}
163
- typeSchemas={type.schemas}
164
- operation={operation}
165
- dataReturnType={options.client.dataReturnType || 'data'}
166
- paramsType={options.paramsType}
167
- paramsCasing={options.paramsCasing}
168
- pathParamsType={options.pathParamsType}
169
- mutationKeyName={mutationKey.name}
170
- mutationKeyTypeName={mutationKey.typeName}
171
- paramsToTrigger={options.mutation.paramsToTrigger}
172
- />
173
- )}
174
- </File>
175
- )
176
- },
177
- })
@@ -1,186 +0,0 @@
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'
8
- import { pluginTsName } from '@kubb/plugin-ts'
9
- import { pluginZodName } from '@kubb/plugin-zod'
10
- import { File } from '@kubb/react-fabric'
11
- import { difference } from 'remeda'
12
- import { Query, QueryKey, QueryOptions } from '../components'
13
- import type { PluginSwr } from '../types'
14
-
15
- export const queryGenerator = createReactGenerator<PluginSwr>({
16
- 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'
32
-
33
- const query = {
34
- name: getName(operation, { type: 'function', prefix: 'use' }),
35
- typeName: getName(operation, { type: 'type' }),
36
- file: getFile(operation, { prefix: 'use' }),
37
- }
38
-
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
- }
53
-
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
- }
61
-
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
- }
70
-
71
- const zod = {
72
- file: getFile(operation, { pluginName: pluginZodName }),
73
- schemas: getSchemas(operation, {
74
- pluginName: pluginZodName,
75
- type: 'function',
76
- }),
77
- }
78
-
79
- if (!isQuery || isMutation) {
80
- return null
81
- }
82
-
83
- return (
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 })}
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 ? (
95
- <>
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 />}
99
- </>
100
- ) : (
101
- <>
102
- {!shouldUseClientPlugin && (
103
- <File.Import name={['fetch']} root={query.file.path} path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')} />
104
- )}
105
- <File.Import
106
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
107
- root={query.file.path}
108
- path={path.resolve(config.root, config.output.path, '.kubb/fetch.ts')}
109
- isTypeOnly
110
- />
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 />
113
- )}
114
- </>
115
- )}
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')} />
119
- )}
120
-
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
- <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}
142
- />
143
- {!shouldUseClientPlugin && (
144
- <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}
155
- />
156
- )}
157
- <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}
164
- />
165
- {options.query && (
166
- <>
167
- <File.Import name="useSWR" path={importPath} />
168
- <File.Import name={['SWRResponse']} path={importPath} isTypeOnly />
169
- <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}
180
- />
181
- </>
182
- )}
183
- </File>
184
- )
185
- },
186
- })
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { pluginSwr, pluginSwrName } from './plugin.ts'
2
- export type { PluginSwr } from './types.ts'
package/src/plugin.ts DELETED
@@ -1,207 +0,0 @@
1
- import path from 'node:path'
2
- import { camelCase, pascalCase } from '@internals/utils'
3
- import { createPlugin, type Group, getBarrelFiles, getMode } from '@kubb/core'
4
- import { 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 { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
9
- import { pluginTsName } from '@kubb/plugin-ts'
10
- import { pluginZodName } from '@kubb/plugin-zod'
11
- import { MutationKey, QueryKey } from './components'
12
- import { mutationGenerator, queryGenerator } from './generators'
13
- import type { PluginSwr } from './types.ts'
14
-
15
- export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
16
-
17
- export const pluginSwr = createPlugin<PluginSwr>((options) => {
18
- const {
19
- output = { path: 'hooks', barrelType: 'named' },
20
- group,
21
- exclude = [],
22
- include,
23
- override = [],
24
- parser = 'client',
25
- transformers = {},
26
- query,
27
- mutation,
28
- client,
29
- paramsType = 'inline',
30
- pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
31
- mutationKey = MutationKey.getTransformer,
32
- queryKey = QueryKey.getTransformer,
33
- generators = [queryGenerator, mutationGenerator].filter(Boolean),
34
- paramsCasing,
35
- contentType,
36
- } = options
37
-
38
- const clientName = client?.client ?? 'axios'
39
- const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
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
- }
90
-
91
- if (group && (options?.group?.path || options?.group?.tag)) {
92
- const groupName: Group['name'] = group?.name
93
- ? group.name
94
- : (ctx) => {
95
- if (group?.type === 'path') {
96
- return `${ctx.group.split('/')[1]}`
97
- }
98
- 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
- },
156
- ],
157
- imports: [],
158
- exports: [],
159
- })
160
- }
161
-
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: [],
176
- })
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)
205
- },
206
- }
207
- })