@kubb/plugin-client 5.0.0-alpha.33 → 5.0.0-alpha.35

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/src/plugin.ts CHANGED
@@ -1,16 +1,16 @@
1
1
  import path from 'node:path'
2
2
  import { camelCase } from '@internals/utils'
3
- import { createFile, createSource, createText } from '@kubb/ast'
4
- import { createPlugin, type Group, getPreset, mergeGenerators } from '@kubb/core'
3
+
4
+ import { ast, definePlugin, type Group } from '@kubb/core'
5
5
  import { pluginTsName } from '@kubb/plugin-ts'
6
6
  import { pluginZodName } from '@kubb/plugin-zod'
7
- import { version } from '../package.json'
8
7
  import { classClientGenerator } from './generators/classClientGenerator.tsx'
9
8
  import { clientGenerator } from './generators/clientGenerator.tsx'
10
9
  import { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'
11
10
  import { operationsGenerator } from './generators/operationsGenerator.tsx'
12
11
  import { staticClassClientGenerator } from './generators/staticClassClientGenerator.tsx'
13
- import { presets } from './presets.ts'
12
+ import { resolverClient } from './resolvers/resolverClient.ts'
13
+ import { resolverClientLegacy } from './resolvers/resolverClientLegacy.ts'
14
14
  import { source as axiosClientSource } from './templates/clients/axios.source.ts'
15
15
  import { source as fetchClientSource } from './templates/clients/fetch.source.ts'
16
16
  import { source as configSource } from './templates/config.source.ts'
@@ -38,7 +38,7 @@ export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
38
38
  * })
