@kubb/plugin-swr 5.0.0-beta.56 → 5.0.0-beta.64

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 (37) hide show
  1. package/dist/{components-C8FJ3-Cb.cjs → components-DxWGjLWg.cjs} +73 -134
  2. package/dist/components-DxWGjLWg.cjs.map +1 -0
  3. package/dist/{components-CR9NLFcO.js → components-mWBRE6E4.js} +73 -134
  4. package/dist/components-mWBRE6E4.js.map +1 -0
  5. package/dist/components.cjs +1 -1
  6. package/dist/components.d.ts +1 -1
  7. package/dist/components.js +1 -1
  8. package/dist/{generators-mw3FvFJD.cjs → generators-CJ4ziuNk.cjs} +2 -2
  9. package/dist/{generators-mw3FvFJD.cjs.map → generators-CJ4ziuNk.cjs.map} +1 -1
  10. package/dist/{generators-BB6csINY.js → generators-D2blgLOY.js} +2 -2
  11. package/dist/{generators-BB6csINY.js.map → generators-D2blgLOY.js.map} +1 -1
  12. package/dist/generators.cjs +1 -1
  13. package/dist/generators.d.ts +1 -1
  14. package/dist/generators.js +1 -1
  15. package/dist/index.cjs +8 -8
  16. package/dist/index.cjs.map +1 -1
  17. package/dist/index.d.ts +1 -1
  18. package/dist/index.js +8 -8
  19. package/dist/index.js.map +1 -1
  20. package/dist/{types-Dz_j0Bqv.d.ts → types-DcBaH0gA.d.ts} +3 -3
  21. package/package.json +8 -8
  22. package/dist/components-C8FJ3-Cb.cjs.map +0 -1
  23. package/dist/components-CR9NLFcO.js.map +0 -1
  24. package/src/components/Mutation.tsx +0 -155
  25. package/src/components/MutationKey.tsx +0 -25
  26. package/src/components/Query.tsx +0 -128
  27. package/src/components/QueryKey.tsx +0 -1
  28. package/src/components/QueryOptions.tsx +0 -56
  29. package/src/components/index.ts +0 -5
  30. package/src/generators/index.ts +0 -2
  31. package/src/generators/mutationGenerator.tsx +0 -166
  32. package/src/generators/queryGenerator.tsx +0 -170
  33. package/src/index.ts +0 -2
  34. package/src/plugin.ts +0 -142
  35. package/src/resolvers/resolverSwr.ts +0 -56
  36. package/src/types.ts +0 -194
  37. package/src/utils.ts +0 -17
