@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
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { camelCase } 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 camelCase(name, { isFile: type === 'file' })
|
|
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
|
+
}))
|
package/src/types.ts
CHANGED
|
@@ -1,31 +1,82 @@
|
|
|
1
1
|
import type { Transformer } from '@internals/tanstack-query'
|
|
2
|
-
import type { Group, Output, PluginFactoryOptions,
|
|
3
|
-
import type { contentType, HttpMethod, Oas } from '@kubb/oas'
|
|
2
|
+
import type { ast, Exclude, Generator, Group, Include, Output, Override, PluginFactoryOptions, Resolver } from '@kubb/core'
|
|
4
3
|
import type { ClientImportPath, PluginClient } from '@kubb/plugin-client'
|
|
5
|
-
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
6
|
-
import type { Generator } from '@kubb/plugin-oas/generators'
|
|
7
4
|
|
|
8
5
|
export type { Transformer } from '@internals/tanstack-query'
|
|
9
6
|
|
|
10
7
|
/**
|
|
11
|
-
*
|
|
8
|
+
* Resolver for SWR that provides naming methods for hook functions.
|
|
9
|
+
*/
|
|
10
|
+
export type ResolverSwr = Resolver & {
|
|
11
|
+
/**
|
|
12
|
+
* Resolves the base function name for an operation.
|
|
13
|
+
*
|
|
14
|
+
* @example Resolving base operation names
|
|
15
|
+
* `resolver.resolveName('show pet by id') // -> 'showPetById'`
|
|
16
|
+
*/
|
|
17
|
+
resolveName(this: ResolverSwr, name: string): string
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the output file name for a hook module.
|
|
20
|
+
*/
|
|
21
|
+
resolvePathName(this: ResolverSwr, name: string, type?: 'file' | 'function' | 'type' | 'const'): string
|
|
22
|
+
/**
|
|
23
|
+
* Resolves a query hook function name.
|
|
24
|
+
*/
|
|
25
|
+
resolveQueryName(this: ResolverSwr, node: ast.OperationNode): string
|
|
26
|
+
/**
|
|
27
|
+
* Resolves a mutation hook function name.
|
|
28
|
+
*/
|
|
29
|
+
resolveMutationName(this: ResolverSwr, node: ast.OperationNode): string
|
|
30
|
+
/**
|
|
31
|
+
* Resolves the query options helper name.
|
|
32
|
+
*/
|
|
33
|
+
resolveQueryOptionsName(this: ResolverSwr, node: ast.OperationNode): string
|
|
34
|
+
/**
|
|
35
|
+
* Resolves the query key helper name.
|
|
36
|
+
*/
|
|
37
|
+
resolveQueryKeyName(this: ResolverSwr, node: ast.OperationNode): string
|
|
38
|
+
/**
|
|
39
|
+
* Resolves the mutation key helper name.
|
|
40
|
+
*/
|
|
41
|
+
resolveMutationKeyName(this: ResolverSwr, node: ast.OperationNode): string
|
|
42
|
+
/**
|
|
43
|
+
* Resolves the query key type name.
|
|
44
|
+
*/
|
|
45
|
+
resolveQueryKeyTypeName(this: ResolverSwr, node: ast.OperationNode): string
|
|
46
|
+
/**
|
|
47
|
+
* Resolves the mutation key type name.
|
|
48
|
+
*/
|
|
49
|
+
resolveMutationKeyTypeName(this: ResolverSwr, node: ast.OperationNode): string
|
|
50
|
+
/**
|
|
51
|
+
* Resolves the mutation argument type name emitted alongside the mutation hook.
|
|
52
|
+
*/
|
|
53
|
+
resolveMutationArgTypeName(this: ResolverSwr, node: ast.OperationNode): string
|
|
54
|
+
/**
|
|
55
|
+
* Resolves the client function name generated inline by query hooks.
|
|
56
|
+
*/
|
|
57
|
+
resolveClientName(this: ResolverSwr, node: ast.OperationNode): string
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Customize the queryKey.
|
|
12
62
|
*/
|
|
13
63
|
type QueryKey = Transformer
|
|
14
64
|
|
|
15
65
|
/**
|
|
16
|
-
* Customize the mutationKey
|
|
66
|
+
* Customize the mutationKey.
|
|
17
67
|
*/
|
|
18
68
|
type MutationKey = Transformer
|
|
19
69
|
|
|
20
70
|
type Query = {
|
|
21
71
|
/**
|
|
22
|
-
*
|
|
72
|
+
* HTTP methods to use for queries.
|
|
73
|
+
*
|
|
23
74
|
* @default ['get']
|
|
24
75
|
*/
|
|
25
|
-
methods?: Array<
|
|
76
|
+
methods?: Array<string>
|
|
26
77
|
/**
|
|
27
|
-
* Path to the
|
|
28
|
-
* Used as `import
|
|
78
|
+
* Path to the useSWR hook for useSWR functionality.
|
|
79
|
+
* Used as `import useSWR from '${importPath}'`.
|
|
29
80
|
* Accepts relative and absolute paths.
|
|
30
81
|
* Path is used as-is; relative paths are based on the generated file location.
|
|
31
82
|
* @default 'swr'
|
|
@@ -35,25 +86,19 @@ type Query = {
|
|
|
35
86
|
|
|
36
87
|
type Mutation = {
|
|
37
88
|
/**
|
|
38
|
-
*
|
|
89
|
+
* HTTP methods to use for mutations.
|
|
90
|
+
*
|
|
39
91
|
* @default ['post', 'put', 'delete', 'patch']
|
|
40
92
|
*/
|
|
41
|
-
methods?: Array<
|
|
93
|
+
methods?: Array<string>
|
|
42
94
|
/**
|
|
43
|
-
* Path to the
|
|
44
|
-
* Used as `import
|
|
95
|
+
* Path to the useSWRMutation hook for useSWRMutation functionality.
|
|
96
|
+
* Used as `import useSWRMutation from '${importPath}'`.
|
|
45
97
|
* Accepts relative and absolute paths.
|
|
46
98
|
* Path is used as-is; relative paths are based on the generated file location.
|
|
47
99
|
* @default 'swr/mutation'
|
|
48
100
|
*/
|
|
49
101
|
importPath?: string
|
|
50
|
-
/**
|
|
51
|
-
* When true, mutation parameters (path params, query params, headers, body) is passed via `trigger()` instead of as hook arguments.
|
|
52
|
-
* This aligns with React Query's mutation pattern where variables are passed when triggering the mutation.
|
|
53
|
-
* @default false
|
|
54
|
-
* @deprecated This will become the default behavior in v5. Set to `true` to opt-in early.
|
|
55
|
-
*/
|
|
56
|
-
paramsToTrigger?: boolean
|
|
57
102
|
}
|
|
58
103
|
|
|
59
104
|
export type Options = {
|
|
@@ -61,81 +106,92 @@ export type Options = {
|
|
|
61
106
|
* Specify the export location for the files and define the behavior of the output
|
|
62
107
|
* @default { path: 'hooks', barrelType: 'named' }
|
|
63
108
|
*/
|
|
64
|
-
output?: Output
|
|
65
|
-
/**
|
|
66
|
-
* Define which contentType should be used.
|
|
67
|
-
* By default, the first JSON valid mediaType is used
|
|
68
|
-
*/
|
|
69
|
-
contentType?: contentType
|
|
109
|
+
output?: Output
|
|
70
110
|
/**
|
|
71
111
|
* Group the SWR hooks based on the provided name.
|
|
72
112
|
*/
|
|
73
113
|
group?: Group
|
|
114
|
+
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
74
115
|
/**
|
|
75
|
-
*
|
|
116
|
+
* Tags, operations, or paths to exclude from generation.
|
|
76
117
|
*/
|
|
77
118
|
exclude?: Array<Exclude>
|
|
78
119
|
/**
|
|
79
|
-
*
|
|
120
|
+
* Tags, operations, or paths to include in generation.
|
|
80
121
|
*/
|
|
81
122
|
include?: Array<Include>
|
|
82
123
|
/**
|
|
83
|
-
*
|
|
124
|
+
* Override options for specific tags, operations, or paths.
|
|
84
125
|
*/
|
|
85
126
|
override?: Array<Override<ResolvedOptions>>
|
|
86
|
-
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
87
|
-
queryKey?: QueryKey
|
|
88
|
-
query?: Query | false
|
|
89
|
-
mutationKey?: MutationKey
|
|
90
|
-
mutation?: Mutation | false
|
|
91
127
|
/**
|
|
92
|
-
*
|
|
93
|
-
* - 'camelcase' uses camelcase for the params names
|
|
128
|
+
* Apply casing to parameter names.
|
|
94
129
|
*/
|
|
95
130
|
paramsCasing?: 'camelcase'
|
|
96
131
|
/**
|
|
97
|
-
* How
|
|
98
|
-
*
|
|
99
|
-
* - 'inline' returns the params as comma separated params.
|
|
132
|
+
* How parameters are passed: grouped in an object or spread inline.
|
|
133
|
+
*
|
|
100
134
|
* @default 'inline'
|
|
101
135
|
*/
|
|
102
136
|
paramsType?: 'object' | 'inline'
|
|
103
137
|
/**
|
|
104
|
-
* How
|
|
105
|
-
*
|
|
106
|
-
* - 'inline': returns the pathParams as comma separated params.
|
|
138
|
+
* How path parameters are passed: grouped in an object or spread inline.
|
|
139
|
+
*
|
|
107
140
|
* @default 'inline'
|
|
108
141
|
*/
|
|
109
142
|
pathParamsType?: PluginClient['options']['pathParamsType']
|
|
143
|
+
queryKey?: QueryKey
|
|
144
|
+
/**
|
|
145
|
+
* Configure useSWR behavior.
|
|
146
|
+
*/
|
|
147
|
+
query?: Partial<Query> | false
|
|
148
|
+
mutationKey?: MutationKey
|
|
110
149
|
/**
|
|
111
|
-
*
|
|
112
|
-
|
|
150
|
+
* Configure useSWRMutation behavior.
|
|
151
|
+
*/
|
|
152
|
+
mutation?: Partial<Mutation> | false
|
|
153
|
+
/**
|
|
154
|
+
* Parser to use for validating response data.
|
|
113
155
|
*/
|
|
114
156
|
parser?: PluginClient['options']['parser']
|
|
115
|
-
transformers?: {
|
|
116
|
-
/**
|
|
117
|
-
* Customize the names based on the type that is provided by the plugin.
|
|
118
|
-
*/
|
|
119
|
-
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
120
|
-
}
|
|
121
157
|
/**
|
|
122
|
-
*
|
|
158
|
+
* Override naming conventions for function names and types.
|
|
159
|
+
*/
|
|
160
|
+
resolver?: Partial<ResolverSwr> & ThisType<ResolverSwr>
|
|
161
|
+
/**
|
|
162
|
+
* AST visitor to transform generated nodes.
|
|
163
|
+
*/
|
|
164
|
+
transformer?: ast.Visitor
|
|
165
|
+
/**
|
|
166
|
+
* Additional generators alongside the default generators.
|
|
123
167
|
*/
|
|
124
168
|
generators?: Array<Generator<PluginSwr>>
|
|
125
169
|
}
|
|
126
170
|
|
|
127
171
|
type ResolvedOptions = {
|
|
128
|
-
output: Output
|
|
172
|
+
output: Output
|
|
173
|
+
group: Group | undefined
|
|
174
|
+
exclude: NonNullable<Options['exclude']>
|
|
175
|
+
include: Options['include']
|
|
176
|
+
override: NonNullable<Options['override']>
|
|
129
177
|
client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>
|
|
130
178
|
parser: Required<NonNullable<Options['parser']>>
|
|
179
|
+
pathParamsType: NonNullable<Options['pathParamsType']>
|
|
180
|
+
paramsCasing: Options['paramsCasing']
|
|
181
|
+
paramsType: NonNullable<Options['paramsType']>
|
|
131
182
|
queryKey: QueryKey | undefined
|
|
132
183
|
query: NonNullable<Required<Query>> | false
|
|
133
184
|
mutationKey: MutationKey | undefined
|
|
134
|
-
mutation:
|
|
135
|
-
|
|
136
|
-
paramsType: NonNullable<Options['paramsType']>
|
|
137
|
-
pathParamsType: NonNullable<Options['pathParamsType']>
|
|
138
|
-
group: Options['group']
|
|
185
|
+
mutation: NonNullable<Required<Mutation>> | false
|
|
186
|
+
resolver: ResolverSwr
|
|
139
187
|
}
|
|
140
188
|
|
|
141
|
-
export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions,
|
|
189
|
+
export type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, ResolverSwr>
|
|
190
|
+
|
|
191
|
+
declare global {
|
|
192
|
+
namespace Kubb {
|
|
193
|
+
interface PluginRegistry {
|
|
194
|
+
'plugin-swr': PluginSwr
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export {
|
|
2
|
+
buildGroupParam,
|
|
3
|
+
buildQueryKeyParams,
|
|
4
|
+
resolveHeaderGroupType,
|
|
5
|
+
resolveOperationOverrides,
|
|
6
|
+
resolvePathParamType,
|
|
7
|
+
resolveQueryGroupType,
|
|
8
|
+
resolveZodSchemaNames,
|
|
9
|
+
} from '@internals/tanstack-query'
|
|
10
|
+
export { buildOperationComments as getComments, buildRequestConfigType, getContentTypeInfo, resolveErrorNames, resolveStatusCodeNames } from '@internals/shared'
|