@kubb/plugin-swr 5.0.0-alpha.9 → 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,123 +0,0 @@
1
- import { getDefaultValue, isOptional } from '@kubb/oas'
2
- import { Client } from '@kubb/plugin-client/components'
3
- import type { OperationSchemas } from '@kubb/plugin-oas'
4
- import { getPathParams } from '@kubb/plugin-oas/utils'
5
- import { File, Function, FunctionParams } from '@kubb/react-fabric'
6
- import type { FabricReactNode } from '@kubb/react-fabric/types'
7
- import type { PluginSwr } from '../types.ts'
8
-
9
- type Props = {
10
- name: string
11
- clientName: string
12
- typeSchemas: OperationSchemas
13
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
14
- paramsType: PluginSwr['resolvedOptions']['paramsType']
15
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
16
- }
17
-
18
- type GetParamsProps = {
19
- paramsType: PluginSwr['resolvedOptions']['paramsType']
20
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
21
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
22
- typeSchemas: OperationSchemas
23
- }
24
-
25
- function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: GetParamsProps) {
26
- if (paramsType === 'object') {
27
- const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
28
-
29
- return FunctionParams.factory({
30
- data: {
31
- mode: 'object',
32
- children: {
33
- ...pathParams,
34
- data: typeSchemas.request?.name
35
- ? {
36
- type: typeSchemas.request?.name,
37
- optional: isOptional(typeSchemas.request?.schema),
38
- }
39
- : undefined,
40
- params: typeSchemas.queryParams?.name
41
- ? {
42
- type: typeSchemas.queryParams?.name,
43
- optional: isOptional(typeSchemas.queryParams?.schema),
44
- }
45
- : undefined,
46
- headers: typeSchemas.headerParams?.name
47
- ? {
48
- type: typeSchemas.headerParams?.name,
49
- optional: isOptional(typeSchemas.headerParams?.schema),
50
- }
51
- : undefined,
52
- },
53
- },
54
- config: {
55
- type: typeSchemas.request?.name
56
- ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
57
- : 'Partial<RequestConfig> & { client?: Client }',
58
- default: '{}',
59
- },
60
- })
61
- }
62
-
63
- return FunctionParams.factory({
64
- pathParams: typeSchemas.pathParams?.name
65
- ? {
66
- mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
67
- children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
68
- default: getDefaultValue(typeSchemas.pathParams?.schema),
69
- }
70
- : undefined,
71
- data: typeSchemas.request?.name
72
- ? {
73
- type: typeSchemas.request?.name,
74
- optional: isOptional(typeSchemas.request?.schema),
75
- }
76
- : undefined,
77
- params: typeSchemas.queryParams?.name
78
- ? {
79
- type: typeSchemas.queryParams?.name,
80
- optional: isOptional(typeSchemas.queryParams?.schema),
81
- }
82
- : undefined,
83
- headers: typeSchemas.headerParams?.name
84
- ? {
85
- type: typeSchemas.headerParams?.name,
86
- optional: isOptional(typeSchemas.headerParams?.schema),
87
- }
88
- : undefined,
89
- config: {
90
- type: typeSchemas.request?.name
91
- ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
92
- : 'Partial<RequestConfig> & { client?: Client }',
93
- default: '{}',
94
- },
95
- })
96
- }
97
-
98
- export function QueryOptions({ name, clientName, typeSchemas, paramsCasing, paramsType, pathParamsType }: Props): FabricReactNode {
99
- const params = getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas })
100
- const clientParams = Client.getParams({
101
- paramsCasing,
102
- paramsType,
103
- typeSchemas,
104
- pathParamsType,
105
- isConfigurable: true,
106
- })
107
-
108
- return (
109
- <File.Source name={name} isExportable isIndexable>
110
- <Function name={name} export params={params.toConstructor()}>
111
- {`
112
- return {
113
- fetcher: async () => {
114
- return ${clientName}(${clientParams.toCall()})
115
- },
116
- }
117
- `}
118
- </Function>
119
- </File.Source>
120
- )
121
- }
122
-
123
- QueryOptions.getParams = getParams
@@ -1,5 +0,0 @@
1
- export { Mutation } from './Mutation.tsx'
2
- export { MutationKey } from './MutationKey.tsx'
3
- export { Query } from './Query.tsx'
4
- export { QueryKey } from './QueryKey.tsx'
5
- export { QueryOptions } from './QueryOptions.tsx'
@@ -1,2 +0,0 @@
1
- export { mutationGenerator } from './mutationGenerator.tsx'
2
- export { queryGenerator } from './queryGenerator.tsx'
@@ -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'