@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,276 +0,0 @@
1
- import { isOptional, type Operation } from '@kubb/oas'
2
- import { Client } from '@kubb/plugin-client/components'
3
- import type { OperationSchemas } from '@kubb/plugin-oas'
4
- import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
5
- import { File, Function, FunctionParams, Type } from '@kubb/react-fabric'
6
- import type { FabricReactNode, Params } from '@kubb/react-fabric/types'
7
- import type { PluginSwr } from '../types.ts'
8
- import { MutationKey } from './MutationKey.tsx'
9
-
10
- type Props = {
11
- /**
12
- * Name of the function
13
- */
14
- name: string
15
- typeName: string
16
- clientName: string
17
- mutationKeyName: string
18
- mutationKeyTypeName: string
19
- typeSchemas: OperationSchemas
20
- operation: Operation
21
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
22
- paramsType: PluginSwr['resolvedOptions']['paramsType']
23
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
24
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
25
- /**
26
- * When true, mutation parameters are passed via trigger() instead of as hook arguments
27
- * @default false
28
- */
29
- paramsToTrigger?: boolean
30
- }
31
-
32
- type GetParamsProps = {
33
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
34
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
35
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
36
- typeSchemas: OperationSchemas
37
- mutationKeyTypeName: string
38
- }
39
-
40
- // Original getParams - used when paramsToTrigger is false (default)
41
- function getParams({ pathParamsType, paramsCasing, dataReturnType, typeSchemas, mutationKeyTypeName }: GetParamsProps) {
42
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
43
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
44
- const TExtraArg = typeSchemas.request?.name || 'never'
45
-
46
- return FunctionParams.factory({
47
- pathParams: {
48
- mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
49
- children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
50
- },
51
- params: typeSchemas.queryParams?.name
52
- ? {
53
- type: typeSchemas.queryParams?.name,
54
- optional: isOptional(typeSchemas.queryParams?.schema),
55
- }
56
- : undefined,
57
- headers: typeSchemas.headerParams?.name
58
- ? {
59
- type: typeSchemas.headerParams?.name,
60
- optional: isOptional(typeSchemas.headerParams?.schema),
61
- }
62
- : undefined,
63
- options: {
64
- type: `
65
- {
66
- mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${TExtraArg}> & { throwOnError?: boolean },
67
- client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
68
- shouldFetch?: boolean,
69
- }
70
- `,
71
- default: '{}',
72
- },
73
- })
74
- }
75
-
76
- type GetTriggerParamsProps = {
77
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
78
- typeSchemas: OperationSchemas
79
- mutationKeyTypeName: string
80
- mutationArgTypeName: string
81
- }
82
-
83
- // New getParams - used when paramsToTrigger is true
84
- function getTriggerParams({ dataReturnType, typeSchemas, mutationKeyTypeName, mutationArgTypeName }: GetTriggerParamsProps) {
85
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
86
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
87
-
88
- return FunctionParams.factory({
89
- options: {
90
- type: `
91
- {
92
- mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${mutationArgTypeName}> & { throwOnError?: boolean },
93
- client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
94
- shouldFetch?: boolean,
95
- }
96
- `,
97
- default: '{}',
98
- },
99
- })
100
- }
101
-
102
- type GetMutationParamsProps = {
103
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
104
- typeSchemas: OperationSchemas
105
- }
106
-
107
- function getMutationParams({ paramsCasing, typeSchemas }: GetMutationParamsProps) {
108
- return FunctionParams.factory({
109
- ...getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
110
- data: typeSchemas.request?.name
111
- ? {
112
- type: typeSchemas.request?.name,
113
- optional: isOptional(typeSchemas.request?.schema),
114
- }
115
- : undefined,
116
- params: typeSchemas.queryParams?.name
117
- ? {
118
- type: typeSchemas.queryParams?.name,
119
- optional: isOptional(typeSchemas.queryParams?.schema),
120
- }
121
- : undefined,
122
- headers: typeSchemas.headerParams?.name
123
- ? {
124
- type: typeSchemas.headerParams?.name,
125
- optional: isOptional(typeSchemas.headerParams?.schema),
126
- }
127
- : undefined,
128
- })
129
- }
130
-
131
- export function Mutation({
132
- name,
133
- clientName,
134
- mutationKeyName,
135
- mutationKeyTypeName,
136
- paramsType,
137
- paramsCasing,
138
- pathParamsType,
139
- dataReturnType,
140
- typeSchemas,
141
- operation,
142
- paramsToTrigger = false,
143
- }: Props): FabricReactNode {
144
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
145
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
146
-
147
- const mutationKeyParams = MutationKey.getParams({
148
- pathParamsType,
149
- typeSchemas,
150
- })
151
-
152
- const clientParams = Client.getParams({
153
- paramsCasing,
154
- paramsType,
155
- typeSchemas,
156
- pathParamsType,
157
- isConfigurable: true,
158
- })
159
-
160
- // Use the new trigger-based approach when paramsToTrigger is true
161
- if (paramsToTrigger) {
162
- // Build the mutation params type (for arg destructuring)
163
- const mutationParams = getMutationParams({
164
- paramsCasing,
165
- typeSchemas,
166
- })
167
-
168
- // Get the arg type name
169
- const mutationArgTypeName = `${mutationKeyTypeName.replace('MutationKey', '')}MutationArg`
170
-
171
- // Check if there are any mutation params (path, data, params, headers)
172
- const hasMutationParams = Object.keys(mutationParams.params).length > 0
173
-
174
- // Build the arg type for useSWRMutation
175
- const argParams = FunctionParams.factory({
176
- data: {
177
- mode: 'object',
178
- children: Object.entries(mutationParams.params).reduce((acc, [key, value]) => {
179
- if (value) {
180
- acc[key] = {
181
- ...value,
182
- type: undefined,
183
- }
184
- }
185
- return acc
186
- }, {} as Params),
187
- },
188
- })
189
-
190
- const params = getTriggerParams({
191
- dataReturnType,
192
- typeSchemas,
193
- mutationKeyTypeName,
194
- mutationArgTypeName,
195
- })
196
-
197
- const generics = [TData, TError, `${mutationKeyTypeName} | null`, mutationArgTypeName].filter(Boolean)
198
-
199
- const mutationArg = mutationParams.toConstructor()
200
-
201
- return (
202
- <>
203
- <File.Source name={mutationArgTypeName} isExportable isIndexable isTypeOnly>
204
- <Type name={mutationArgTypeName} export>
205
- {hasMutationParams ? `{${mutationArg}}` : 'never'}
206
- </Type>
207
- </File.Source>
208
- <File.Source name={name} isExportable isIndexable>
209
- <Function
210
- name={name}
211
- export
212
- params={params.toConstructor()}
213
- JSDoc={{
214
- comments: getComments(operation),
215
- }}
216
- >
217
- {`
218
- const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
219
- const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
220
-
221
- return useSWRMutation<${generics.join(', ')}>(
222
- shouldFetch ? mutationKey : null,
223
- async (_url${hasMutationParams ? `, { arg: ${argParams.toCall()} }` : ''}) => {
224
- return ${clientName}(${clientParams.toCall()})
225
- },
226
- mutationOptions
227
- )
228
- `}
229
- </Function>
230
- </File.Source>
231
- </>
232
- )
233
- }
234
-
235
- // Original behavior (default)
236
- const generics = [
237
- TData,
238
- TError,
239
- `${mutationKeyTypeName} | null`,
240
- typeSchemas.request?.name, // TExtraArg - the arg type for useSWRMutation
241
- ].filter(Boolean)
242
-
243
- const params = getParams({
244
- paramsCasing,
245
- pathParamsType,
246
- dataReturnType,
247
- typeSchemas,
248
- mutationKeyTypeName,
249
- })
250
-
251
- return (
252
- <File.Source name={name} isExportable isIndexable>
253
- <Function
254
- name={name}
255
- export
256
- params={params.toConstructor()}
257
- JSDoc={{
258
- comments: getComments(operation),
259
- }}
260
- >
261
- {`
262
- const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
263
- const mutationKey = ${mutationKeyName}(${mutationKeyParams.toCall()})
264
-
265
- return useSWRMutation<${generics.join(', ')}>(
266
- shouldFetch ? mutationKey : null,
267
- async (_url${typeSchemas.request?.name ? ', { arg: data }' : ''}) => {
268
- return ${clientName}(${clientParams.toCall()})
269
- },
270
- mutationOptions
271
- )
272
- `}
273
- </Function>
274
- </File.Source>
275
- )
276
- }
@@ -1 +0,0 @@
1
- export { MutationKey } from '@internals/tanstack-query'
@@ -1,193 +0,0 @@
1
- import { getDefaultValue, isOptional, type Operation } from '@kubb/oas'
2
- import type { OperationSchemas } from '@kubb/plugin-oas'
3
- import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
4
- import { File, Function, FunctionParams } from '@kubb/react-fabric'
5
- import type { FabricReactNode } from '@kubb/react-fabric/types'
6
- import type { PluginSwr } from '../types.ts'
7
- import { QueryKey } from './QueryKey.tsx'
8
- import { QueryOptions } from './QueryOptions.tsx'
9
-
10
- type Props = {
11
- /**
12
- * Name of the function
13
- */
14
- name: string
15
- queryOptionsName: string
16
- queryKeyName: string
17
- queryKeyTypeName: string
18
- typeSchemas: OperationSchemas
19
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
20
- paramsType: PluginSwr['resolvedOptions']['paramsType']
21
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
22
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
23
- operation: Operation
24
- }
25
-
26
- type GetParamsProps = {
27
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
28
- paramsType: PluginSwr['resolvedOptions']['paramsType']
29
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
30
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
31
- typeSchemas: OperationSchemas
32
- }
33
-
34
- function getParams({ paramsType, paramsCasing, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
35
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
36
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
37
-
38
- if (paramsType === 'object') {
39
- const pathParams = getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing })
40
-
41
- const children = {
42
- ...pathParams,
43
- data: typeSchemas.request?.name
44
- ? {
45
- type: typeSchemas.request?.name,
46
- optional: isOptional(typeSchemas.request?.schema),
47
- }
48
- : undefined,
49
- params: typeSchemas.queryParams?.name
50
- ? {
51
- type: typeSchemas.queryParams?.name,
52
- optional: isOptional(typeSchemas.queryParams?.schema),
53
- }
54
- : undefined,
55
- headers: typeSchemas.headerParams?.name
56
- ? {
57
- type: typeSchemas.headerParams?.name,
58
- optional: isOptional(typeSchemas.headerParams?.schema),
59
- }
60
- : undefined,
61
- }
62
-
63
- // Check if all children are optional or undefined
64
- const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
65
-
66
- return FunctionParams.factory({
67
- data: {
68
- mode: 'object',
69
- children,
70
- default: allChildrenAreOptional ? '{}' : undefined,
71
- },
72
- options: {
73
- type: `
74
- {
75
- query?: Parameters<typeof useSWR<${[TData, TError].join(', ')}>>[2],
76
- client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
77
- shouldFetch?: boolean,
78
- immutable?: boolean
79
- }
80
- `,
81
- default: '{}',
82
- },
83
- })
84
- }
85
-
86
- return FunctionParams.factory({
87
- pathParams: typeSchemas.pathParams?.name
88
- ? {
89
- mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
90
- children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
91
- default: getDefaultValue(typeSchemas.pathParams?.schema),
92
- }
93
- : undefined,
94
- data: typeSchemas.request?.name
95
- ? {
96
- type: typeSchemas.request?.name,
97
- optional: isOptional(typeSchemas.request?.schema),
98
- }
99
- : undefined,
100
- params: typeSchemas.queryParams?.name
101
- ? {
102
- type: typeSchemas.queryParams?.name,
103
- optional: isOptional(typeSchemas.queryParams?.schema),
104
- }
105
- : undefined,
106
- headers: typeSchemas.headerParams?.name
107
- ? {
108
- type: typeSchemas.headerParams?.name,
109
- optional: isOptional(typeSchemas.headerParams?.schema),
110
- }
111
- : undefined,
112
- options: {
113
- type: `
114
- {
115
- query?: Parameters<typeof useSWR<${[TData, TError].join(', ')}>>[2],
116
- client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
117
- shouldFetch?: boolean,
118
- immutable?: boolean
119
- }
120
- `,
121
- default: '{}',
122
- },
123
- })
124
- }
125
-
126
- export function Query({
127
- name,
128
- typeSchemas,
129
- queryKeyName,
130
- queryKeyTypeName,
131
- queryOptionsName,
132
- operation,
133
- dataReturnType,
134
- paramsType,
135
- paramsCasing,
136
- pathParamsType,
137
- }: Props): FabricReactNode {
138
- const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
139
- const TError = `ResponseErrorConfig<${typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'}>`
140
- const generics = [TData, TError, `${queryKeyTypeName} | null`]
141
-
142
- const queryKeyParams = QueryKey.getParams({
143
- pathParamsType,
144
- typeSchemas,
145
- paramsCasing,
146
- })
147
- const params = getParams({
148
- paramsCasing,
149
- paramsType,
150
- pathParamsType,
151
- dataReturnType,
152
- typeSchemas,
153
- })
154
-
155
- const queryOptionsParams = QueryOptions.getParams({
156
- paramsCasing,
157
- paramsType,
158
- pathParamsType,
159
- typeSchemas,
160
- })
161
-
162
- return (
163
- <File.Source name={name} isExportable isIndexable>
164
- <Function
165
- name={name}
166
- export
167
- params={params.toConstructor()}
168
- JSDoc={{
169
- comments: getComments(operation),
170
- }}
171
- >
172
- {`
173
- const { query: queryOptions, client: config = {}, shouldFetch = true, immutable } = options ?? {}
174
-
175
- const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
176
-
177
- return useSWR<${generics.join(', ')}>(
178
- shouldFetch ? queryKey : null,
179
- {
180
- ...${queryOptionsName}(${queryOptionsParams.toCall()}),
181
- ...(immutable ? {
182
- revalidateIfStale: false,
183
- revalidateOnFocus: false,
184
- revalidateOnReconnect: false
185
- } : { }),
186
- ...queryOptions
187
- }
188
- )
189
- `}
190
- </Function>
191
- </File.Source>
192
- )
193
- }
@@ -1 +0,0 @@
1
- export { QueryKey } from '@internals/tanstack-query'
@@ -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'