@kubb/plugin-swr 5.0.0-alpha.33 → 5.0.0-alpha.35
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/dist/components-BJSzUg7M.cjs +955 -0
- package/dist/components-BJSzUg7M.cjs.map +1 -0
- package/dist/components-JQ2KRFCa.js +877 -0
- package/dist/components-JQ2KRFCa.js.map +1 -0
- package/dist/components.cjs +1 -1
- package/dist/components.d.ts +77 -37
- package/dist/components.js +1 -1
- package/dist/generators-17ulS9mu.cjs +537 -0
- package/dist/generators-17ulS9mu.cjs.map +1 -0
- package/dist/generators-Cl7nr-FB.js +526 -0
- package/dist/generators-Cl7nr-FB.js.map +1 -0
- package/dist/generators.cjs +1 -1
- package/dist/generators.d.ts +4 -4
- package/dist/generators.js +1 -1
- package/dist/index.cjs +132 -110
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +22 -2
- package/dist/index.js +132 -110
- package/dist/index.js.map +1 -1
- package/dist/{types-BVDtH9S7.d.ts → types-FA5mH9Ch.d.ts} +46 -90
- package/package.json +7 -11
- package/src/components/Mutation.tsx +165 -170
- package/src/components/MutationKey.tsx +50 -1
- package/src/components/Query.tsx +122 -126
- package/src/components/QueryKey.tsx +65 -1
- package/src/components/QueryOptions.tsx +38 -93
- package/src/generators/mutationGenerator.tsx +194 -117
- package/src/generators/queryGenerator.tsx +205 -139
- package/src/plugin.ts +117 -152
- package/src/resolvers/resolverSwr.ts +26 -0
- package/src/resolvers/resolverSwrLegacy.ts +17 -0
- package/src/types.ts +55 -18
- package/src/utils.ts +209 -0
- package/dist/components-DaCTPplv.js +0 -756
- package/dist/components-DaCTPplv.js.map +0 -1
- package/dist/components-Qs8_faOt.cjs +0 -834
- package/dist/components-Qs8_faOt.cjs.map +0 -1
- package/dist/generators-0YayIrse.js +0 -400
- package/dist/generators-0YayIrse.js.map +0 -1
- package/dist/generators-Bd4rCa3l.cjs +0 -411
- package/dist/generators-Bd4rCa3l.cjs.map +0 -1
package/src/components/Query.tsx
CHANGED
|
@@ -1,184 +1,180 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
3
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
5
4
|
import { File, Function } from '@kubb/renderer-jsx'
|
|
6
5
|
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
6
|
import type { PluginSwr } from '../types.ts'
|
|
7
|
+
import { buildGroupParam, getComments, resolveErrorNames, resolvePathParamType, resolveQueryGroupType } from '../utils.ts'
|
|
8
8
|
import { QueryKey } from './QueryKey.tsx'
|
|
9
|
-
import {
|
|
9
|
+
import { getQueryOptionsParams } from './QueryOptions.tsx'
|
|
10
10
|
|
|
11
11
|
type Props = {
|
|
12
|
-
/**
|
|
13
|
-
* Name of the function
|
|
14
|
-
*/
|
|
15
12
|
name: string
|
|
16
13
|
queryOptionsName: string
|
|
17
14
|
queryKeyName: string
|
|
18
15
|
queryKeyTypeName: string
|
|
19
|
-
|
|
16
|
+
node: ast.OperationNode
|
|
17
|
+
tsResolver: PluginTs['resolver']
|
|
20
18
|
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
21
19
|
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
22
20
|
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
23
21
|
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
24
|
-
operation: Operation
|
|
25
22
|
}
|
|
26
23
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
30
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
31
|
-
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
32
|
-
typeSchemas: OperationSchemas
|
|
33
|
-
}
|
|
24
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
25
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
34
26
|
|
|
35
|
-
function getParams(
|
|
36
|
-
|
|
37
|
-
|
|
27
|
+
function getParams(
|
|
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: PluginTs['resolver']
|
|
35
|
+
},
|
|
36
|
+
): ast.FunctionParametersNode {
|
|
37
|
+
const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
54
|
-
}
|
|
55
|
-
: undefined,
|
|
56
|
-
headers: typeSchemas.headerParams?.name
|
|
57
|
-
? {
|
|
58
|
-
type: typeSchemas.headerParams?.name,
|
|
59
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
60
|
-
}
|
|
61
|
-
: undefined,
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
// Check if all children are optional or undefined
|
|
65
|
-
const allChildrenAreOptional = Object.values(children).every((child) => !child || child.optional)
|
|
66
|
-
|
|
67
|
-
return FunctionParams.factory({
|
|
68
|
-
data: {
|
|
69
|
-
mode: 'object',
|
|
70
|
-
children,
|
|
71
|
-
default: allChildrenAreOptional ? '{}' : undefined,
|
|
72
|
-
},
|
|
73
|
-
options: {
|
|
74
|
-
type: `
|
|
75
|
-
{
|
|
76
|
-
query?: Parameters<typeof useSWR<${[TData, TError].join(', ')}>>[2],
|
|
77
|
-
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
|
|
39
|
+
const responseName = resolver.resolveResponseName(node)
|
|
40
|
+
const requestName = node.requestBody?.schema ? resolver.resolveDataName(node) : undefined
|
|
41
|
+
const errorNames = resolveErrorNames(node, resolver)
|
|
42
|
+
|
|
43
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
44
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
45
|
+
|
|
46
|
+
const optionsParam = ast.createFunctionParameter({
|
|
47
|
+
name: 'options',
|
|
48
|
+
type: ast.createParamsType({
|
|
49
|
+
variant: 'reference',
|
|
50
|
+
name: `{
|
|
51
|
+
query?: Parameters<typeof useSWR<${TData}, ${TError}>>[2],
|
|
52
|
+
client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
|
|
78
53
|
shouldFetch?: boolean,
|
|
79
54
|
immutable?: boolean
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
55
|
+
}`,
|
|
56
|
+
}),
|
|
57
|
+
default: '{}',
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
if (paramsType === 'object') {
|
|
61
|
+
// Use createOperationParams for the grouped params (path + body + query + headers),
|
|
62
|
+
// but replace the config param with SWR's options param
|
|
63
|
+
const baseParams = ast.createOperationParams(node, {
|
|
64
|
+
paramsType: 'object',
|
|
65
|
+
pathParamsType: 'object',
|
|
66
|
+
paramsCasing,
|
|
67
|
+
resolver,
|
|
68
|
+
extraParams: [],
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
return ast.createFunctionParameters({
|
|
72
|
+
params: [...baseParams.params, optionsParam],
|
|
84
73
|
})
|
|
85
74
|
}
|
|
86
75
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
76
|
+
// Inline params: build path + body + query (NO headers for query hooks) + options
|
|
77
|
+
const casedParams = ast.caseParams(node.parameters, paramsCasing)
|
|
78
|
+
const pathParams = casedParams.filter((p) => p.in === 'path')
|
|
79
|
+
const queryParams = casedParams.filter((p) => p.in === 'query')
|
|
80
|
+
const headerParams = casedParams.filter((p) => p.in === 'header')
|
|
81
|
+
|
|
82
|
+
const queryGroupType = resolveQueryGroupType(node, queryParams, resolver)
|
|
83
|
+
|
|
84
|
+
const bodyType = node.requestBody?.schema ? ast.createParamsType({ variant: 'reference', name: resolver.resolveDataName(node) }) : undefined
|
|
85
|
+
const bodyRequired = node.requestBody?.required ?? false
|
|
86
|
+
|
|
87
|
+
const params: Array<ast.FunctionParameterNode | ast.ParameterGroupNode> = []
|
|
88
|
+
|
|
89
|
+
// Path params
|
|
90
|
+
if (pathParams.length) {
|
|
91
|
+
const pathChildren = pathParams.map((p) =>
|
|
92
|
+
ast.createFunctionParameter({ name: p.name, type: resolvePathParamType(node, p, resolver), optional: !p.required }),
|
|
93
|
+
)
|
|
94
|
+
params.push({
|
|
95
|
+
kind: 'ParameterGroup',
|
|
96
|
+
properties: pathChildren,
|
|
97
|
+
inline: pathParamsType === 'inline',
|
|
98
|
+
default: pathChildren.every((c) => c.optional) ? '{}' : undefined,
|
|
99
|
+
})
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Body
|
|
103
|
+
if (bodyType) {
|
|
104
|
+
params.push(ast.createFunctionParameter({ name: 'data', type: bodyType, optional: !bodyRequired }))
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Query params
|
|
108
|
+
params.push(...buildGroupParam('params', node, queryParams, queryGroupType, resolver))
|
|
109
|
+
|
|
110
|
+
// Header params (included for the query hook params, consistent with old behavior)
|
|
111
|
+
const headerGroupType = node.parameters.some((p) => p.in === 'header')
|
|
112
|
+
? (() => {
|
|
113
|
+
const hParams = casedParams.filter((p) => p.in === 'header')
|
|
114
|
+
const firstParam = hParams[0]!
|
|
115
|
+
const groupName = resolver.resolveHeaderParamsName(node, firstParam)
|
|
116
|
+
const individualName = resolver.resolveParamName(node, firstParam)
|
|
117
|
+
if (groupName !== individualName) {
|
|
118
|
+
return { type: ast.createParamsType({ variant: 'reference', name: groupName }), optional: hParams.every((p) => !p.required) }
|
|
111
119
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}
|
|
121
|
-
`,
|
|
122
|
-
default: '{}',
|
|
123
|
-
},
|
|
124
|
-
})
|
|
120
|
+
return undefined
|
|
121
|
+
})()
|
|
122
|
+
: undefined
|
|
123
|
+
params.push(...buildGroupParam('headers', node, headerParams, headerGroupType, resolver))
|
|
124
|
+
|
|
125
|
+
// SWR options
|
|
126
|
+
params.push(optionsParam)
|
|
127
|
+
|
|
128
|
+
return ast.createFunctionParameters({ params })
|
|
125
129
|
}
|
|
126
130
|
|
|
127
131
|
export function Query({
|
|
128
132
|
name,
|
|
129
|
-
|
|
133
|
+
node,
|
|
134
|
+
tsResolver,
|
|
130
135
|
queryKeyName,
|
|
131
136
|
queryKeyTypeName,
|
|
132
137
|
queryOptionsName,
|
|
133
|
-
operation,
|
|
134
138
|
dataReturnType,
|
|
135
139
|
paramsType,
|
|
136
140
|
paramsCasing,
|
|
137
141
|
pathParamsType,
|
|
138
142
|
}: Props): KubbReactNode {
|
|
139
|
-
const
|
|
140
|
-
const
|
|
143
|
+
const responseName = tsResolver.resolveResponseName(node)
|
|
144
|
+
const errorNames = resolveErrorNames(node, tsResolver)
|
|
145
|
+
|
|
146
|
+
const TData = dataReturnType === 'data' ? responseName : `ResponseConfig<${responseName}>`
|
|
147
|
+
const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
|
|
141
148
|
const generics = [TData, TError, `${queryKeyTypeName} | null`]
|
|
142
149
|
|
|
143
|
-
const
|
|
144
|
-
|
|
145
|
-
typeSchemas,
|
|
146
|
-
paramsCasing,
|
|
147
|
-
})
|
|
148
|
-
const params = getParams({
|
|
149
|
-
paramsCasing,
|
|
150
|
-
paramsType,
|
|
151
|
-
pathParamsType,
|
|
152
|
-
dataReturnType,
|
|
153
|
-
typeSchemas,
|
|
154
|
-
})
|
|
150
|
+
const queryKeyParamsNode = QueryKey.getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
151
|
+
const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
|
|
155
152
|
|
|
156
|
-
const
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
})
|
|
153
|
+
const paramsNode = getParams(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver })
|
|
154
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
155
|
+
|
|
156
|
+
const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
157
|
+
const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
|
|
162
158
|
|
|
163
159
|
return (
|
|
164
160
|
<File.Source name={name} isExportable isIndexable>
|
|
165
161
|
<Function
|
|
166
162
|
name={name}
|
|
167
163
|
export
|
|
168
|
-
params={
|
|
164
|
+
params={paramsSignature}
|
|
169
165
|
JSDoc={{
|
|
170
|
-
comments: getComments(
|
|
166
|
+
comments: getComments(node),
|
|
171
167
|
}}
|
|
172
168
|
>
|
|
173
169
|
{`
|
|
174
170
|
const { query: queryOptions, client: config = {}, shouldFetch = true, immutable } = options ?? {}
|
|
175
171
|
|
|
176
|
-
const queryKey = ${queryKeyName}(${
|
|
172
|
+
const queryKey = ${queryKeyName}(${queryKeyParamsCall})
|
|
177
173
|
|
|
178
174
|
return useSWR<${generics.join(', ')}>(
|
|
179
175
|
shouldFetch ? queryKey : null,
|
|
180
176
|
{
|
|
181
|
-
...${queryOptionsName}(${
|
|
177
|
+
...${queryOptionsName}(${queryOptionsParamsCall}),
|
|
182
178
|
...(immutable ? {
|
|
183
179
|
revalidateIfStale: false,
|
|
184
180
|
revalidateOnFocus: false,
|
|
@@ -1 +1,65 @@
|
|
|
1
|
-
|
|
1
|
+
import { URLPath } from '@internals/utils'
|
|
2
|
+
import type { ast } from '@kubb/core'
|
|
3
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
4
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
5
|
+
import { File, Function, Type } from '@kubb/renderer-jsx'
|
|
6
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
|
+
import type { Transformer } from '../types.ts'
|
|
8
|
+
import { buildQueryKeyParams } from '../utils.ts'
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
name: string
|
|
12
|
+
typeName: string
|
|
13
|
+
node: ast.OperationNode
|
|
14
|
+
tsResolver: PluginTs['resolver']
|
|
15
|
+
paramsCasing: 'camelcase' | undefined
|
|
16
|
+
pathParamsType: 'object' | 'inline'
|
|
17
|
+
transformer: Transformer | undefined
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
21
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
22
|
+
|
|
23
|
+
function getParams(
|
|
24
|
+
node: ast.OperationNode,
|
|
25
|
+
options: { pathParamsType: 'object' | 'inline'; paramsCasing: 'camelcase' | undefined; resolver: PluginTs['resolver'] },
|
|
26
|
+
): ast.FunctionParametersNode {
|
|
27
|
+
return buildQueryKeyParams(node, options)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const getTransformer: Transformer = ({ node, casing }) => {
|
|
31
|
+
const path = new URLPath(node.path, { casing })
|
|
32
|
+
const hasQueryParams = node.parameters.some((p) => p.in === 'query')
|
|
33
|
+
const hasRequestBody = !!node.requestBody?.schema
|
|
34
|
+
|
|
35
|
+
return [
|
|
36
|
+
path.toObject({ type: 'path', stringify: true }),
|
|
37
|
+
hasQueryParams ? '...(params ? [params] : [])' : undefined,
|
|
38
|
+
hasRequestBody ? '...(data ? [data] : [])' : undefined,
|
|
39
|
+
].filter(Boolean) as string[]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function QueryKey({ name, node, tsResolver, paramsCasing, pathParamsType, typeName, transformer = getTransformer }: Props): KubbReactNode {
|
|
43
|
+
const paramsNode = getParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
|
|
44
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
45
|
+
const keys = transformer({ node, casing: paramsCasing })
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<>
|
|
49
|
+
<File.Source name={name} isExportable isIndexable>
|
|
50
|
+
<Function.Arrow name={name} export params={paramsSignature} singleLine>
|
|
51
|
+
{`[${keys.join(', ')}] as const`}
|
|
52
|
+
</Function.Arrow>
|
|
53
|
+
</File.Source>
|
|
54
|
+
<File.Source name={typeName} isExportable isIndexable isTypeOnly>
|
|
55
|
+
<Type name={typeName} export>
|
|
56
|
+
{`ReturnType<typeof ${name}>`}
|
|
57
|
+
</Type>
|
|
58
|
+
</File.Source>
|
|
59
|
+
</>
|
|
60
|
+
)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
QueryKey.getParams = getParams
|
|
64
|
+
QueryKey.getTransformer = getTransformer
|
|
65
|
+
QueryKey.callPrinter = callPrinter
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
5
|
-
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import type { PluginTs } from '@kubb/plugin-ts'
|
|
3
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
6
4
|
import { File, Function } from '@kubb/renderer-jsx'
|
|
7
5
|
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
8
6
|
import type { PluginSwr } from '../types.ts'
|
|
@@ -10,109 +8,58 @@ import type { PluginSwr } from '../types.ts'
|
|
|
10
8
|
type Props = {
|
|
11
9
|
name: string
|
|
12
10
|
clientName: string
|
|
13
|
-
|
|
11
|
+
node: ast.OperationNode
|
|
12
|
+
tsResolver: PluginTs['resolver']
|
|
14
13
|
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
15
14
|
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
16
15
|
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
17
16
|
}
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
22
|
-
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
23
|
-
typeSchemas: OperationSchemas
|
|
24
|
-
}
|
|
18
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
19
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
25
20
|
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
export function getQueryOptionsParams(
|
|
22
|
+
node: ast.OperationNode,
|
|
23
|
+
options: {
|
|
24
|
+
paramsType: PluginSwr['resolvedOptions']['paramsType']
|
|
25
|
+
paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
|
|
26
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
27
|
+
resolver: PluginTs['resolver']
|
|
28
|
+
},
|
|
29
|
+
): ast.FunctionParametersNode {
|
|
30
|
+
const { paramsType, paramsCasing, pathParamsType, resolver } = options
|
|
31
|
+
const requestName = node.requestBody?.schema ? resolver.resolveDataName(node) : undefined
|
|
29
32
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
? {
|
|
43
|
-
type: typeSchemas.queryParams?.name,
|
|
44
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
45
|
-
}
|
|
46
|
-
: undefined,
|
|
47
|
-
headers: typeSchemas.headerParams?.name
|
|
48
|
-
? {
|
|
49
|
-
type: typeSchemas.headerParams?.name,
|
|
50
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
51
|
-
}
|
|
52
|
-
: undefined,
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
config: {
|
|
56
|
-
type: typeSchemas.request?.name
|
|
57
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
58
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
33
|
+
return ast.createOperationParams(node, {
|
|
34
|
+
paramsType,
|
|
35
|
+
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
36
|
+
paramsCasing,
|
|
37
|
+
resolver,
|
|
38
|
+
extraParams: [
|
|
39
|
+
ast.createFunctionParameter({
|
|
40
|
+
name: 'config',
|
|
41
|
+
type: ast.createParamsType({
|
|
42
|
+
variant: 'reference',
|
|
43
|
+
name: requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }',
|
|
44
|
+
}),
|
|
59
45
|
default: '{}',
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return FunctionParams.factory({
|
|
65
|
-
pathParams: typeSchemas.pathParams?.name
|
|
66
|
-
? {
|
|
67
|
-
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
68
|
-
children: getPathParams(typeSchemas.pathParams, { typed: true, casing: paramsCasing }),
|
|
69
|
-
default: getDefaultValue(typeSchemas.pathParams?.schema),
|
|
70
|
-
}
|
|
71
|
-
: undefined,
|
|
72
|
-
data: typeSchemas.request?.name
|
|
73
|
-
? {
|
|
74
|
-
type: typeSchemas.request?.name,
|
|
75
|
-
optional: isOptional(typeSchemas.request?.schema),
|
|
76
|
-
}
|
|
77
|
-
: undefined,
|
|
78
|
-
params: typeSchemas.queryParams?.name
|
|
79
|
-
? {
|
|
80
|
-
type: typeSchemas.queryParams?.name,
|
|
81
|
-
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
82
|
-
}
|
|
83
|
-
: undefined,
|
|
84
|
-
headers: typeSchemas.headerParams?.name
|
|
85
|
-
? {
|
|
86
|
-
type: typeSchemas.headerParams?.name,
|
|
87
|
-
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
88
|
-
}
|
|
89
|
-
: undefined,
|
|
90
|
-
config: {
|
|
91
|
-
type: typeSchemas.request?.name
|
|
92
|
-
? `Partial<RequestConfig<${typeSchemas.request?.name}>> & { client?: Client }`
|
|
93
|
-
: 'Partial<RequestConfig> & { client?: Client }',
|
|
94
|
-
default: '{}',
|
|
95
|
-
},
|
|
46
|
+
}),
|
|
47
|
+
],
|
|
96
48
|
})
|
|
97
49
|
}
|
|
98
50
|
|
|
99
|
-
export function QueryOptions({ name, clientName,
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
paramsType,
|
|
104
|
-
typeSchemas,
|
|
105
|
-
pathParamsType,
|
|
106
|
-
isConfigurable: true,
|
|
107
|
-
})
|
|
51
|
+
export function QueryOptions({ name, clientName, node, tsResolver, paramsCasing, paramsType, pathParamsType }: Props): KubbReactNode {
|
|
52
|
+
const paramsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
|
|
53
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
54
|
+
const paramsCall = callPrinter.print(paramsNode) ?? ''
|
|
108
55
|
|
|
109
56
|
return (
|
|
110
57
|
<File.Source name={name} isExportable isIndexable>
|
|
111
|
-
<Function name={name} export params={
|
|
58
|
+
<Function name={name} export params={paramsSignature}>
|
|
112
59
|
{`
|
|
113
60
|
return {
|
|
114
61
|
fetcher: async () => {
|
|
115
|
-
return ${clientName}(${
|
|
62
|
+
return ${clientName}(${paramsCall})
|
|
116
63
|
},
|
|
117
64
|
}
|
|
118
65
|
`}
|
|
@@ -120,5 +67,3 @@ export function QueryOptions({ name, clientName, typeSchemas, paramsCasing, para
|
|
|
120
67
|
</File.Source>
|
|
121
68
|
)
|
|
122
69
|
}
|
|
123
|
-
|
|
124
|
-
QueryOptions.getParams = getParams
|