@kubb/plugin-react-query 3.0.0-alpha.0
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 +21 -0
- package/README.md +114 -0
- package/dist/chunk-5IL6M74X.js +1504 -0
- package/dist/chunk-5IL6M74X.js.map +1 -0
- package/dist/chunk-JFX7DCS7.cjs +1504 -0
- package/dist/chunk-JFX7DCS7.cjs.map +1 -0
- package/dist/components.cjs +15 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +8 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.js +15 -0
- package/dist/components.js.map +1 -0
- package/dist/index-yXskx3Td.d.cts +584 -0
- package/dist/index-yXskx3Td.d.ts +584 -0
- package/dist/index.cjs +223 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +223 -0
- package/dist/index.js.map +1 -0
- package/package.json +97 -0
- package/src/OperationGenerator.tsx +86 -0
- package/src/__snapshots__/mutateAsQuery/updatePetWithForm.ts +64 -0
- package/src/__snapshots__/pathParamsTypeInline/getPetById.ts +57 -0
- package/src/__snapshots__/pathParamsTypeObject/getPetById.ts +63 -0
- package/src/__snapshots__/queryOptions/getPetById.ts +37 -0
- package/src/__snapshots__/queryWithoutQueryOptions/getPetById.ts +47 -0
- package/src/__snapshots__/upload/UploadFile.ts +67 -0
- package/src/__snapshots__/uploadMutation/UploadFile.ts +44 -0
- package/src/__snapshots__/variablesTypeMutate/deletePet.ts +21 -0
- package/src/components/Mutation.tsx +341 -0
- package/src/components/Operations.tsx +74 -0
- package/src/components/Query.tsx +627 -0
- package/src/components/QueryImports.tsx +167 -0
- package/src/components/QueryKey.tsx +200 -0
- package/src/components/QueryOptions.tsx +487 -0
- package/src/components/SchemaType.tsx +55 -0
- package/src/components/__snapshots__/gen/showPetById.ts +57 -0
- package/src/components/__snapshots__/gen/useCreatePets.ts +46 -0
- package/src/components/__snapshots__/gen/useCreatePetsMutate.ts +47 -0
- package/src/components/index.ts +5 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +183 -0
- package/src/types.ts +240 -0
- package/src/utils.ts +96 -0
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
import { PackageManager } from '@kubb/core'
|
|
2
|
+
import transformers from '@kubb/core/transformers'
|
|
3
|
+
import { FunctionParams, URLPath } from '@kubb/core/utils'
|
|
4
|
+
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
+
import { getASTParams } from '@kubb/plugin-oas/utils'
|
|
6
|
+
import { Function, useApp } from '@kubb/react'
|
|
7
|
+
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
|
+
|
|
9
|
+
import { isRequired } from '@kubb/oas'
|
|
10
|
+
import type { HttpMethod } from '@kubb/oas'
|
|
11
|
+
import type { ReactNode } from 'react'
|
|
12
|
+
import type { Infinite, PluginReactQuery, Suspense } from '../types.ts'
|
|
13
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
14
|
+
import { reactQueryDepRegex } from '../utils.ts'
|
|
15
|
+
|
|
16
|
+
type TemplateProps = {
|
|
17
|
+
/**
|
|
18
|
+
* Name of the function
|
|
19
|
+
*/
|
|
20
|
+
name: string
|
|
21
|
+
/**
|
|
22
|
+
* Parameters/options/props that need to be used
|
|
23
|
+
*/
|
|
24
|
+
params: string
|
|
25
|
+
/**
|
|
26
|
+
* Generics that needs to be added for TypeScript
|
|
27
|
+
*/
|
|
28
|
+
generics?: string
|
|
29
|
+
/**
|
|
30
|
+
* ReturnType(see async for adding Promise type)
|
|
31
|
+
*/
|
|
32
|
+
returnType?: string
|
|
33
|
+
/**
|
|
34
|
+
* Options for JSdocs
|
|
35
|
+
*/
|
|
36
|
+
JSDoc?: {
|
|
37
|
+
comments: string[]
|
|
38
|
+
}
|
|
39
|
+
hook: {
|
|
40
|
+
queryKey: string
|
|
41
|
+
children?: string
|
|
42
|
+
}
|
|
43
|
+
client: {
|
|
44
|
+
generics: string
|
|
45
|
+
method: HttpMethod
|
|
46
|
+
path: URLPath
|
|
47
|
+
withQueryParams: boolean
|
|
48
|
+
withPathParams: boolean
|
|
49
|
+
withData: boolean
|
|
50
|
+
withHeaders: boolean
|
|
51
|
+
contentType: string
|
|
52
|
+
}
|
|
53
|
+
infinite: Infinite | false
|
|
54
|
+
dataReturnType: NonNullable<PluginReactQuery['options']['dataReturnType']>
|
|
55
|
+
parser: string | undefined
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function Template({ name, params, generics, returnType, JSDoc, hook, client, infinite, dataReturnType, parser }: TemplateProps): ReactNode {
|
|
59
|
+
const isV5 = new PackageManager().isValidSync(reactQueryDepRegex, '>=5')
|
|
60
|
+
const isFormData = client.contentType === 'multipart/form-data'
|
|
61
|
+
const headers = [
|
|
62
|
+
client.contentType !== 'application/json' ? `'Content-Type': '${client.contentType}'` : undefined,
|
|
63
|
+
client.withHeaders ? '...headers' : undefined,
|
|
64
|
+
]
|
|
65
|
+
.filter(Boolean)
|
|
66
|
+
.join(', ')
|
|
67
|
+
|
|
68
|
+
const clientOptions = [
|
|
69
|
+
`method: "${client.method}"`,
|
|
70
|
+
`url: ${client.path.template}`,
|
|
71
|
+
client.withQueryParams && !infinite ? 'params' : undefined,
|
|
72
|
+
client.withData && !isFormData ? 'data' : undefined,
|
|
73
|
+
client.withData && isFormData ? 'data: formData' : undefined,
|
|
74
|
+
headers.length ? `headers: { ${headers}, ...options.headers }` : undefined,
|
|
75
|
+
'...options',
|
|
76
|
+
client.withQueryParams && !!infinite
|
|
77
|
+
? `params: {
|
|
78
|
+
...params,
|
|
79
|
+
['${infinite.queryParam}']: pageParam,
|
|
80
|
+
...(options.params || {}),
|
|
81
|
+
}`
|
|
82
|
+
: undefined,
|
|
83
|
+
].filter(Boolean)
|
|
84
|
+
|
|
85
|
+
const queryOptions = [
|
|
86
|
+
isV5 && !!infinite ? `initialPageParam: ${infinite.initialPageParam}` : undefined,
|
|
87
|
+
isV5 && !!infinite && !!infinite.cursorParam ? `getNextPageParam: (lastPage) => lastPage['${infinite.cursorParam}']` : undefined,
|
|
88
|
+
isV5 && !!infinite && !!infinite.cursorParam ? `getPreviousPageParam: (firstPage) => firstPage['${infinite.cursorParam}']` : undefined,
|
|
89
|
+
isV5 && !!infinite && !infinite.cursorParam && dataReturnType === 'full'
|
|
90
|
+
? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage.data) && lastPage.data.length === 0 ? undefined : lastPageParam + 1'
|
|
91
|
+
: undefined,
|
|
92
|
+
isV5 && !!infinite && !infinite.cursorParam && dataReturnType === 'data'
|
|
93
|
+
? 'getNextPageParam: (lastPage, _allPages, lastPageParam) => Array.isArray(lastPage) && lastPage.length === 0 ? undefined : lastPageParam + 1'
|
|
94
|
+
: undefined,
|
|
95
|
+
isV5 && !!infinite && !infinite.cursorParam
|
|
96
|
+
? 'getPreviousPageParam: (_firstPage, _allPages, firstPageParam) => firstPageParam <= 1 ? undefined : firstPageParam - 1'
|
|
97
|
+
: undefined,
|
|
98
|
+
].filter(Boolean)
|
|
99
|
+
|
|
100
|
+
const resolvedClientOptions = `${transformers.createIndent(4)}${clientOptions.join(`,\n${transformers.createIndent(4)}`)}`
|
|
101
|
+
const resolvedQueryOptions = `${transformers.createIndent(4)}${queryOptions.join(`,\n${transformers.createIndent(4)}`)}`
|
|
102
|
+
|
|
103
|
+
let returnRes = parser ? `return ${parser}(res.data)` : 'return res.data'
|
|
104
|
+
|
|
105
|
+
if (dataReturnType === 'full') {
|
|
106
|
+
returnRes = parser ? `return {...res, data: ${parser}(res.data)}` : 'return res'
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const formData = isFormData
|
|
110
|
+
? `
|
|
111
|
+
const formData = new FormData()
|
|
112
|
+
if(data) {
|
|
113
|
+
Object.keys(data).forEach((key) => {
|
|
114
|
+
const value = data[key];
|
|
115
|
+
if (typeof key === "string" && (typeof value === "string" || value instanceof Blob)) {
|
|
116
|
+
formData.append(key, value);
|
|
117
|
+
}
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
`
|
|
121
|
+
: undefined
|
|
122
|
+
|
|
123
|
+
if (infinite) {
|
|
124
|
+
if (isV5) {
|
|
125
|
+
return (
|
|
126
|
+
<Function name={name} export params={params} JSDoc={JSDoc}>
|
|
127
|
+
{`
|
|
128
|
+
const queryKey = ${hook.queryKey}
|
|
129
|
+
|
|
130
|
+
return infiniteQueryOptions({
|
|
131
|
+
queryKey,
|
|
132
|
+
queryFn: async ({ pageParam }) => {
|
|
133
|
+
${hook.children || ''}
|
|
134
|
+
${formData || ''}
|
|
135
|
+
const res = await client<${client.generics}>({
|
|
136
|
+
${resolvedClientOptions}
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
${returnRes}
|
|
140
|
+
},
|
|
141
|
+
${resolvedQueryOptions}
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
`}
|
|
145
|
+
</Function>
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return (
|
|
150
|
+
<Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
151
|
+
{`
|
|
152
|
+
const queryKey = ${hook.queryKey}
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
queryKey,
|
|
156
|
+
queryFn: async ({ pageParam }) => {
|
|
157
|
+
${hook.children || ''}
|
|
158
|
+
${formData || ''}
|
|
159
|
+
const res = await client<${client.generics}>({
|
|
160
|
+
${resolvedClientOptions}
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
${returnRes}
|
|
164
|
+
},
|
|
165
|
+
${resolvedQueryOptions}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
`}
|
|
169
|
+
</Function>
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (isV5) {
|
|
174
|
+
return (
|
|
175
|
+
<Function name={name} export params={params} JSDoc={JSDoc}>
|
|
176
|
+
{`
|
|
177
|
+
const queryKey = ${hook.queryKey}
|
|
178
|
+
|
|
179
|
+
return queryOptions({
|
|
180
|
+
queryKey,
|
|
181
|
+
queryFn: async () => {
|
|
182
|
+
${hook.children || ''}
|
|
183
|
+
${formData || ''}
|
|
184
|
+
const res = await client<${client.generics}>({
|
|
185
|
+
${resolvedClientOptions}
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
${returnRes}
|
|
189
|
+
},
|
|
190
|
+
${resolvedQueryOptions}
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
`}
|
|
194
|
+
</Function>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
<Function name={name} export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
200
|
+
{`
|
|
201
|
+
const queryKey = ${hook.queryKey}
|
|
202
|
+
|
|
203
|
+
return {
|
|
204
|
+
queryKey,
|
|
205
|
+
queryFn: async () => {
|
|
206
|
+
${hook.children || ''}
|
|
207
|
+
${formData || ''}
|
|
208
|
+
const res = await client<${client.generics}>({
|
|
209
|
+
${resolvedClientOptions}
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
${returnRes}
|
|
213
|
+
},
|
|
214
|
+
${resolvedQueryOptions}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
`}
|
|
218
|
+
</Function>
|
|
219
|
+
)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
type FrameworkProps = TemplateProps & {
|
|
223
|
+
context: {
|
|
224
|
+
factory: {
|
|
225
|
+
name: string
|
|
226
|
+
}
|
|
227
|
+
queryKey: string
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const defaultTemplates = {
|
|
232
|
+
get react() {
|
|
233
|
+
return function (props: FrameworkProps): ReactNode {
|
|
234
|
+
return <Template {...props} />
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
get solid() {
|
|
238
|
+
return function (props: FrameworkProps): ReactNode {
|
|
239
|
+
return <Template {...props} />
|
|
240
|
+
}
|
|
241
|
+
},
|
|
242
|
+
get svelte() {
|
|
243
|
+
return function (props: FrameworkProps): ReactNode {
|
|
244
|
+
return <Template {...props} />
|
|
245
|
+
}
|
|
246
|
+
},
|
|
247
|
+
get vue() {
|
|
248
|
+
return function ({ client, context, ...rest }: FrameworkProps): ReactNode {
|
|
249
|
+
const { factory, queryKey } = context
|
|
250
|
+
|
|
251
|
+
const {
|
|
252
|
+
plugin: {
|
|
253
|
+
options: { pathParamsType },
|
|
254
|
+
},
|
|
255
|
+
} = useApp<PluginReactQuery>()
|
|
256
|
+
|
|
257
|
+
const { getSchemas } = useOperationManager()
|
|
258
|
+
const operation = useOperation()
|
|
259
|
+
|
|
260
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
261
|
+
const params = new FunctionParams()
|
|
262
|
+
const queryKeyParams = new FunctionParams()
|
|
263
|
+
|
|
264
|
+
params.add([
|
|
265
|
+
...(pathParamsType === 'object'
|
|
266
|
+
? [
|
|
267
|
+
getASTParams(schemas.pathParams, {
|
|
268
|
+
typed: true,
|
|
269
|
+
override: (item) => ({
|
|
270
|
+
...item,
|
|
271
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
272
|
+
type: `MaybeRef<${item.type}>`,
|
|
273
|
+
}),
|
|
274
|
+
}),
|
|
275
|
+
]
|
|
276
|
+
: getASTParams(schemas.pathParams, {
|
|
277
|
+
typed: true,
|
|
278
|
+
override: (item) => ({
|
|
279
|
+
...item,
|
|
280
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
281
|
+
type: `MaybeRef<${item.type}>`,
|
|
282
|
+
}),
|
|
283
|
+
})),
|
|
284
|
+
{
|
|
285
|
+
name: 'refParams',
|
|
286
|
+
type: `MaybeRef<${schemas.queryParams?.name}>`,
|
|
287
|
+
enabled: client.withQueryParams,
|
|
288
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
289
|
+
},
|
|
290
|
+
{
|
|
291
|
+
name: 'refHeaders',
|
|
292
|
+
type: `MaybeRef<${schemas.headerParams?.name}>`,
|
|
293
|
+
enabled: client.withHeaders,
|
|
294
|
+
required: isRequired(schemas.headerParams?.schema),
|
|
295
|
+
},
|
|
296
|
+
{
|
|
297
|
+
name: 'refData',
|
|
298
|
+
type: `MaybeRef<${schemas.request?.name}>`,
|
|
299
|
+
enabled: client.withData,
|
|
300
|
+
required: isRequired(schemas.request?.schema),
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
name: 'options',
|
|
304
|
+
type: `${factory.name}['client']['parameters']`,
|
|
305
|
+
default: '{}',
|
|
306
|
+
},
|
|
307
|
+
])
|
|
308
|
+
|
|
309
|
+
queryKeyParams.add([
|
|
310
|
+
...(pathParamsType === 'object'
|
|
311
|
+
? [
|
|
312
|
+
getASTParams(schemas.pathParams, {
|
|
313
|
+
override: (item) => ({
|
|
314
|
+
...item,
|
|
315
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
316
|
+
}),
|
|
317
|
+
}),
|
|
318
|
+
]
|
|
319
|
+
: getASTParams(schemas.pathParams, {
|
|
320
|
+
override: (item) => ({
|
|
321
|
+
...item,
|
|
322
|
+
name: item.name ? `ref${transformers.pascalCase(item.name)}` : undefined,
|
|
323
|
+
}),
|
|
324
|
+
})),
|
|
325
|
+
{
|
|
326
|
+
name: 'refParams',
|
|
327
|
+
enabled: client.withQueryParams,
|
|
328
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
name: 'refData',
|
|
332
|
+
enabled: client.withData,
|
|
333
|
+
required: isRequired(schemas.request?.schema),
|
|
334
|
+
},
|
|
335
|
+
])
|
|
336
|
+
|
|
337
|
+
const unrefs = params.items
|
|
338
|
+
.filter((item) => item.enabled)
|
|
339
|
+
.map((item) => {
|
|
340
|
+
return item.name ? `const ${transformers.camelCase(item.name.replace('ref', ''))} = unref(${item.name})` : undefined
|
|
341
|
+
})
|
|
342
|
+
.join('\n')
|
|
343
|
+
|
|
344
|
+
const hook = {
|
|
345
|
+
queryKey: `${queryKey}(${queryKeyParams.toString()})`,
|
|
346
|
+
children: unrefs,
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return <Template {...rest} params={params.toString()} hook={hook} client={client} />
|
|
350
|
+
}
|
|
351
|
+
},
|
|
352
|
+
} as const
|
|
353
|
+
|
|
354
|
+
type Props = {
|
|
355
|
+
infinite: Infinite | false
|
|
356
|
+
suspense: Suspense | false
|
|
357
|
+
factory: {
|
|
358
|
+
name: string
|
|
359
|
+
}
|
|
360
|
+
resultType: string
|
|
361
|
+
/**
|
|
362
|
+
* This will make it possible to override the default behaviour.
|
|
363
|
+
*/
|
|
364
|
+
Template?: React.ComponentType<FrameworkProps>
|
|
365
|
+
dataReturnType: NonNullable<PluginReactQuery['options']['dataReturnType']>
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export function QueryOptions({ factory, infinite, suspense, resultType, dataReturnType, Template = defaultTemplates.react }: Props): ReactNode {
|
|
369
|
+
const {
|
|
370
|
+
pluginManager,
|
|
371
|
+
plugin: {
|
|
372
|
+
key: pluginKey,
|
|
373
|
+
options: { parser, pathParamsType, queryOptions },
|
|
374
|
+
},
|
|
375
|
+
} = useApp<PluginReactQuery>()
|
|
376
|
+
|
|
377
|
+
const { getSchemas } = useOperationManager()
|
|
378
|
+
const operation = useOperation()
|
|
379
|
+
|
|
380
|
+
const contentType = operation.getContentType()
|
|
381
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
382
|
+
const zodSchemas = getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' })
|
|
383
|
+
|
|
384
|
+
const queryKey = pluginManager.resolveName({
|
|
385
|
+
name: [factory.name, infinite ? 'Infinite' : undefined, suspense ? 'Suspense' : undefined, 'QueryKey'].filter(Boolean).join(''),
|
|
386
|
+
pluginKey,
|
|
387
|
+
})
|
|
388
|
+
const queryOptionsName = pluginManager.resolveName({
|
|
389
|
+
name: [factory.name, infinite ? 'Infinite' : undefined, suspense ? 'Suspense' : undefined, 'QueryOptions'].filter(Boolean).join(''),
|
|
390
|
+
pluginKey,
|
|
391
|
+
})
|
|
392
|
+
|
|
393
|
+
const generics = new FunctionParams()
|
|
394
|
+
const params = new FunctionParams()
|
|
395
|
+
const queryKeyParams = new FunctionParams()
|
|
396
|
+
|
|
397
|
+
const clientGenerics = [`${factory.name}['data']`, `${factory.name}['error']`]
|
|
398
|
+
// suspense is having 4 generics instead of 5, TQueryData is not needed because data will always be defined
|
|
399
|
+
const resultGenerics = suspense
|
|
400
|
+
? [`${factory.name}['response']`, `${factory.name}["error"]`, 'TData']
|
|
401
|
+
: [`${factory.name}['response']`, `${factory.name}["error"]`, 'TData', 'TQueryData']
|
|
402
|
+
|
|
403
|
+
const client = {
|
|
404
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
405
|
+
withData: !!schemas.request?.name,
|
|
406
|
+
withPathParams: !!schemas.pathParams?.name,
|
|
407
|
+
withHeaders: !!schemas.headerParams?.name,
|
|
408
|
+
method: operation.method,
|
|
409
|
+
path: new URLPath(operation.path),
|
|
410
|
+
generics: clientGenerics.toString(),
|
|
411
|
+
contentType,
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
generics.add([
|
|
415
|
+
{ type: 'TData', default: `${factory.name}["response"]` },
|
|
416
|
+
suspense ? undefined : { type: 'TQueryData', default: `${factory.name}["response"]` },
|
|
417
|
+
])
|
|
418
|
+
|
|
419
|
+
params.add([
|
|
420
|
+
...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams, { typed: true })] : getASTParams(schemas.pathParams, { typed: true })),
|
|
421
|
+
{
|
|
422
|
+
name: 'params',
|
|
423
|
+
type: `${factory.name}['queryParams']`,
|
|
424
|
+
enabled: client.withQueryParams,
|
|
425
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
426
|
+
},
|
|
427
|
+
{
|
|
428
|
+
name: 'headers',
|
|
429
|
+
type: `${factory.name}['headerParams']`,
|
|
430
|
+
enabled: client.withHeaders,
|
|
431
|
+
required: isRequired(schemas.headerParams?.schema),
|
|
432
|
+
},
|
|
433
|
+
{
|
|
434
|
+
name: 'data',
|
|
435
|
+
type: `${factory.name}['request']`,
|
|
436
|
+
enabled: client.withData,
|
|
437
|
+
required: isRequired(schemas.request?.schema),
|
|
438
|
+
},
|
|
439
|
+
{
|
|
440
|
+
name: 'options',
|
|
441
|
+
type: `${factory.name}['client']['parameters']`,
|
|
442
|
+
default: '{}',
|
|
443
|
+
},
|
|
444
|
+
])
|
|
445
|
+
|
|
446
|
+
queryKeyParams.add([
|
|
447
|
+
...(pathParamsType === 'object' ? [getASTParams(schemas.pathParams)] : getASTParams(schemas.pathParams)),
|
|
448
|
+
{
|
|
449
|
+
name: 'params',
|
|
450
|
+
enabled: client.withQueryParams,
|
|
451
|
+
required: isRequired(schemas.queryParams?.schema),
|
|
452
|
+
},
|
|
453
|
+
{
|
|
454
|
+
name: 'data',
|
|
455
|
+
enabled: client.withData,
|
|
456
|
+
required: isRequired(schemas.request?.schema),
|
|
457
|
+
},
|
|
458
|
+
])
|
|
459
|
+
|
|
460
|
+
const hook = {
|
|
461
|
+
queryKey: `${queryKey}(${queryKeyParams.toString()})`,
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
if (!queryOptions) {
|
|
465
|
+
return null
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
return (
|
|
469
|
+
<Template
|
|
470
|
+
name={queryOptionsName}
|
|
471
|
+
params={params.toString()}
|
|
472
|
+
generics={generics.toString()}
|
|
473
|
+
returnType={`WithRequired<${resultType}<${resultGenerics.join(', ')}>, 'queryKey'>`}
|
|
474
|
+
client={client}
|
|
475
|
+
hook={hook}
|
|
476
|
+
infinite={infinite}
|
|
477
|
+
dataReturnType={dataReturnType}
|
|
478
|
+
parser={parser === 'zod' ? `${zodSchemas.response.name}.parse` : undefined}
|
|
479
|
+
context={{
|
|
480
|
+
factory,
|
|
481
|
+
queryKey,
|
|
482
|
+
}}
|
|
483
|
+
/>
|
|
484
|
+
)
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
QueryOptions.templates = defaultTemplates
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
2
|
+
import { Type, useApp } from '@kubb/react'
|
|
3
|
+
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
import type { PluginReactQuery } from '../types.ts'
|
|
6
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
|
+
|
|
8
|
+
export function SchemaType(): ReactNode {
|
|
9
|
+
const {
|
|
10
|
+
plugin: {
|
|
11
|
+
options: { dataReturnType },
|
|
12
|
+
},
|
|
13
|
+
} = useApp<PluginReactQuery>()
|
|
14
|
+
const { getSchemas, getName } = useOperationManager()
|
|
15
|
+
const operation = useOperation()
|
|
16
|
+
|
|
17
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
18
|
+
|
|
19
|
+
const [TData, TError, TRequest, TPathParams, TQueryParams, THeaderParams, TResponse] = [
|
|
20
|
+
schemas.response.name,
|
|
21
|
+
schemas.errors?.map((item) => item.name).join(' | ') || 'never',
|
|
22
|
+
schemas.request?.name || 'never',
|
|
23
|
+
schemas.pathParams?.name || 'never',
|
|
24
|
+
schemas.queryParams?.name || 'never',
|
|
25
|
+
schemas.headerParams?.name || 'never',
|
|
26
|
+
schemas.response.name,
|
|
27
|
+
]
|
|
28
|
+
const factoryName = getName(operation, { type: 'type' })
|
|
29
|
+
|
|
30
|
+
const clientType = `${factoryName}Client`
|
|
31
|
+
const isFormData = operation.getContentType() === 'multipart/form-data'
|
|
32
|
+
|
|
33
|
+
return (
|
|
34
|
+
<>
|
|
35
|
+
<Type name={clientType}>{`typeof client<${TResponse}, ${TError}, ${isFormData ? 'FormData' : TRequest}>`}</Type>
|
|
36
|
+
<Type name={factoryName}>
|
|
37
|
+
{`
|
|
38
|
+
{
|
|
39
|
+
data: ${TData}
|
|
40
|
+
error: ${TError}
|
|
41
|
+
request: ${isFormData ? 'FormData' : TRequest}
|
|
42
|
+
pathParams: ${TPathParams}
|
|
43
|
+
queryParams: ${TQueryParams}
|
|
44
|
+
headerParams: ${THeaderParams}
|
|
45
|
+
response: ${dataReturnType === 'data' ? TData : `Awaited<ReturnType<${clientType}>>`}
|
|
46
|
+
client: {
|
|
47
|
+
parameters: Partial<Parameters<${clientType}>[0]>
|
|
48
|
+
return: Awaited<ReturnType<${clientType}>>
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
`}
|
|
52
|
+
</Type>
|
|
53
|
+
</>
|
|
54
|
+
)
|
|
55
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
type GetPetsUuidClient = typeof client<GetPetsUuidQueryResponse, never, never>
|
|
2
|
+
type GetPetsUuid = {
|
|
3
|
+
data: GetPetsUuidQueryResponse
|
|
4
|
+
error: never
|
|
5
|
+
request: never
|
|
6
|
+
pathParams: never
|
|
7
|
+
queryParams: never
|
|
8
|
+
headerParams: never
|
|
9
|
+
response: GetPetsUuidQueryResponse
|
|
10
|
+
client: {
|
|
11
|
+
parameters: Partial<Parameters<GetPetsUuidClient>[0]>
|
|
12
|
+
return: Awaited<ReturnType<GetPetsUuidClient>>
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const GetPetsUuidQueryKey = () => [{ url: '/pets/:uuid', params: { uuid: uuid } }] as const
|
|
17
|
+
export type GetPetsUuidQueryKey = ReturnType<typeof GetPetsUuidQueryKey>
|
|
18
|
+
export function GetPetsUuidQueryOptions<TData = GetPetsUuid['response'], TQueryData = GetPetsUuid['response']>(
|
|
19
|
+
options: GetPetsUuid['client']['parameters'] = {},
|
|
20
|
+
): WithRequired<UseBaseQueryOptions<GetPetsUuid['response'], GetPetsUuid['error'], TData, TQueryData>, 'queryKey'> {
|
|
21
|
+
const queryKey = GetPetsUuidQueryKey()
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
queryKey,
|
|
25
|
+
queryFn: async () => {
|
|
26
|
+
const res = await client<GetPetsUuid['data'], GetPetsUuid['error']>({
|
|
27
|
+
method: 'get',
|
|
28
|
+
url: `/pets/${uuid}`,
|
|
29
|
+
...options,
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
return res.data
|
|
33
|
+
},
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* @link /pets/:uuid
|
|
38
|
+
*/
|
|
39
|
+
export function useGetPetsUuid<TData = GetPetsUuid['response'], TQueryData = GetPetsUuid['response'], TQueryKey extends QueryKey = GetPetsUuidQueryKey>(
|
|
40
|
+
options: {
|
|
41
|
+
query?: Partial<UseBaseQueryOptions<GetPetsUuid['response'], GetPetsUuid['error'], TData, TQueryData, TQueryKey>>
|
|
42
|
+
client?: GetPetsUuid['client']['parameters']
|
|
43
|
+
} = {},
|
|
44
|
+
): UseQueryResult<TData, GetPetsUuid['error']> & { queryKey: TQueryKey } {
|
|
45
|
+
const { query: queryOptions, client: clientOptions = {} } = options ?? {}
|
|
46
|
+
const queryKey = queryOptions?.queryKey ?? GetPetsUuidQueryKey()
|
|
47
|
+
|
|
48
|
+
const query = useQuery<GetPetsUuid['data'], GetPetsUuid['error'], TData, any>({
|
|
49
|
+
...GetPetsUuidQueryOptions<TData, TQueryData>(clientOptions),
|
|
50
|
+
queryKey,
|
|
51
|
+
...queryOptions,
|
|
52
|
+
}) as UseQueryResult<TData, GetPetsUuid['error']> & { queryKey: TQueryKey }
|
|
53
|
+
|
|
54
|
+
query.queryKey = queryKey as TQueryKey
|
|
55
|
+
|
|
56
|
+
return query
|
|
57
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
type CreatePetsClient = typeof client<CreatePetsMutationResponse, never, CreatePetsMutationRequest>
|
|
2
|
+
type CreatePets = {
|
|
3
|
+
data: CreatePetsMutationResponse
|
|
4
|
+
error: never
|
|
5
|
+
request: CreatePetsMutationRequest
|
|
6
|
+
pathParams: CreatePetsPathParams
|
|
7
|
+
queryParams: CreatePetsQueryParams
|
|
8
|
+
headerParams: CreatePetsHeaderParams
|
|
9
|
+
response: CreatePetsMutationResponse
|
|
10
|
+
client: {
|
|
11
|
+
parameters: Partial<Parameters<CreatePetsClient>[0]>
|
|
12
|
+
return: Awaited<ReturnType<CreatePetsClient>>
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @summary Create a pet
|
|
18
|
+
* @link /pets/:uuid
|
|
19
|
+
*/
|
|
20
|
+
export function useCreatePets(
|
|
21
|
+
uuid: CreatePetsPathParams['uuid'],
|
|
22
|
+
headers: CreatePets['headerParams'],
|
|
23
|
+
params?: CreatePets['queryParams'],
|
|
24
|
+
options: {
|
|
25
|
+
mutation?: UseMutationOptions<CreatePets['response'], CreatePets['error'], CreatePets['request']>
|
|
26
|
+
client?: CreatePets['client']['parameters']
|
|
27
|
+
} = {},
|
|
28
|
+
) {
|
|
29
|
+
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
|
|
30
|
+
|
|
31
|
+
return useMutation({
|
|
32
|
+
mutationFn: async (data) => {
|
|
33
|
+
const res = await client<CreatePets['data'], CreatePets['error'], CreatePets['request']>({
|
|
34
|
+
method: 'post',
|
|
35
|
+
url: `/pets/${uuid}`,
|
|
36
|
+
params,
|
|
37
|
+
data,
|
|
38
|
+
headers: { ...headers, ...clientOptions.headers },
|
|
39
|
+
...clientOptions,
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return res.data
|
|
43
|
+
},
|
|
44
|
+
...mutationOptions,
|
|
45
|
+
})
|
|
46
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
type CreatePetsClient = typeof client<CreatePetsMutationResponse, never, CreatePetsMutationRequest>
|
|
2
|
+
type CreatePets = {
|
|
3
|
+
data: CreatePetsMutationResponse
|
|
4
|
+
error: never
|
|
5
|
+
request: CreatePetsMutationRequest
|
|
6
|
+
pathParams: CreatePetsPathParams
|
|
7
|
+
queryParams: CreatePetsQueryParams
|
|
8
|
+
headerParams: CreatePetsHeaderParams
|
|
9
|
+
response: CreatePetsMutationResponse
|
|
10
|
+
client: {
|
|
11
|
+
parameters: Partial<Parameters<CreatePetsClient>[0]>
|
|
12
|
+
return: Awaited<ReturnType<CreatePetsClient>>
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @summary Create a pet
|
|
18
|
+
* @link /pets/:uuid
|
|
19
|
+
*/
|
|
20
|
+
export function useCreatePets(
|
|
21
|
+
options: {
|
|
22
|
+
mutation?: UseMutationOptions<
|
|
23
|
+
CreatePets['response'],
|
|
24
|
+
CreatePets['error'],
|
|
25
|
+
{ uuid: CreatePetsPathParams['uuid']; params?: CreatePets['queryParams']; headers: CreatePets['headerParams']; data: CreatePets['request'] }
|
|
26
|
+
>
|
|
27
|
+
client?: CreatePets['client']['parameters']
|
|
28
|
+
} = {},
|
|
29
|
+
) {
|
|
30
|
+
const { mutation: mutationOptions, client: clientOptions = {} } = options ?? {}
|
|
31
|
+
|
|
32
|
+
return useMutation({
|
|
33
|
+
mutationFn: async ({ uuid, headers, data, params }) => {
|
|
34
|
+
const res = await client<CreatePets['data'], CreatePets['error'], CreatePets['request']>({
|
|
35
|
+
method: 'post',
|
|
36
|
+
url: `/pets/${uuid}`,
|
|
37
|
+
params,
|
|
38
|
+
data,
|
|
39
|
+
headers: { ...headers, ...clientOptions.headers },
|
|
40
|
+
...clientOptions,
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
return res.data
|
|
44
|
+
},
|
|
45
|
+
...mutationOptions,
|
|
46
|
+
})
|
|
47
|
+
}
|
package/src/index.ts
ADDED