@kubb/plugin-svelte-query 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/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/chunk-QPJYCH77.js +376 -0
- package/dist/chunk-QPJYCH77.js.map +1 -0
- package/dist/chunk-RE36BJQW.cjs +384 -0
- package/dist/chunk-RE36BJQW.cjs.map +1 -0
- package/dist/chunk-TD7H4OF4.cjs +376 -0
- package/dist/chunk-TD7H4OF4.cjs.map +1 -0
- package/dist/chunk-V5DEG2OQ.js +369 -0
- package/dist/chunk-V5DEG2OQ.js.map +1 -0
- package/dist/components.cjs +24 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +78 -0
- package/dist/components.d.ts +78 -0
- package/dist/components.js +3 -0
- package/dist/components.js.map +1 -0
- 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 +131 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +124 -0
- package/dist/index.js.map +1 -0
- package/dist/types-64YHTP7S.d.cts +148 -0
- package/dist/types-64YHTP7S.d.ts +148 -0
- package/package.json +102 -0
- package/src/components/Mutation.tsx +133 -0
- package/src/components/Query.tsx +120 -0
- package/src/components/QueryKey.tsx +73 -0
- package/src/components/QueryOptions.tsx +84 -0
- package/src/components/index.ts +4 -0
- package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +50 -0
- package/src/generators/__snapshots__/clientGetImportPath.ts +50 -0
- package/src/generators/__snapshots__/clientPostImportPath.ts +38 -0
- package/src/generators/__snapshots__/findByTags.ts +50 -0
- package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +50 -0
- package/src/generators/__snapshots__/findByTagsWithCustomQueryKey.ts +50 -0
- package/src/generators/__snapshots__/findByTagsWithZod.ts +50 -0
- package/src/generators/__snapshots__/postAsQuery.ts +48 -0
- package/src/generators/__snapshots__/updatePetById.ts +38 -0
- package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +40 -0
- package/src/generators/index.ts +2 -0
- package/src/generators/mutationGenerator.tsx +96 -0
- package/src/generators/queryGenerator.tsx +122 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +143 -0
- package/src/types.ts +153 -0
package/package.json
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kubb/plugin-svelte-query",
|
|
3
|
+
"version": "3.0.0-alpha.20",
|
|
4
|
+
"description": "Generator svelte-query hooks",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"faker",
|
|
7
|
+
"faker.js",
|
|
8
|
+
"msw",
|
|
9
|
+
"mock",
|
|
10
|
+
"mocking",
|
|
11
|
+
"plugins",
|
|
12
|
+
"kubb",
|
|
13
|
+
"codegen",
|
|
14
|
+
"swagger",
|
|
15
|
+
"openapi"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git://github.com/kubb-labs/kubb.git",
|
|
20
|
+
"directory": "packages/plugin-svelte-query"
|
|
21
|
+
},
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"author": "Stijn Van Hulle <stijn@stijnvanhulle.be",
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"type": "module",
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"import": "./dist/index.js",
|
|
29
|
+
"require": "./dist/index.cjs",
|
|
30
|
+
"default": "./dist/index.cjs"
|
|
31
|
+
},
|
|
32
|
+
"./generators": {
|
|
33
|
+
"import": "./dist/generators.js",
|
|
34
|
+
"require": "./dist/generators.cjs",
|
|
35
|
+
"default": "./dist/generators.cjs"
|
|
36
|
+
},
|
|
37
|
+
"./components": {
|
|
38
|
+
"import": "./dist/components.js",
|
|
39
|
+
"require": "./dist/components.cjs",
|
|
40
|
+
"default": "./dist/components.cjs"
|
|
41
|
+
},
|
|
42
|
+
"./package.json": "./package.json",
|
|
43
|
+
"./*": "./*"
|
|
44
|
+
},
|
|
45
|
+
"main": "dist/index.cjs",
|
|
46
|
+
"module": "dist/index.js",
|
|
47
|
+
"types": "./dist/index.d.ts",
|
|
48
|
+
"typesVersions": {
|
|
49
|
+
"*": {
|
|
50
|
+
"components": [
|
|
51
|
+
"./dist/components.d.ts"
|
|
52
|
+
],
|
|
53
|
+
"generators": [
|
|
54
|
+
"./dist/generators.d.ts"
|
|
55
|
+
]
|
|
56
|
+
}
|
|
57
|
+
},
|
|
58
|
+
"files": [
|
|
59
|
+
"src",
|
|
60
|
+
"dist",
|
|
61
|
+
"!/**/**.test.**",
|
|
62
|
+
"!/**/__tests__/**"
|
|
63
|
+
],
|
|
64
|
+
"dependencies": {
|
|
65
|
+
"@kubb/core": "3.0.0-alpha.20",
|
|
66
|
+
"@kubb/fs": "3.0.0-alpha.20",
|
|
67
|
+
"@kubb/oas": "3.0.0-alpha.20",
|
|
68
|
+
"@kubb/plugin-oas": "3.0.0-alpha.20",
|
|
69
|
+
"@kubb/plugin-ts": "3.0.0-alpha.20",
|
|
70
|
+
"@kubb/plugin-zod": "3.0.0-alpha.20",
|
|
71
|
+
"@kubb/react": "3.0.0-alpha.20"
|
|
72
|
+
},
|
|
73
|
+
"devDependencies": {
|
|
74
|
+
"@types/react": "^18.3.6",
|
|
75
|
+
"react": "^18.3.1",
|
|
76
|
+
"tsup": "^8.2.4",
|
|
77
|
+
"typescript": "^5.6.2",
|
|
78
|
+
"@kubb/config-ts": "3.0.0-alpha.20",
|
|
79
|
+
"@kubb/config-tsup": "3.0.0-alpha.20"
|
|
80
|
+
},
|
|
81
|
+
"peerDependencies": {
|
|
82
|
+
"@kubb/react": "3.0.0-alpha.20"
|
|
83
|
+
},
|
|
84
|
+
"engines": {
|
|
85
|
+
"node": ">=20"
|
|
86
|
+
},
|
|
87
|
+
"publishConfig": {
|
|
88
|
+
"access": "public",
|
|
89
|
+
"registry": "https://registry.npmjs.org/"
|
|
90
|
+
},
|
|
91
|
+
"scripts": {
|
|
92
|
+
"build": "tsup",
|
|
93
|
+
"clean": "npx rimraf ./dist",
|
|
94
|
+
"lint": "bun biome lint .",
|
|
95
|
+
"lint:fix": "bun biome lint --apply-unsafe .",
|
|
96
|
+
"release": "pnpm publish --no-git-check",
|
|
97
|
+
"release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
|
|
98
|
+
"start": "tsup --watch",
|
|
99
|
+
"test": "vitest --passWithNoTests",
|
|
100
|
+
"typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
2
|
+
|
|
3
|
+
import { type Operation, isOptional } from '@kubb/oas'
|
|
4
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
5
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
6
|
+
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
7
|
+
import type { ReactNode } from 'react'
|
|
8
|
+
import type { PluginSvelteQuery } from '../types.ts'
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
/**
|
|
12
|
+
* Name of the function
|
|
13
|
+
*/
|
|
14
|
+
name: string
|
|
15
|
+
typeName: string
|
|
16
|
+
clientName: string
|
|
17
|
+
typeSchemas: OperationSchemas
|
|
18
|
+
operation: Operation
|
|
19
|
+
dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType']
|
|
20
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
type GetParamsProps = {
|
|
24
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
25
|
+
dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType']
|
|
26
|
+
typeSchemas: OperationSchemas
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getParams({ dataReturnType, typeSchemas }: GetParamsProps) {
|
|
30
|
+
const mutateParams = FunctionParams.factory({
|
|
31
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
32
|
+
data: typeSchemas.request?.name
|
|
33
|
+
? {
|
|
34
|
+
type: typeSchemas.request?.name,
|
|
35
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
36
|
+
}
|
|
37
|
+
: undefined,
|
|
38
|
+
params: typeSchemas.queryParams?.name
|
|
39
|
+
? {
|
|
40
|
+
type: typeSchemas.queryParams?.name,
|
|
41
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
42
|
+
}
|
|
43
|
+
: undefined,
|
|
44
|
+
headers: typeSchemas.headerParams?.name
|
|
45
|
+
? {
|
|
46
|
+
type: typeSchemas.headerParams?.name,
|
|
47
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
48
|
+
}
|
|
49
|
+
: undefined,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
53
|
+
const TRequest = mutateParams.toConstructor({ valueAsType: true })
|
|
54
|
+
|
|
55
|
+
return FunctionParams.factory({
|
|
56
|
+
options: {
|
|
57
|
+
type: `
|
|
58
|
+
{
|
|
59
|
+
mutation?: CreateMutationOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', `{${TRequest}`].join(', ')}>,
|
|
60
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'},
|
|
61
|
+
}
|
|
62
|
+
`,
|
|
63
|
+
default: '{}',
|
|
64
|
+
},
|
|
65
|
+
})
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function Mutation({ name, clientName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode {
|
|
69
|
+
const params = getParams({
|
|
70
|
+
pathParamsType,
|
|
71
|
+
dataReturnType,
|
|
72
|
+
typeSchemas,
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
const clientParams = Client.getParams({
|
|
76
|
+
typeSchemas,
|
|
77
|
+
pathParamsType,
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
const mutationParams = FunctionParams.factory({
|
|
81
|
+
data: {
|
|
82
|
+
// No use of pathParams because useMutation can only take one argument in object form,
|
|
83
|
+
// see https://tanstack.com/query/latest/docs/framework/react/reference/useMutation#usemutation
|
|
84
|
+
mode: 'object',
|
|
85
|
+
//TODO rename with value
|
|
86
|
+
children: {
|
|
87
|
+
...getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
88
|
+
data: typeSchemas.request?.name
|
|
89
|
+
? {
|
|
90
|
+
type: typeSchemas.request?.name,
|
|
91
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
92
|
+
}
|
|
93
|
+
: undefined,
|
|
94
|
+
params: typeSchemas.queryParams?.name
|
|
95
|
+
? {
|
|
96
|
+
type: typeSchemas.queryParams?.name,
|
|
97
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
98
|
+
}
|
|
99
|
+
: undefined,
|
|
100
|
+
headers: typeSchemas.headerParams?.name
|
|
101
|
+
? {
|
|
102
|
+
type: typeSchemas.headerParams?.name,
|
|
103
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
104
|
+
}
|
|
105
|
+
: undefined,
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
return (
|
|
111
|
+
<File.Source name={name} isExportable isIndexable>
|
|
112
|
+
<Function
|
|
113
|
+
name={name}
|
|
114
|
+
export
|
|
115
|
+
params={params.toConstructor()}
|
|
116
|
+
JSDoc={{
|
|
117
|
+
comments: getComments(operation),
|
|
118
|
+
}}
|
|
119
|
+
>
|
|
120
|
+
{`
|
|
121
|
+
const { mutation: mutationOptions, client: config = {} } = options ?? {}
|
|
122
|
+
|
|
123
|
+
return createMutation({
|
|
124
|
+
mutationFn: async(${mutationParams.toConstructor()}) => {
|
|
125
|
+
return ${clientName}(${clientParams.toCall()})
|
|
126
|
+
},
|
|
127
|
+
...mutationOptions
|
|
128
|
+
})
|
|
129
|
+
`}
|
|
130
|
+
</Function>
|
|
131
|
+
</File.Source>
|
|
132
|
+
)
|
|
133
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
2
|
+
|
|
3
|
+
import { type Operation, isOptional } from '@kubb/oas'
|
|
4
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
5
|
+
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
6
|
+
import type { ReactNode } from 'react'
|
|
7
|
+
import type { PluginSvelteQuery } from '../types.ts'
|
|
8
|
+
import { QueryKey } from './QueryKey.tsx'
|
|
9
|
+
import { QueryOptions } from './QueryOptions.tsx'
|
|
10
|
+
|
|
11
|
+
type Props = {
|
|
12
|
+
/**
|
|
13
|
+
* Name of the function
|
|
14
|
+
*/
|
|
15
|
+
name: string
|
|
16
|
+
queryOptionsName: string
|
|
17
|
+
queryKeyName: string
|
|
18
|
+
queryKeyTypeName: string
|
|
19
|
+
typeSchemas: OperationSchemas
|
|
20
|
+
operation: Operation
|
|
21
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
22
|
+
dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType']
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type GetParamsProps = {
|
|
26
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
27
|
+
dataReturnType: PluginSvelteQuery['resolvedOptions']['client']['dataReturnType']
|
|
28
|
+
typeSchemas: OperationSchemas
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function getParams({ pathParamsType, dataReturnType, typeSchemas }: GetParamsProps) {
|
|
32
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
33
|
+
|
|
34
|
+
return FunctionParams.factory({
|
|
35
|
+
pathParams: {
|
|
36
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
37
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
38
|
+
},
|
|
39
|
+
data: typeSchemas.request?.name
|
|
40
|
+
? {
|
|
41
|
+
type: typeSchemas.request?.name,
|
|
42
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
43
|
+
}
|
|
44
|
+
: undefined,
|
|
45
|
+
params: typeSchemas.queryParams?.name
|
|
46
|
+
? {
|
|
47
|
+
type: typeSchemas.queryParams?.name,
|
|
48
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
49
|
+
}
|
|
50
|
+
: undefined,
|
|
51
|
+
headers: typeSchemas.headerParams?.name
|
|
52
|
+
? {
|
|
53
|
+
type: typeSchemas.headerParams?.name,
|
|
54
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
55
|
+
}
|
|
56
|
+
: undefined,
|
|
57
|
+
options: {
|
|
58
|
+
type: `
|
|
59
|
+
{
|
|
60
|
+
query?: Partial<CreateBaseQueryOptions<${[TData, typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown', 'TData', 'TQueryData', 'TQueryKey'].join(', ')}>>,
|
|
61
|
+
client?: ${typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>'}
|
|
62
|
+
}
|
|
63
|
+
`,
|
|
64
|
+
default: '{}',
|
|
65
|
+
},
|
|
66
|
+
})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function Query({ name, queryKeyTypeName, queryOptionsName, queryKeyName, pathParamsType, dataReturnType, typeSchemas, operation }: Props): ReactNode {
|
|
70
|
+
const TData = dataReturnType === 'data' ? typeSchemas.response.name : `ResponseConfig<${typeSchemas.response.name}>`
|
|
71
|
+
const returnType = `CreateQueryResult<${['TData', typeSchemas.errors?.map((item) => item.name).join(' | ') || 'unknown'].join(', ')}> & { queryKey: TQueryKey }`
|
|
72
|
+
const generics = [`TData = ${TData}`, `TQueryData = ${TData}`, `TQueryKey extends QueryKey = ${queryKeyTypeName}`]
|
|
73
|
+
|
|
74
|
+
const queryKeyParams = QueryKey.getParams({
|
|
75
|
+
pathParamsType,
|
|
76
|
+
typeSchemas,
|
|
77
|
+
})
|
|
78
|
+
const queryOptionsParams = QueryOptions.getParams({
|
|
79
|
+
pathParamsType,
|
|
80
|
+
typeSchemas,
|
|
81
|
+
})
|
|
82
|
+
const params = getParams({
|
|
83
|
+
pathParamsType,
|
|
84
|
+
dataReturnType,
|
|
85
|
+
typeSchemas,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const queryOptions = `${queryOptionsName}(${queryOptionsParams.toCall()}) as unknown as CreateBaseQueryOptions`
|
|
89
|
+
|
|
90
|
+
return (
|
|
91
|
+
<File.Source name={name} isExportable isIndexable>
|
|
92
|
+
<Function
|
|
93
|
+
name={name}
|
|
94
|
+
export
|
|
95
|
+
generics={generics.join(', ')}
|
|
96
|
+
params={params.toConstructor()}
|
|
97
|
+
JSDoc={{
|
|
98
|
+
comments: getComments(operation),
|
|
99
|
+
}}
|
|
100
|
+
>
|
|
101
|
+
{`
|
|
102
|
+
const { query: queryOptions, client: config = {} } = options ?? {}
|
|
103
|
+
const queryKey = queryOptions?.queryKey ?? ${queryKeyName}(${queryKeyParams.toCall()})
|
|
104
|
+
|
|
105
|
+
const query = createQuery({
|
|
106
|
+
...${queryOptions},
|
|
107
|
+
queryKey,
|
|
108
|
+
...queryOptions as unknown as Omit<CreateBaseQueryOptions, "queryKey">
|
|
109
|
+
}) as ${returnType}
|
|
110
|
+
|
|
111
|
+
query.queryKey = queryKey as TQueryKey
|
|
112
|
+
|
|
113
|
+
return query
|
|
114
|
+
`}
|
|
115
|
+
</Function>
|
|
116
|
+
</File.Source>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
Query.getParams = getParams
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { URLPath } from '@kubb/core/utils'
|
|
2
|
+
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
3
|
+
import { File, Function, FunctionParams, Type } from '@kubb/react'
|
|
4
|
+
|
|
5
|
+
import { type Operation, isOptional } from '@kubb/oas'
|
|
6
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
7
|
+
import type { ReactNode } from 'react'
|
|
8
|
+
import type { PluginSvelteQuery } from '../types'
|
|
9
|
+
|
|
10
|
+
type Props = {
|
|
11
|
+
name: string
|
|
12
|
+
typeName: string
|
|
13
|
+
typeSchemas: OperationSchemas
|
|
14
|
+
operation: Operation
|
|
15
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
16
|
+
keysFn: ((keys: unknown[]) => unknown[]) | undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
type GetParamsProps = {
|
|
20
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
21
|
+
typeSchemas: OperationSchemas
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
25
|
+
return FunctionParams.factory({
|
|
26
|
+
pathParams: {
|
|
27
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
28
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
29
|
+
},
|
|
30
|
+
data: typeSchemas.request?.name
|
|
31
|
+
? {
|
|
32
|
+
type: typeSchemas.request?.name,
|
|
33
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
34
|
+
}
|
|
35
|
+
: undefined,
|
|
36
|
+
params: typeSchemas.queryParams?.name
|
|
37
|
+
? {
|
|
38
|
+
type: typeSchemas.queryParams?.name,
|
|
39
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
40
|
+
}
|
|
41
|
+
: undefined,
|
|
42
|
+
})
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function QueryKey({ name, typeSchemas, pathParamsType, operation, typeName, keysFn = (name) => name }: Props): ReactNode {
|
|
46
|
+
const path = new URLPath(operation.path)
|
|
47
|
+
const params = getParams({ pathParamsType, typeSchemas })
|
|
48
|
+
const keys = [
|
|
49
|
+
path.toObject({
|
|
50
|
+
type: 'path',
|
|
51
|
+
stringify: true,
|
|
52
|
+
}),
|
|
53
|
+
typeSchemas.queryParams?.name ? '...(params ? [params] : [])' : undefined,
|
|
54
|
+
typeSchemas.request?.name ? '...(data ? [data] : [])' : undefined,
|
|
55
|
+
].filter(Boolean)
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<>
|
|
59
|
+
<File.Source name={name} isExportable isIndexable>
|
|
60
|
+
<Function.Arrow name={name} export params={params.toConstructor()} singleLine>
|
|
61
|
+
{`[${keysFn(keys).join(', ')}] as const`}
|
|
62
|
+
</Function.Arrow>
|
|
63
|
+
</File.Source>
|
|
64
|
+
<File.Source name={typeName} isExportable isIndexable isTypeOnly>
|
|
65
|
+
<Type name={typeName} export>
|
|
66
|
+
{`ReturnType<typeof ${name}>`}
|
|
67
|
+
</Type>
|
|
68
|
+
</File.Source>
|
|
69
|
+
</>
|
|
70
|
+
)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
QueryKey.getParams = getParams
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { getPathParams } from '@kubb/plugin-oas/utils'
|
|
2
|
+
import { File, Function, FunctionParams } from '@kubb/react'
|
|
3
|
+
|
|
4
|
+
import type { ReactNode } from 'react'
|
|
5
|
+
|
|
6
|
+
import { isOptional } from '@kubb/oas'
|
|
7
|
+
import { Client } from '@kubb/plugin-client/components'
|
|
8
|
+
import type { OperationSchemas } from '@kubb/plugin-oas'
|
|
9
|
+
import type { PluginSvelteQuery } from '../types.ts'
|
|
10
|
+
import { QueryKey } from './QueryKey.tsx'
|
|
11
|
+
|
|
12
|
+
type Props = {
|
|
13
|
+
name: string
|
|
14
|
+
clientName: string
|
|
15
|
+
queryKeyName: string
|
|
16
|
+
typeSchemas: OperationSchemas
|
|
17
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type GetParamsProps = {
|
|
21
|
+
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
22
|
+
typeSchemas: OperationSchemas
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getParams({ pathParamsType, typeSchemas }: GetParamsProps) {
|
|
26
|
+
return FunctionParams.factory({
|
|
27
|
+
pathParams: {
|
|
28
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
29
|
+
children: getPathParams(typeSchemas.pathParams, { typed: true }),
|
|
30
|
+
},
|
|
31
|
+
data: typeSchemas.request?.name
|
|
32
|
+
? {
|
|
33
|
+
type: typeSchemas.request?.name,
|
|
34
|
+
optional: isOptional(typeSchemas.request?.schema),
|
|
35
|
+
}
|
|
36
|
+
: undefined,
|
|
37
|
+
params: typeSchemas.queryParams?.name
|
|
38
|
+
? {
|
|
39
|
+
type: typeSchemas.queryParams?.name,
|
|
40
|
+
optional: isOptional(typeSchemas.queryParams?.schema),
|
|
41
|
+
}
|
|
42
|
+
: undefined,
|
|
43
|
+
headers: typeSchemas.headerParams?.name
|
|
44
|
+
? {
|
|
45
|
+
type: typeSchemas.headerParams?.name,
|
|
46
|
+
optional: isOptional(typeSchemas.headerParams?.schema),
|
|
47
|
+
}
|
|
48
|
+
: undefined,
|
|
49
|
+
config: {
|
|
50
|
+
type: typeSchemas.request?.name ? `Partial<RequestConfig<${typeSchemas.request?.name}>>` : 'Partial<RequestConfig>',
|
|
51
|
+
default: '{}',
|
|
52
|
+
},
|
|
53
|
+
})
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function QueryOptions({ name, clientName, typeSchemas, pathParamsType, queryKeyName }: Props): ReactNode {
|
|
57
|
+
const params = getParams({ pathParamsType, typeSchemas })
|
|
58
|
+
const clientParams = Client.getParams({
|
|
59
|
+
typeSchemas,
|
|
60
|
+
pathParamsType,
|
|
61
|
+
})
|
|
62
|
+
const queryKeyParams = QueryKey.getParams({
|
|
63
|
+
pathParamsType,
|
|
64
|
+
typeSchemas,
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<File.Source name={name} isExportable isIndexable>
|
|
69
|
+
<Function name={name} export params={params.toConstructor()}>
|
|
70
|
+
{`
|
|
71
|
+
const queryKey = ${queryKeyName}(${queryKeyParams.toCall()})
|
|
72
|
+
return queryOptions({
|
|
73
|
+
queryKey,
|
|
74
|
+
queryFn: async () => {
|
|
75
|
+
return ${clientName}(${clientParams.toCall()})
|
|
76
|
+
},
|
|
77
|
+
})
|
|
78
|
+
`}
|
|
79
|
+
</Function>
|
|
80
|
+
</File.Source>
|
|
81
|
+
)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
QueryOptions.getParams = getParams
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import client from "@kubb/plugin-client/client";
|
|
2
|
+
import type { RequestConfig, ResponseConfig } from "@kubb/plugin-client/client";
|
|
3
|
+
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
4
|
+
import { createQuery, queryOptions } from "@tanstack/svelte-query";
|
|
5
|
+
|
|
6
|
+
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
|
+
|
|
8
|
+
export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
12
|
+
* @summary Finds Pets by tags
|
|
13
|
+
* @link /pet/findByTags
|
|
14
|
+
*/
|
|
15
|
+
async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
|
|
16
|
+
const res = await client<FindPetsByTagsQueryResponse, FindPetsByTags400, unknown>({ method: "get", url: `/pet/findByTags`, params, headers: { ...headers, ...config.headers }, ...config });
|
|
17
|
+
return { ...res, data: findPetsByTagsQueryResponse.parse(res.data) };
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
|
|
21
|
+
const queryKey = findPetsByTagsQueryKey(params);
|
|
22
|
+
return queryOptions({
|
|
23
|
+
queryKey,
|
|
24
|
+
queryFn: async () => {
|
|
25
|
+
return findPetsByTags(headers, params, config);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
32
|
+
* @summary Finds Pets by tags
|
|
33
|
+
* @link /pet/findByTags
|
|
34
|
+
*/
|
|
35
|
+
export function createFindPetsByTags<TData = ResponseConfig<FindPetsByTagsQueryResponse>, TQueryData = ResponseConfig<FindPetsByTagsQueryResponse>, TQueryKey extends QueryKey = FindPetsByTagsQueryKey>(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, options: {
|
|
36
|
+
query?: Partial<CreateBaseQueryOptions<ResponseConfig<FindPetsByTagsQueryResponse>, FindPetsByTags400, TData, TQueryData, TQueryKey>>;
|
|
37
|
+
client?: Partial<RequestConfig>;
|
|
38
|
+
} = {}) {
|
|
39
|
+
const { query: queryOptions, client: config = {} } = options ?? {};
|
|
40
|
+
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params);
|
|
41
|
+
const query = createQuery({
|
|
42
|
+
...findPetsByTagsQueryOptions(headers, params, config) as unknown as CreateBaseQueryOptions,
|
|
43
|
+
queryKey,
|
|
44
|
+
...queryOptions as unknown as Omit<CreateBaseQueryOptions, "queryKey">
|
|
45
|
+
}) as CreateQueryResult<TData, FindPetsByTags400> & {
|
|
46
|
+
queryKey: TQueryKey;
|
|
47
|
+
};
|
|
48
|
+
query.queryKey = queryKey as TQueryKey;
|
|
49
|
+
return query;
|
|
50
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import client from "axios";
|
|
2
|
+
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
3
|
+
import type { RequestConfig } from "axios";
|
|
4
|
+
import { createQuery, queryOptions } from "@tanstack/svelte-query";
|
|
5
|
+
|
|
6
|
+
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
|
+
|
|
8
|
+
export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
12
|
+
* @summary Finds Pets by tags
|
|
13
|
+
* @link /pet/findByTags
|
|
14
|
+
*/
|
|
15
|
+
async function findPetsByTags(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
|
|
16
|
+
const res = await client<FindPetsByTagsQueryResponse, FindPetsByTags400, unknown>({ method: "get", url: `/pet/findByTags`, params, headers: { ...headers, ...config.headers }, ...config });
|
|
17
|
+
return findPetsByTagsQueryResponse.parse(res.data);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function findPetsByTagsQueryOptions(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
|
|
21
|
+
const queryKey = findPetsByTagsQueryKey(params);
|
|
22
|
+
return queryOptions({
|
|
23
|
+
queryKey,
|
|
24
|
+
queryFn: async () => {
|
|
25
|
+
return findPetsByTags(headers, params, config);
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
32
|
+
* @summary Finds Pets by tags
|
|
33
|
+
* @link /pet/findByTags
|
|
34
|
+
*/
|
|
35
|
+
export function createFindPetsByTags<TData = FindPetsByTagsQueryResponse, TQueryData = FindPetsByTagsQueryResponse, TQueryKey extends QueryKey = FindPetsByTagsQueryKey>(headers: FindPetsByTagsHeaderParams, params?: FindPetsByTagsQueryParams, options: {
|
|
36
|
+
query?: Partial<CreateBaseQueryOptions<FindPetsByTagsQueryResponse, FindPetsByTags400, TData, TQueryData, TQueryKey>>;
|
|
37
|
+
client?: Partial<RequestConfig>;
|
|
38
|
+
} = {}) {
|
|
39
|
+
const { query: queryOptions, client: config = {} } = options ?? {};
|
|
40
|
+
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params);
|
|
41
|
+
const query = createQuery({
|
|
42
|
+
...findPetsByTagsQueryOptions(headers, params, config) as unknown as CreateBaseQueryOptions,
|
|
43
|
+
queryKey,
|
|
44
|
+
...queryOptions as unknown as Omit<CreateBaseQueryOptions, "queryKey">
|
|
45
|
+
}) as CreateQueryResult<TData, FindPetsByTags400> & {
|
|
46
|
+
queryKey: TQueryKey;
|
|
47
|
+
};
|
|
48
|
+
query.queryKey = queryKey as TQueryKey;
|
|
49
|
+
return query;
|
|
50
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import client from "axios";
|
|
2
|
+
import type { CreateMutationOptions } from "@tanstack/svelte-query";
|
|
3
|
+
import type { RequestConfig } from "axios";
|
|
4
|
+
import { createMutation } from "@tanstack/svelte-query";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @summary Updates a pet in the store with form data
|
|
8
|
+
* @link /pet/:petId
|
|
9
|
+
*/
|
|
10
|
+
async function updatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams, config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {}) {
|
|
11
|
+
const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationRequest>({ method: "post", url: `/pet/${petId}`, params, data, ...config });
|
|
12
|
+
return updatePetWithFormMutationResponse.parse(res.data);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @summary Updates a pet in the store with form data
|
|
17
|
+
* @link /pet/:petId
|
|
18
|
+
*/
|
|
19
|
+
export function createUpdatePetWithForm(options: {
|
|
20
|
+
mutation?: CreateMutationOptions<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, {
|
|
21
|
+
petId: UpdatePetWithFormPathParams["petId"];
|
|
22
|
+
data?: UpdatePetWithFormMutationRequest;
|
|
23
|
+
params?: UpdatePetWithFormQueryParams;
|
|
24
|
+
}>;
|
|
25
|
+
client?: Partial<RequestConfig<UpdatePetWithFormMutationRequest>>;
|
|
26
|
+
} = {}) {
|
|
27
|
+
const { mutation: mutationOptions, client: config = {} } = options ?? {};
|
|
28
|
+
return createMutation({
|
|
29
|
+
mutationFn: async ({ petId, data, params }: {
|
|
30
|
+
petId: UpdatePetWithFormPathParams["petId"];
|
|
31
|
+
data?: UpdatePetWithFormMutationRequest;
|
|
32
|
+
params?: UpdatePetWithFormQueryParams;
|
|
33
|
+
}) => {
|
|
34
|
+
return updatePetWithForm(petId, data, params, config);
|
|
35
|
+
},
|
|
36
|
+
...mutationOptions
|
|
37
|
+
});
|
|
38
|
+
}
|