@kubb/plugin-svelte-query 3.0.0-beta.9 → 3.0.1
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-OOD5MIJ6.js → chunk-C3ROXVUH.js} +71 -52
- package/dist/chunk-C3ROXVUH.js.map +1 -0
- package/dist/{chunk-KPPYUC6N.cjs → chunk-EU35MUKK.cjs} +77 -58
- package/dist/chunk-EU35MUKK.cjs.map +1 -0
- package/dist/{chunk-X3FL2LB3.cjs → chunk-KRGCKSGR.cjs} +143 -27
- package/dist/chunk-KRGCKSGR.cjs.map +1 -0
- package/dist/{chunk-MGY7LGCC.js → chunk-R6ZVBNG7.js} +143 -27
- package/dist/chunk-R6ZVBNG7.js.map +1 -0
- package/dist/components.cjs +6 -6
- package/dist/components.d.cts +17 -10
- package/dist/components.d.ts +17 -10
- package/dist/components.js +1 -1
- package/dist/generators.cjs +4 -4
- 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 -25
- 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 -24
- package/dist/index.js.map +1 -1
- package/dist/types-B9W9aYra.d.cts +365 -0
- package/dist/types-B9W9aYra.d.ts +365 -0
- package/package.json +13 -12
- 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/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__/postAsQuery.ts +1 -1
- package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +1 -3
- package/src/generators/mutationGenerator.tsx +30 -18
- package/src/generators/queryGenerator.tsx +29 -17
- package/src/plugin.ts +30 -26
- package/src/types.ts +35 -14
- package/dist/chunk-KPPYUC6N.cjs.map +0 -1
- package/dist/chunk-MGY7LGCC.js.map +0 -1
- package/dist/chunk-OOD5MIJ6.js.map +0 -1
- package/dist/chunk-X3FL2LB3.cjs.map +0 -1
- package/dist/types-CvXX_phR.d.cts +0 -184
- package/dist/types-CvXX_phR.d.ts +0 -184
|
@@ -14,20 +14,59 @@ type Props = {
|
|
|
14
14
|
clientName: string
|
|
15
15
|
queryKeyName: string
|
|
16
16
|
typeSchemas: OperationSchemas
|
|
17
|
+
paramsType: PluginSvelteQuery['resolvedOptions']['paramsType']
|
|
17
18
|
pathParamsType: PluginSvelteQuery['resolvedOptions']['pathParamsType']
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
type GetParamsProps = {
|
|
22
|
+
paramsType: PluginSvelteQuery['resolvedOptions']['paramsType']
|
|
21
23
|
pathParamsType: PluginSvelteQuery['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,9 +92,10 @@ 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({
|
|
98
|
+
paramsType,
|
|
59
99
|
typeSchemas,
|
|
60
100
|
pathParamsType,
|
|
61
101
|
})
|
|
@@ -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, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "@tanstack/svelte-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, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
3
3
|
import type { RequestConfig } from "axios";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "@tanstack/svelte-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, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "@tanstack/svelte-query";
|
|
5
5
|
|
|
6
6
|
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
7
|
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import client from "@kubb/plugin-client/client";
|
|
2
|
+
import type { RequestConfig } from "@kubb/plugin-client/client";
|
|
3
|
+
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
4
|
+
import { queryOptions, createQuery } 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, params }: {
|
|
16
|
+
headers: FindPetsByTagsHeaderParams;
|
|
17
|
+
params?: FindPetsByTagsQueryParams;
|
|
18
|
+
}, config: Partial<RequestConfig> = {}) {
|
|
19
|
+
const res = await client<FindPetsByTagsQueryResponse, FindPetsByTags400, unknown>({ method: "GET", url: `/pet/findByTags`, params, headers: { ...headers, ...config.headers }, ...config });
|
|
20
|
+
return findPetsByTagsQueryResponse.parse(res.data);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function findPetsByTagsQueryOptions({ headers, params }: {
|
|
24
|
+
headers: FindPetsByTagsHeaderParams;
|
|
25
|
+
params?: FindPetsByTagsQueryParams;
|
|
26
|
+
}, config: Partial<RequestConfig> = {}) {
|
|
27
|
+
const queryKey = findPetsByTagsQueryKey(params);
|
|
28
|
+
return queryOptions({
|
|
29
|
+
queryKey,
|
|
30
|
+
queryFn: async ({ signal }) => {
|
|
31
|
+
config.signal = signal;
|
|
32
|
+
return findPetsByTags({ headers, params }, config);
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
39
|
+
* @summary Finds Pets by tags
|
|
40
|
+
* @link /pet/findByTags
|
|
41
|
+
*/
|
|
42
|
+
export function createFindPetsByTags<TData = FindPetsByTagsQueryResponse, TQueryData = FindPetsByTagsQueryResponse, TQueryKey extends QueryKey = FindPetsByTagsQueryKey>({ headers, params }: {
|
|
43
|
+
headers: FindPetsByTagsHeaderParams;
|
|
44
|
+
params?: FindPetsByTagsQueryParams;
|
|
45
|
+
}, options: {
|
|
46
|
+
query?: Partial<CreateBaseQueryOptions<FindPetsByTagsQueryResponse, FindPetsByTags400, TData, TQueryData, TQueryKey>>;
|
|
47
|
+
client?: Partial<RequestConfig>;
|
|
48
|
+
} = {}) {
|
|
49
|
+
const { query: queryOptions, client: config = {} } = options ?? {};
|
|
50
|
+
const queryKey = queryOptions?.queryKey ?? findPetsByTagsQueryKey(params);
|
|
51
|
+
const query = createQuery({
|
|
52
|
+
...findPetsByTagsQueryOptions({ headers, params }, config) as unknown as CreateBaseQueryOptions,
|
|
53
|
+
queryKey,
|
|
54
|
+
...queryOptions as unknown as Omit<CreateBaseQueryOptions, "queryKey">
|
|
55
|
+
}) as CreateQueryResult<TData, FindPetsByTags400> & {
|
|
56
|
+
queryKey: TQueryKey;
|
|
57
|
+
};
|
|
58
|
+
query.queryKey = queryKey as TQueryKey;
|
|
59
|
+
return query;
|
|
60
|
+
}
|
|
@@ -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, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "@tanstack/svelte-query";
|
|
5
5
|
|
|
6
6
|
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
7
|
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import client from "@kubb/plugin-client/client";
|
|
2
2
|
import type { RequestConfig } from "@kubb/plugin-client/client";
|
|
3
3
|
import type { QueryKey, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/react-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "@tanstack/react-query";
|
|
5
5
|
|
|
6
|
-
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [test, { url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
6
|
+
export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => ["test", { url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
|
|
7
7
|
|
|
8
8
|
export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>;
|
|
9
9
|
|
|
@@ -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, CreateBaseQueryOptions, CreateQueryResult } from "@tanstack/svelte-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "@tanstack/svelte-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, CreateBaseQueryOptions, CreateQueryResult } from "custom-query";
|
|
4
|
-
import {
|
|
4
|
+
import { queryOptions, createQuery } from "custom-query";
|
|
5
5
|
|
|
6
6
|
export const updatePetWithFormQueryKey = (petId: UpdatePetWithFormPathParams["petId"], data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams) => [{ url: "/pet/:petId", params: { petId: petId } }, ...(params ? [params] : []), ...(data ? [data] : [])] as const;
|
|
7
7
|
|
|
@@ -11,9 +11,7 @@ import { createMutation } from "@tanstack/svelte-query";
|
|
|
11
11
|
* @summary Updates a pet in the store with form data
|
|
12
12
|
* @link /pet/:petId
|
|
13
13
|
*/
|
|
14
|
-
async function updatePetWithForm({ petId }: {
|
|
15
|
-
petId: UpdatePetWithFormPathParams["petId"];
|
|
16
|
-
}, data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams, config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {}) {
|
|
14
|
+
async function updatePetWithForm({ petId }: UpdatePetWithFormPathParams, data?: UpdatePetWithFormMutationRequest, params?: UpdatePetWithFormQueryParams, config: Partial<RequestConfig<UpdatePetWithFormMutationRequest>> = {}) {
|
|
17
15
|
const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationRequest>({ method: "POST", url: `/pet/${petId}`, params, data, ...config });
|
|
18
16
|
return updatePetWithFormMutationResponse.parse(res.data);
|
|
19
17
|
}
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import transformers from '@kubb/core/transformers'
|
|
2
1
|
import { pluginClientName } from '@kubb/plugin-client'
|
|
3
2
|
import { Client } from '@kubb/plugin-client/components'
|
|
4
3
|
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
@@ -6,6 +5,7 @@ import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
|
6
5
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
6
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
7
|
import { File, useApp } from '@kubb/react'
|
|
8
|
+
import { difference } from 'remeda'
|
|
9
9
|
import { Mutation, MutationKey } from '../components'
|
|
10
10
|
import type { PluginSvelteQuery } from '../types'
|
|
11
11
|
|
|
@@ -19,8 +19,12 @@ export const mutationGenerator = createReactGenerator<PluginSvelteQuery>({
|
|
|
19
19
|
} = useApp<PluginSvelteQuery>()
|
|
20
20
|
const { getSchemas, getName, getFile } = useOperationManager()
|
|
21
21
|
|
|
22
|
-
const isQuery =
|
|
23
|
-
const isMutation =
|
|
22
|
+
const isQuery = !!options.query && options.query?.methods.some((method) => operation.method === method)
|
|
23
|
+
const isMutation =
|
|
24
|
+
!isQuery &&
|
|
25
|
+
difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some((method) => operation.method === method)
|
|
26
|
+
|
|
27
|
+
const importPath = options.mutation ? options.mutation.importPath : '@tanstack/svelte-query'
|
|
24
28
|
|
|
25
29
|
const mutation = {
|
|
26
30
|
name: getName(operation, { type: 'function', prefix: 'create' }),
|
|
@@ -48,15 +52,14 @@ export const mutationGenerator = createReactGenerator<PluginSvelteQuery>({
|
|
|
48
52
|
typeName: getName(operation, { type: 'type', suffix: 'MutationKey' }),
|
|
49
53
|
}
|
|
50
54
|
|
|
51
|
-
if (!isMutation
|
|
55
|
+
if (!isMutation) {
|
|
52
56
|
return null
|
|
53
57
|
}
|
|
54
58
|
|
|
55
59
|
return (
|
|
56
60
|
<File baseName={mutation.file.baseName} path={mutation.file.path} meta={mutation.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
57
61
|
{options.parser === 'zod' && <File.Import name={[zod.schemas.response.name]} root={mutation.file.path} path={zod.file.path} />}
|
|
58
|
-
|
|
59
|
-
<File.Import name={['CreateMutationOptions', 'CreateMutationResult']} path={options.mutation.importPath} isTypeOnly />
|
|
62
|
+
|
|
60
63
|
<File.Import name={'client'} path={options.client.importPath} />
|
|
61
64
|
<File.Import name={['RequestConfig', 'ResponseConfig']} path={options.client.importPath} isTypeOnly />
|
|
62
65
|
<File.Import
|
|
@@ -72,36 +75,45 @@ export const mutationGenerator = createReactGenerator<PluginSvelteQuery>({
|
|
|
72
75
|
path={type.file.path}
|
|
73
76
|
isTypeOnly
|
|
74
77
|
/>
|
|
78
|
+
|
|
75
79
|
<MutationKey
|
|
76
80
|
name={mutationKey.name}
|
|
77
81
|
typeName={mutationKey.typeName}
|
|
78
82
|
operation={operation}
|
|
79
83
|
pathParamsType={options.pathParamsType}
|
|
80
84
|
typeSchemas={type.schemas}
|
|
81
|
-
|
|
85
|
+
transformer={options.mutationKey}
|
|
82
86
|
/>
|
|
83
87
|
<Client
|
|
84
88
|
name={client.name}
|
|
85
89
|
isExportable={false}
|
|
86
90
|
isIndexable={false}
|
|
87
|
-
baseURL={options.baseURL}
|
|
91
|
+
baseURL={options.client.baseURL}
|
|
88
92
|
operation={operation}
|
|
89
93
|
typeSchemas={type.schemas}
|
|
90
94
|
zodSchemas={zod.schemas}
|
|
91
95
|
dataReturnType={options.client.dataReturnType}
|
|
96
|
+
paramsType={options.paramsType}
|
|
92
97
|
pathParamsType={options.pathParamsType}
|
|
93
98
|
parser={options.parser}
|
|
94
99
|
/>
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
100
|
+
{options.mutation && (
|
|
101
|
+
<>
|
|
102
|
+
<File.Import name={['createMutation']} path={importPath} />
|
|
103
|
+
<File.Import name={['CreateMutationOptions', 'CreateMutationResult']} path={importPath} isTypeOnly />
|
|
104
|
+
<Mutation
|
|
105
|
+
name={mutation.name}
|
|
106
|
+
clientName={client.name}
|
|
107
|
+
typeName={mutation.typeName}
|
|
108
|
+
typeSchemas={type.schemas}
|
|
109
|
+
operation={operation}
|
|
110
|
+
dataReturnType={options.client.dataReturnType}
|
|
111
|
+
paramsType={options.paramsType}
|
|
112
|
+
pathParamsType={options.pathParamsType}
|
|
113
|
+
mutationKeyName={mutationKey.name}
|
|
114
|
+
/>
|
|
115
|
+
</>
|
|
116
|
+
)}
|
|
105
117
|
</File>
|
|
106
118
|
)
|
|
107
119
|
},
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import transformers from '@kubb/core/transformers'
|
|
2
1
|
import { pluginClientName } from '@kubb/plugin-client'
|
|
3
2
|
import { Client } from '@kubb/plugin-client/components'
|
|
4
3
|
import { createReactGenerator } from '@kubb/plugin-oas'
|
|
@@ -6,6 +5,7 @@ import { useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
|
6
5
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
7
6
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
8
7
|
import { File, useApp } from '@kubb/react'
|
|
8
|
+
import { difference } from 'remeda'
|
|
9
9
|
import { Query, QueryKey, QueryOptions } from '../components'
|
|
10
10
|
import type { PluginSvelteQuery } from '../types'
|
|
11
11
|
|
|
@@ -20,7 +20,10 @@ export const queryGenerator = createReactGenerator<PluginSvelteQuery>({
|
|
|
20
20
|
const { getSchemas, getName, getFile } = useOperationManager()
|
|
21
21
|
|
|
22
22
|
const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
|
|
23
|
-
const isMutation =
|
|
23
|
+
const isMutation = difference(options.mutation ? options.mutation.methods : [], options.query ? options.query.methods : []).some(
|
|
24
|
+
(method) => operation.method === method,
|
|
25
|
+
)
|
|
26
|
+
const importPath = options.query ? options.query.importPath : '@tanstack/svelte-query'
|
|
24
27
|
|
|
25
28
|
const query = {
|
|
26
29
|
name: getName(operation, { type: 'function', prefix: 'create' }),
|
|
@@ -52,15 +55,13 @@ export const queryGenerator = createReactGenerator<PluginSvelteQuery>({
|
|
|
52
55
|
schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
if (!isQuery ||
|
|
58
|
+
if (!isQuery || isMutation) {
|
|
56
59
|
return null
|
|
57
60
|
}
|
|
58
61
|
|
|
59
62
|
return (
|
|
60
63
|
<File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta} banner={output?.banner} footer={output?.footer}>
|
|
61
64
|
{options.parser === 'zod' && <File.Import name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
|
|
62
|
-
<File.Import name={['createQuery', 'queryOptions']} path={options.query.importPath} />
|
|
63
|
-
<File.Import name={['QueryKey', 'WithRequired', 'CreateBaseQueryOptions', 'CreateQueryResult']} path={options.query.importPath} isTypeOnly />
|
|
64
65
|
<File.Import name={'client'} path={options.client.importPath} />
|
|
65
66
|
<File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
|
|
66
67
|
{options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
|
|
@@ -84,37 +85,48 @@ export const queryGenerator = createReactGenerator<PluginSvelteQuery>({
|
|
|
84
85
|
operation={operation}
|
|
85
86
|
pathParamsType={options.pathParamsType}
|
|
86
87
|
typeSchemas={type.schemas}
|
|
87
|
-
|
|
88
|
+
transformer={options.queryKey}
|
|
88
89
|
/>
|
|
90
|
+
|
|
89
91
|
<Client
|
|
90
92
|
name={client.name}
|
|
91
93
|
isExportable={false}
|
|
92
94
|
isIndexable={false}
|
|
93
|
-
baseURL={options.baseURL}
|
|
95
|
+
baseURL={options.client.baseURL}
|
|
94
96
|
operation={operation}
|
|
95
97
|
typeSchemas={type.schemas}
|
|
96
98
|
zodSchemas={zod.schemas}
|
|
97
99
|
dataReturnType={options.client.dataReturnType}
|
|
100
|
+
paramsType={options.paramsType}
|
|
98
101
|
pathParamsType={options.pathParamsType}
|
|
99
102
|
parser={options.parser}
|
|
100
103
|
/>
|
|
104
|
+
<File.Import name={['queryOptions']} path={importPath} />
|
|
101
105
|
<QueryOptions
|
|
102
106
|
name={queryOptions.name}
|
|
103
107
|
clientName={client.name}
|
|
104
108
|
queryKeyName={queryKey.name}
|
|
105
109
|
typeSchemas={type.schemas}
|
|
110
|
+
paramsType={options.paramsType}
|
|
106
111
|
pathParamsType={options.pathParamsType}
|
|
107
112
|
/>
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
113
|
+
{options.query && (
|
|
114
|
+
<>
|
|
115
|
+
<File.Import name={['createQuery']} path={importPath} />
|
|
116
|
+
<File.Import name={['QueryKey', 'CreateBaseQueryOptions', 'CreateQueryResult']} path={importPath} isTypeOnly />
|
|
117
|
+
<Query
|
|
118
|
+
name={query.name}
|
|
119
|
+
queryOptionsName={queryOptions.name}
|
|
120
|
+
typeSchemas={type.schemas}
|
|
121
|
+
pathParamsType={options.pathParamsType}
|
|
122
|
+
operation={operation}
|
|
123
|
+
paramsType={options.paramsType}
|
|
124
|
+
dataReturnType={options.client.dataReturnType}
|
|
125
|
+
queryKeyName={queryKey.name}
|
|
126
|
+
queryKeyTypeName={queryKey.typeName}
|
|
127
|
+
/>
|
|
128
|
+
</>
|
|
129
|
+
)}
|
|
118
130
|
</File>
|
|
119
131
|
)
|
|
120
132
|
},
|
package/src/plugin.ts
CHANGED
|
@@ -2,7 +2,6 @@ import path from 'node:path'
|
|
|
2
2
|
|
|
3
3
|
import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
|
|
4
4
|
import { camelCase, pascalCase } from '@kubb/core/transformers'
|
|
5
|
-
import { renderTemplate } from '@kubb/core/utils'
|
|
6
5
|
import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
|
|
7
6
|
|
|
8
7
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
@@ -10,6 +9,7 @@ import { pluginZodName } from '@kubb/plugin-zod'
|
|
|
10
9
|
|
|
11
10
|
import type { Plugin } from '@kubb/core'
|
|
12
11
|
import type { PluginOas } from '@kubb/plugin-oas'
|
|
12
|
+
import { MutationKey, QueryKey } from './components'
|
|
13
13
|
import { mutationGenerator, queryGenerator } from './generators'
|
|
14
14
|
import type { PluginSvelteQuery } from './types.ts'
|
|
15
15
|
|
|
@@ -24,9 +24,12 @@ export const pluginSvelteQuery = createPlugin<PluginSvelteQuery>((options) => {
|
|
|
24
24
|
override = [],
|
|
25
25
|
parser = 'client',
|
|
26
26
|
transformers = {},
|
|
27
|
+
paramsType = 'inline',
|
|
27
28
|
pathParamsType = 'inline',
|
|
28
29
|
mutation = {},
|
|
29
30
|
query = {},
|
|
31
|
+
mutationKey = MutationKey.getTransformer,
|
|
32
|
+
queryKey = QueryKey.getTransformer,
|
|
30
33
|
generators = [queryGenerator, mutationGenerator].filter(Boolean),
|
|
31
34
|
} = options
|
|
32
35
|
|
|
@@ -34,26 +37,29 @@ export const pluginSvelteQuery = createPlugin<PluginSvelteQuery>((options) => {
|
|
|
34
37
|
name: pluginSvelteQueryName,
|
|
35
38
|
options: {
|
|
36
39
|
output,
|
|
37
|
-
baseURL: undefined,
|
|
38
40
|
client: {
|
|
39
41
|
importPath: '@kubb/plugin-client/client',
|
|
40
42
|
dataReturnType: 'data',
|
|
41
43
|
pathParamsType: 'inline',
|
|
42
44
|
...options.client,
|
|
43
45
|
},
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
46
|
+
queryKey,
|
|
47
|
+
query:
|
|
48
|
+
query === false
|
|
49
|
+
? false
|
|
50
|
+
: {
|
|
51
|
+
methods: ['get'],
|
|
52
|
+
importPath: '@tanstack/svelte-query',
|
|
53
|
+
...query,
|
|
54
|
+
},
|
|
55
|
+
mutationKey,
|
|
50
56
|
mutation: {
|
|
51
|
-
key: (key: unknown[]) => key,
|
|
52
57
|
methods: ['post', 'put', 'patch', 'delete'],
|
|
53
58
|
importPath: '@tanstack/svelte-query',
|
|
54
59
|
...mutation,
|
|
55
60
|
},
|
|
56
|
-
|
|
61
|
+
paramsType,
|
|
62
|
+
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType,
|
|
57
63
|
parser,
|
|
58
64
|
},
|
|
59
65
|
pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
|
|
@@ -103,22 +109,20 @@ export const pluginSvelteQuery = createPlugin<PluginSvelteQuery>((options) => {
|
|
|
103
109
|
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
104
110
|
const baseURL = await swaggerPlugin.context.getBaseURL()
|
|
105
111
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
},
|
|
121
|
-
)
|
|
112
|
+
if (baseURL) {
|
|
113
|
+
this.plugin.options.client.baseURL = baseURL
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
117
|
+
oas,
|
|
118
|
+
pluginManager: this.pluginManager,
|
|
119
|
+
plugin: this.plugin,
|
|
120
|
+
contentType: swaggerPlugin.context.contentType,
|
|
121
|
+
exclude,
|
|
122
|
+
include,
|
|
123
|
+
override,
|
|
124
|
+
mode,
|
|
125
|
+
})
|
|
122
126
|
|
|
123
127
|
const files = await operationGenerator.build(...generators)
|
|
124
128
|
await this.addFile(...files)
|
package/src/types.ts
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
import type { Group, Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
2
|
|
|
3
|
-
import type { HttpMethod } from '@kubb/oas'
|
|
3
|
+
import type { HttpMethod, Operation } from '@kubb/oas'
|
|
4
4
|
import type { PluginClient } from '@kubb/plugin-client'
|
|
5
|
-
import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
5
|
+
import type { Exclude, Generator, Include, OperationSchemas, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
6
|
+
import type { PluginReactQuery } from '@kubb/plugin-react-query'
|
|
7
|
+
|
|
8
|
+
type TransformerProps = {
|
|
9
|
+
operation: Operation
|
|
10
|
+
schemas: OperationSchemas
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export type Transformer = (props: TransformerProps) => unknown[]
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Customize the queryKey
|
|
17
|
+
*/
|
|
18
|
+
type QueryKey = Transformer
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Customize the mutationKey
|
|
22
|
+
*/
|
|
23
|
+
type MutationKey = Transformer
|
|
6
24
|
|
|
7
25
|
type Query = {
|
|
8
|
-
/**
|
|
9
|
-
* Customize the queryKey, here you can specify a suffix.
|
|
10
|
-
*/
|
|
11
|
-
key: (key: unknown[]) => unknown[]
|
|
12
26
|
/**
|
|
13
27
|
* Define which HttpMethods can be used for queries
|
|
14
28
|
* @default ['get']
|
|
@@ -25,10 +39,6 @@ type Query = {
|
|
|
25
39
|
}
|
|
26
40
|
|
|
27
41
|
type Mutation = {
|
|
28
|
-
/**
|
|
29
|
-
* Customize the queryKey, here you can specify a suffix.
|
|
30
|
-
*/
|
|
31
|
-
key: (key: unknown[]) => unknown[]
|
|
32
42
|
/**
|
|
33
43
|
* Define which HttpMethods can be used for mutations
|
|
34
44
|
* @default ['post', 'put', 'delete']
|
|
@@ -55,7 +65,7 @@ export type Options = {
|
|
|
55
65
|
*/
|
|
56
66
|
group?: Group
|
|
57
67
|
|
|
58
|
-
client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath'>
|
|
68
|
+
client?: Pick<PluginClient['options'], 'dataReturnType' | 'importPath' | 'baseURL'>
|
|
59
69
|
|
|
60
70
|
/**
|
|
61
71
|
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
@@ -69,6 +79,13 @@ export type Options = {
|
|
|
69
79
|
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
70
80
|
*/
|
|
71
81
|
override?: Array<Override<ResolvedOptions>>
|
|
82
|
+
/**
|
|
83
|
+
* How to pass your params
|
|
84
|
+
* - 'object' will return the params and pathParams as an object.
|
|
85
|
+
* - 'inline' will return the params as comma separated params.
|
|
86
|
+
* @default 'inline'
|
|
87
|
+
*/
|
|
88
|
+
paramsType?: 'object' | 'inline'
|
|
72
89
|
/**
|
|
73
90
|
* How to pass your pathParams.
|
|
74
91
|
* - 'object' will return the pathParams as an object.
|
|
@@ -76,16 +93,18 @@ export type Options = {
|
|
|
76
93
|
* @default 'inline'
|
|
77
94
|
*/
|
|
78
95
|
pathParamsType?: PluginClient['options']['pathParamsType']
|
|
96
|
+
queryKey?: QueryKey
|
|
79
97
|
/**
|
|
80
98
|
* Override some useQuery behaviours.
|
|
81
99
|
*/
|
|
82
100
|
query?: Partial<Query> | false
|
|
101
|
+
mutationKey?: MutationKey
|
|
83
102
|
/**
|
|
84
103
|
* Override some useMutation behaviours.
|
|
85
104
|
*/
|
|
86
105
|
mutation?: Mutation | false
|
|
87
106
|
/**
|
|
88
|
-
* Which parser
|
|
107
|
+
* Which parser should be used before returning the data to `@tanstack/query`.
|
|
89
108
|
* `'zod'` will use `@kubb/plugin-zod` to parse the data.
|
|
90
109
|
*/
|
|
91
110
|
parser?: PluginClient['options']['parser']
|
|
@@ -103,11 +122,13 @@ export type Options = {
|
|
|
103
122
|
|
|
104
123
|
type ResolvedOptions = {
|
|
105
124
|
output: Output
|
|
106
|
-
|
|
107
|
-
client: Required<NonNullable<PluginSvelteQuery['options']['client']>>
|
|
125
|
+
client: Required<Omit<NonNullable<PluginReactQuery['options']['client']>, 'baseURL'>> & { baseURL?: string }
|
|
108
126
|
parser: Required<NonNullable<Options['parser']>>
|
|
127
|
+
paramsType: NonNullable<Options['paramsType']>
|
|
109
128
|
pathParamsType: NonNullable<Options['pathParamsType']>
|
|
129
|
+
queryKey: QueryKey | undefined
|
|
110
130
|
query: NonNullable<Required<Query>> | false
|
|
131
|
+
mutationKey: MutationKey | undefined
|
|
111
132
|
mutation: NonNullable<Required<Mutation>> | false
|
|
112
133
|
}
|
|
113
134
|
|