@kubb/plugin-swr 3.0.0-alpha.2 → 3.0.0-alpha.20
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 +13 -4
- package/dist/chunk-7WG6LPZJ.js +185 -0
- package/dist/chunk-7WG6LPZJ.js.map +1 -0
- package/dist/chunk-DGY6XSP2.js +187 -0
- package/dist/chunk-DGY6XSP2.js.map +1 -0
- package/dist/chunk-RMG5RYPG.cjs +189 -0
- package/dist/chunk-RMG5RYPG.cjs.map +1 -0
- package/dist/chunk-WI3XUYBA.cjs +194 -0
- package/dist/chunk-WI3XUYBA.cjs.map +1 -0
- package/dist/components.cjs +15 -6
- package/dist/components.cjs.map +1 -1
- package/dist/components.d.cts +49 -6
- package/dist/components.d.ts +49 -6
- package/dist/components.js +2 -10
- package/dist/components.js.map +1 -1
- package/dist/generators.cjs +17 -0
- package/dist/generators.cjs.map +1 -0
- package/dist/generators.d.cts +10 -0
- package/dist/generators.d.ts +10 -0
- package/dist/generators.js +4 -0
- package/dist/generators.js.map +1 -0
- package/dist/index.cjs +90 -127
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -4
- package/dist/index.d.ts +1 -4
- package/dist/index.js +70 -114
- package/dist/index.js.map +1 -1
- package/dist/types-BCd4p4VZ.d.cts +127 -0
- package/dist/types-BCd4p4VZ.d.ts +127 -0
- package/package.json +22 -16
- package/src/components/Mutation.tsx +82 -227
- package/src/components/Query.tsx +86 -264
- package/src/components/QueryOptions.tsx +58 -179
- package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +40 -0
- package/src/generators/__snapshots__/clientGetImportPath.ts +40 -0
- package/src/generators/__snapshots__/clientPostImportPath.ts +30 -0
- package/src/generators/__snapshots__/findByTags.ts +40 -0
- package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +40 -0
- package/src/generators/__snapshots__/findByTagsWithZod.ts +40 -0
- package/src/generators/__snapshots__/getAsMutation.ts +32 -0
- package/src/generators/__snapshots__/postAsQuery.ts +39 -0
- package/src/generators/__snapshots__/updatePetById.ts +30 -0
- package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +34 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/mutationGenerator.tsx +96 -0
- package/src/generators/queryGenerator.tsx +101 -0
- package/src/plugin.ts +61 -50
- package/src/types.ts +63 -55
- package/dist/chunk-ECJ346AA.js +0 -542
- package/dist/chunk-ECJ346AA.js.map +0 -1
- package/dist/chunk-H4LHXYRJ.cjs +0 -542
- package/dist/chunk-H4LHXYRJ.cjs.map +0 -1
- package/dist/index-B3C-JOIU.d.cts +0 -299
- package/dist/index-B3C-JOIU.d.ts +0 -299
- package/src/OperationGenerator.tsx +0 -75
- package/src/components/SchemaType.tsx +0 -59
- package/src/components/__snapshots__/Mutation/Pets.ts +0 -42
- package/src/components/__snapshots__/Query/showPetById.ts +0 -55
package/src/components/Query.tsx
CHANGED
|
@@ -1,290 +1,112 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
4
|
-
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
-
import { getASTParams, getComments } from '@kubb/plugin-oas/utils'
|
|
6
|
-
import { pluginZodName } from '@kubb/plugin-zod'
|
|
7
|
-
|
|
8
|
-
import { QueryOptions } from './QueryOptions.tsx'
|
|
9
|
-
import { SchemaType } from './SchemaType.tsx'
|
|
1
|
+
import { URLPath } from '@kubb/core/utils'
|
|
2
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
10
3
|
|
|
4
|
+
import { type Operation, isOptional } from '@kubb/oas'
|
|
5
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
6
|
+
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
11
7
|
import type { ReactNode } from 'react'
|
|
12
|
-
import type {
|
|
8
|
+
import type { PluginSwr } from '../types.ts'
|
|
9
|
+
import { QueryOptions } from './QueryOptions.tsx'
|
|
13
10
|
|
|
14
|
-
type
|
|
11
|
+
type Props = {
|
|
15
12
|
/**
|
|
16
13
|
* Name of the function
|
|
17
14
|
*/
|
|
18
15
|
name: string
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
* Generics that needs to be added for TypeScript
|
|
25
|
-
*/
|
|
26
|
-
generics?: string
|
|
27
|
-
/**
|
|
28
|
-
* ReturnType(see async for adding Promise type)
|
|
29
|
-
*/
|
|
30
|
-
returnType?: string
|
|
31
|
-
/**
|
|
32
|
-
* Options for JSdocs
|
|
33
|
-
*/
|
|
34
|
-
JSDoc?: {
|
|
35
|
-
comments: string[]
|
|
36
|
-
}
|
|
37
|
-
hook: {
|
|
38
|
-
name: string
|
|
39
|
-
generics?: string
|
|
40
|
-
queryOptions: string
|
|
41
|
-
}
|
|
42
|
-
client: {
|
|
43
|
-
path: URLPath
|
|
44
|
-
withQueryParams: boolean
|
|
45
|
-
}
|
|
16
|
+
queryOptionsName: string
|
|
17
|
+
typeSchemas: OperationSchemas
|
|
18
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
19
|
+
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
20
|
+
operation: Operation
|
|
46
21
|
}
|
|
47
22
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
<>
|
|
52
|
-
<Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
53
|
-
{`
|
|
54
|
-
const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
55
|
-
|
|
56
|
-
const url = ${client.path.template}
|
|
57
|
-
const query = ${hook.name}<${hook.generics}>(
|
|
58
|
-
shouldFetch ? [url, params]: null,
|
|
59
|
-
{
|
|
60
|
-
...${hook.queryOptions},
|
|
61
|
-
...queryOptions
|
|
62
|
-
}
|
|
63
|
-
)
|
|
23
|
+
type GetParamsProps = {
|
|
24
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
25
|
+
dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
|
|
64
26
|
|
|
65
|
-
|
|
66
|
-
`}
|
|
67
|
-
</Function>
|
|
68
|
-
</>
|
|
69
|
-
)
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
return (
|
|
73
|
-
<>
|
|
74
|
-
<Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
75
|
-
{`
|
|
76
|
-
const { query: queryOptions, client: clientOptions = {}, shouldFetch = true } = options ?? {}
|
|
77
|
-
|
|
78
|
-
const url = ${client.path.template}
|
|
79
|
-
const query = ${hook.name}<${hook.generics}>(
|
|
80
|
-
shouldFetch ? url : null,
|
|
81
|
-
{
|
|
82
|
-
...${hook.queryOptions},
|
|
83
|
-
...queryOptions
|
|
84
|
-
}
|
|
85
|
-
)
|
|
86
|
-
|
|
87
|
-
return query
|
|
88
|
-
`}
|
|
89
|
-
</Function>
|
|
90
|
-
</>
|
|
91
|
-
)
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const defaultTemplates = {
|
|
95
|
-
default: Template,
|
|
96
|
-
} as const
|
|
97
|
-
|
|
98
|
-
type Props = {
|
|
99
|
-
factory: {
|
|
100
|
-
name: string
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* This will make it possible to override the default behaviour.
|
|
104
|
-
*/
|
|
105
|
-
Template?: React.ComponentType<TemplateProps>
|
|
106
|
-
/**
|
|
107
|
-
* This will make it possible to override the default behaviour.
|
|
108
|
-
*/
|
|
109
|
-
QueryOptionsTemplate?: React.ComponentType<React.ComponentProps<typeof QueryOptions.templates.default>>
|
|
27
|
+
typeSchemas: OperationSchemas
|
|
110
28
|
}
|
|
111
29
|
|
|
112
|
-
|
|
113
|
-
const {
|
|
114
|
-
pluginManager,
|
|
115
|
-
plugin: {
|
|
116
|
-
key: pluginKey,
|
|
117
|
-
options: { dataReturnType },
|
|
118
|
-
},
|
|
119
|
-
} = useApp<PluginSwr>()
|
|
120
|
-
|
|
121
|
-
const operation = useOperation()
|
|
122
|
-
const { getSchemas, getName } = useOperationManager()
|
|
123
|
-
|
|
124
|
-
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
125
|
-
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
|
|
126
|
-
|
|
127
|
-
const name = getName(operation, { type: 'function' })
|
|
30
|
+
function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
31
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
128
32
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
const generics = new FunctionParams()
|
|
134
|
-
const params = new FunctionParams()
|
|
135
|
-
const queryParams = new FunctionParams()
|
|
136
|
-
const client = {
|
|
137
|
-
method: operation.method,
|
|
138
|
-
path: new URLPath(operation.path),
|
|
139
|
-
withQueryParams: !!schemas.queryParams?.name,
|
|
140
|
-
withData: !!schemas.request?.name,
|
|
141
|
-
withPathParams: !!schemas.pathParams?.name,
|
|
142
|
-
withHeaders: !!schemas.headerParams?.name,
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const resultGenerics = ['TData', `${factory.name}["error"]`]
|
|
146
|
-
|
|
147
|
-
generics.add([{ type: 'TData', default: `${factory.name}["response"]` }])
|
|
148
|
-
|
|
149
|
-
const queryOptionsGenerics = ['TData']
|
|
150
|
-
|
|
151
|
-
params.add([
|
|
152
|
-
...getASTParams(schemas.pathParams, { typed: true }),
|
|
153
|
-
{
|
|
154
|
-
name: 'params',
|
|
155
|
-
type: `${factory.name}['queryParams']`,
|
|
156
|
-
enabled: client.withQueryParams,
|
|
157
|
-
required: false,
|
|
33
|
+
return FunctionParams.factory({
|
|
34
|
+
pathParams: {
|
|
35
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
36
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
158
37
|
},
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
38
|
+
data: typeSchemas.request?.name
|
|
39
|
+
? {
|
|
40
|
+
type: typeSchemas.request?.name,
|
|
41
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
42
|
+
}
|
|
43
|
+
: undefined,
|
|
44
|
+
params: typeSchemas.queryParams?.name
|
|
45
|
+
? {
|
|
46
|
+
type: typeSchemas.queryParams?.name,
|
|
47
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
48
|
+
}
|
|
49
|
+
: undefined,
|
|
50
|
+
headers: typeSchemas.headerParams?.name
|
|
51
|
+
? {
|
|
52
|
+
type: typeSchemas.headerParams?.name,
|
|
53
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
54
|
+
}
|
|
55
|
+
: undefined,
|
|
56
|
+
options: {
|
|
57
|
+
type: `
|
|
58
|
+
{
|
|
59
|
+
query?: SWRConfiguration<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}>,
|
|
60
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
|
|
61
|
+
shouldFetch?: boolean,
|
|
62
|
+
}
|
|
63
|
+
`,
|
|
173
64
|
default: '{}',
|
|
174
65
|
},
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
queryParams.add([
|
|
178
|
-
...getASTParams(schemas.pathParams, { typed: false }),
|
|
179
|
-
{
|
|
180
|
-
name: 'params',
|
|
181
|
-
enabled: client.withQueryParams,
|
|
182
|
-
required: false,
|
|
183
|
-
},
|
|
184
|
-
{
|
|
185
|
-
name: 'headers',
|
|
186
|
-
enabled: client.withHeaders,
|
|
187
|
-
required: false,
|
|
188
|
-
},
|
|
189
|
-
{
|
|
190
|
-
name: 'clientOptions',
|
|
191
|
-
required: false,
|
|
192
|
-
},
|
|
193
|
-
])
|
|
194
|
-
|
|
195
|
-
const hook = {
|
|
196
|
-
name: 'useSWR',
|
|
197
|
-
generics: [...resultGenerics, client.withQueryParams ? '[typeof url, typeof params] | null' : 'typeof url | null'].join(', '),
|
|
198
|
-
queryOptions: `${queryOptionsName}<${queryOptionsGenerics.join(', ')}>(${queryParams.toString()})`,
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
return (
|
|
202
|
-
<>
|
|
203
|
-
<QueryOptions factory={factory} Template={QueryOptionsTemplate} dataReturnType={dataReturnType} />
|
|
204
|
-
|
|
205
|
-
<Template
|
|
206
|
-
name={name}
|
|
207
|
-
generics={generics.toString()}
|
|
208
|
-
JSDoc={{ comments: getComments(operation) }}
|
|
209
|
-
client={client}
|
|
210
|
-
hook={hook}
|
|
211
|
-
params={params.toString()}
|
|
212
|
-
returnType={`SWRResponse<${resultGenerics.join(', ')}>`}
|
|
213
|
-
/>
|
|
214
|
-
</>
|
|
215
|
-
)
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
type FileProps = {
|
|
219
|
-
/**
|
|
220
|
-
* This will make it possible to override the default behaviour.
|
|
221
|
-
*/
|
|
222
|
-
templates?: {
|
|
223
|
-
query: typeof defaultTemplates
|
|
224
|
-
queryOptions: typeof QueryOptions.templates
|
|
225
|
-
}
|
|
66
|
+
})
|
|
226
67
|
}
|
|
227
68
|
|
|
228
|
-
|
|
229
|
-
const
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
},
|
|
237
|
-
},
|
|
238
|
-
} = useApp<PluginSwr>()
|
|
239
|
-
const { getSchemas, getFile, getName } = useOperationManager()
|
|
240
|
-
const operation = useOperation()
|
|
241
|
-
|
|
242
|
-
const file = getFile(operation)
|
|
243
|
-
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
244
|
-
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
|
|
245
|
-
const fileType = getFile(operation, { pluginKey: [pluginTsName] })
|
|
246
|
-
const fileZodSchemas = getFile(operation, {
|
|
247
|
-
pluginKey: [pluginZodName],
|
|
69
|
+
export function Query({ name, typeSchemas, queryOptionsName, operation, dataReturnType, pathParamsType }: Props): ReactNode {
|
|
70
|
+
const path = new URLPath(operation.path)
|
|
71
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
72
|
+
const hookGenerics = [TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'Key']
|
|
73
|
+
const params = getParams({
|
|
74
|
+
pathParamsType,
|
|
75
|
+
dataReturnType,
|
|
76
|
+
typeSchemas,
|
|
248
77
|
})
|
|
249
78
|
|
|
250
|
-
const
|
|
79
|
+
const queryOptionsParams = QueryOptions.getParams({
|
|
80
|
+
pathParamsType,
|
|
81
|
+
typeSchemas,
|
|
82
|
+
})
|
|
251
83
|
|
|
252
|
-
|
|
253
|
-
const
|
|
254
|
-
const factory = {
|
|
255
|
-
name: factoryName,
|
|
256
|
-
}
|
|
84
|
+
//fixed name, see Query.getParams and params
|
|
85
|
+
const swrKey = [path.template, typeSchemas.queryParams?.name ? 'params' : undefined].filter(Boolean)
|
|
257
86
|
|
|
258
87
|
return (
|
|
259
|
-
<
|
|
260
|
-
<
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
schemas.response.name,
|
|
271
|
-
schemas.pathParams?.name,
|
|
272
|
-
schemas.queryParams?.name,
|
|
273
|
-
schemas.headerParams?.name,
|
|
274
|
-
...(schemas.statusCodes?.map((item) => item.name) || []),
|
|
275
|
-
].filter(Boolean)}
|
|
276
|
-
root={file.path}
|
|
277
|
-
path={fileType.path}
|
|
278
|
-
isTypeOnly
|
|
279
|
-
/>
|
|
88
|
+
<File.Source name={name} isExportable isIndexable>
|
|
89
|
+
<Function
|
|
90
|
+
name={name}
|
|
91
|
+
export
|
|
92
|
+
params={params.toConstructor()}
|
|
93
|
+
JSDoc={{
|
|
94
|
+
comments: getComments(operation),
|
|
95
|
+
}}
|
|
96
|
+
>
|
|
97
|
+
{`
|
|
98
|
+
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {}
|
|
280
99
|
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
100
|
+
const swrKey = [${swrKey.join(', ')}] as const
|
|
101
|
+
return useSWR<${hookGenerics.join(', ')}>(
|
|
102
|
+
shouldFetch ? swrKey : null,
|
|
103
|
+
{
|
|
104
|
+
...${queryOptionsName}(${queryOptionsParams.toCall()})
|
|
105
|
+
...queryOptions
|
|
106
|
+
}
|
|
107
|
+
)
|
|
108
|
+
`}
|
|
109
|
+
</Function>
|
|
110
|
+
</File.Source>
|
|
287
111
|
)
|
|
288
112
|
}
|
|
289
|
-
|
|
290
|
-
Query.templates = defaultTemplates
|
|
@@ -1,197 +1,76 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
4
|
-
import { getASTParams } from '@kubb/plugin-oas/utils'
|
|
5
|
-
import { Function, useApp } from '@kubb/react'
|
|
6
|
-
import { pluginZodName } from '@kubb/plugin-zod'
|
|
1
|
+
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
2
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
7
3
|
|
|
8
|
-
import type { HttpMethod } from '@kubb/oas'
|
|
9
4
|
import type { ReactNode } from 'react'
|
|
10
5
|
import type { PluginSwr } from '../types.ts'
|
|
11
|
-
import { pluginTsName } from '@kubb/plugin-ts'
|
|
12
6
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*/
|
|
17
|
-
name: string
|
|
18
|
-
/**
|
|
19
|
-
* Parameters/options/props that need to be used
|
|
20
|
-
*/
|
|
21
|
-
params: string
|
|
22
|
-
/**
|
|
23
|
-
* Generics that needs to be added for TypeScript
|
|
24
|
-
*/
|
|
25
|
-
generics?: string
|
|
26
|
-
/**
|
|
27
|
-
* ReturnType(see async for adding Promise type)
|
|
28
|
-
*/
|
|
29
|
-
returnType?: string
|
|
30
|
-
/**
|
|
31
|
-
* Options for JSdocs
|
|
32
|
-
*/
|
|
33
|
-
JSDoc?: {
|
|
34
|
-
comments: string[]
|
|
35
|
-
}
|
|
36
|
-
client: {
|
|
37
|
-
generics: string
|
|
38
|
-
method: HttpMethod
|
|
39
|
-
path: URLPath
|
|
40
|
-
withQueryParams: boolean
|
|
41
|
-
withPathParams: boolean
|
|
42
|
-
withData: boolean
|
|
43
|
-
withHeaders: boolean
|
|
44
|
-
contentType: string
|
|
45
|
-
}
|
|
46
|
-
dataReturnType: NonNullable<PluginSwr['options']['dataReturnType']>
|
|
47
|
-
parser: string | undefined
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function Template({ name, params, generics, returnType, JSDoc, client, dataReturnType, parser }: TemplateProps): ReactNode {
|
|
51
|
-
const isFormData = client.contentType === 'multipart/form-data'
|
|
52
|
-
const headers = [
|
|
53
|
-
client.contentType !== 'application/json' ? `'Content-Type': '${client.contentType}'` : undefined,
|
|
54
|
-
client.withHeaders ? '...headers' : undefined,
|
|
55
|
-
]
|
|
56
|
-
.filter(Boolean)
|
|
57
|
-
.join(', ')
|
|
58
|
-
|
|
59
|
-
const clientOptions = [
|
|
60
|
-
`method: "${client.method}"`,
|
|
61
|
-
`url: ${client.path.template}`,
|
|
62
|
-
client.withQueryParams ? 'params' : undefined,
|
|
63
|
-
client.withData && !isFormData ? 'data' : undefined,
|
|
64
|
-
client.withData && isFormData ? 'data: formData' : undefined,
|
|
65
|
-
headers.length ? `headers: { ${headers}, ...options.headers }` : undefined,
|
|
66
|
-
'...options',
|
|
67
|
-
].filter(Boolean)
|
|
68
|
-
|
|
69
|
-
const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}`
|
|
70
|
-
|
|
71
|
-
let returnRes = parser ? `return ${parser}(res.data)` : 'return res.data'
|
|
72
|
-
|
|
73
|
-
if (dataReturnType === 'full') {
|
|
74
|
-
returnRes = parser ? `return {...res, data: ${parser}(res.data)}` : 'return res'
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const formData = isFormData
|
|
78
|
-
? `
|
|
79
|
-
const formData = new FormData()
|
|
80
|
-
if(data) {
|
|
81
|
-
Object.keys(data).forEach((key) => {
|
|
82
|
-
const value = data[key];
|
|
83
|
-
if (typeof key === "string" && (typeof value === "string" || value instanceof Blob)) {
|
|
84
|
-
formData.append(key, value);
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
`
|
|
89
|
-
: undefined
|
|
90
|
-
|
|
91
|
-
return (
|
|
92
|
-
<Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
93
|
-
{`
|
|
94
|
-
return {
|
|
95
|
-
fetcher: async () => {
|
|
96
|
-
${formData || ''}
|
|
97
|
-
const res = await client<${client.generics}>({
|
|
98
|
-
${resolvedClientOptions}
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
${returnRes}
|
|
102
|
-
},
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
`}
|
|
106
|
-
</Function>
|
|
107
|
-
)
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const defaultTemplates = {
|
|
111
|
-
default: Template,
|
|
112
|
-
} as const
|
|
7
|
+
import { isOptional } from '@kubb/oas'
|
|
8
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
9
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
113
10
|
|
|
114
11
|
type Props = {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* This will make it possible to override the default behaviour.
|
|
121
|
-
*/
|
|
122
|
-
Template?: React.ComponentType<TemplateProps>
|
|
12
|
+
name: string
|
|
13
|
+
clientName: string
|
|
14
|
+
typeSchemas: OperationSchemas
|
|
15
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
123
16
|
}
|
|
124
17
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
key: pluginKey,
|
|
130
|
-
options: { parser },
|
|
131
|
-
},
|
|
132
|
-
} = useApp<PluginSwr>()
|
|
133
|
-
const { getSchemas } = useOperationManager()
|
|
134
|
-
const operation = useOperation()
|
|
135
|
-
|
|
136
|
-
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
137
|
-
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
|
|
138
|
-
const name = pluginManager.resolveName({
|
|
139
|
-
name: `${factory.name}QueryOptions`,
|
|
140
|
-
pluginKey,
|
|
141
|
-
})
|
|
142
|
-
const contentType = operation.getContentType()
|
|
143
|
-
|
|
144
|
-
const generics = new FunctionParams()
|
|
145
|
-
const params = new FunctionParams()
|
|
146
|
-
|
|
147
|
-
const clientGenerics = ['TData', `${factory.name}['error']`]
|
|
148
|
-
const resultGenerics = ['TData', `${factory.name}['error']`]
|
|
149
|
-
|
|
150
|
-
generics.add([{ type: 'TData', default: `${factory.name}['response']` }])
|
|
18
|
+
type GetParamsProps = {
|
|
19
|
+
pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
|
|
20
|
+
typeSchemas: OperationSchemas
|
|
21
|
+
}
|
|
151
22
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
{
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
enabled: !!schemas.queryParams?.name,
|
|
158
|
-
required: false,
|
|
159
|
-
},
|
|
160
|
-
{
|
|
161
|
-
name: 'headers',
|
|
162
|
-
type: `${factory.name}['headerParams']`,
|
|
163
|
-
enabled: !!schemas.headerParams?.name,
|
|
164
|
-
required: false,
|
|
23
|
+
function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
24
|
+
return FunctionParams.factory({
|
|
25
|
+
pathParams: {
|
|
26
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
27
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
165
28
|
},
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
29
|
+
data: typeSchemas.request?.name
|
|
30
|
+
? {
|
|
31
|
+
type: typeSchemas.request?.name,
|
|
32
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
33
|
+
}
|
|
34
|
+
: undefined,
|
|
35
|
+
params: typeSchemas.queryParams?.name
|
|
36
|
+
? {
|
|
37
|
+
type: typeSchemas.queryParams?.name,
|
|
38
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
39
|
+
}
|
|
40
|
+
: undefined,
|
|
41
|
+
headers: typeSchemas.headerParams?.name
|
|
42
|
+
? {
|
|
43
|
+
type: typeSchemas.headerParams?.name,
|
|
44
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
45
|
+
}
|
|
46
|
+
: undefined,
|
|
47
|
+
config: {
|
|
48
|
+
type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
|
|
169
49
|
default: '{}',
|
|
170
50
|
},
|
|
171
|
-
|
|
51
|
+
})
|
|
52
|
+
}
|
|
172
53
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
path: new URLPath(operation.path),
|
|
180
|
-
generics: clientGenerics.join(', '),
|
|
181
|
-
contentType,
|
|
182
|
-
}
|
|
54
|
+
export function QueryOptions({ name, clientName, typeSchemas, pathParamsType }: Props): ReactNode {
|
|
55
|
+
const params = getParams({ pathParamsType, typeSchemas })
|
|
56
|
+
const clientParams = Client.getParams({
|
|
57
|
+
typeSchemas,
|
|
58
|
+
pathParamsType,
|
|
59
|
+
})
|
|
183
60
|
|
|
184
61
|
return (
|
|
185
|
-
<
|
|
186
|
-
name={name}
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
62
|
+
<File.Source name={name} isExportable isIndexable>
|
|
63
|
+
<Function name={name} export params={params.toConstructor()}>
|
|
64
|
+
{`
|
|
65
|
+
return {
|
|
66
|
+
fetcher: async () => {
|
|
67
|
+
return ${clientName}(${clientParams.toCall()})
|
|
68
|
+
},
|
|
69
|
+
}
|
|
70
|
+
`}
|
|
71
|
+
</Function>
|
|
72
|
+
</File.Source>
|
|
194
73
|
)
|
|
195
74
|
}
|
|
196
75
|
|
|
197
|
-
QueryOptions.
|
|
76
|
+
QueryOptions.getParams = getParams
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import client from "@kubb/plugin-client/client";
|
|
2
|
+
import useSWR from "swr";
|
|
3
|
+
import type { RequestConfig, ResponseConfig } from "@kubb/plugin-client/client";
|
|
4
|
+
import type { Key, SWRConfiguration } from "swr";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
8
|
+
* @summary Finds Pets by tags
|
|
9
|
+
* @link /pet/findByTags
|
|
10
|
+
*/
|
|
11
|
+
async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
|
|
12
|
+
const res = await client<FindPetsByTagsQueryResponse, FindPetsByTags400, unknown>({ method: "get", url: `/pet/findByTags`, params, ...config });
|
|
13
|
+
return res;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function findPetsByTagsQueryOptions(params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
|
|
17
|
+
return {
|
|
18
|
+
fetcher: async () => {
|
|
19
|
+
return findPetsByTags(params, config);
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
26
|
+
* @summary Finds Pets by tags
|
|
27
|
+
* @link /pet/findByTags
|
|
28
|
+
*/
|
|
29
|
+
export function useFindPetsByTags(params?: FindPetsByTagsQueryParams, options: {
|
|
30
|
+
query?: SWRConfiguration<ResponseConfig<FindPetsByTagsQueryResponse>, FindPetsByTags400>;
|
|
31
|
+
client?: Partial<RequestConfig>;
|
|
32
|
+
shouldFetch?: boolean;
|
|
33
|
+
} = {}) {
|
|
34
|
+
const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {};
|
|
35
|
+
const swrKey = [`/pet/findByTags`, params] as const;
|
|
36
|
+
return useSWR<ResponseConfig<FindPetsByTagsQueryResponse>, FindPetsByTags400, Key>(shouldFetch ? swrKey : null, {
|
|
37
|
+
...findPetsByTagsQueryOptions(params, config),
|
|
38
|
+
...queryOptions
|
|
39
|
+
});
|
|
40
|
+
}
|