@@ -1,128 +0,0 @@
1
- import { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import { getEnabledParamNames, markParamsOptional } from '@internals/tanstack-query'
7
- import type { PluginSwr } from '../types.ts'
8
- import { buildQueryKeyParams, buildStatusUnionType, getComments, resolveErrorNames } from '../utils.ts'
9
- import { getQueryOptionsParams } from './QueryOptions.tsx'
10
-
11
- type Props = {
12
- name: string
13
- queryOptionsName: string
14
- queryKeyName: string
15
- queryKeyTypeName: string
16
- node: ast.OperationNode
17
- tsResolver: ResolverTs
18
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
19
- paramsType: PluginSwr['resolvedOptions']['paramsType']
20
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
21
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
22
- }
23
-
24
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
25
- const callPrinter = functionPrinter({ mode: 'call' })
26
-
27
- function buildQueryParamsNode(
28
- node: ast.OperationNode,
29
- options: {
30
- paramsType: PluginSwr['resolvedOptions']['paramsType']
31
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
32
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
33
- dataReturnType: PluginSwr['resolvedOptions']['client']['dataReturnType']
34
- resolver: ResolverTs
35
- },
36
- ): ast.FunctionParametersNode {
37
- const { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver } = options
38
- const responseName = resolver.resolveResponseName(node)
39
- const requestName = node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined
40
- const errorNames = resolveErrorNames(node, resolver)
41
-
42
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, resolver)
43
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
44
-
45
- const optionsParam = ast.createFunctionParameter({
46
- name: 'options',
47
- type: ast.createParamsType({
48
- variant: 'reference',
49
- name: `{
50
- query?: SWRConfiguration<${[TData, TError].join(', ')}>,
51
- client?: ${requestName ? `Partial<RequestConfig<${requestName}>> & { client?: Client }` : 'Partial<RequestConfig> & { client?: Client }'},
52
- shouldFetch?: boolean,
53
- immutable?: boolean
54
- }`,
55
- }),
56
- default: '{}',
57
- })
58
-
59
- return ast.createOperationParams(node, {
60
- paramsType,
61
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
62
- paramsCasing,
63
- resolver,
64
- extraParams: [optionsParam],
65
- })
66
- }
67
-
68
- export function Query({
69
- name,
70
- queryKeyTypeName,
71
- queryOptionsName,
72
- queryKeyName,
73
- paramsType,
74
- paramsCasing,
75
- pathParamsType,
76
- dataReturnType,
77
- node,
78
- tsResolver,
79
- }: Props): KubbReactNode {
80
- const responseName = tsResolver.resolveResponseName(node)
81
- const errorNames = resolveErrorNames(node, tsResolver)
82
-
83
- const TData = dataReturnType === 'data' ? responseName : buildStatusUnionType(node, tsResolver)
84
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
85
- const generics = [TData, TError, `${queryKeyTypeName} | null`]
86
-
87
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
88
- const queryKeyParamsCall = callPrinter.print(queryKeyParamsNode) ?? ''
89
- const enabledNames = getEnabledParamNames(queryKeyParamsNode)
90
-
91
- const queryOptionsParamsNode = getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver })
92
- const queryOptionsParamsCall = callPrinter.print(queryOptionsParamsNode) ?? ''
93
-
94
- const paramsNode = markParamsOptional(
95
- buildQueryParamsNode(node, { paramsType, paramsCasing, pathParamsType, dataReturnType, resolver: tsResolver }),
96
- enabledNames,
97
- )
98
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
99
-
100
- // SWR has no `enabled` option; fold the param-presence check into the null-key gate
101
- // so passing `undefined` for a required param disables the request (mirrors React Query #60).
102
- const shouldFetchExpr = enabledNames.length ? `shouldFetch && !!(${enabledNames.join(' && ')})` : 'shouldFetch'
103
-
104
- return (
105
- <File.Source name={name} isExportable isIndexable>
106
- <Function name={name} export params={paramsSignature} JSDoc={{ comments: getComments(node) }}>
107
- {`
108
- const { query: queryOptions, client: config = {}, shouldFetch = true, immutable } = options ?? {}
109
-
110
- const queryKey = ${queryKeyName}(${queryKeyParamsCall})
111
-
112
- return useSWR<${generics.join(', ')}>(
113
- ${shouldFetchExpr} ? queryKey : null,
114
- {
115
- ...${queryOptionsName}(${queryOptionsParamsCall}),
116
- ...(immutable ? {
117
- revalidateIfStale: false,
118
- revalidateOnFocus: false,
119
- revalidateOnReconnect: false
120
- } : { }),
121
- ...queryOptions,
122
- }
123
- )
124
- `}
125
- </Function>
126
- </File.Source>
127
- )
128
- }
@@ -1 +0,0 @@
1
- export { QueryKey } from '@internals/tanstack-query'
@@ -1,56 +0,0 @@
1
- import type { ast } from '@kubb/core'
2
- import type { ResolverTs } from '@kubb/plugin-ts'
3
- import { functionPrinter } from '@kubb/plugin-ts'
4
- import { File, Function } from '@kubb/renderer-jsx'
5
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
6
- import { buildQueryOptionsParams, getEnabledParamNames, injectNonNullAssertions, markParamsOptional } from '@internals/tanstack-query'
7
- import type { PluginSwr } from '../types.ts'
8
- import { buildQueryKeyParams } from '../utils.ts'
9
-
10
- type Props = {
11
- name: string
12
- clientName: string
13
- node: ast.OperationNode
14
- tsResolver: ResolverTs
15
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
16
- paramsType: PluginSwr['resolvedOptions']['paramsType']
17
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
18
- }
19
-
20
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
21
- const callPrinter = functionPrinter({ mode: 'call' })
22
-
23
- export function getQueryOptionsParams(
24
- node: ast.OperationNode,
25
- options: {
26
- paramsType: PluginSwr['resolvedOptions']['paramsType']
27
- paramsCasing: PluginSwr['resolvedOptions']['paramsCasing']
28
- pathParamsType: PluginSwr['resolvedOptions']['pathParamsType']
29
- resolver: ResolverTs
30
- },
31
- ): ast.FunctionParametersNode {
32
- return buildQueryOptionsParams(node, options)
33
- }
34
-
35
- export function QueryOptions({ name, clientName, node, tsResolver, paramsCasing, paramsType, pathParamsType }: Props): KubbReactNode {
36
- const queryKeyParamsNode = buildQueryKeyParams(node, { pathParamsType, paramsCasing, resolver: tsResolver })
37
- const enabledNames = getEnabledParamNames(queryKeyParamsNode)
38
-
39
- const paramsNode = markParamsOptional(getQueryOptionsParams(node, { paramsType, paramsCasing, pathParamsType, resolver: tsResolver }), enabledNames)
40
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
41
- const clientCallStr = injectNonNullAssertions(callPrinter.print(paramsNode) ?? '', enabledNames)
42
-
43
- return (
44
- <File.Source name={name} isExportable isIndexable>
45
- <Function name={name} export params={paramsSignature}>
46
- {`
47
- return {
48
- fetcher: async () => {
49
- return ${clientName}(${clientCallStr})
50
- },
51
- }
52
- `}
53
- </Function>
54
- </File.Source>
55
- )
56
- }
@@ -1,5 +0,0 @@
1
- export { Mutation } from './Mutation.tsx'
2
- export { MutationKey } from './MutationKey.tsx'
3
- export { Query } from './Query.tsx'
4
- export { QueryKey } from './QueryKey.tsx'
5
- export { QueryOptions } from './QueryOptions.tsx'
@@ -1,2 +0,0 @@
1
- export { mutationGenerator } from './mutationGenerator.tsx'
2
- export { queryGenerator } from './queryGenerator.tsx'
@@ -1,166 +0,0 @@
1
- import path from 'node:path'
2
- import { resolveOperationTypeNames } from '@internals/shared'
3
- import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
- import { ast, defineGenerator } from '@kubb/core'
5
- import { Client, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
9
- import { Mutation, MutationKey } from '../components'
10
- import type { PluginSwr } from '../types'
11
-
12
- /**
13
- * Built-in generator for `useSWRMutation` hooks. Emits one `useFooMutation` hook
14
- * per POST/PUT/DELETE operation (configurable via `mutation.methods`) plus
15
- * the matching `fooMutationKey` helper.
16
- */
17
- export const mutationGenerator = defineGenerator<PluginSwr>({
18
- name: 'swr-mutation',
19
- renderer: jsxRenderer,
20
- operation(node, ctx) {
21
- if (!ast.isHttpOperationNode(node)) return null
22
- const { config, driver, resolver, root } = ctx
23
- const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group } = ctx.options
24
-
25
- const pluginTs = driver.getPlugin(pluginTsName)
26
- if (!pluginTs) return null
27
- const tsResolver = driver.getResolver(pluginTsName)
28
-
29
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
30
- const queryMethods = new Set(query ? query.methods : [])
31
- const isMutation =
32
- mutation !== false &&
33
- !isQuery &&
34
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
35
-
36
- if (!isMutation) return null
37
-
38
- const importPath = mutation ? mutation.importPath : 'swr/mutation'
39
-
40
- const mutationHookName = resolver.resolveMutationName(node)
41
- const mutationKeyName = resolver.resolveMutationKeyName(node)
42
- const mutationKeyTypeName = resolver.resolveMutationKeyTypeName(node)
43
- const mutationArgTypeName = resolver.resolveMutationArgTypeName(node)
44
- const clientName = resolver.resolveClientName(node)
45
-
46
- const meta = {
47
- file: resolver.resolveFile({ name: mutationHookName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group }),
48
- fileTs: tsResolver.resolveFile(
49
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
50
- { root, output: pluginTs.options?.output ?? output, group: pluginTs.options?.group },
51
- ),
52
- }
53
-
54
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing, order: 'body-response-first' })
55
-
56
- const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : undefined
57
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
58
- const fileZod = zodResolver
59
- ? zodResolver.resolveFile(
60
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
61
- { root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
62
- )
63
- : undefined
64
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
65
-
66
- const clientPlugin = driver.getPlugin(pluginClientName)
67
- const hasClientPlugin = clientPlugin?.name === pluginClientName
68
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
69
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : undefined
70
-
71
- const clientFile = shouldUseClientPlugin
72
- ? clientResolver?.resolveFile(
73
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
74
- {
75
- root,
76
- output: clientPlugin?.options?.output ?? output,
77
- group: clientPlugin?.options?.group,
78
- },
79
- )
80
- : undefined
81
-
82
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
83
-
84
- return (
85
- <File
86
- baseName={meta.file.baseName}
87
- path={meta.file.path}
88
- meta={meta.file.meta}
89
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
90
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
91
- >
92
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
93
- {clientOptions.importPath ? (
94
- <>
95
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
96
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
97
- </>
98
- ) : (
99
- <>
100
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
101
- <File.Import
102
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
103
- root={meta.file.path}
104
- path={path.resolve(root, '.kubb/client.ts')}
105
- isTypeOnly
106
- />
107
- </>
108
- )}
109
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
110
- {!shouldUseClientPlugin && node.requestBody?.content?.some((e) => e.contentType === 'multipart/form-data') && (
111
- <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />
112
- )}
113
- {!shouldUseClientPlugin && parser === 'zod' && zodResolver && node.requestBody?.content?.[0]?.schema && (
114
- <File.Import name={['z']} path="zod" isTypeOnly />
115
- )}
116
- {meta.fileTs && importedTypeNames.length > 0 && (
117
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
118
- )}
119
-
120
- <MutationKey
121
- name={mutationKeyName}
122
- typeName={mutationKeyTypeName}
123
- node={node}
124
- pathParamsType={pathParamsType}
125
- paramsCasing={paramsCasing}
126
- transformer={ctx.options.mutationKey}
127
- />
128
-
129
- {!shouldUseClientPlugin && (
130
- <Client
131
- name={resolvedClientName}
132
- baseURL={clientOptions.baseURL}
133
- dataReturnType={clientOptions.dataReturnType || 'data'}
134
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
135
- paramsType={paramsType}
136
- pathParamsType={pathParamsType}
137
- parser={parser}
138
- node={node}
139
- tsResolver={tsResolver}
140
- zodResolver={zodResolver}
141
- />
142
- )}
143
-
144
- {mutation && (
145
- <>
146
- <File.Import name={'useSWRMutation'} path={importPath} />
147
- <File.Import name={['SWRMutationConfiguration']} path={importPath} isTypeOnly />
148
- <Mutation
149
- name={mutationHookName}
150
- clientName={resolvedClientName}
151
- mutationKeyName={mutationKeyName}
152
- mutationKeyTypeName={mutationKeyTypeName}
153
- mutationArgTypeName={mutationArgTypeName}
154
- node={node}
155
- tsResolver={tsResolver}
156
- dataReturnType={clientOptions.dataReturnType || 'data'}
157
- paramsCasing={paramsCasing}
158
- paramsType={paramsType}
159
- pathParamsType={pathParamsType}
160
- />
161
- </>
162
- )}
163
- </File>
164
- )
165
- },
166
- })
@@ -1,170 +0,0 @@
1
- import path from 'node:path'
2
- import { resolveOperationTypeNames } from '@internals/shared'
3
- import { resolveZodSchemaNames } from '@internals/tanstack-query'
4
- import { ast, defineGenerator } from '@kubb/core'
5
- import { Client, isParserEnabled, pluginClientName } from '@kubb/plugin-client'
6
- import { pluginTsName } from '@kubb/plugin-ts'
7
- import { pluginZodName } from '@kubb/plugin-zod'
8
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
9
- import { Query, QueryKey, QueryOptions } from '../components'
10
- import type { PluginSwr } from '../types'
11
-
12
- export const queryGenerator = defineGenerator<PluginSwr>({
13
- name: 'swr-query',
14
- renderer: jsxRenderer,
15
- operation(node, ctx) {
16
- if (!ast.isHttpOperationNode(node)) return null
17
- const { config, driver, resolver, root } = ctx
18
- const { output, query, mutation, paramsCasing, paramsType, pathParamsType, parser, client: clientOptions, group } = ctx.options
19
-
20
- const pluginTs = driver.getPlugin(pluginTsName)
21
- if (!pluginTs) return null
22
- const tsResolver = driver.getResolver(pluginTsName)
23
-
24
- const isQuery = query === false || (!!query && query.methods.some((method) => node.method.toLowerCase() === method.toLowerCase()))
25
- const queryMethods = new Set(query ? query.methods : [])
26
- const isMutation =
27
- mutation !== false &&
28
- !isQuery &&
29
- (mutation ? mutation.methods : []).some((method) => !queryMethods.has(method) && node.method.toLowerCase() === method.toLowerCase())
30
-
31
- if (!isQuery || isMutation) return null
32
-
33
- const importPath = query ? query.importPath : 'swr'
34
-
35
- const queryName = resolver.resolveQueryName(node)
36
- const queryOptionsName = resolver.resolveQueryOptionsName(node)
37
- const queryKeyName = resolver.resolveQueryKeyName(node)
38
- const queryKeyTypeName = resolver.resolveQueryKeyTypeName(node)
39
- const clientName = resolver.resolveClientName(node)
40
-
41
- const meta = {
42
- file: resolver.resolveFile({ name: queryName, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path }, { root, output, group }),
43
- fileTs: tsResolver.resolveFile(
44
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
45
- { root, output: pluginTs.options?.output ?? output, group: pluginTs.options?.group },
46
- ),
47
- }
48
-
49
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, {
50
- paramsCasing,
51
- exclude: [queryKeyTypeName],
52
- order: 'body-response-first',
53
- })
54
-
55
- const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : undefined
56
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : undefined
57
- const fileZod = zodResolver
58
- ? zodResolver.resolveFile(
59
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
60
- { root, output: pluginZod?.options?.output ?? output, group: pluginZod?.options?.group },
61
- )
62
- : undefined
63
- const zodSchemaNames = resolveZodSchemaNames(node, zodResolver, parser)
64
-
65
- const clientPlugin = driver.getPlugin(pluginClientName)
66
- const hasClientPlugin = clientPlugin?.name === pluginClientName
67
- const shouldUseClientPlugin = hasClientPlugin && clientOptions.clientType !== 'class'
68
- const clientResolver = shouldUseClientPlugin ? driver.getResolver(pluginClientName) : undefined
69
-
70
- const clientFile = shouldUseClientPlugin
71
- ? clientResolver?.resolveFile(
72
- { name: node.operationId, extname: '.ts', tag: node.tags[0] ?? 'default', path: node.path },
73
- {
74
- root,
75
- output: clientPlugin?.options?.output ?? output,
76
- group: clientPlugin?.options?.group,
77
- },
78
- )
79
- : undefined
80
-
81
- const resolvedClientName = shouldUseClientPlugin ? (clientResolver?.resolveName(node.operationId) ?? clientName) : clientName
82
-
83
- return (
84
- <File
85
- baseName={meta.file.baseName}
86
- path={meta.file.path}
87
- meta={meta.file.meta}
88
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
89
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
90
- >
91
- {fileZod && zodSchemaNames.length > 0 && <File.Import name={zodSchemaNames} root={meta.file.path} path={fileZod.path} />}
92
- {clientOptions.importPath ? (
93
- <>
94
- {!shouldUseClientPlugin && <File.Import name={'client'} path={clientOptions.importPath} />}
95
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={clientOptions.importPath} isTypeOnly />
96
- </>
97
- ) : (
98
- <>
99
- {!shouldUseClientPlugin && <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />}
100
- <File.Import
101
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
102
- root={meta.file.path}
103
- path={path.resolve(root, '.kubb/client.ts')}
104
- isTypeOnly
105
- />
106
- </>
107
- )}
108
- {shouldUseClientPlugin && clientFile && <File.Import name={[resolvedClientName]} root={meta.file.path} path={clientFile.path} />}
109
- {!shouldUseClientPlugin && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
110
- {meta.fileTs && importedTypeNames.length > 0 && (
111
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
112
- )}
113
-
114
- <QueryKey
115
- name={queryKeyName}
116
- typeName={queryKeyTypeName}
117
- node={node}
118
- tsResolver={tsResolver}
119
- pathParamsType={pathParamsType}
120
- paramsCasing={paramsCasing}
121
- transformer={ctx.options.queryKey}
122
- />
123
-
124
- {!shouldUseClientPlugin && (
125
- <Client
126
- name={resolvedClientName}
127
- baseURL={clientOptions.baseURL}
128
- dataReturnType={clientOptions.dataReturnType || 'data'}
129
- paramsCasing={clientOptions.paramsCasing || paramsCasing}
130
- paramsType={paramsType}
131
- pathParamsType={pathParamsType}
132
- parser={parser}
133
- node={node}
134
- tsResolver={tsResolver}
135
- zodResolver={zodResolver}
136
- />
137
- )}
138
-
139
- <QueryOptions
140
- name={queryOptionsName}
141
- clientName={resolvedClientName}
142
- node={node}
143
- tsResolver={tsResolver}
144
- paramsCasing={paramsCasing}
145
- paramsType={paramsType}
146
- pathParamsType={pathParamsType}
147
- />
148
-
149
- {query && (
150
- <>
151
- <File.Import name={'useSWR'} path={importPath} />
152
- <File.Import name={['SWRConfiguration']} path={importPath} isTypeOnly />
153
- <Query
154
- name={queryName}
155
- queryOptionsName={queryOptionsName}
156
- queryKeyName={queryKeyName}
157
- queryKeyTypeName={queryKeyTypeName}
158
- node={node}
159
- tsResolver={tsResolver}
160
- paramsCasing={paramsCasing}
161
- paramsType={paramsType}
162
- pathParamsType={pathParamsType}
163
- dataReturnType={clientOptions.dataReturnType || 'data'}
164
- />
165
- </>
166
- )}
167
- </File>
168
- )
169
- },
170
- })
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { default, pluginSwr, pluginSwrName } from './plugin.ts'
2
- export type { PluginSwr, Transformer } from './types.ts'
package/src/plugin.ts DELETED
@@ -1,142 +0,0 @@
1
- import path from 'node:path'
2
- import { createGroupConfig } from '@internals/shared'
3
- import { ast, definePlugin } from '@kubb/core'
4
- import { isParserEnabled, pluginClientName } from '@kubb/plugin-client'
5
- import { source as axiosClientSource } from '@kubb/plugin-client/templates/clients/axios.source'
6
- import { source as fetchClientSource } from '@kubb/plugin-client/templates/clients/fetch.source'
7
- import { source as configSource } from '@kubb/plugin-client/templates/config.source'
8
- import { pluginTsName } from '@kubb/plugin-ts'
9
- import { pluginZodName } from '@kubb/plugin-zod'
10
- import { mutationKeyTransformer, queryKeyTransformer } from '@internals/tanstack-query'
11
- import { mutationGenerator, queryGenerator } from './generators'
12
- import { resolverSwr } from './resolvers/resolverSwr.ts'
13
- import type { PluginSwr } from './types.ts'
14
-
15
- export const pluginSwrName = 'plugin-swr' satisfies PluginSwr['name']
16
-
17
- export const pluginSwr = definePlugin<PluginSwr>((options) => {
18
- const {
19
- output = { path: 'hooks', barrel: { type: 'named' } },
20
- group,
21
- exclude = [],
22
- include,
23
- override = [],
24
- parser = false,
25
- paramsType = 'inline',
26
- pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
27
- mutation = {},
28
- query = {},
29
- mutationKey = mutationKeyTransformer,
30
- queryKey = queryKeyTransformer,
31
- paramsCasing,
32
- client,
33
- resolver: userResolver,
34
- transformer: userTransformer,
35
- generators: userGenerators = [],
36
- } = options
37
-
38
- const clientName = client?.client ?? 'axios'
39
- const clientImportPath = client?.importPath ?? (!client?.bundle ? `@kubb/plugin-client/clients/${clientName}` : undefined)
40
-
41
- const selectedGenerators =
42
- options.generators ?? [queryGenerator, mutationGenerator].filter((generator): generator is NonNullable<typeof generator> => Boolean(generator))
43
-
44
- const groupConfig = createGroupConfig(group) ?? undefined
45
-
46
- return {
47
- name: pluginSwrName,
48
- options,
49
- dependencies: [pluginTsName, isParserEnabled(parser) ? pluginZodName : undefined].filter((dependency): dependency is string => Boolean(dependency)),
50
- hooks: {
51
- 'kubb:plugin:setup'(ctx) {
52
- const resolver = userResolver ? { ...resolverSwr, ...userResolver } : resolverSwr
53
-
54
- ctx.setOptions({
55
- output,
56
- client: {
57
- bundle: client?.bundle,
58
- baseURL: client?.baseURL,
59
- client: clientName,
60
- clientType: client?.clientType ?? 'function',
61
- importPath: clientImportPath,
62
- dataReturnType: client?.dataReturnType ?? 'data',
63
- paramsCasing,
64
- },
65
- queryKey,
66
- query:
67
- query === false
68
- ? false
69
- : {
70
- importPath: 'swr',
71
- methods: ['get'],
72
- ...query,
73
- },
74
- mutationKey,
75
- mutation:
76
- mutation === false
77
- ? false
78
- : {
79
- importPath: 'swr/mutation',
80
- methods: ['post', 'put', 'patch', 'delete'],
81
- ...mutation,
82
- },
83
- parser,
84
- paramsType,
85
- pathParamsType,
86
- paramsCasing,
87
- group: groupConfig,
88
- exclude,
89
- include,
90
- override,
91
- resolver,
92
- })
93
- ctx.setResolver(resolver)
94
- if (userTransformer) {
95
- ctx.setTransformer(userTransformer)
96
- }
97
-
98
- for (const gen of selectedGenerators) {
99
- ctx.addGenerator(gen)
100
- }
101
- for (const gen of userGenerators) {
102
- ctx.addGenerator(gen)
103
- }
104
-
105
- const root = path.resolve(ctx.config.root, ctx.config.output.path)
106
- const hasClientPlugin = !!ctx.config.plugins?.some((p) => (p as { name?: string }).name === pluginClientName)
107
-
108
- if (client?.bundle && !hasClientPlugin && !clientImportPath) {
109
- ctx.injectFile({
110
- baseName: 'client.ts',
111
- path: path.resolve(root, '.kubb/client.ts'),
112
- sources: [
113
- ast.createSource({
114
- name: 'client',
115
- nodes: [ast.createText(clientName === 'fetch' ? fetchClientSource : axiosClientSource)],
116
- isExportable: true,
117
- isIndexable: true,
118
- }),
119
- ],
120
- })
121
- }
122
-
123
- if (!hasClientPlugin) {
124
- ctx.injectFile({
125
- baseName: 'config.ts',
126
- path: path.resolve(root, '.kubb/config.ts'),
127
- sources: [
128
- ast.createSource({
129
- name: 'config',
130
- nodes: [ast.createText(configSource)],
131
- isExportable: false,
132
- isIndexable: false,
133
- }),
134
- ],
135
- })
136
- }
137
- },
138
- },
139
- }
140
- })
141
-
142
- export default pluginSwr