@kubb/plugin-client 5.0.0-alpha.28 → 5.0.0-alpha.29

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 (41) hide show
  1. package/dist/index.cjs +1911 -62
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.ts +468 -2
  4. package/dist/index.js +1903 -65
  5. package/dist/index.js.map +1 -1
  6. package/package.json +5 -20
  7. package/src/components/ClassClient.tsx +42 -138
  8. package/src/components/Client.tsx +85 -125
  9. package/src/components/ClientLegacy.tsx +501 -0
  10. package/src/components/Operations.tsx +8 -8
  11. package/src/components/StaticClassClient.tsx +41 -135
  12. package/src/components/Url.tsx +37 -46
  13. package/src/generators/classClientGenerator.tsx +121 -131
  14. package/src/generators/clientGenerator.tsx +104 -80
  15. package/src/generators/groupedClientGenerator.tsx +28 -30
  16. package/src/generators/operationsGenerator.tsx +11 -17
  17. package/src/generators/staticClassClientGenerator.tsx +115 -121
  18. package/src/index.ts +11 -1
  19. package/src/plugin.ts +121 -92
  20. package/src/presets.ts +25 -0
  21. package/src/resolvers/resolverClient.ts +26 -0
  22. package/src/resolvers/resolverClientLegacy.ts +26 -0
  23. package/src/types.ts +93 -39
  24. package/src/utils.ts +148 -0
  25. package/dist/StaticClassClient-D6v3vhZL.js +0 -695
  26. package/dist/StaticClassClient-D6v3vhZL.js.map +0 -1
  27. package/dist/StaticClassClient-GyNiWMHA.cjs +0 -736
  28. package/dist/StaticClassClient-GyNiWMHA.cjs.map +0 -1
  29. package/dist/components.cjs +0 -7
  30. package/dist/components.d.ts +0 -216
  31. package/dist/components.js +0 -2
  32. package/dist/generators-C0t5dIvZ.js +0 -723
  33. package/dist/generators-C0t5dIvZ.js.map +0 -1
  34. package/dist/generators-D8A8QE4S.cjs +0 -753
  35. package/dist/generators-D8A8QE4S.cjs.map +0 -1
  36. package/dist/generators.cjs +0 -7
  37. package/dist/generators.d.ts +0 -21
  38. package/dist/generators.js +0 -2
  39. package/dist/types-jdcuAELq.d.ts +0 -169
  40. package/src/components/index.ts +0 -5
  41. package/src/generators/index.ts +0 -5
package/src/plugin.ts CHANGED
@@ -1,19 +1,43 @@
1
1
  import path from 'node:path'
2
2
  import { camelCase } from '@internals/utils'
3
- import { createPlugin, getBarrelFiles, getMode, type UserGroup } from '@kubb/core'
4
- import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
3
+ import { walk } from '@kubb/ast'
4
+ import type { OperationNode } from '@kubb/ast/types'
5
+ import { createPlugin, type Group, getBarrelFiles, getPreset, runGeneratorOperation, runGeneratorOperations } from '@kubb/core'
6
+ import { pluginTsName } from '@kubb/plugin-ts'
5
7
  import { pluginZodName } from '@kubb/plugin-zod'
6
- import { classClientGenerator, operationsGenerator } from './generators'
8
+ import { classClientGenerator } from './generators/classClientGenerator.tsx'
7
9
  import { clientGenerator } from './generators/clientGenerator.tsx'
