@kubb/plugin-swr 5.0.0-alpha.9 → 5.0.0-beta.103
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 +41 -26
- package/dist/index.cjs +1226 -127
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +292 -6
- package/dist/index.js +1196 -128
- package/dist/index.js.map +1 -1
- package/package.json +44 -77
- 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/components.cjs +0 -7
- package/dist/components.d.ts +0 -106
- package/dist/components.js +0 -2
- 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/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -508
- package/dist/generators.js +0 -2
- package/dist/types-BIaGRPjD.d.ts +0 -210
- package/src/components/Mutation.tsx +0 -276
- package/src/components/MutationKey.tsx +0 -1
- package/src/components/Query.tsx +0 -193
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -123
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -2
- package/src/generators/mutationGenerator.tsx +0 -177
- package/src/generators/queryGenerator.tsx +0 -186
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -207
- package/src/types.ts +0 -141
- /package/dist/{chunk--u3MIqq1.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/dist/types-BIaGRPjD.d.ts
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { t as __name } from "./chunk--u3MIqq1.js";
|
|
2
|
-
import { Group, Output, PluginFactoryOptions, ResolveNameParams } from "@kubb/core";
|
|
3
|
-
import { ClientImportPath, PluginClient } from "@kubb/plugin-client";
|
|
4
|
-
import { Exclude, Include, OperationSchemas, Override, ResolvePathOptions } from "@kubb/plugin-oas";
|
|
5
|
-
import { HttpMethod, Oas, Operation, contentType } from "@kubb/oas";
|
|
6
|
-
import { FunctionParams } from "@kubb/react-fabric";
|
|
7
|
-
import { Generator as Generator$1 } from "@kubb/plugin-oas/generators";
|
|
8
|
-
import { FabricReactNode } from "@kubb/react-fabric/types";
|
|
9
|
-
|
|
10
|
-
//#region ../../internals/tanstack-query/src/types.d.ts
|
|
11
|
-
type ParamsCasing = 'camelcase' | undefined;
|
|
12
|
-
type PathParamsType = 'object' | 'inline';
|
|
13
|
-
type TransformerProps = {
|
|
14
|
-
operation: Operation;
|
|
15
|
-
schemas: OperationSchemas;
|
|
16
|
-
casing: ParamsCasing;
|
|
17
|
-
};
|
|
18
|
-
type Transformer = (props: TransformerProps) => unknown[];
|
|
19
|
-
//#endregion
|
|
20
|
-
//#region ../../internals/tanstack-query/src/components/MutationKey.d.ts
|
|
21
|
-
type Props$1 = {
|
|
22
|
-
name: string;
|
|
23
|
-
typeName: string;
|
|
24
|
-
typeSchemas: OperationSchemas;
|
|
25
|
-
operation: Operation;
|
|
26
|
-
paramsCasing: ParamsCasing;
|
|
27
|
-
pathParamsType: PathParamsType;
|
|
28
|
-
transformer: Transformer | undefined;
|
|
29
|
-
};
|
|
30
|
-
type GetParamsProps$1 = {
|
|
31
|
-
pathParamsType: PathParamsType;
|
|
32
|
-
typeSchemas: OperationSchemas;
|
|
33
|
-
};
|
|
34
|
-
declare function MutationKey$1({
|
|
35
|
-
name,
|
|
36
|
-
typeSchemas,
|
|
37
|
-
pathParamsType,
|
|
38
|
-
paramsCasing,
|
|
39
|
-
operation,
|
|
40
|
-
typeName,
|
|
41
|
-
transformer
|
|
42
|
-
}: Props$1): FabricReactNode;
|
|
43
|
-
declare namespace MutationKey$1 {
|
|
44
|
-
var getParams: ({}: GetParamsProps$1) => FunctionParams;
|
|
45
|
-
var getTransformer: Transformer;
|
|
46
|
-
}
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region ../../internals/tanstack-query/src/components/QueryKey.d.ts
|
|
49
|
-
type Props = {
|
|
50
|
-
name: string;
|
|
51
|
-
typeName: string;
|
|
52
|
-
typeSchemas: OperationSchemas;
|
|
53
|
-
operation: Operation;
|
|
54
|
-
paramsCasing: ParamsCasing;
|
|
55
|
-
pathParamsType: PathParamsType;
|
|
56
|
-
transformer: Transformer | undefined;
|
|
57
|
-
};
|
|
58
|
-
type GetParamsProps = {
|
|
59
|
-
paramsCasing: ParamsCasing;
|
|
60
|
-
pathParamsType: PathParamsType;
|
|
61
|
-
typeSchemas: OperationSchemas;
|
|
62
|
-
};
|
|
63
|
-
declare function QueryKey$1({
|
|
64
|
-
name,
|
|
65
|
-
typeSchemas,
|
|
66
|
-
paramsCasing,
|
|
67
|
-
pathParamsType,
|
|
68
|
-
operation,
|
|
69
|
-
typeName,
|
|
70
|
-
transformer
|
|
71
|
-
}: Props): FabricReactNode;
|
|
72
|
-
declare namespace QueryKey$1 {
|
|
73
|
-
var getParams: ({
|
|
74
|
-
pathParamsType,
|
|
75
|
-
paramsCasing,
|
|
76
|
-
typeSchemas
|
|
77
|
-
}: GetParamsProps) => FunctionParams;
|
|
78
|
-
var getTransformer: Transformer;
|
|
79
|
-
}
|
|
80
|
-
//#endregion
|
|
81
|
-
//#region src/types.d.ts
|
|
82
|
-
/**
|
|
83
|
-
* Customize the queryKey
|
|
84
|
-
*/
|
|
85
|
-
type QueryKey = Transformer;
|
|
86
|
-
/**
|
|
87
|
-
* Customize the mutationKey
|
|
88
|
-
*/
|
|
89
|
-
type MutationKey = Transformer;
|
|
90
|
-
type Query = {
|
|
91
|
-
/**
|
|
92
|
-
* Define which HttpMethods can be used for queries
|
|
93
|
-
* @default ['get']
|
|
94
|
-
*/
|
|
95
|
-
methods?: Array<HttpMethod>;
|
|
96
|
-
/**
|
|
97
|
-
* Path to the useQuery hook for useQuery functionality.
|
|
98
|
-
* Used as `import { useQuery } from '${importPath}'`.
|
|
99
|
-
* Accepts relative and absolute paths.
|
|
100
|
-
* Path is used as-is; relative paths are based on the generated file location.
|
|
101
|
-
* @default 'swr'
|
|
102
|
-
*/
|
|
103
|
-
importPath?: string;
|
|
104
|
-
};
|
|
105
|
-
type Mutation = {
|
|
106
|
-
/**
|
|
107
|
-
* Define which HttpMethods can be used for queries
|
|
108
|
-
* @default ['post', 'put', 'delete', 'patch']
|
|
109
|
-
*/
|
|
110
|
-
methods?: Array<HttpMethod>;
|
|
111
|
-
/**
|
|
112
|
-
* Path to the useQuery hook for useQuery functionality.
|
|
113
|
-
* Used as `import { useQuery } from '${importPath}'`.
|
|
114
|
-
* Accepts relative and absolute paths.
|
|
115
|
-
* Path is used as-is; relative paths are based on the generated file location.
|
|
116
|
-
* @default 'swr/mutation'
|
|
117
|
-
*/
|
|
118
|
-
importPath?: string;
|
|
119
|
-
/**
|
|
120
|
-
* When true, mutation parameters (path params, query params, headers, body) is passed via `trigger()` instead of as hook arguments.
|
|
121
|
-
* This aligns with React Query's mutation pattern where variables are passed when triggering the mutation.
|
|
122
|
-
* @default false
|
|
123
|
-
* @deprecated This will become the default behavior in v5. Set to `true` to opt-in early.
|
|
124
|
-
*/
|
|
125
|
-
paramsToTrigger?: boolean;
|
|
126
|
-
};
|
|
127
|
-
type Options = {
|
|
128
|
-
/**
|
|
129
|
-
* Specify the export location for the files and define the behavior of the output
|
|
130
|
-
* @default { path: 'hooks', barrelType: 'named' }
|
|
131
|
-
*/
|
|
132
|
-
output?: Output<Oas>;
|
|
133
|
-
/**
|
|
134
|
-
* Define which contentType should be used.
|
|
135
|
-
* By default, the first JSON valid mediaType is used
|
|
136
|
-
*/
|
|
137
|
-
contentType?: contentType;
|
|
138
|
-
/**
|
|
139
|
-
* Group the SWR hooks based on the provided name.
|
|
140
|
-
*/
|
|
141
|
-
group?: Group;
|
|
142
|
-
/**
|
|
143
|
-
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
144
|
-
*/
|
|
145
|
-
exclude?: Array<Exclude>;
|
|
146
|
-
/**
|
|
147
|
-
* Array containing include parameters to include tags/operations/methods/paths.
|
|
148
|
-
*/
|
|
149
|
-
include?: Array<Include>;
|
|
150
|
-
/**
|
|
151
|
-
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
152
|
-
*/
|
|
153
|
-
override?: Array<Override<ResolvedOptions>>;
|
|
154
|
-
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>;
|
|
155
|
-
queryKey?: QueryKey;
|
|
156
|
-
query?: Query | false;
|
|
157
|
-
mutationKey?: MutationKey;
|
|
158
|
-
mutation?: Mutation | false;
|
|
159
|
-
/**
|
|
160
|
-
* How to style your params, by default no casing is applied
|
|
161
|
-
* - 'camelcase' uses camelcase for the params names
|
|
162
|
-
*/
|
|
163
|
-
paramsCasing?: 'camelcase';
|
|
164
|
-
/**
|
|
165
|
-
* How to pass your params
|
|
166
|
-
* - 'object' returns the params and pathParams as an object.
|
|
167
|
-
* - 'inline' returns the params as comma separated params.
|
|
168
|
-
* @default 'inline'
|
|
169
|
-
*/
|
|
170
|
-
paramsType?: 'object' | 'inline';
|
|
171
|
-
/**
|
|
172
|
-
* How to pass your pathParams.
|
|
173
|
-
* - 'object' returns the pathParams as an object.
|
|
174
|
-
* - 'inline': returns the pathParams as comma separated params.
|
|
175
|
-
* @default 'inline'
|
|
176
|
-
*/
|
|
177
|
-
pathParamsType?: PluginClient['options']['pathParamsType'];
|
|
178
|
-
/**
|
|
179
|
-
* Which parser should be used before returning the data to `swr`.
|
|
180
|
-
* `'zod'` uses `@kubb/plugin-zod` to parse the data.
|
|
181
|
-
*/
|
|
182
|
-
parser?: PluginClient['options']['parser'];
|
|
183
|
-
transformers?: {
|
|
184
|
-
/**
|
|
185
|
-
* Customize the names based on the type that is provided by the plugin.
|
|
186
|
-
*/
|
|
187
|
-
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string;
|
|
188
|
-
};
|
|
189
|
-
/**
|
|
190
|
-
* Define some generators next to the swr generators
|
|
191
|
-
*/
|
|
192
|
-
generators?: Array<Generator$1<PluginSwr>>;
|
|
193
|
-
};
|
|
194
|
-
type ResolvedOptions = {
|
|
195
|
-
output: Output<Oas>;
|
|
196
|
-
client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>;
|
|
197
|
-
parser: Required<NonNullable<Options['parser']>>;
|
|
198
|
-
queryKey: QueryKey | undefined;
|
|
199
|
-
query: NonNullable<Required<Query>> | false;
|
|
200
|
-
mutationKey: MutationKey | undefined;
|
|
201
|
-
mutation: (Required<Pick<Mutation, 'methods' | 'importPath'>> & Pick<Mutation, 'paramsToTrigger'>) | false;
|
|
202
|
-
paramsCasing: Options['paramsCasing'];
|
|
203
|
-
paramsType: NonNullable<Options['paramsType']>;
|
|
204
|
-
pathParamsType: NonNullable<Options['pathParamsType']>;
|
|
205
|
-
group: Options['group'];
|
|
206
|
-
};
|
|
207
|
-
type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, never, ResolvePathOptions>;
|
|
208
|
-
//#endregion
|
|
209
|
-
export { MutationKey$1 as i, PluginSwr as n, QueryKey$1 as r, Options as t };
|
|
210
|
-
//# sourceMappingURL=types-BIaGRPjD.d.ts.map
|
|
@@ -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'
|
package/src/components/Query.tsx
DELETED
|
@@ -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'
|