@kubb/plugin-react-query 3.0.0-beta.9 → 3.0.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/dist/{chunk-D5PFCZWQ.cjs → chunk-EOG7AHFO.cjs} +258 -40
- package/dist/chunk-EOG7AHFO.cjs.map +1 -0
- package/dist/{chunk-U35NELYJ.js → chunk-EY5KE7R7.js} +258 -40
- package/dist/chunk-EY5KE7R7.js.map +1 -0
- package/dist/{chunk-JKG4EZID.cjs → chunk-NBC6BPMV.cjs} +141 -110
- package/dist/chunk-NBC6BPMV.cjs.map +1 -0
- package/dist/{chunk-3OOSVEDM.js → chunk-NZKAIPYC.js} +130 -99
- package/dist/chunk-NZKAIPYC.js.map +1 -0
- package/dist/components.cjs +9 -9
- package/dist/components.d.cts +29 -16
- package/dist/components.d.ts +29 -16
- package/dist/components.js +1 -1
- package/dist/generators.cjs +6 -6
- package/dist/generators.d.cts +1 -1
- package/dist/generators.d.ts +1 -1
- package/dist/generators.js +2 -2
- package/dist/index.cjs +24 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +23 -23
- package/dist/index.js.map +1 -1
- package/dist/{types-LhwfnVo7.d.cts → types-IuxCCG1K.d.cts} +46 -16
- package/dist/{types-LhwfnVo7.d.ts → types-IuxCCG1K.d.ts} +46 -16
- package/package.json +12 -12
- package/src/components/InfiniteQuery.tsx +52 -5
- package/src/components/InfiniteQueryOptions.tsx +47 -6
- package/src/components/Mutation.tsx +3 -1
- package/src/components/MutationKey.tsx +11 -5
- package/src/components/Query.tsx +62 -6
- package/src/components/QueryKey.tsx +17 -7
- package/src/components/QueryOptions.tsx +47 -7
- package/src/components/SuspenseQuery.tsx +52 -5
- package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +1 -1
- package/src/generators/__snapshots__/clientGetImportPath.ts +1 -1
- package/src/generators/__snapshots__/findByTags.ts +1 -1
- package/src/generators/__snapshots__/findByTagsObject.ts +60 -0
- package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +1 -1
- package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +2 -2
- package/src/generators/__snapshots__/findByTagsWithZod.ts +1 -1
- package/src/generators/__snapshots__/findInfiniteByTags.ts +1 -1
- package/src/generators/__snapshots__/findInfiniteByTagsCursor.ts +1 -1
- package/src/generators/__snapshots__/postAsQuery.ts +1 -1
- package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +1 -3
- package/src/generators/infiniteQueryGenerator.tsx +40 -28
- package/src/generators/mutationGenerator.tsx +25 -16
- package/src/generators/queryGenerator.tsx +24 -17
- package/src/generators/suspenseQueryGenerator.tsx +26 -16
- package/src/plugin.ts +23 -20
- package/src/types.ts +35 -15
- package/dist/chunk-3OOSVEDM.js.map +0 -1
- package/dist/chunk-D5PFCZWQ.cjs.map +0 -1
- package/dist/chunk-JKG4EZID.cjs.map +0 -1
- package/dist/chunk-U35NELYJ.js.map +0 -1
|
@@ -18,24 +18,68 @@ type Props = {
|
|
|
18
18
|
queryKeyTypeName: string
|
|
19
19
|
typeSchemas: OperationSchemas
|
|
20
20
|
operation: Operation
|
|
21
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
21
22
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
22
23
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
type GetParamsProps = {
|
|
27
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
26
28
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
27
29
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
28
30
|
typeSchemas: OperationSchemas
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
33
|
+
function getParams({ paramsType, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
32
34
|
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
33
35
|
|
|
36
|
+
if (paramsType === 'object') {
|
|
37
|
+
return FunctionParams.factory({
|
|
38
|
+
data: {
|
|
39
|
+
mode: 'object',
|
|
40
|
+
children: {
|
|
41
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
42
|
+
data: typeSchemas.request?.name
|
|
43
|
+
? {
|
|
44
|
+
type: typeSchemas.request?.name,
|
|
45
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
46
|
+
}
|
|
47
|
+
: undefined,
|
|
48
|
+
params: typeSchemas.queryParams?.name
|
|
49
|
+
? {
|
|
50
|
+
type: typeSchemas.queryParams?.name,
|
|
51
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
headers: typeSchemas.headerParams?.name
|
|
55
|
+
? {
|
|
56
|
+
type: typeSchemas.headerParams?.name,
|
|
57
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
58
|
+
}
|
|
59
|
+
: undefined,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
options: {
|
|
63
|
+
type: `
|
|
64
|
+
{
|
|
65
|
+
query?: Partial<InfiniteQueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
|
|
66
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
|
|
67
|
+
}
|
|
68
|
+
`,
|
|
69
|
+
default: '{}',
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
34
74
|
return FunctionParams.factory({
|
|
35
|
-
pathParams:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
75
|
+
pathParams: typeSchemas.pathParams?.name
|
|
76
|
+
? {
|
|
77
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
78
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
79
|
+
type: typeSchemas.pathParams?.name,
|
|
80
|
+
optional: isOptional(typeSchemas.pathParams?.schema),
|
|
81
|
+
}
|
|
82
|
+
: undefined,
|
|
39
83
|
data: typeSchemas.request?.name
|
|
40
84
|
? {
|
|
41
85
|
type: typeSchemas.request?.name,
|
|
@@ -71,6 +115,7 @@ export function InfiniteQuery({
|
|
|
71
115
|
queryKeyTypeName,
|
|
72
116
|
queryOptionsName,
|
|
73
117
|
queryKeyName,
|
|
118
|
+
paramsType,
|
|
74
119
|
pathParamsType,
|
|
75
120
|
dataReturnType,
|
|
76
121
|
typeSchemas,
|
|
@@ -85,10 +130,12 @@ export function InfiniteQuery({
|
|
|
85
130
|
typeSchemas,
|
|
86
131
|
})
|
|
87
132
|
const queryOptionsParams = QueryOptions.getParams({
|
|
133
|
+
paramsType,
|
|
88
134
|
pathParamsType,
|
|
89
135
|
typeSchemas,
|
|
90
136
|
})
|
|
91
137
|
const params = getParams({
|
|
138
|
+
paramsType,
|
|
92
139
|
pathParamsType,
|
|
93
140
|
dataReturnType,
|
|
94
141
|
typeSchemas,
|
|
@@ -14,6 +14,7 @@ type Props = {
|
|
|
14
14
|
clientName: string
|
|
15
15
|
queryKeyName: string
|
|
16
16
|
typeSchemas: OperationSchemas
|
|
17
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
17
18
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
18
19
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
19
20
|
initialPageParam: Infinite['initialPageParam']
|
|
@@ -22,16 +23,54 @@ type Props = {
|
|
|
22
23
|
}
|
|
23
24
|
|
|
24
25
|
type GetParamsProps = {
|
|
26
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
25
27
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
26
28
|
typeSchemas: OperationSchemas
|
|
27
29
|
}
|
|
28
30
|
|
|
29
|
-
function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
31
|
+
function getParams({ paramsType, pathParamsType, typeSchemas }: GetParamsProps) {
|
|
32
|
+
if (paramsType === 'object') {
|
|
33
|
+
return FunctionParams.factory({
|
|
34
|
+
data: {
|
|
35
|
+
mode: 'object',
|
|
36
|
+
children: {
|
|
37
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
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
|
+
},
|
|
57
|
+
},
|
|
58
|
+
config: {
|
|
59
|
+
type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
|
|
60
|
+
default: '{}',
|
|
61
|
+
},
|
|
62
|
+
})
|
|
63
|
+
}
|
|
64
|
+
|
|
30
65
|
return FunctionParams.factory({
|
|
31
|
-
pathParams:
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
66
|
+
pathParams: typeSchemas.pathParams?.name
|
|
67
|
+
? {
|
|
68
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
69
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
70
|
+
type: typeSchemas.pathParams?.name,
|
|
71
|
+
optional: isOptional(typeSchemas.pathParams?.schema),
|
|
72
|
+
}
|
|
73
|
+
: undefined,
|
|
35
74
|
data: typeSchemas.request?.name
|
|
36
75
|
? {
|
|
37
76
|
type: typeSchemas.request?.name,
|
|
@@ -63,14 +102,16 @@ export function InfiniteQueryOptions({
|
|
|
63
102
|
initialPageParam,
|
|
64
103
|
cursorParam,
|
|
65
104
|
typeSchemas,
|
|
105
|
+
paramsType,
|
|
66
106
|
dataReturnType,
|
|
67
107
|
pathParamsType,
|
|
68
108
|
queryParam,
|
|
69
109
|
queryKeyName,
|
|
70
110
|
}: Props): ReactNode {
|
|
71
|
-
const params = getParams({ pathParamsType, typeSchemas })
|
|
111
|
+
const params = getParams({ paramsType, pathParamsType, typeSchemas })
|
|
72
112
|
const clientParams = Client.getParams({
|
|
73
113
|
typeSchemas,
|
|
114
|
+
paramsType,
|
|
74
115
|
pathParamsType,
|
|
75
116
|
})
|
|
76
117
|
const queryKeyParams = QueryKey.getParams({
|
|
@@ -20,6 +20,7 @@ type Props = {
|
|
|
20
20
|
typeSchemas: OperationSchemas
|
|
21
21
|
operation: Operation
|
|
22
22
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
23
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
23
24
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
24
25
|
}
|
|
25
26
|
|
|
@@ -70,7 +71,7 @@ function getParams({ dataReturnType, typeSchemas }: GetParamsProps) {
|
|
|
70
71
|
})
|
|
71
72
|
}
|
|
72
73
|
|
|
73
|
-
export function Mutation({ name, clientName, pathParamsType, dataReturnType, typeSchemas, operation, mutationKeyName }: Props): ReactNode {
|
|
74
|
+
export function Mutation({ name, clientName, paramsType, pathParamsType, dataReturnType, typeSchemas, operation, mutationKeyName }: Props): ReactNode {
|
|
74
75
|
const mutationKeyParams = MutationKey.getParams({
|
|
75
76
|
pathParamsType,
|
|
76
77
|
typeSchemas,
|
|
@@ -83,6 +84,7 @@ export function Mutation({ name, clientName, pathParamsType, dataReturnType, typ
|
|
|
83
84
|
})
|
|
84
85
|
|
|
85
86
|
const clientParams = Client.getParams({
|
|
87
|
+
paramsType,
|
|
86
88
|
typeSchemas,
|
|
87
89
|
pathParamsType,
|
|
88
90
|
})
|
|
@@ -4,7 +4,7 @@ import { File, Function, FunctionParams, Type } from '@kubb/react'
|
|
|
4
4
|
import type { Operation } from '@kubb/oas'
|
|
5
5
|
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
6
6
|
import type { ReactNode } from 'react'
|
|
7
|
-
import type { PluginReactQuery } from '../types'
|
|
7
|
+
import type { PluginReactQuery, Transformer } from '../types'
|
|
8
8
|
|
|
9
9
|
type Props = {
|
|
10
10
|
name: string
|
|
@@ -12,7 +12,7 @@ type Props = {
|
|
|
12
12
|
typeSchemas: OperationSchemas
|
|
13
13
|
operation: Operation
|
|
14
14
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
15
|
-
|
|
15
|
+
transformer: Transformer | undefined
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
type GetParamsProps = {
|
|
@@ -24,16 +24,21 @@ function getParams({}: GetParamsProps) {
|
|
|
24
24
|
return FunctionParams.factory({})
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
const getTransformer: Transformer = ({ operation }) => {
|
|
28
28
|
const path = new URLPath(operation.path)
|
|
29
|
+
|
|
30
|
+
return [JSON.stringify({ url: path.path })].filter(Boolean)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function MutationKey({ name, typeSchemas, pathParamsType, operation, typeName, transformer = getTransformer }: Props): ReactNode {
|
|
29
34
|
const params = getParams({ pathParamsType, typeSchemas })
|
|
30
|
-
const keys =
|
|
35
|
+
const keys = transformer({ operation, schemas: typeSchemas })
|
|
31
36
|
|
|
32
37
|
return (
|
|
33
38
|
<>
|
|
34
39
|
<File.Source name={name} isExportable isIndexable>
|
|
35
40
|
<Function.Arrow name={name} export params={params.toConstructor()} singleLine>
|
|
36
|
-
{`[${
|
|
41
|
+
{`[${keys.join(', ')}] as const`}
|
|
37
42
|
</Function.Arrow>
|
|
38
43
|
</File.Source>
|
|
39
44
|
<File.Source name={typeName} isExportable isIndexable isTypeOnly>
|
|
@@ -46,3 +51,4 @@ export function MutationKey({ name, typeSchemas, pathParamsType, operation, type
|
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
MutationKey.getParams = getParams
|
|
54
|
+
MutationKey.getTransformer = getTransformer
|
package/src/components/Query.tsx
CHANGED
|
@@ -18,24 +18,68 @@ type Props = {
|
|
|
18
18
|
queryKeyTypeName: string
|
|
19
19
|
typeSchemas: OperationSchemas
|
|
20
20
|
operation: Operation
|
|
21
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
21
22
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
22
23
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
type GetParamsProps = {
|
|
27
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
26
28
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
27
29
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
28
30
|
typeSchemas: OperationSchemas
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
33
|
+
function getParams({ paramsType, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
32
34
|
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
33
35
|
|
|
36
|
+
if (paramsType === 'object') {
|
|
37
|
+
return FunctionParams.factory({
|
|
38
|
+
data: {
|
|
39
|
+
mode: 'object',
|
|
40
|
+
children: {
|
|
41
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
42
|
+
data: typeSchemas.request?.name
|
|
43
|
+
? {
|
|
44
|
+
type: typeSchemas.request?.name,
|
|
45
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
46
|
+
}
|
|
47
|
+
: undefined,
|
|
48
|
+
params: typeSchemas.queryParams?.name
|
|
49
|
+
? {
|
|
50
|
+
type: typeSchemas.queryParams?.name,
|
|
51
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
headers: typeSchemas.headerParams?.name
|
|
55
|
+
? {
|
|
56
|
+
type: typeSchemas.headerParams?.name,
|
|
57
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
58
|
+
}
|
|
59
|
+
: undefined,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
options: {
|
|
63
|
+
type: `
|
|
64
|
+
{
|
|
65
|
+
query?: Partial<QueryObserverOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
|
|
66
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
|
|
67
|
+
}
|
|
68
|
+
`,
|
|
69
|
+
default: '{}',
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
34
74
|
return FunctionParams.factory({
|
|
35
|
-
pathParams:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
75
|
+
pathParams: typeSchemas.pathParams?.name
|
|
76
|
+
? {
|
|
77
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
78
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
79
|
+
type: typeSchemas.pathParams?.name,
|
|
80
|
+
optional: isOptional(typeSchemas.pathParams?.schema),
|
|
81
|
+
}
|
|
82
|
+
: undefined,
|
|
39
83
|
data: typeSchemas.request?.name
|
|
40
84
|
? {
|
|
41
85
|
type: typeSchemas.request?.name,
|
|
@@ -66,7 +110,17 @@ function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsPro
|
|
|
66
110
|
})
|
|
67
111
|
}
|
|
68
112
|
|
|
69
|
-
export function Query({
|
|
113
|
+
export function Query({
|
|
114
|
+
name,
|
|
115
|
+
queryKeyTypeName,
|
|
116
|
+
queryOptionsName,
|
|
117
|
+
queryKeyName,
|
|
118
|
+
paramsType,
|
|
119
|
+
pathParamsType,
|
|
120
|
+
dataReturnType,
|
|
121
|
+
typeSchemas,
|
|
122
|
+
operation,
|
|
123
|
+
}: Props): ReactNode {
|
|
70
124
|
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
71
125
|
const returnType = `UseQueryResult<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error'].join(', ')}> & { queryKey: TQueryKey }`
|
|
72
126
|
const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
|
|
@@ -76,10 +130,12 @@ export function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName,
|
|
|
76
130
|
typeSchemas,
|
|
77
131
|
})
|
|
78
132
|
const queryOptionsParams = QueryOptions.getParams({
|
|
133
|
+
paramsType,
|
|
79
134
|
pathParamsType,
|
|
80
135
|
typeSchemas,
|
|
81
136
|
})
|
|
82
137
|
const params = getParams({
|
|
138
|
+
paramsType,
|
|
83
139
|
pathParamsType,
|
|
84
140
|
dataReturnType,
|
|
85
141
|
typeSchemas,
|
|
@@ -5,7 +5,7 @@ import { File, Function, FunctionParams, Type } from '@kubb/react'
|
|
|
5
5
|
import { type Operation, isOptional } from '@kubb/oas'
|
|
6
6
|
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
7
7
|
import type { ReactNode } from 'react'
|
|
8
|
-
import type { PluginReactQuery } from '../types'
|
|
8
|
+
import type { PluginReactQuery, Transformer } from '../types'
|
|
9
9
|
|
|
10
10
|
type Props = {
|
|
11
11
|
name: string
|
|
@@ -13,7 +13,7 @@ type Props = {
|
|
|
13
13
|
typeSchemas: OperationSchemas
|
|
14
14
|
operation: Operation
|
|
15
15
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
16
|
-
|
|
16
|
+
transformer: Transformer | undefined
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
type GetParamsProps = {
|
|
@@ -42,23 +42,32 @@ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
|
42
42
|
})
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
const getTransformer: Transformer = ({ operation, schemas }) => {
|
|
46
46
|
const path = new URLPath(operation.path)
|
|
47
|
-
const params = getParams({ pathParamsType, typeSchemas })
|
|
48
47
|
const keys = [
|
|
49
48
|
path.toObject({
|
|
50
49
|
type: 'path',
|
|
51
50
|
stringify: true,
|
|
52
51
|
}),
|
|
53
|
-
|
|
54
|
-
|
|
52
|
+
schemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
|
|
53
|
+
schemas.request?.name ? '...(data ? [data] : [])' : undefined,
|
|
55
54
|
].filter(Boolean)
|
|
56
55
|
|
|
56
|
+
return keys
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, transformer = getTransformer }: Props): ReactNode {
|
|
60
|
+
const params = getParams({ pathParamsType, typeSchemas })
|
|
61
|
+
const keys = transformer({
|
|
62
|
+
operation,
|
|
63
|
+
schemas: typeSchemas,
|
|
64
|
+
})
|
|
65
|
+
|
|
57
66
|
return (
|
|
58
67
|
<>
|
|
59
68
|
<File.Source name={name} isExportable isIndexable>
|
|
60
69
|
<Function.Arrow name={name} export params={params.toConstructor()} singleLine>
|
|
61
|
-
{`[${
|
|
70
|
+
{`[${keys.join(', ')}] as const`}
|
|
62
71
|
</Function.Arrow>
|
|
63
72
|
</File.Source>
|
|
64
73
|
<File.Source name={typeName} isExportable isIndexable isTypeOnly>
|
|
@@ -71,3 +80,4 @@ export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeNam
|
|
|
71
80
|
}
|
|
72
81
|
|
|
73
82
|
QueryKey.getParams = getParams
|
|
83
|
+
QueryKey.getTransformer = getTransformer
|
|
@@ -14,20 +14,59 @@ type Props = {
|
|
|
14
14
|
clientName: string
|
|
15
15
|
queryKeyName: string
|
|
16
16
|
typeSchemas: OperationSchemas
|
|
17
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
17
18
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
type GetParamsProps = {
|
|
22
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
21
23
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
22
24
|
typeSchemas: OperationSchemas
|
|
23
25
|
}
|
|
24
26
|
|
|
25
|
-
function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
27
|
+
function getParams({ paramsType, pathParamsType, typeSchemas }: GetParamsProps) {
|
|
28
|
+
if (paramsType === 'object') {
|
|
29
|
+
return FunctionParams.factory({
|
|
30
|
+
data: {
|
|
31
|
+
mode: 'object',
|
|
32
|
+
children: {
|
|
33
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
34
|
+
data: typeSchemas.request?.name
|
|
35
|
+
? {
|
|
36
|
+
type: typeSchemas.request?.name,
|
|
37
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
38
|
+
}
|
|
39
|
+
: undefined,
|
|
40
|
+
params: typeSchemas.queryParams?.name
|
|
41
|
+
? {
|
|
42
|
+
type: typeSchemas.queryParams?.name,
|
|
43
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
44
|
+
}
|
|
45
|
+
: undefined,
|
|
46
|
+
headers: typeSchemas.headerParams?.name
|
|
47
|
+
? {
|
|
48
|
+
type: typeSchemas.headerParams?.name,
|
|
49
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
50
|
+
}
|
|
51
|
+
: undefined,
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
config: {
|
|
55
|
+
type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
|
|
56
|
+
default: '{}',
|
|
57
|
+
},
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
26
61
|
return FunctionParams.factory({
|
|
27
|
-
pathParams:
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
62
|
+
pathParams: typeSchemas.pathParams?.name
|
|
63
|
+
? {
|
|
64
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
65
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
66
|
+
type: typeSchemas.pathParams?.name,
|
|
67
|
+
optional: isOptional(typeSchemas.pathParams?.schema),
|
|
68
|
+
}
|
|
69
|
+
: undefined,
|
|
31
70
|
data: typeSchemas.request?.name
|
|
32
71
|
? {
|
|
33
72
|
type: typeSchemas.request?.name,
|
|
@@ -53,10 +92,11 @@ function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
|
53
92
|
})
|
|
54
93
|
}
|
|
55
94
|
|
|
56
|
-
export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, queryKeyName }: Props): ReactNode {
|
|
57
|
-
const params = getParams({ pathParamsType, typeSchemas })
|
|
95
|
+
export function QueryOptions({ name, clientName, typeSchemas, paramsType, pathParamsType, queryKeyName }: Props): ReactNode {
|
|
96
|
+
const params = getParams({ paramsType, pathParamsType, typeSchemas })
|
|
58
97
|
const clientParams = Client.getParams({
|
|
59
98
|
typeSchemas,
|
|
99
|
+
paramsType,
|
|
60
100
|
pathParamsType,
|
|
61
101
|
})
|
|
62
102
|
const queryKeyParams = QueryKey.getParams({
|
|
@@ -18,24 +18,68 @@ type Props = {
|
|
|
18
18
|
queryKeyTypeName: string
|
|
19
19
|
typeSchemas: OperationSchemas
|
|
20
20
|
operation: Operation
|
|
21
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
21
22
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
22
23
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
type GetParamsProps = {
|
|
27
|
+
paramsType: PluginReactQuery['resolvedOptions']['paramsType']
|
|
26
28
|
pathParamsType: PluginReactQuery['resolvedOptions']['pathParamsType']
|
|
27
29
|
dataReturnType: PluginReactQuery['resolvedOptions']['client']['dataReturnType']
|
|
28
30
|
typeSchemas: OperationSchemas
|
|
29
31
|
}
|
|
30
32
|
|
|
31
|
-
function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
33
|
+
function getParams({ paramsType, pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
32
34
|
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
33
35
|
|
|
36
|
+
if (paramsType === 'object') {
|
|
37
|
+
return FunctionParams.factory({
|
|
38
|
+
data: {
|
|
39
|
+
mode: 'object',
|
|
40
|
+
children: {
|
|
41
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
42
|
+
data: typeSchemas.request?.name
|
|
43
|
+
? {
|
|
44
|
+
type: typeSchemas.request?.name,
|
|
45
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
46
|
+
}
|
|
47
|
+
: undefined,
|
|
48
|
+
params: typeSchemas.queryParams?.name
|
|
49
|
+
? {
|
|
50
|
+
type: typeSchemas.queryParams?.name,
|
|
51
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
52
|
+
}
|
|
53
|
+
: undefined,
|
|
54
|
+
headers: typeSchemas.headerParams?.name
|
|
55
|
+
? {
|
|
56
|
+
type: typeSchemas.headerParams?.name,
|
|
57
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
58
|
+
}
|
|
59
|
+
: undefined,
|
|
60
|
+
},
|
|
61
|
+
},
|
|
62
|
+
options: {
|
|
63
|
+
type: `
|
|
64
|
+
{
|
|
65
|
+
query?: Partial<UseSuspenseQueryOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'Error', 'TData', 'TQueryKey'].join(', ')}>>,
|
|
66
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
|
|
67
|
+
}
|
|
68
|
+
`,
|
|
69
|
+
default: '{}',
|
|
70
|
+
},
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
34
74
|
return FunctionParams.factory({
|
|
35
|
-
pathParams:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
75
|
+
pathParams: typeSchemas.pathParams?.name
|
|
76
|
+
? {
|
|
77
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
78
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
79
|
+
type: typeSchemas.pathParams?.name,
|
|
80
|
+
optional: isOptional(typeSchemas.pathParams?.schema),
|
|
81
|
+
}
|
|
82
|
+
: undefined,
|
|
39
83
|
data: typeSchemas.request?.name
|
|
40
84
|
? {
|
|
41
85
|
type: typeSchemas.request?.name,
|
|
@@ -71,6 +115,7 @@ export function SuspenseQuery({
|
|
|
71
115
|
queryKeyTypeName,
|
|
72
116
|
queryOptionsName,
|
|
73
117
|
queryKeyName,
|
|
118
|
+
paramsType,
|
|
74
119
|
pathParamsType,
|
|
75
120
|
dataReturnType,
|
|
76
121
|
typeSchemas,
|
|
@@ -85,10 +130,12 @@ export function SuspenseQuery({
|
|
|
85
130
|
typeSchemas,
|
|
86
131
|
})
|
|
87
132
|
const queryOptionsParams = QueryOptions.getParams({
|
|
133
|
+
paramsType,
|
|
88
134
|
pathParamsType,
|
|
89
135
|
typeSchemas,
|
|
90
136
|
})
|
|
91
137
|
const params = getParams({
|
|
138
|
+
paramsType,
|
|
92
139
|
pathParamsType,
|
|
93
140
|
dataReturnType,
|
|
94
141
|
typeSchemas,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import client from "@kubb/plugin-client/client";
|
|
2
2
|
import type { RequestConfig, ResponseConfig } from "@kubb/plugin-client/client";
|
|
3
3
|
import type { QueryKey, QueryObserverOptions, UseQueryResult } from "@tanstack/react-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, useQuery } from "@tanstack/react-query";
|
|
5
5
|
|
|
6
6
|
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import client from "axios";
|
|
2
2
|
import type { QueryKey, QueryObserverOptions, UseQueryResult } from "@tanstack/react-query";
|
|
3
3
|
import type { RequestConfig } from "axios";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, useQuery } from "@tanstack/react-query";
|
|
5
5
|
|
|
6
6
|
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
7
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import client from "@kubb/plugin-client/client";
|
|
2
2
|
import type { RequestConfig } from "@kubb/plugin-client/client";
|
|
3
3
|
import type { QueryKey, QueryObserverOptions, UseQueryResult } from "@tanstack/react-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, useQuery } from "@tanstack/react-query";
|
|
5
5
|
|
|
6
6
|
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
7
|
|