8
10
  import { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'
11
+ import { operationsGenerator } from './generators/operationsGenerator.tsx'
9
12
  import { staticClassClientGenerator } from './generators/staticClassClientGenerator.tsx'
13
+ import { presets } from './presets.ts'
10
14
  import { source as axiosClientSource } from './templates/clients/axios.source.ts'
11
15
  import { source as fetchClientSource } from './templates/clients/fetch.source.ts'
12
16
  import { source as configSource } from './templates/config.source.ts'
13
17
  import type { PluginClient } from './types.ts'
14
18
 
19
+ /**
20
+ * Canonical plugin name for `@kubb/plugin-client`, used to identify the plugin
21
+ * in driver lookups and warnings.
22
+ */
15
23
  export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
16
24
 
25
+ /**
26
+ * The `@kubb/plugin-client` plugin factory.
27
+ *
28
+ * Generates type-safe HTTP client functions (or classes) from an OpenAPI/AST `RootNode`.
29
+ * Walks operations, delegates rendering to the active generators,
30
+ * and writes barrel files based on `output.barrelType`.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * import { pluginClient } from '@kubb/plugin-client'
35
+ *
36
+ * export default defineConfig({
37
+ * plugins: [pluginClient({ output: { path: 'clients' } })],
38
+ * })
39
+ * ```
40
+ */
17
41
  export const pluginClient = createPlugin<PluginClient>((options) => {
18
42
  const {
19
43
  output = { path: 'clients', barrelType: 'named' },
@@ -22,109 +46,121 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
22
46
  exclude = [],
23
47
  include,
24
48
  override = [],
25
- transformers = {},
26
49
  dataReturnType = 'data',
27
50
  paramsType = 'inline',
28
51
  pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
29
52
  operations = false,
30
- baseURL,
31
53
  paramsCasing,
32
54
  clientType = 'function',
33
55
  parser = 'client',
34
56
  client = 'axios',
35
57
  importPath,
36
- contentType,
37
58
  bundle = false,
38
59
  wrapper,
60
+ baseURL,
61
+ compatibilityPreset = 'default',
62
+ resolver: userResolver,
63
+ transformer: userTransformer,
39
64
  } = options
40
65
 
41
66
  const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : undefined)
42
67
 
43
- const defaultGenerators = [
44
- clientType === 'staticClass' ? staticClassClientGenerator : clientType === 'class' ? classClientGenerator : clientGenerator,
45
- group && clientType === 'function' ? groupedClientGenerator : undefined,
46
- operations ? operationsGenerator : undefined,
47
- ].filter((x): x is NonNullable<typeof x> => Boolean(x))
48
-
49
- const generators = options.generators ?? defaultGenerators
68
+ const selectedGenerators =
69
+ options.generators ??
70
+ [
71
+ clientType === 'staticClass' ? staticClassClientGenerator : clientType === 'class' ? classClientGenerator : clientGenerator,
72
+ group && clientType === 'function' ? groupedClientGenerator : undefined,
73
+ operations ? operationsGenerator : undefined,
74
+ ].filter((x): x is NonNullable<typeof x> => Boolean(x))
75
+
76
+ const preset = getPreset({
77
+ preset: compatibilityPreset,
78
+ presets,
79
+ resolver: userResolver,
80
+ transformer: userTransformer,
81
+ generators: selectedGenerators,
82
+ })
83
+
84
+ let resolveNameWarning = false
85
+ let resolvePathWarning = false
50
86
 
51
87
  return {
52
88
  name: pluginClientName,
53
- options: {
54
- client,
55
- clientType,
56
- bundle,
57
- output,
58
- group,
59
- parser,
60
- dataReturnType,
61
- importPath: resolvedImportPath,
62
- paramsType,
63
- paramsCasing,
64
- pathParamsType,
65
- baseURL,
66
- urlType,
67
- wrapper,
89
+ get resolver() {
90
+ return preset.resolver
68
91
  },
69
- pre: [pluginOasName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
70
- resolvePath(baseName, pathMode, options) {
71
- const root = path.resolve(this.config.root, this.config.output.path)
72
- const mode = pathMode ?? getMode(path.resolve(root, output.path))
73
-
74
- if (mode === 'single') {
75
- /**
76
- * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
77
- * Other plugins then need to call addOrAppend instead of just add from the fileManager class
78
- */
79
- return path.resolve(root, output.path)
92
+ get transformer() {
93
+ return preset.transformer
94
+ },
95
+ get options() {
96
+ return {
97
+ client,
98
+ clientType,
99
+ bundle,
100
+ output,
101
+ group: group
102
+ ? ({
103
+ ...group,
104
+ name: group.name
105
+ ? group.name
106
+ : (ctx: { group: string }) => {
107
+ if (group.type === 'path') {
108
+ return `${ctx.group.split('/')[1]}`
109
+ }
110
+ return `${camelCase(ctx.group)}Controller`
111
+ },
112
+ } satisfies Group)
113
+ : undefined,
114
+ parser,
115
+ dataReturnType,
116
+ importPath: resolvedImportPath,
117
+ baseURL,
118
+ paramsType,
119
+ paramsCasing,
120
+ pathParamsType,
121
+ urlType,
122
+ wrapper,
123
+ resolver: preset.resolver,
80
124
  }
81
-
82
- if (group && (options?.group?.path || options?.group?.tag)) {
83
- const groupName: UserGroup['name'] = group?.name
84
- ? group.name
85
- : (ctx) => {
86
- if (group?.type === 'path') {
87
- return `${ctx.group.split('/')[1]}`
88
- }
89
- return `${camelCase(ctx.group)}Controller`
90
- }
91
-
92
- return path.resolve(
93
- root,
94
- output.path,
95
- groupName({
96
- group: group.type === 'path' ? options.group.path! : options.group.tag!,
97
- }),
98
- baseName,
99
- )
125
+ },
126
+ pre: [pluginTsName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
127
+ resolvePath(baseName, pathMode, options) {
128
+ if (!resolvePathWarning) {
129
+ this.events.emit('warn', 'Do not use resolvePath for pluginClient, use resolverClient.resolvePath instead')
130
+ resolvePathWarning = true
100
131
  }
101
132
 
102
- return path.resolve(root, output.path, baseName)
133
+ return this.plugin.resolver.resolvePath(
134
+ { baseName, pathMode, tag: options?.group?.tag, path: options?.group?.path },
135
+ { root: path.resolve(this.config.root, this.config.output.path), output, group: this.plugin.options.group },
136
+ )
103
137
  },
104
138
  resolveName(name, type) {
105
- const resolvedName = camelCase(name, { isFile: type === 'file' })
106
-
107
- if (type) {
108
- return transformers?.name?.(resolvedName, type) || resolvedName
139
+ if (!resolveNameWarning) {
140
+ this.events.emit('warn', 'Do not use resolveName for pluginClient, use resolverClient.default instead')
141
+ resolveNameWarning = true
109
142
  }
110
143
 
111
- return resolvedName
144
+ return this.plugin.resolver.default(name, type)
112
145
  },
113
146
  async install() {
114
- const root = path.resolve(this.config.root, this.config.output.path)
115
- const mode = getMode(path.resolve(root, output.path))
116
- const oas = await this.getOas()
117
- const baseURL = await this.getBaseURL()
147
+ const { config, fabric, plugin, adapter, rootNode, driver } = this
148
+ const root = path.resolve(config.root, config.output.path)
149
+ const resolver = preset.resolver
150
+
151
+ if (!adapter) {
152
+ throw new Error('Plugin cannot work without adapter being set')
153
+ }
118
154
 
119
155
  // pre add bundled fetch
120
- if (bundle && !this.plugin.options.importPath) {
156
+ if (bundle && !plugin.options.importPath) {
121
157
  await this.addFile({
122
158
  baseName: 'fetch.ts',
123
159
  path: path.resolve(root, '.kubb/fetch.ts'),
124
160
  sources: [
125
161
  {
126
162
  name: 'fetch',
127
- value: this.plugin.options.client === 'fetch' ? fetchClientSource : axiosClientSource,
163
+ value: plugin.options.client === 'fetch' ? fetchClientSource : axiosClientSource,
128
164
  isExportable: true,
129
165
  isIndexable: true,
130
166
  },
@@ -149,30 +185,23 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
149
185
  exports: [],
150
186
  })
151
187
 
152
- const operationGenerator = new OperationGenerator(
153
- baseURL
154
- ? {
155
- ...this.plugin.options,
156
- baseURL,
157
- }
158
- : this.plugin.options,
159
- {
160
- fabric: this.fabric,
161
- oas,
162
- driver: this.driver,
163
- events: this.events,
164
- plugin: this.plugin,
165
- contentType,
166
- exclude,
167
- include,
168
- override,
169
- mode,
170
- },
171
- )
188
+ const collectedOperations: Array<OperationNode> = []
189
+ const generatorContext = { generators: preset.generators, plugin, resolver, exclude, include, override, fabric, adapter, config, driver }
172
190
 
173
- const files = await operationGenerator.build(...generators)
191
+ await walk(rootNode, {
192
+ depth: 'shallow',
193
+ async operation(operationNode) {
194
+ const baseOptions = resolver.resolveOptions(operationNode, { options: plugin.options, exclude, include, override })
195
+
196
+ if (baseOptions !== null) {
197
+ collectedOperations.push(operationNode)
198
+ }
199
+
200
+ await runGeneratorOperation(operationNode, generatorContext)
201
+ },
202
+ })
174
203
 
175
- await this.upsertFile(...files)
204
+ await runGeneratorOperations(collectedOperations, generatorContext)
176
205
 
177
206
  const barrelFiles = await getBarrelFiles(this.fabric.files, {
178
207
  type: output.barrelType ?? 'named',
package/src/presets.ts ADDED
@@ -0,0 +1,25 @@
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
+ })
@@ -0,0 +1,26 @@
1
+ import { camelCase } from '@internals/utils'
2
+ import { defineResolver } from '@kubb/core'
3
+ import type { PluginClient } from '../types.ts'
4
+
5
+ /**
6
+ * Resolver for `@kubb/plugin-client` that provides the default naming
7
+ * and path-resolution helpers used by the plugin.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { resolverClient } from '@kubb/plugin-client'
12
+ *
13
+ * resolverClient.default('list pets', 'function') // -> 'listPets'
14
+ * resolverClient.resolveName('show pet by id') // -> 'showPetById'
15
+ * ```
16
+ */
17
+ export const resolverClient = defineResolver<PluginClient>(() => ({
18
+ name: 'default',
19
+ pluginName: 'plugin-client',
20
+ default(name, type) {
21
+ return camelCase(name, { isFile: type === 'file' })
22
+ },
23
+ resolveName(name) {
24
+ return this.default(name, 'function')
25
+ },
26
+ }))
@@ -0,0 +1,26 @@
1
+ import { camelCase } from '@internals/utils'
2
+ import { defineResolver } from '@kubb/core'
3
+ import type { PluginClient } from '../types.ts'
4
+
5
+ /**
6
+ * Legacy resolver for `@kubb/plugin-client` that provides backward-compatible
7
+ * naming conventions matching the v4 behavior.
8
+ *
9
+ * @example
10
+ * ```ts
11
+ * import { resolverClientLegacy } from '@kubb/plugin-client'
12
+ *
13
+ * resolverClientLegacy.default('list pets', 'function') // -> 'listPets'
14
+ * resolverClientLegacy.resolveName('show pet by id') // -> 'showPetById'
15
+ * ```
16
+ */
17
+ export const resolverClientLegacy = defineResolver<PluginClient>(() => ({
18
+ name: 'kubbV4',
19
+ pluginName: 'plugin-client',
20
+ default(name, type) {
21
+ return camelCase(name, { isFile: type === 'file' })
22
+ },
23
+ resolveName(name) {
24
+ return this.default(name, 'function')
25
+ },
26
+ }))
package/src/types.ts CHANGED
@@ -1,8 +1,30 @@
1
- import type { Output, PluginFactoryOptions, ResolveNameParams, UserGroup } from '@kubb/core'
1
+ import type { Visitor } from '@kubb/ast/types'
2
+ import type {
3
+ CompatibilityPreset,
4
+ Exclude,
5
+ Generator,
6
+ Group,
7
+ Include,
8
+ Output,
9
+ Override,
10
+ PluginFactoryOptions,
11
+ ResolvePathOptions,
12
+ Resolver,
13
+ UserGroup,
14
+ } from '@kubb/core'
2
15
 
3
- import type { contentType, Oas } from '@kubb/oas'
4
- import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
5
- import type { Generator } from '@kubb/plugin-oas/generators'
16
+ /**
17
+ * The concrete resolver type for `@kubb/plugin-client`.
18
+ * Extends the base `Resolver` with a `resolveName` helper for client function names.
19
+ */
20
+ export type ResolverClient = Resolver & {
21
+ /**
22
+ * Resolves the function name for a given raw operation name.
23
+ * @example
24
+ * resolver.resolveName('show pet by id') // -> 'showPetById'
25
+ */
26
+ resolveName(this: ResolverClient, name: string): string
27
+ }
6
28
 
7
29
  /**
8
30
  * Use either a preset `client` type OR a custom `importPath`, not both.
@@ -36,17 +58,52 @@ export type ClientImportPath =
36
58
  bundle?: never
37
59
  }
38
60
 
61
+ /**
62
+ * Discriminated union that ties `pathParamsType` to the `paramsType` values where it is meaningful.
63
+ *
64
+ * - `paramsType: 'object'` — all parameters (including path params) are merged into a single
65
+ * destructured object. `pathParamsType` is never reached in this code path and has no effect.
66
+ * - `paramsType?: 'inline'` (or omitted) — each parameter group is a separate function argument.
67
+ * `pathParamsType` controls whether the path-param group itself is destructured (`'object'`)
68
+ * or spread as individual arguments (`'inline'`).
69
+ */
70
+ type ParamsTypeOptions =
71
+ | {
72
+ /**
73
+ * All parameters — path, query, headers, and body — are merged into a single
74
+ * destructured object argument.
75
+ * - 'object' returns the params and pathParams as an object.
76
+ * @default 'inline'
77
+ */
78
+ paramsType: 'object'
79
+ /**
80
+ * `pathParamsType` has no effect when `paramsType` is `'object'`.
81
+ * Path params are already inside the single destructured object.
82
+ */
83
+ pathParamsType?: never
84
+ }
85
+ | {
86
+ /**
87
+ * Each parameter group is emitted as a separate function argument.
88
+ * - 'inline' returns the params as comma separated params.
89
+ * @default 'inline'
90
+ */
91
+ paramsType?: 'inline'
92
+ /**
93
+ * Controls how path parameters are arranged within the inline argument list.
94
+ * - 'object' groups path params into a destructured object: `{ petId }: PathParams`.
95
+ * - 'inline' emits each path param as its own argument: `petId: string`.
96
+ * @default 'inline'
97
+ */
98
+ pathParamsType?: 'object' | 'inline'
99
+ }
100
+
39
101
  export type Options = {
40
102
  /**
41
- * Specify the export location for the files and define the behavior of the output
103
+ * Specify the export location for the files and define the behavior of the output.
42
104
  * @default { path: 'clients', barrelType: 'named' }
43
105
  */
44
- output?: Output<Oas>
45
- /**
46
- * Define which contentType should be used.
47
- * By default, the first JSON valid mediaType is used
48
- */
49
- contentType?: contentType
106
+ output?: Output
50
107
  /**
51
108
  * Group the clients based on the provided name.
52
109
  */
@@ -88,25 +145,11 @@ export type Options = {
88
145
  */
89
146
  dataReturnType?: 'data' | 'full'
90
147
  /**
91
- * How to style your params, by default no casing is applied
148
+ * How to style your params, by default no casing is applied.
92
149
  * - 'camelcase' uses camelCase for pathParams, queryParams and headerParams names
93
150
  * @note response types (data/body) are not affected by this option
94
151
  */
95
152
  paramsCasing?: 'camelcase'
96
- /**
97
- * How to pass your params.
98
- * - 'object' returns the params and pathParams as an object.
99
- * - 'inline' returns the params as comma separated params.
100
- * @default 'inline'
101
- */
102
- paramsType?: 'object' | 'inline'
103
- /**
104
- * How to pass your pathParams.
105
- * - 'object' returns the pathParams as an object.
106
- * - 'inline' returns the pathParams as comma separated params.
107
- * @default 'inline'
108
- */
109
- pathParamsType?: 'object' | 'inline'
110
153
  /**
111
154
  * Which parser can be used before returning the data.
112
155
  * - 'client' returns the data as-is from the client.
@@ -138,33 +181,44 @@ export type Options = {
138
181
  */
139
182
  className: string
140
183
  }
141
- transformers?: {
142
- /**
143
- * Customize the names based on the type that is provided by the plugin.
144
- */
145
- name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
146
- }
147
184
  /**
148
- * Define some generators next to the client generators
185
+ * Apply a compatibility naming preset.
186
+ * @default 'default'
187
+ */
188
+ compatibilityPreset?: CompatibilityPreset
189
+ /**
190
+ * Override individual resolver methods. Any method you omit falls back to the
191
+ * preset resolver's implementation. Use `this.default(...)` to call it.
192
+ */
193
+ resolver?: Partial<ResolverClient> & ThisType<ResolverClient>
194
+ /**
195
+ * Single AST visitor applied to each node before printing.
196
+ * Return `null` or `undefined` from a method to leave the node unchanged.
197
+ */
198
+ transformer?: Visitor
199
+ /**
200
+ * Define some generators next to the client generators.
149
201
  */
150
202
  generators?: Array<Generator<PluginClient>>
151
- } & ClientImportPath
203
+ } & ClientImportPath &
204
+ ParamsTypeOptions
152
205
 
153
206
  type ResolvedOptions = {
154
- output: Output<Oas>
155
- group?: Options['group']
156
- baseURL: string | undefined
207
+ output: Output
208
+ group: Group | undefined
157
209
  client: Options['client']
158
210
  clientType: NonNullable<Options['clientType']>
159
211
  bundle: NonNullable<Options['bundle']>
160
212
  parser: NonNullable<Options['parser']>
161
213
  urlType: NonNullable<Options['urlType']>
162
214
  importPath: Options['importPath']
215
+ baseURL: Options['baseURL']
163
216
  dataReturnType: NonNullable<Options['dataReturnType']>
164
- pathParamsType: NonNullable<Options['pathParamsType']>
217
+ pathParamsType: NonNullable<NonNullable<Options['pathParamsType']>>
165
218
  paramsType: NonNullable<Options['paramsType']>
166
219
  paramsCasing: Options['paramsCasing']
167
220
  wrapper: Options['wrapper']
221
+ resolver: ResolverClient
168
222
  }
169
223
 
170
- export type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>
224
+ export type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions, ResolverClient>