39
39
  * ```
40
40
  */
41
- export const pluginClient = createPlugin<PluginClient>((options) => {
41
+ export const pluginClient = definePlugin<PluginClient>((options) => {
42
42
  const {
43
43
  output = { path: 'clients', barrelType: 'named' },
44
44
  group,
@@ -58,11 +58,13 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
58
58
  bundle = false,
59
59
  wrapper,
60
60
  baseURL,
61
- compatibilityPreset = 'default',
62
61
  resolver: userResolver,
63
62
  transformer: userTransformer,
63
+ compatibilityPreset = 'default',
64
64
  } = options
65
65
 
66
+ const defaultResolver = compatibilityPreset === 'kubbV4' ? resolverClientLegacy : resolverClient
67
+
66
68
  const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : undefined)
67
69
 
68
70
  const selectedGenerators =
@@ -73,125 +75,86 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
73
75
  operations ? operationsGenerator : undefined,
74
76
  ].filter((x): x is NonNullable<typeof x> => Boolean(x))
75
77
 
76
- const preset = getPreset({
77
- preset: compatibilityPreset,
78
- presets,
79
- resolver: userResolver,
80
- transformer: userTransformer,
81
- generators: selectedGenerators,
82
- })
83
-
84
- const generators = preset.generators ?? []
85
- const mergedGenerator = mergeGenerators(generators)
86
-
87
- let resolveNameWarning = false
88
- let resolvePathWarning = false
78
+ const groupConfig = group
79
+ ? ({
80
+ ...group,
81
+ name: group.name
82
+ ? group.name
83
+ : (ctx: { group: string }) => {
84
+ if (group.type === 'path') {
85
+ return `${ctx.group.split('/')[1]}`
86
+ }
87
+ return `${camelCase(ctx.group)}Controller`
88
+ },
89
+ } satisfies Group)
90
+ : undefined
89
91
 
90
92
  return {
91
93
  name: pluginClientName,
92
- version,
93
- get resolver() {
94
- return preset.resolver
95
- },
96
- get transformer() {
97
- return preset.transformer
98
- },
99
- get options() {
100
- return {
101
- client,
102
- clientType,
103
- bundle,
104
- output,
105
- exclude,
106
- include,
107
- override,
108
- group: group
109
- ? ({
110
- ...group,
111
- name: group.name
112
- ? group.name
113
- : (ctx: { group: string }) => {
114
- if (group.type === 'path') {
115
- return `${ctx.group.split('/')[1]}`
116
- }
117
- return `${camelCase(ctx.group)}Controller`
118
- },
119
- } satisfies Group)
120
- : undefined,
121
- parser,
122
- dataReturnType,
123
- importPath: resolvedImportPath,
124
- baseURL,
125
- paramsType,
126
- paramsCasing,
127
- pathParamsType,
128
- urlType,
129
- wrapper,
130
- resolver: preset.resolver,
131
- }
132
- },
133
- pre: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
134
- resolvePath(baseName, pathMode, options) {
135
- if (!resolvePathWarning) {
136
- this.warn('Do not use resolvePath for pluginClient, use resolverClient.resolvePath instead')
137
- resolvePathWarning = true
138
- }
94
+ options,
95
+ dependencies: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
96
+ hooks: {
97
+ 'kubb:plugin:setup'(ctx) {
98
+ const resolver = userResolver ? { ...defaultResolver, ...userResolver } : defaultResolver
139
99
 
140
- return this.plugin.resolver.resolvePath(
141
- { baseName, pathMode, tag: options?.group?.tag, path: options?.group?.path },
142
- { root: this.root, output, group: this.plugin.options.group },
143
- )
144
- },
145
- resolveName(name, type) {
146
- if (!resolveNameWarning) {
147
- this.warn('Do not use resolveName for pluginClient, use resolverClient.default instead')
148
- resolveNameWarning = true
149
- }
100
+ ctx.setOptions({
101
+ client,
102
+ clientType,
103
+ bundle,
104
+ output,
105
+ exclude,
106
+ include,
107
+ override,
108
+ group: groupConfig,
109
+ parser,
110
+ dataReturnType,
111
+ importPath: resolvedImportPath,
112
+ baseURL,
113
+ paramsType,
114
+ paramsCasing,
115
+ pathParamsType,
116
+ urlType,
117
+ wrapper,
118
+ resolver,
119
+ })
120
+ ctx.setResolver(resolver)
121
+ if (userTransformer) {
122
+ ctx.setTransformer(userTransformer)
123
+ }
124
+ for (const gen of selectedGenerators) {
125
+ ctx.addGenerator(gen)
126
+ }
150
127
 
151
- return this.plugin.resolver.default(name, type)
152
- },
153
- async operation(node, options) {
154
- return mergedGenerator.operation?.call(this, node, options)
155
- },
156
- async operations(nodes, options) {
157
- return mergedGenerator.operations?.call(this, nodes, options)
158
- },
159
- async buildStart() {
160
- const { plugin } = this
161
- const root = this.root
128
+ const root = path.resolve(ctx.config.root, ctx.config.output.path)
162
129
 
163
- // pre add bundled fetch
164
- if (bundle && !plugin.options.importPath) {
165
- await this.addFile(
166
- createFile({
130
+ if (bundle && !resolvedImportPath) {
131
+ ctx.injectFile({
167
132
  baseName: 'fetch.ts',
168
133
  path: path.resolve(root, '.kubb/fetch.ts'),
169
134
  sources: [
170
- createSource({
135
+ ast.createSource({
171
136
  name: 'fetch',
172
- nodes: [createText(plugin.options.client === 'fetch' ? fetchClientSource : axiosClientSource)],
137
+ nodes: [ast.createText(client === 'fetch' ? fetchClientSource : axiosClientSource)],
173
138
  isExportable: true,
174
139
  isIndexable: true,
175
140
  }),
176
141
  ],
177
- }),
178
- )
179
- }
142
+ })
143
+ }
180
144
 
181
- await this.addFile(
182
- createFile({
145
+ ctx.injectFile({
183
146
  baseName: 'config.ts',
184
147
  path: path.resolve(root, '.kubb/config.ts'),
185
148
  sources: [
186
- createSource({
149
+ ast.createSource({
187
150
  name: 'config',
188
- nodes: [createText(configSource)],
151
+ nodes: [ast.createText(configSource)],
189
152
  isExportable: false,
190
153
  isIndexable: false,
191
154
  }),
192
155
  ],
193
- }),
194
- )
156
+ })
157
+ },
195
158
  },
196
159
  }
197
160
  })
package/src/types.ts CHANGED
@@ -1,5 +1,5 @@
1
- import type { Visitor } from '@kubb/ast/types'
2
1
  import type {
2
+ ast,
3
3
  CompatibilityPreset,
4
4
  Exclude,
5
5
  Generator,
@@ -195,7 +195,7 @@ export type Options = {
195
195
  * Single AST visitor applied to each node before printing.
196
196
  * Return `null` or `undefined` from a method to leave the node unchanged.
197
197
  */
198
- transformer?: Visitor
198
+ transformer?: ast.Visitor
199
199
  /**
200
200
  * Define some generators next to the client generators.
201
201
  */
package/src/utils.ts CHANGED
@@ -1,11 +1,11 @@
1
1
  import { URLPath } from '@internals/utils'
2
- import type { OperationNode, ParameterNode } from '@kubb/ast/types'
2
+ import type { ast } from '@kubb/core'
3
3
  import { FunctionParams } from '@kubb/core'
4
4
  import type { PluginTs } from '@kubb/plugin-ts'
5
5
  import type { PluginZod } from '@kubb/plugin-zod'
6
6
  import type { PluginClient } from './types.ts'
7
7
 
8
- export function getComments(node: OperationNode): Array<string> {
8
+ export function getComments(node: ast.OperationNode): Array<string> {
9
9
  return [
10
10
  node.description && `@description ${node.description}`,
11
11
  node.summary && `@summary ${node.summary}`,
@@ -17,7 +17,7 @@ export function getComments(node: OperationNode): Array<string> {
17
17
  .filter((x): x is string => Boolean(x))
18
18
  }
19
19
 
20
- export function buildParamsMapping(originalParams: Array<ParameterNode>, casedParams: Array<ParameterNode>): Record<string, string> | undefined {
20
+ export function buildParamsMapping(originalParams: Array<ast.ParameterNode>, casedParams: Array<ast.ParameterNode>): Record<string, string> | undefined {
21
21
  const mapping: Record<string, string> = {}
22
22
  let hasChanged = false
23
23
  originalParams.forEach((param, i) => {
@@ -37,7 +37,7 @@ export function buildHeaders(contentType: string, hasHeaderParams: boolean): Arr
37
37
  ].filter(Boolean) as Array<string>
38
38
  }
39
39
 
40
- export function buildGenerics(node: OperationNode, tsResolver: PluginTs['resolver']): Array<string> {
40
+ export function buildGenerics(node: ast.OperationNode, tsResolver: PluginTs['resolver']): Array<string> {
41
41
  const responseName = tsResolver.resolveResponseName(node)
42
42
  const requestName = node.requestBody?.schema ? tsResolver.resolveDataName(node) : undefined
43
43
  const errorNames = node.responses.filter((r) => Number.parseInt(r.statusCode, 10) >= 400).map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
@@ -53,7 +53,7 @@ export function buildClassClientParams({
53
53
  isFormData,
54
54
  headers,
55
55
  }: {
56
- node: OperationNode
56
+ node: ast.OperationNode
57
57
  path: URLPath
58
58
  baseURL: string | undefined
59
59
  tsResolver: PluginTs['resolver']
@@ -106,7 +106,7 @@ export function buildRequestDataLine({
106
106
  zodResolver,
107
107
  }: {
108
108
  parser: PluginClient['resolvedOptions']['parser'] | undefined
109
- node: OperationNode
109
+ node: ast.OperationNode
110
110
  zodResolver?: PluginZod['resolver']
111
111
  }): string {
112
112
  const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.schema ? zodResolver.resolveDataName?.(node) : undefined
@@ -131,7 +131,7 @@ export function buildReturnStatement({
131
131
  }: {
132
132
  dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
133
133
  parser: PluginClient['resolvedOptions']['parser'] | undefined
134
- node: OperationNode
134
+ node: ast.OperationNode
135
135
  zodResolver?: PluginZod['resolver']
136
136
  }): string {
137
137
  const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) : undefined
package/src/presets.ts DELETED
@@ -1,25 +0,0 @@
1
- import { definePresets } from '@kubb/core'
2
- import { resolverClient } from './resolvers/resolverClient.ts'
3
- import { resolverClientLegacy } from './resolvers/resolverClientLegacy.ts'
4
- import type { ResolverClient } from './types.ts'
5
-
6
- /**
7
- * Built-in preset registry for `@kubb/plugin-client`.
8
- *
9
- * - `default` — uses `resolverClient` with v5 naming conventions.
10
- * - `kubbV4` — uses `resolverClientLegacy` with backward-compatible naming.
11
- *
12
- * Note: Unlike plugin-ts/plugin-zod, generators are not defined here because
13
- * plugin-client selects generators dynamically based on `clientType`, `group`,
14
- * and `operations` options. Generator selection happens in `plugin.ts`.
15
- */
16
- export const presets = definePresets<ResolverClient>({
17
- default: {
18
- name: 'default',
19
- resolver: resolverClient,
20
- },
21
- kubbV4: {
22
- name: 'kubbV4',
23
- resolver: resolverClientLegacy,
24
- },
25
- })