@kubb/plugin-swr 0.0.0-canary-20241104172400

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.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -0
  3. package/dist/chunk-2KDZWGWG.js +342 -0
  4. package/dist/chunk-2KDZWGWG.js.map +1 -0
  5. package/dist/chunk-64KLXAGI.cjs +237 -0
  6. package/dist/chunk-64KLXAGI.cjs.map +1 -0
  7. package/dist/chunk-AFAHUG3Q.cjs +348 -0
  8. package/dist/chunk-AFAHUG3Q.cjs.map +1 -0
  9. package/dist/chunk-T6K6P5I7.js +234 -0
  10. package/dist/chunk-T6K6P5I7.js.map +1 -0
  11. package/dist/components.cjs +28 -0
  12. package/dist/components.cjs.map +1 -0
  13. package/dist/components.d.cts +95 -0
  14. package/dist/components.d.ts +95 -0
  15. package/dist/components.js +3 -0
  16. package/dist/components.js.map +1 -0
  17. package/dist/generators.cjs +17 -0
  18. package/dist/generators.cjs.map +1 -0
  19. package/dist/generators.d.cts +11 -0
  20. package/dist/generators.d.ts +11 -0
  21. package/dist/generators.js +4 -0
  22. package/dist/generators.js.map +1 -0
  23. package/dist/index.cjs +127 -0
  24. package/dist/index.cjs.map +1 -0
  25. package/dist/index.d.cts +10 -0
  26. package/dist/index.d.ts +10 -0
  27. package/dist/index.js +120 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/types-AhGZx7jH.d.cts +272 -0
  30. package/dist/types-AhGZx7jH.d.ts +272 -0
  31. package/package.json +104 -0
  32. package/src/components/Mutation.tsx +129 -0
  33. package/src/components/MutationKey.tsx +54 -0
  34. package/src/components/Query.tsx +175 -0
  35. package/src/components/QueryKey.tsx +83 -0
  36. package/src/components/QueryOptions.tsx +116 -0
  37. package/src/components/index.ts +5 -0
  38. package/src/generators/__snapshots__/clientDataReturnTypeFull.ts +49 -0
  39. package/src/generators/__snapshots__/clientGetImportPath.ts +49 -0
  40. package/src/generators/__snapshots__/clientPostImportPath.ts +32 -0
  41. package/src/generators/__snapshots__/findByTags.ts +49 -0
  42. package/src/generators/__snapshots__/findByTagsObject.ts +49 -0
  43. package/src/generators/__snapshots__/findByTagsPathParamsObject.ts +49 -0
  44. package/src/generators/__snapshots__/findByTagsWithZod.ts +49 -0
  45. package/src/generators/__snapshots__/getAsMutation.ts +34 -0
  46. package/src/generators/__snapshots__/postAsQuery.ts +50 -0
  47. package/src/generators/__snapshots__/updatePetById.ts +32 -0
  48. package/src/generators/__snapshots__/updatePetByIdPathParamsObject.ts +34 -0
  49. package/src/generators/index.ts +2 -0
  50. package/src/generators/mutationGenerator.tsx +117 -0
  51. package/src/generators/queryGenerator.tsx +127 -0
  52. package/src/index.ts +4 -0
  53. package/src/plugin.ts +141 -0
  54. package/src/types.ts +126 -0
