@kubb/plugin-swr 5.0.0-beta.56 → 5.0.0-beta.73
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/README.md +2 -2
- package/dist/index.cjs +1274 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +365 -3
- package/dist/index.js +1245 -49
- package/dist/index.js.map +1 -1
- package/package.json +10 -19
- package/dist/components-C8FJ3-Cb.cjs +0 -1008
- package/dist/components-C8FJ3-Cb.cjs.map +0 -1
- package/dist/components-CR9NLFcO.js +0 -912
- package/dist/components-CR9NLFcO.js.map +0 -1
- package/dist/components.cjs +0 -7
- package/dist/components.d.ts +0 -100
- package/dist/components.js +0 -2
- package/dist/generators-BB6csINY.js +0 -411
- package/dist/generators-BB6csINY.js.map +0 -1
- package/dist/generators-mw3FvFJD.cjs +0 -422
- package/dist/generators-mw3FvFJD.cjs.map +0 -1
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -16
- package/dist/generators.js +0 -2
- package/dist/types-Dz_j0Bqv.d.ts +0 -217
- package/src/components/Mutation.tsx +0 -155
- package/src/components/MutationKey.tsx +0 -25
- package/src/components/Query.tsx +0 -128
- package/src/components/QueryKey.tsx +0 -1
- package/src/components/QueryOptions.tsx +0 -56
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -2
- package/src/generators/mutationGenerator.tsx +0 -166
- package/src/generators/queryGenerator.tsx +0 -170
- package/src/index.ts +0 -2
- package/src/plugin.ts +0 -142
- package/src/resolvers/resolverSwr.ts +0 -56
- package/src/types.ts +0 -194
- package/src/utils.ts +0 -17
- /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/dist/types-Dz_j0Bqv.d.ts
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
1
|
-
import { t as __name } from "./chunk-C0LytTxp.js";
|
|
2
|
-
import { Exclude, Generator, Group, Include, Output, OutputOptions, Override, PluginFactoryOptions, Resolver, ast } from "@kubb/core";
|
|
3
|
-
import { ClientImportPath, PluginClient } from "@kubb/plugin-client";
|
|
4
|
-
import { PluginTs, ResolverTs } from "@kubb/plugin-ts";
|
|
5
|
-
import { KubbReactNode } from "@kubb/renderer-jsx/types";
|
|
6
|
-
|
|
7
|
-
//#region ../../internals/tanstack-query/src/types.d.ts
|
|
8
|
-
type ParamsCasing = 'camelcase' | undefined;
|
|
9
|
-
type Transformer = (props: {
|
|
10
|
-
node: ast.OperationNode;
|
|
11
|
-
casing: ParamsCasing;
|
|
12
|
-
}) => unknown[];
|
|
13
|
-
//#endregion
|
|
14
|
-
//#region ../../internals/tanstack-query/src/components/QueryKey.d.ts
|
|
15
|
-
type Props = {
|
|
16
|
-
name: string;
|
|
17
|
-
typeName: string;
|
|
18
|
-
node: ast.OperationNode;
|
|
19
|
-
tsResolver: PluginTs['resolver'];
|
|
20
|
-
paramsCasing: 'camelcase' | undefined;
|
|
21
|
-
pathParamsType: 'object' | 'inline';
|
|
22
|
-
transformer: Transformer | null | undefined;
|
|
23
|
-
};
|
|
24
|
-
declare function QueryKey$1({
|
|
25
|
-
name,
|
|
26
|
-
node,
|
|
27
|
-
tsResolver,
|
|
28
|
-
paramsCasing,
|
|
29
|
-
pathParamsType,
|
|
30
|
-
typeName,
|
|
31
|
-
transformer
|
|
32
|
-
}: Props): KubbReactNode;
|
|
33
|
-
//#endregion
|
|
34
|
-
//#region src/types.d.ts
|
|
35
|
-
/**
|
|
36
|
-
* Resolver for SWR that provides naming methods for hook functions.
|
|
37
|
-
*/
|
|
38
|
-
type ResolverSwr = Resolver & {
|
|
39
|
-
/**
|
|
40
|
-
* Resolves the base function name for an operation.
|
|
41
|
-
*
|
|
42
|
-
* @example Resolving base operation names
|
|
43
|
-
* `resolver.resolveName('show pet by id') // -> 'showPetById'`
|
|
44
|
-
*/
|
|
45
|
-
resolveName(this: ResolverSwr, name: string): string;
|
|
46
|
-
/**
|
|
47
|
-
* Resolves the output file name for a hook module.
|
|
48
|
-
*/
|
|
49
|
-
resolvePathName(this: ResolverSwr, name: string, type?: 'file' | 'function' | 'type' | 'const'): string;
|
|
50
|
-
/**
|
|
51
|
-
* Resolves a query hook function name.
|
|
52
|
-
*/
|
|
53
|
-
resolveQueryName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
54
|
-
/**
|
|
55
|
-
* Resolves a mutation hook function name.
|
|
56
|
-
*/
|
|
57
|
-
resolveMutationName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
58
|
-
/**
|
|
59
|
-
* Resolves the query options helper name.
|
|
60
|
-
*/
|
|
61
|
-
resolveQueryOptionsName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
62
|
-
/**
|
|
63
|
-
* Resolves the query key helper name.
|
|
64
|
-
*/
|
|
65
|
-
resolveQueryKeyName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
66
|
-
/**
|
|
67
|
-
* Resolves the mutation key helper name.
|
|
68
|
-
*/
|
|
69
|
-
resolveMutationKeyName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
70
|
-
/**
|
|
71
|
-
* Resolves the query key type name.
|
|
72
|
-
*/
|
|
73
|
-
resolveQueryKeyTypeName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
74
|
-
/**
|
|
75
|
-
* Resolves the mutation key type name.
|
|
76
|
-
*/
|
|
77
|
-
resolveMutationKeyTypeName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
78
|
-
/**
|
|
79
|
-
* Resolves the mutation argument type name emitted alongside the mutation hook.
|
|
80
|
-
*/
|
|
81
|
-
resolveMutationArgTypeName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
82
|
-
/**
|
|
83
|
-
* Resolves the client function name generated inline by query hooks.
|
|
84
|
-
*/
|
|
85
|
-
resolveClientName(this: ResolverSwr, node: ast.OperationNode): string;
|
|
86
|
-
};
|
|
87
|
-
/**
|
|
88
|
-
* Customize the queryKey.
|
|
89
|
-
*/
|
|
90
|
-
type QueryKey = Transformer;
|
|
91
|
-
/**
|
|
92
|
-
* Customize the mutationKey.
|
|
93
|
-
*/
|
|
94
|
-
type MutationKey = Transformer;
|
|
95
|
-
type Query = {
|
|
96
|
-
/**
|
|
97
|
-
* HTTP methods to use for queries.
|
|
98
|
-
*
|
|
99
|
-
* @default ['get']
|
|
100
|
-
*/
|
|
101
|
-
methods?: Array<string>;
|
|
102
|
-
/**
|
|
103
|
-
* Path to the useSWR hook for useSWR functionality.
|
|
104
|
-
* Used as `import useSWR from '${importPath}'`.
|
|
105
|
-
* Accepts relative and absolute paths.
|
|
106
|
-
* Path is used as-is; relative paths are based on the generated file location.
|
|
107
|
-
* @default 'swr'
|
|
108
|
-
*/
|
|
109
|
-
importPath?: string;
|
|
110
|
-
};
|
|
111
|
-
type Mutation = {
|
|
112
|
-
/**
|
|
113
|
-
* HTTP methods to use for mutations.
|
|
114
|
-
*
|
|
115
|
-
* @default ['post', 'put', 'delete', 'patch']
|
|
116
|
-
*/
|
|
117
|
-
methods?: Array<string>;
|
|
118
|
-
/**
|
|
119
|
-
* Path to the useSWRMutation hook for useSWRMutation functionality.
|
|
120
|
-
* Used as `import useSWRMutation from '${importPath}'`.
|
|
121
|
-
* Accepts relative and absolute paths.
|
|
122
|
-
* Path is used as-is; relative paths are based on the generated file location.
|
|
123
|
-
* @default 'swr/mutation'
|
|
124
|
-
*/
|
|
125
|
-
importPath?: string;
|
|
126
|
-
};
|
|
127
|
-
/**
|
|
128
|
-
* Where the generated SWR hooks are written and how they are exported, plus the optional `group`
|
|
129
|
-
* strategy. The `group` option organizes `output.mode: 'directory'` output into per-tag or per-path subdirectories.
|
|
130
|
-
*
|
|
131
|
-
* @default { path: 'hooks', barrel: { type: 'named' } }
|
|
132
|
-
*/
|
|
133
|
-
type Options = OutputOptions & {
|
|
134
|
-
client?: ClientImportPath & Pick<PluginClient['options'], 'clientType' | 'dataReturnType' | 'baseURL' | 'bundle' | 'paramsCasing'>;
|
|
135
|
-
/**
|
|
136
|
-
* Tags, operations, or paths to exclude from generation.
|
|
137
|
-
*/
|
|
138
|
-
exclude?: Array<Exclude>;
|
|
139
|
-
/**
|
|
140
|
-
* Tags, operations, or paths to include in generation.
|
|
141
|
-
*/
|
|
142
|
-
include?: Array<Include>;
|
|
143
|
-
/**
|
|
144
|
-
* Override options for specific tags, operations, or paths.
|
|
145
|
-
*/
|
|
146
|
-
override?: Array<Override<ResolvedOptions>>;
|
|
147
|
-
/**
|
|
148
|
-
* Apply casing to parameter names.
|
|
149
|
-
*/
|
|
150
|
-
paramsCasing?: 'camelcase';
|
|
151
|
-
/**
|
|
152
|
-
* How parameters are passed: grouped in an object or spread inline.
|
|
153
|
-
*
|
|
154
|
-
* @default 'inline'
|
|
155
|
-
*/
|
|
156
|
-
paramsType?: 'object' | 'inline';
|
|
157
|
-
/**
|
|
158
|
-
* How path parameters are passed: grouped in an object or spread inline.
|
|
159
|
-
*
|
|
160
|
-
* @default 'inline'
|
|
161
|
-
*/
|
|
162
|
-
pathParamsType?: PluginClient['options']['pathParamsType'];
|
|
163
|
-
queryKey?: QueryKey;
|
|
164
|
-
/**
|
|
165
|
-
* Configure useSWR behavior.
|
|
166
|
-
*/
|
|
167
|
-
query?: Partial<Query> | false;
|
|
168
|
-
mutationKey?: MutationKey;
|
|
169
|
-
/**
|
|
170
|
-
* Configure useSWRMutation behavior.
|
|
171
|
-
*/
|
|
172
|
-
mutation?: Partial<Mutation> | false;
|
|
173
|
-
/**
|
|
174
|
-
* Parser to use for validating response data.
|
|
175
|
-
*/
|
|
176
|
-
parser?: PluginClient['options']['parser'];
|
|
177
|
-
/**
|
|
178
|
-
* Override naming conventions for function names and types.
|
|
179
|
-
*/
|
|
180
|
-
resolver?: Partial<ResolverSwr> & ThisType<ResolverSwr>;
|
|
181
|
-
/**
|
|
182
|
-
* AST visitor to transform generated nodes.
|
|
183
|
-
*/
|
|
184
|
-
transformer?: ast.Visitor;
|
|
185
|
-
/**
|
|
186
|
-
* Additional generators alongside the default generators.
|
|
187
|
-
*/
|
|
188
|
-
generators?: Array<Generator<PluginSwr>>;
|
|
189
|
-
};
|
|
190
|
-
type ResolvedOptions = {
|
|
191
|
-
output: Output;
|
|
192
|
-
group: Group | undefined;
|
|
193
|
-
exclude: NonNullable<Options['exclude']>;
|
|
194
|
-
include: Options['include'];
|
|
195
|
-
override: NonNullable<Options['override']>;
|
|
196
|
-
client: Pick<PluginClient['options'], 'client' | 'clientType' | 'dataReturnType' | 'importPath' | 'baseURL' | 'bundle' | 'paramsCasing'>;
|
|
197
|
-
parser: NonNullable<Options['parser']>;
|
|
198
|
-
pathParamsType: NonNullable<Options['pathParamsType']>;
|
|
199
|
-
paramsCasing: Options['paramsCasing'];
|
|
200
|
-
paramsType: NonNullable<Options['paramsType']>;
|
|
201
|
-
queryKey: QueryKey | undefined;
|
|
202
|
-
query: NonNullable<Required<Query>> | false;
|
|
203
|
-
mutationKey: MutationKey | undefined;
|
|
204
|
-
mutation: NonNullable<Required<Mutation>> | false;
|
|
205
|
-
resolver: ResolverSwr;
|
|
206
|
-
};
|
|
207
|
-
type PluginSwr = PluginFactoryOptions<'plugin-swr', Options, ResolvedOptions, ResolverSwr>;
|
|
208
|
-
declare global {
|
|
209
|
-
namespace Kubb {
|
|
210
|
-
interface PluginRegistry {
|
|
211
|
-
'plugin-swr': PluginSwr;
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
//#endregion
|
|
216
|
-
export { Transformer as i, PluginSwr as n, QueryKey$1 as r, Options as t };
|
|
217
|
-
//# sourceMappingURL=types-Dz_j0Bqv.d.ts.map
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, Function, Type } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import type { PluginSwr } from '../types.ts'
|
|
7
|
-
import { buildRequestConfigType, buildStatusUnionType, getComments, resolveErrorNames } from '../utils.ts'
|
|
8
|
-
|
|
9
|
-
type Props = {
|
|
10
|
-
name: string
|
|
11
|
-
clientName: string
|
|
12
|
-
mutationKeyName: string
|
|
13
|
-
mutationKeyTypeName: string
|
|
14
|
-
mutationArgTypeName: string
|
|
15
|
-
node: ast.OperationNode
|
|
16
|
-
tsResolver: ResolverTs
|
|
17
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
18
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
19
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
20
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
24
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
25
|
-
const keysPrinter = functionPrinter({ mode: 'keys' })
|
|
26
|
-
|
|
27
|
-
function createMutationArgParams(
|
|
28
|
-
node: ast.OperationNode,
|
|
29
|
-
options: {
|
|
30
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
31
|
-
resolver: ResolverTs
|
|
32
|
-
},
|
|
33
|
-
): ast.FunctionParametersNode {
|
|
34
|
-
return ast.createOperationParams(node, {
|
|
35
|
-
paramsType: 'inline',
|
|
36
|
-
pathParamsType: 'inline',
|
|
37
|
-
paramsCasing: options.paramsCasing,
|
|
38
|
-
resolver: options.resolver,
|
|
39
|
-
})
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function buildMutationParamsNode(
|
|
43
|
-
node: ast.OperationNode,
|
|
44
|
-
options: {
|
|
45
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
46
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
47
|
-
mutationKeyTypeName: string
|
|
48
|
-
mutationArgTypeName: string
|
|
49
|
-
resolver: ResolverTs
|
|
50
|
-
},
|
|
51
|
-
): ast.FunctionParametersNode {
|
|
52
|
-
const { dataReturnType, mutationKeyTypeName, mutationArgTypeName, resolver } = options
|
|
53
|
-
const responseName = resolver.resolveResponseName(node)
|
|
54
|
-
const errorNames = resolveErrorNames(node, resolver)
|
|
55
|
-
|
|
56
|
-
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
|
|
57
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
58
|
-
|
|
59
|
-
return ast.createFunctionParameters({
|
|
60
|
-
params: [
|
|
61
|
-
ast.createFunctionParameter({
|
|
62
|
-
name: 'options',
|
|
63
|
-
type: ast.createParamsType({
|
|
64
|
-
variant: 'reference',
|
|
65
|
-
name: `{
|
|
66
|
-
mutation?: SWRMutationConfiguration<${TData}, ${TError}, ${mutationKeyTypeName} | null, ${mutationArgTypeName}> & { throwOnError?: boolean },
|
|
67
|
-
client?: ${buildRequestConfigType(node, resolver)},
|
|
68
|
-
shouldFetch?: boolean,
|
|
69
|
-
}`,
|
|
70
|
-
}),
|
|
71
|
-
default: '{}',
|
|
72
|
-
}),
|
|
73
|
-
],
|
|
74
|
-
})
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function Mutation({
|
|
78
|
-
name,
|
|
79
|
-
clientName,
|
|
80
|
-
mutationKeyName,
|
|
81
|
-
mutationKeyTypeName,
|
|
82
|
-
mutationArgTypeName,
|
|
83
|
-
paramsCasing,
|
|
84
|
-
paramsType,
|
|
85
|
-
pathParamsType,
|
|
86
|
-
dataReturnType,
|
|
87
|
-
node,
|
|
88
|
-
tsResolver,
|
|
89
|
-
}: Props): KubbReactNode {
|
|
90
|
-
const responseName = tsResolver.resolveResponseName(node)
|
|
91
|
-
const errorNames = resolveErrorNames(node, tsResolver)
|
|
92
|
-
|
|
93
|
-
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
|
|
94
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
95
|
-
|
|
96
|
-
const mutationArgParamsNode = createMutationArgParams(node, { paramsCasing, resolver: tsResolver })
|
|
97
|
-
const hasMutationParams = mutationArgParamsNode.params.length > 0
|
|
98
|
-
const argTypeBody = hasMutationParams ? (declarationPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
99
|
-
const argKeysStr = hasMutationParams ? (keysPrinter.print(mutationArgParamsNode) ?? '') : ''
|
|
100
|
-
|
|
101
|
-
const generics = [TData, TError, `${mutationKeyTypeName} | null`, mutationArgTypeName]
|
|
102
|
-
|
|
103
|
-
const paramsNode = buildMutationParamsNode(node, {
|
|
104
|
-
paramsCasing,
|
|
105
|
-
dataReturnType,
|
|
106
|
-
mutationKeyTypeName,
|
|
107
|
-
mutationArgTypeName,
|
|
108
|
-
resolver: tsResolver,
|
|
109
|
-
})
|
|
110
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
111
|
-
|
|
112
|
-
const clientCallParamsNode = ast.createOperationParams(node, {
|
|
113
|
-
paramsType,
|
|
114
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
115
|
-
paramsCasing,
|
|
116
|
-
resolver: tsResolver,
|
|
117
|
-
extraParams: [
|
|
118
|
-
ast.createFunctionParameter({
|
|
119
|
-
name: 'config',
|
|
120
|
-
type: ast.createParamsType({
|
|
121
|
-
variant: 'reference',
|
|
122
|
-
name: buildRequestConfigType(node, tsResolver),
|
|
123
|
-
}),
|
|
124
|
-
default: '{}',
|
|
125
|
-
}),
|
|
126
|
-
],
|
|
127
|
-
})
|
|
128
|
-
const clientCallStr = callPrinter.print(clientCallParamsNode) ?? ''
|
|
129
|
-
|
|
130
|
-
return (
|
|
131
|
-
<>
|
|
132
|
-
<File.Source name={mutationArgTypeName} isExportable isIndexable isTypeOnly>
|
|
133
|
-
<Type name={mutationArgTypeName} export>
|
|
134
|
-
{hasMutationParams ? `{ ${argTypeBody} }` : 'never'}
|
|
135
|
-
</Type>
|
|
136
|
-
</File.Source>
|
|
137
|
-
<File.Source name={name} isExportable isIndexable>
|
|
138
|
-
<Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
|
|
139
|
-
{`
|
|
140
|
-
const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {}
|
|
141
|
-
const mutationKey = ${mutationKeyName}()
|
|
142
|
-
|
|
143
|
-
return useSWRMutation<${generics.join(', ')}>(
|
|
144
|
-
shouldFetch ? mutationKey : null,
|
|
145
|
-
async (_url${hasMutationParams ? `, { arg: { ${argKeysStr} } }` : ''}) => {
|
|
146
|
-
return ${clientName}(${clientCallStr})
|
|
147
|
-
},
|
|
148
|
-
mutationOptions
|
|
149
|
-
)
|
|
150
|
-
`}
|
|
151
|
-
</Function>
|
|
152
|
-
</File.Source>
|
|
153
|
-
</>
|
|
154
|
-
)
|
|
155
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import type { Transformer } from '@internals/tanstack-query'
|
|
2
|
-
import { MutationKey as SharedMutationKey } from '@internals/tanstack-query'
|
|
3
|
-
import type { ast } from '@kubb/core'
|
|
4
|
-
import { File, Type } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
|
|
7
|
-
type Props = {
|
|
8
|
-
name: string
|
|
9
|
-
typeName: string
|
|
10
|
-
node: ast.OperationNode
|
|
11
|
-
paramsCasing: 'camelcase' | undefined
|
|
12
|
-
pathParamsType: 'object' | 'inline'
|
|
13
|
-
transformer: Transformer | undefined
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function MutationKey({ name, typeName, node, paramsCasing, pathParamsType, transformer }: Props): KubbReactNode {
|
|
17
|
-
return (
|
|
18
|
-
<>
|
|
19
|
-
<SharedMutationKey name={name} node={node} paramsCasing={paramsCasing} pathParamsType={pathParamsType} transformer={transformer} />
|
|
20
|
-
<File.Source name={typeName} isExportable isIndexable isTypeOnly>
|
|
21
|
-
<Type export name={typeName}>{`ReturnType<typeof ${name}>`}</Type>
|
|
22
|
-
</File.Source>
|
|
23
|
-
</>
|
|
24
|
-
)
|
|
25
|
-
}
|
package/src/components/Query.tsx
DELETED
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import { ast } from '@kubb/core'
|
|
2
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
|
|
7
|
-
import type { PluginSwr } from '../types.ts'
|
|
8
|
-
import { buildQueryKeyParams, buildStatusUnionType, getComments, resolveErrorNames } from '../utils.ts'
|
|
9
|
-
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
10
|
-
|
|
11
|
-
type Props = {
|
|
12
|
-
name: string
|
|
13
|
-
queryOptionsName: string
|
|
14
|
-
queryKeyName: string
|
|
15
|
-
queryKeyTypeName: string
|
|
16
|
-
node: ast.OperationNode
|
|
17
|
-
tsResolver: ResolverTs
|
|
18
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
19
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
20
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
21
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
25
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
26
|
-
|
|
27
|
-
function buildQueryParamsNode(
|
|
28
|
-
node: ast.OperationNode,
|
|
29
|
-
options: {
|
|
30
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
31
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
32
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
33
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
34
|
-
resolver: ResolverTs
|
|
35
|
-
},
|
|
36
|
-
): ast.FunctionParametersNode {
|
|
37
|
-
const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
|
|
38
|
-
const responseName = resolver.resolveResponseName(node)
|
|
39
|
-
const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
|
|
40
|
-
const errorNames = resolveErrorNames(node, resolver)
|
|
41
|
-
|
|
42
|
-
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
|
|
43
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
44
|
-
|
|
45
|
-
const optionsParam = ast.createFunctionParameter({
|
|
46
|
-
name: 'options',
|
|
47
|
-
type: ast.createParamsType({
|
|
48
|
-
variant: 'reference',
|
|
49
|
-
name: `{
|
|
50
|
-
query?: SWRConfiguration<${[TData, TError].join(', ')}>,
|
|
51
|
-
client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
|
|
52
|
-
shouldFetch?: boolean,
|
|
53
|
-
immutable?: boolean
|
|
54
|
-
}`,
|
|
55
|
-
}),
|
|
56
|
-
default: '{}',
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
return ast.createOperationParams(node, {
|
|
60
|
-
paramsType,
|
|
61
|
-
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
62
|
-
paramsCasing,
|
|
63
|
-
resolver,
|
|
64
|
-
extraParams: [optionsParam],
|
|
65
|
-
})
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export function Query({
|
|
69
|
-
name,
|
|
70
|
-
queryKeyTypeName,
|
|
71
|
-
queryOptionsName,
|
|
72
|
-
queryKeyName,
|
|
73
|
-
paramsType,
|
|
74
|
-
paramsCasing,
|
|
75
|
-
pathParamsType,
|
|
76
|
-
dataReturnType,
|
|
77
|
-
node,
|
|
78
|
-
tsResolver,
|
|
79
|
-
}: Props): KubbReactNode {
|
|
80
|
-
const responseName = tsResolver.resolveResponseName(node)
|
|
81
|
-
const errorNames = resolveErrorNames(node, tsResolver)
|
|
82
|
-
|
|
83
|
-
const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
|
|
84
|
-
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
85
|
-
const generics = [TData, TError, `${queryKeyTypeName} | null`]
|
|
86
|
-
|
|
87
|
-
const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
88
|
-
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
|
|
89
|
-
const enabledNames = getEnabledParamNames(queryKeyParamsNode)
|
|
90
|
-
|
|
91
|
-
const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
92
|
-
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
|
|
93
|
-
|
|
94
|
-
const paramsNode = markParamsOptional(
|
|
95
|
-
buildQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver }),
|
|
96
|
-
enabledNames,
|
|
97
|
-
)
|
|
98
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
99
|
-
|
|
100
|
-
// SWR has no `enabled` option; fold the param-presence check into the null-key gate
|
|
101
|
-
// so passing `undefined` for a required param disables the request (mirrors React Query #60).
|
|
102
|
-
const shouldFetchExpr = enabledNames.length ? `shouldFetch && !!(${enabledNames.join(' && ')})` : 'shouldFetch'
|
|
103
|
-
|
|
104
|
-
return (
|
|
105
|
-
<File.Source name={name} isExportable isIndexable>
|
|
106
|
-
<Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
|
|
107
|
-
{`
|
|
108
|
-
const { query: queryOptions, client: config = {}, shouldFetch = true, immutable } = options ?? {}
|
|
109
|
-
|
|
110
|
-
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
111
|
-
|
|
112
|
-
return useSWR<${generics.join(', ')}>(
|
|
113
|
-
${shouldFetchExpr} ? queryKey : null,
|
|
114
|
-
{
|
|
115
|
-
...${queryOptionsName}(${queryOptionsParamsCall}),
|
|
116
|
-
...(immutable ? {
|
|
117
|
-
revalidateIfStale: false,
|
|
118
|
-
revalidateOnFocus: false,
|
|
119
|
-
revalidateOnReconnect: false
|
|
120
|
-
} : { }),
|
|
121
|
-
...queryOptions,
|
|
122
|
-
}
|
|
123
|
-
)
|
|
124
|
-
`}
|
|
125
|
-
</Function>
|
|
126
|
-
</File.Source>
|
|
127
|
-
)
|
|
128
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { QueryKey } from '@internals/tanstack-query'
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
import type { ast } from '@kubb/core'
|
|
2
|
-
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
3
|
-
import { functionPrinter } from '@kubb/plugin-ts'
|
|
4
|
-
import { File, Function } from '@kubb/renderer-jsx'
|
|
5
|
-
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
6
|
-
import { buildQueryOptionsParams, getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
|
|
7
|
-
import type { PluginSwr } from '../types.ts'
|
|
8
|
-
import { buildQueryKeyParams } from '../utils.ts'
|
|
9
|
-
|
|
10
|
-
type Props = {
|
|
11
|
-
name: string
|
|
12
|
-
clientName: string
|
|
13
|
-
node: ast.OperationNode
|
|
14
|
-
tsResolver: ResolverTs
|
|
15
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
16
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
17
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
21
|
-
const callPrinter = functionPrinter({ mode: 'call' })
|
|
22
|
-
|
|
23
|
-
export function getQueryOptionsParams(
|
|
24
|
-
node: ast.OperationNode,
|
|
25
|
-
options: {
|
|
26
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
27
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
28
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
29
|
-
resolver: ResolverTs
|
|
30
|
-
},
|
|
31
|
-
): ast.FunctionParametersNode {
|
|
32
|
-
return buildQueryOptionsParams(node, options)
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function QueryOptions({ name, clientName, node, tsResolver, paramsCasing, paramsType, pathParamsType }: Props): KubbReactNode {
|
|
36
|
-
const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
37
|
-
const enabledNames = getEnabledParamNames(queryKeyParamsNode)
|
|
38
|
-
|
|
39
|
-
const paramsNode = markParamsOptional(getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver }), enabledNames)
|
|
40
|
-
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
41
|
-
const clientCallStr = injectNonNullAssertions(callPrinter.print(paramsNode) ?? '', enabledNames)
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
<File.Source name={name} isExportable isIndexable>
|
|
45
|
-
<Function name={name} export params={paramsSignature}>
|
|
46
|
-
{`
|
|
47
|
-
return {
|
|
48
|
-
fetcher: async () => {
|
|
49
|
-
return ${clientName}(${clientCallStr})
|
|
50
|
-
},
|
|
51
|
-
}
|
|
52
|
-
`}
|
|
53
|
-
</Function>
|
|
54
|
-
</File.Source>
|
|
55
|
-
)
|
|
56
|
-
}
|
package/src/components/index.ts
DELETED
package/src/generators/index.ts
DELETED