@@ -0,0 +1,49 @@
1
+ import client from "@kubb/plugin-client/client";
2
+ import useSWR from "swr";
3
+ import type { RequestConfig } from "@kubb/plugin-client/client";
4
+
5
+ export const findPetsByTagsQueryKey = (params?: FindPetsByTagsQueryParams) => [{ url: "/pet/findByTags" }, ...(params ? [params] : [])] as const;
6
+
7
+ export type FindPetsByTagsQueryKey = ReturnType<typeof findPetsByTagsQueryKey>;
8
+
9
+ /**
10
+ * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
11
+ * @summary Finds Pets by tags
12
+ * @link /pet/findByTags
13
+ */
14
+ async function findPetsByTags({ params }: {
15
+ params?: FindPetsByTagsQueryParams;
16
+ }, config: Partial<RequestConfig> = {}) {
17
+ const res = await client<FindPetsByTagsQueryResponse, FindPetsByTags400, unknown>({ method: "GET", url: `/pet/findByTags`, params, ...config });
18
+ return findPetsByTagsQueryResponse.parse(res.data);
19
+ }
20
+
21
+ export function findPetsByTagsQueryOptions({ params }: {
22
+ params?: FindPetsByTagsQueryParams;
23
+ }, config: Partial<RequestConfig> = {}) {
24
+ return {
25
+ fetcher: async () => {
26
+ return findPetsByTags({ params }, config);
27
+ },
28
+ };
29
+ }
30
+
31
+ /**
32
+ * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
33
+ * @summary Finds Pets by tags
34
+ * @link /pet/findByTags
35
+ */
36
+ export function useFindPetsByTags({ params }: {
37
+ params?: FindPetsByTagsQueryParams;
38
+ }, options: {
39
+ query?: Parameters<typeof useSWR<FindPetsByTagsQueryResponse, FindPetsByTags400, FindPetsByTagsQueryKey | null, any>>[2];
40
+ client?: Partial<RequestConfig>;
41
+ shouldFetch?: boolean;
42
+ } = {}) {
43
+ const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {};
44
+ const queryKey = findPetsByTagsQueryKey(params);
45
+ return useSWR<FindPetsByTagsQueryResponse, FindPetsByTags400, FindPetsByTagsQueryKey | null>(shouldFetch ? queryKey : null, {
46
+ ...findPetsByTagsQueryOptions({ params }, config),
47
+ ...queryOptions
48
+ });
49
+ }
@@ -0,0 +1,34 @@
1
+ import client from "@kubb/plugin-client/client";
2
+ import useSWRMutation from "custom-swr/mutation";
3
+ import type { RequestConfig } from "@kubb/plugin-client/client";
4
+
5
+ export const findPetsByTagsMutationKey = () => [{ "url": "/pet/findByTags" }] as const;
6
+
7
+ export type FindPetsByTagsMutationKey = ReturnType<typeof findPetsByTagsMutationKey>;
8
+
9
+ /**
10
+ * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
11
+ * @summary Finds Pets by tags
12
+ * @link /pet/findByTags
13
+ */
14
+ async function findPetsByTags(params?: FindPetsByTagsQueryParams, config: Partial<RequestConfig> = {}) {
15
+ const res = await client<FindPetsByTagsQueryResponse, FindPetsByTags400, unknown>({ method: "GET", url: `/pet/findByTags`, params, ...config });
16
+ return res.data;
17
+ }
18
+
19
+ /**
20
+ * @description Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
21
+ * @summary Finds Pets by tags
22
+ * @link /pet/findByTags
23
+ */
24
+ export function useFindPetsByTags(params?: FindPetsByTagsQueryParams, options: {
25
+ mutation?: Parameters<typeof useSWRMutation<FindPetsByTagsQueryResponse, FindPetsByTags400, FindPetsByTagsMutationKey>>[2];
26
+ client?: Partial<RequestConfig>;
27
+ shouldFetch?: boolean;
28
+ } = {}) {
29
+ const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {};
30
+ const mutationKey = findPetsByTagsMutationKey();
31
+ return useSWRMutation<FindPetsByTagsQueryResponse, FindPetsByTags400, FindPetsByTagsMutationKey | null>(shouldFetch ? mutationKey : null, async (_url) => {
32
+ return findPetsByTags(params, config);
33
+ }, mutationOptions);
34
+ }
@@ -0,0 +1,50 @@
1
+ import client from "@kubb/plugin-client/client";
2
+ import useSWR from "custom-swr";
3
+ import type { RequestConfig } from "@kubb/plugin-client/client";
4
+
5
+ export const updatePetWithFormQueryKey = (petId: UpdatePetWithFormPathParams["petId"], params?: UpdatePetWithFormQueryParams) => [{ url: "/pet/:petId", params: { petId: petId } }, ...(params ? [params] : [])] as const;
6
+
7
+ export type UpdatePetWithFormQueryKey = ReturnType<typeof updatePetWithFormQueryKey>;
8
+
9
+ /**
10
+ * @summary Updates a pet in the store with form data
11
+ * @link /pet/:petId
12
+ */
13
+ async function updatePetWithForm({ petId, params }: {
14
+ petId: UpdatePetWithFormPathParams["petId"];
15
+ params?: UpdatePetWithFormQueryParams;
16
+ }, config: Partial<RequestConfig> = {}) {
17
+ const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, unknown>({ method: "POST", url: `/pet/${petId}`, params, ...config });
18
+ return res.data;
19
+ }
20
+
21
+ export function updatePetWithFormQueryOptions({ petId, params }: {
22
+ petId: UpdatePetWithFormPathParams["petId"];
23
+ params?: UpdatePetWithFormQueryParams;
24
+ }, config: Partial<RequestConfig> = {}) {
25
+ return {
26
+ fetcher: async () => {
27
+ return updatePetWithForm({ petId, params }, config);
28
+ },
29
+ };
30
+ }
31
+
32
+ /**
33
+ * @summary Updates a pet in the store with form data
34
+ * @link /pet/:petId
35
+ */
36
+ export function useUpdatePetWithForm({ petId, params }: {
37
+ petId: UpdatePetWithFormPathParams["petId"];
38
+ params?: UpdatePetWithFormQueryParams;
39
+ }, options: {
40
+ query?: Parameters<typeof useSWR<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormQueryKey | null, any>>[2];
41
+ client?: Partial<RequestConfig>;
42
+ shouldFetch?: boolean;
43
+ } = {}) {
44
+ const { query: queryOptions, client: config = {}, shouldFetch = true } = options ?? {};
45
+ const queryKey = updatePetWithFormQueryKey(petId, params);
46
+ return useSWR<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormQueryKey | null>(shouldFetch ? queryKey : null, {
47
+ ...updatePetWithFormQueryOptions({ petId, params }, config),
48
+ ...queryOptions
49
+ });
50
+ }
@@ -0,0 +1,32 @@
1
+ import client from "@kubb/plugin-client/client";
2
+ import useSWRMutation from "swr/mutation";
3
+ import type { RequestConfig } from "@kubb/plugin-client/client";
4
+
5
+ export const updatePetWithFormMutationKey = () => [{ "url": "/pet/{petId}" }] as const;
6
+
7
+ export type UpdatePetWithFormMutationKey = ReturnType<typeof updatePetWithFormMutationKey>;
8
+
9
+ /**
10
+ * @summary Updates a pet in the store with form data
11
+ * @link /pet/:petId
12
+ */
13
+ async function updatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], params?: UpdatePetWithFormQueryParams, config: Partial<RequestConfig> = {}) {
14
+ const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, unknown>({ method: "POST", url: `/pet/${petId}`, params, ...config });
15
+ return res.data;
16
+ }
17
+
18
+ /**
19
+ * @summary Updates a pet in the store with form data
20
+ * @link /pet/:petId
21
+ */
22
+ export function useUpdatePetWithForm(petId: UpdatePetWithFormPathParams["petId"], params?: UpdatePetWithFormQueryParams, options: {
23
+ mutation?: Parameters<typeof useSWRMutation<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationKey>>[2];
24
+ client?: Partial<RequestConfig>;
25
+ shouldFetch?: boolean;
26
+ } = {}) {
27
+ const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {};
28
+ const mutationKey = updatePetWithFormMutationKey();
29
+ return useSWRMutation<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationKey | null>(shouldFetch ? mutationKey : null, async (_url) => {
30
+ return updatePetWithForm(petId, params, config);
31
+ }, mutationOptions);
32
+ }
@@ -0,0 +1,34 @@
1
+ import client from "@kubb/plugin-client/client";
2
+ import useSWRMutation from "swr/mutation";
3
+ import type { RequestConfig } from "@kubb/plugin-client/client";
4
+
5
+ export const updatePetWithFormMutationKey = () => [{ "url": "/pet/{petId}" }] as const;
6
+
7
+ export type UpdatePetWithFormMutationKey = ReturnType<typeof updatePetWithFormMutationKey>;
8
+
9
+ /**
10
+ * @summary Updates a pet in the store with form data
11
+ * @link /pet/:petId
12
+ */
13
+ async function updatePetWithForm({ petId }: UpdatePetWithFormPathParams, params?: UpdatePetWithFormQueryParams, config: Partial<RequestConfig> = {}) {
14
+ const res = await client<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, unknown>({ method: "POST", url: `/pet/${petId}`, params, ...config });
15
+ return res.data;
16
+ }
17
+
18
+ /**
19
+ * @summary Updates a pet in the store with form data
20
+ * @link /pet/:petId
21
+ */
22
+ export function useUpdatePetWithForm({ petId }: {
23
+ petId: UpdatePetWithFormPathParams["petId"];
24
+ }, params?: UpdatePetWithFormQueryParams, options: {
25
+ mutation?: Parameters<typeof useSWRMutation<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationKey>>[2];
26
+ client?: Partial<RequestConfig>;
27
+ shouldFetch?: boolean;
28
+ } = {}) {
29
+ const { mutation: mutationOptions, client: config = {}, shouldFetch = true } = options ?? {};
30
+ const mutationKey = updatePetWithFormMutationKey();
31
+ return useSWRMutation<UpdatePetWithFormMutationResponse, UpdatePetWithForm405, UpdatePetWithFormMutationKey | null>(shouldFetch ? mutationKey : null, async (_url) => {
32
+ return updatePetWithForm({ petId }, params, config);
33
+ }, mutationOptions);
34
+ }
@@ -0,0 +1,2 @@
1
+ export { queryGenerator } from './queryGenerator.tsx'
2
+ export { mutationGenerator } from './mutationGenerator.tsx'
@@ -0,0 +1,117 @@
1
+ import { pluginClientName } from '@kubb/plugin-client'
2
+ import { Client } from '@kubb/plugin-client/components'
3
+ import { createReactGenerator } from '@kubb/plugin-oas'
4
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
5
+ import { pluginTsName } from '@kubb/plugin-ts'
6
+ import { pluginZodName } from '@kubb/plugin-zod'
7
+ import { File, useApp } from '@kubb/react'
8
+ import { MutationKey } from '../components'
9
+ import { Mutation } from '../components'
10
+ import type { PluginSwr } from '../types'
11
+
12
+ export const mutationGenerator = createReactGenerator<PluginSwr>({
13
+ name: 'swr-mutation',
14
+ Operation({ options, operation }) {
15
+ const {
16
+ plugin: {
17
+ options: { output },
18
+ },
19
+ } = useApp<PluginSwr>()
20
+ const { getSchemas, getName, getFile } = useOperationManager()
21
+
22
+ const isQuery = typeof options.query === 'boolean' ? true : options.query?.methods.some((method) => operation.method === method)
23
+ const isMutation = !isQuery && options.mutation && options.mutation.methods.some((method) => operation.method === method)
24
+
25
+ const importPath = options.mutation ? options.mutation.importPath : 'swr'
26
+
27
+ const mutation = {
28
+ name: getName(operation, { type: 'function', prefix: 'use' }),
29
+ typeName: getName(operation, { type: 'type' }),
30
+ file: getFile(operation, { prefix: 'use' }),
31
+ }
32
+
33
+ const type = {
34
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
35
+ //todo remove type?
36
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
37
+ }
38
+
39
+ const zod = {
40
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
41
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
42
+ }
43
+
44
+ const client = {
45
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
46
+ }
47
+
48
+ const mutationKey = {
49
+ name: getName(operation, { type: 'const', suffix: 'MutationKey' }),
50
+ typeName: getName(operation, { type: 'type', suffix: 'MutationKey' }),
51
+ }
52
+
53
+ if (!isMutation) {
54
+ return null
55
+ }
56
+
57
+ return (
58
+ <File baseName={mutation.file.baseName} path={mutation.file.path} meta={mutation.file.meta} banner={output?.banner} footer={output?.footer}>
59
+ {options.parser === 'zod' && <File.Import name={[zod.schemas.response.name]} root={mutation.file.path} path={zod.file.path} />}
60
+ <File.Import name="useSWRMutation" path={importPath} />
61
+ <File.Import name={['SWRMutationResponse']} path={importPath} isTypeOnly />
62
+ <File.Import name={'client'} path={options.client.importPath} />
63
+ <File.Import name={['RequestConfig', 'ResponseConfig']} path={options.client.importPath} isTypeOnly />
64
+ <File.Import
65
+ name={[
66
+ type.schemas.request?.name,
67
+ type.schemas.response.name,
68
+ type.schemas.pathParams?.name,
69
+ type.schemas.queryParams?.name,
70
+ type.schemas.headerParams?.name,
71
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
72
+ ].filter(Boolean)}
73
+ root={mutation.file.path}
74
+ path={type.file.path}
75
+ isTypeOnly
76
+ />
77
+
78
+ <MutationKey
79
+ name={mutationKey.name}
80
+ typeName={mutationKey.typeName}
81
+ operation={operation}
82
+ pathParamsType={options.pathParamsType}
83
+ typeSchemas={type.schemas}
84
+ transformer={options.mutationKey}
85
+ />
86
+
87
+ <Client
88
+ name={client.name}
89
+ isExportable={false}
90
+ isIndexable={false}
91
+ baseURL={options.client.baseURL}
92
+ operation={operation}
93
+ typeSchemas={type.schemas}
94
+ zodSchemas={zod.schemas}
95
+ dataReturnType={options.client.dataReturnType}
96
+ paramsType={options.paramsType}
97
+ pathParamsType={options.pathParamsType}
98
+ parser={options.parser}
99
+ />
100
+ {options.mutation && (
101
+ <Mutation
102
+ name={mutation.name}
103
+ clientName={client.name}
104
+ typeName={mutation.typeName}
105
+ typeSchemas={type.schemas}
106
+ operation={operation}
107
+ dataReturnType={options.client.dataReturnType}
108
+ paramsType={options.paramsType}
109
+ pathParamsType={options.pathParamsType}
110
+ mutationKeyName={mutationKey.name}
111
+ mutationKeyTypeName={mutationKey.typeName}
112
+ />
113
+ )}
114
+ </File>
115
+ )
116
+ },
117
+ })
@@ -0,0 +1,127 @@
1
+ import { pluginClientName } from '@kubb/plugin-client'
2
+ import { Client } from '@kubb/plugin-client/components'
3
+ import { createReactGenerator } from '@kubb/plugin-oas'
4
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
5
+ import { pluginTsName } from '@kubb/plugin-ts'
6
+ import { pluginZodName } from '@kubb/plugin-zod'
7
+ import { File, useApp } from '@kubb/react'
8
+ import { Query, QueryOptions } from '../components'
9
+ import { QueryKey } from '../components'
10
+ import type { PluginSwr } from '../types'
11
+
12
+ export const queryGenerator = createReactGenerator<PluginSwr>({
13
+ name: 'swr-query',
14
+ Operation({ options, operation }) {
15
+ const {
16
+ plugin: {
17
+ options: { output },
18
+ },
19
+ } = useApp<PluginSwr>()
20
+ const { getSchemas, getName, getFile } = useOperationManager()
21
+
22
+ const isQuery = typeof options.query === 'boolean' ? options.query : !!options.query.methods?.some((method) => operation.method === method)
23
+ const importPath = options.query ? options.query.importPath : 'swr/mutation'
24
+
25
+ const query = {
26
+ name: getName(operation, { type: 'function', prefix: 'use' }),
27
+ typeName: getName(operation, { type: 'type' }),
28
+ file: getFile(operation, { prefix: 'use' }),
29
+ }
30
+
31
+ const client = {
32
+ name: getName(operation, { type: 'function', pluginKey: [pluginClientName] }),
33
+ }
34
+
35
+ const queryOptions = {
36
+ name: getName(operation, { type: 'function', suffix: 'QueryOptions' }),
37
+ }
38
+ const queryKey = {
39
+ name: getName(operation, { type: 'const', suffix: 'QueryKey' }),
40
+ typeName: getName(operation, { type: 'type', suffix: 'QueryKey' }),
41
+ }
42
+
43
+ const type = {
44
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
45
+ //todo remove type?
46
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
47
+ }
48
+
49
+ const zod = {
50
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
51
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
52
+ }
53
+
54
+ if (!isQuery) {
55
+ return null
56
+ }
57
+
58
+ return (
59
+ <File baseName={query.file.baseName} path={query.file.path} meta={query.file.meta} banner={output?.banner} footer={output?.footer}>
60
+ {options.parser === 'zod' && <File.Import name={[zod.schemas.response.name]} root={query.file.path} path={zod.file.path} />}
61
+
62
+ <File.Import name={'client'} path={options.client.importPath} />
63
+ <File.Import name={['RequestConfig']} path={options.client.importPath} isTypeOnly />
64
+ {options.client.dataReturnType === 'full' && <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />}
65
+
66
+ <File.Import
67
+ name={[
68
+ type.schemas.request?.name,
69
+ type.schemas.response.name,
70
+ type.schemas.pathParams?.name,
71
+ type.schemas.queryParams?.name,
72
+ type.schemas.headerParams?.name,
73
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
74
+ ].filter(Boolean)}
75
+ root={query.file.path}
76
+ path={type.file.path}
77
+ isTypeOnly
78
+ />
79
+ <QueryKey
80
+ name={queryKey.name}
81
+ typeName={queryKey.typeName}
82
+ operation={operation}
83
+ pathParamsType={options.pathParamsType}
84
+ typeSchemas={type.schemas}
85
+ transformer={options.queryKey}
86
+ />
87
+ <Client
88
+ name={client.name}
89
+ isExportable={false}
90
+ isIndexable={false}
91
+ baseURL={options.client.baseURL}
92
+ operation={operation}
93
+ typeSchemas={type.schemas}
94
+ zodSchemas={zod.schemas}
95
+ dataReturnType={options.client.dataReturnType}
96
+ paramsType={options.paramsType}
97
+ pathParamsType={options.pathParamsType}
98
+ parser={options.parser}
99
+ />
100
+ <QueryOptions
101
+ name={queryOptions.name}
102
+ clientName={client.name}
103
+ typeSchemas={type.schemas}
104
+ paramsType={options.paramsType}
105
+ pathParamsType={options.pathParamsType}
106
+ />
107
+ {options.query && (
108
+ <>
109
+ <File.Import name="useSWR" path={importPath} />
110
+ <File.Import name={['SWRResponse']} path={importPath} isTypeOnly />
111
+ <Query
112
+ name={query.name}
113
+ queryOptionsName={queryOptions.name}
114
+ typeSchemas={type.schemas}
115
+ paramsType={options.paramsType}
116
+ pathParamsType={options.pathParamsType}
117
+ operation={operation}
118
+ dataReturnType={options.client.dataReturnType}
119
+ queryKeyName={queryKey.name}
120
+ queryKeyTypeName={queryKey.typeName}
121
+ />
122
+ </>
123
+ )}
124
+ </File>
125
+ )
126
+ },
127
+ })
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { pluginSwr } from './plugin.ts'
2
+
3
+ export { pluginSwr, pluginSwrName } from './plugin.ts'
4
+ export type { PluginSwr } from './types.ts'
package/src/plugin.ts ADDED
@@ -0,0 +1,141 @@
1
+ import path from 'node:path'
2
+
3
+ import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
4
+ import { camelCase, pascalCase } from '@kubb/core/transformers'
5
+ import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
6
+
7
+ import { pluginTsName } from '@kubb/plugin-ts'
8
+ import { pluginZodName } from '@kubb/plugin-zod'
9
+
10
+ import type { Plugin } from '@kubb/core'
11
+ import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
12
+ import { MutationKey, QueryKey } from './components'
13
+ import { mutationGenerator, queryGenerator } from './generators'
14
+ import type { PluginSwr } from './types.ts'
15
+
16
+ export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
17
+
18
+ export const pluginSwr = createPlugin<PluginSwr>((options) => {
19
+ const {
20
+ output = { path: 'hooks', barrelType: 'named' },
21
+ group,
22
+ exclude = [],
23
+ include,
24
+ override = [],
25
+ parser = 'client',
26
+ transformers = {},
27
+ query,
28
+ mutation,
29
+ client,
30
+ paramsType = 'inline',
31
+ pathParamsType = 'inline',
32
+ mutationKey = MutationKey.getTransformer,
33
+ queryKey = QueryKey.getTransformer,
34
+ generators = [queryGenerator, mutationGenerator].filter(Boolean),
35
+ } = options
36
+
37
+ return {
38
+ name: pluginSwrName,
39
+ options: {
40
+ output,
41
+ client: {
42
+ importPath: '@kubb/plugin-client/client',
43
+ dataReturnType: 'data',
44
+ ...client,
45
+ },
46
+ queryKey,
47
+ query: {
48
+ importPath: 'swr',
49
+ methods: ['get'],
50
+ ...query,
51
+ },
52
+ mutationKey,
53
+ mutation: {
54
+ importPath: 'swr/mutation',
55
+ methods: ['post', 'put', 'delete', 'patch'],
56
+ ...mutation,
57
+ },
58
+ parser,
59
+ paramsType,
60
+ pathParamsType: paramsType === 'object' ? 'object' : pathParamsType,
61
+ },
62
+ pre: [pluginOasName, pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
63
+ resolvePath(baseName, pathMode, options) {
64
+ const root = path.resolve(this.config.root, this.config.output.path)
65
+ const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
66
+
67
+ if (options?.tag && group?.type === 'tag') {
68
+ const groupName: Group['name'] = group?.name ? group.name : (ctx) => `${ctx.group}Controller`
69
+
70
+ return path.resolve(root, output.path, groupName({ group: camelCase(options.tag) }), baseName)
71
+ }
72
+
73
+ if (mode === 'single') {
74
+ /**
75
+ * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
76
+ * Other plugins then need to call addOrAppend instead of just add from the fileManager class
77
+ */
78
+ return path.resolve(root, output.path)
79
+ }
80
+
81
+ return path.resolve(root, output.path, baseName)
82
+ },
83
+ resolveName(name, type) {
84
+ let resolvedName = camelCase(name)
85
+
86
+ if (type === 'file' || type === 'function') {
87
+ resolvedName = camelCase(name, {
88
+ isFile: type === 'file',
89
+ })
90
+ }
91
+
92
+ if (type === 'type') {
93
+ resolvedName = pascalCase(name)
94
+ }
95
+
96
+ if (type) {
97
+ return transformers?.name?.(resolvedName, type) || resolvedName
98
+ }
99
+
100
+ return resolvedName
101
+ },
102
+ async buildStart() {
103
+ const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
104
+
105
+ const oas = await swaggerPlugin.context.getOas()
106
+ const root = path.resolve(this.config.root, this.config.output.path)
107
+ const mode = FileManager.getMode(path.resolve(root, output.path))
108
+ const baseURL = await swaggerPlugin.context.getBaseURL()
109
+
110
+ if (baseURL) {
111
+ this.plugin.options.client.baseURL = baseURL
112
+ }
113
+ const operationGenerator = new OperationGenerator(this.plugin.options, {
114
+ oas,
115
+ pluginManager: this.pluginManager,
116
+ plugin: this.plugin,
117
+ contentType: swaggerPlugin.context.contentType,
118
+ exclude,
119
+ include,
120
+ override,
121
+ mode,
122
+ })
123
+
124
+ const files = await operationGenerator.build(...generators)
125
+ await this.addFile(...files)
126
+
127
+ const barrelFiles = await this.fileManager.getBarrelFiles({
128
+ type: output.barrelType ?? 'named',
129
+ root,
130
+ output,
131
+ files: this.fileManager.files,
132
+ meta: {
133
+ pluginKey: this.plugin.key,
134
+ },
135
+ logger: this.logger,
136
+ })
137
+
138
+ await this.addFile(...barrelFiles)
139
+ },
140
+ }
141
+ })