@kubb/plugin-client 5.0.0-beta.56 → 5.5.2

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 (55) hide show
  1. package/README.md +8 -61
  2. package/dist/index.cjs +754 -1745
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.ts +170 -349
  5. package/dist/index.js +733 -1733
  6. package/dist/index.js.map +1 -1
  7. package/package.json +10 -63
  8. package/src/generators/clientGenerator.tsx +6 -132
  9. package/src/index.ts +1 -9
  10. package/src/plugin.ts +32 -122
  11. package/src/types.ts +27 -249
  12. package/dist/chunk-Bx3C2hgW.cjs +0 -38
  13. package/dist/clients/axios.cjs +0 -68
  14. package/dist/clients/axios.cjs.map +0 -1
  15. package/dist/clients/axios.d.ts +0 -49
  16. package/dist/clients/axios.js +0 -58
  17. package/dist/clients/axios.js.map +0 -1
  18. package/dist/clients/fetch.cjs +0 -128
  19. package/dist/clients/fetch.cjs.map +0 -1
  20. package/dist/clients/fetch.d.ts +0 -50
  21. package/dist/clients/fetch.js +0 -120
  22. package/dist/clients/fetch.js.map +0 -1
  23. package/dist/templates/clients/axios.source.cjs +0 -7
  24. package/dist/templates/clients/axios.source.cjs.map +0 -1
  25. package/dist/templates/clients/axios.source.d.ts +0 -7
  26. package/dist/templates/clients/axios.source.js +0 -6
  27. package/dist/templates/clients/axios.source.js.map +0 -1
  28. package/dist/templates/clients/fetch.source.cjs +0 -7
  29. package/dist/templates/clients/fetch.source.cjs.map +0 -1
  30. package/dist/templates/clients/fetch.source.d.ts +0 -7
  31. package/dist/templates/clients/fetch.source.js +0 -6
  32. package/dist/templates/clients/fetch.source.js.map +0 -1
  33. package/dist/templates/config.source.cjs +0 -7
  34. package/dist/templates/config.source.cjs.map +0 -1
  35. package/dist/templates/config.source.d.ts +0 -7
  36. package/dist/templates/config.source.js +0 -6
  37. package/dist/templates/config.source.js.map +0 -1
  38. package/src/clients/axios.ts +0 -113
  39. package/src/clients/fetch.ts +0 -201
  40. package/src/components/ClassClient.tsx +0 -161
  41. package/src/components/Client.tsx +0 -281
  42. package/src/components/StaticClassClient.tsx +0 -153
  43. package/src/components/Url.tsx +0 -90
  44. package/src/components/WrapperClient.tsx +0 -33
  45. package/src/functionParams.ts +0 -118
  46. package/src/generators/classClientGenerator.tsx +0 -261
  47. package/src/generators/groupedClientGenerator.tsx +0 -82
  48. package/src/generators/operationsGenerator.ts +0 -47
  49. package/src/generators/staticClassClientGenerator.tsx +0 -240
  50. package/src/resolvers/resolverClient.ts +0 -46
  51. package/src/templates/clients/axios.source.ts +0 -4
  52. package/src/templates/clients/fetch.source.ts +0 -4
  53. package/src/templates/config.source.ts +0 -4
  54. package/src/utils.ts +0 -259
  55. /package/dist/{chunk-C0LytTxp.js → rolldown-runtime-C0LytTxp.js} +0 -0
package/package.json CHANGED
@@ -1,19 +1,17 @@
1
1
  {
2
2
  "name": "@kubb/plugin-client",
3
- "version": "5.0.0-beta.56",
4
- "description": "Generate type-safe HTTP clients from your OpenAPI specification. Supports Axios, Fetch, and custom client adapters with full TypeScript inference and zero boilerplate.",
3
+ "version": "5.5.2",
4
+ "description": "Generate typed API operations against your own client module.",
5
5
  "keywords": [
6
6
  "api-client",
7
- "axios",
8
7
  "code-generation",
9
- "codegen",
10
- "fetch",
11
8
  "http-client",
12
9
  "kubb",
13
10
  "openapi",
14
11
  "swagger",
15
12
  "typescript"
16
13
  ],
14
+ "homepage": "https://kubb.dev/plugins/plugin-client",
17
15
  "license": "MIT",
18
16
  "author": "stijnvanhulle",
19
17
  "repository": {
@@ -24,8 +22,6 @@
24
22
  "files": [
25
23
  "src",
26
24
  "dist",
27
- "*.d.ts",
28
- "*.d.cts",
29
25
  "!/**/**.test.**",
30
26
  "!/**/__tests__/**",
31
27
  "!/**/__snapshots__/**"
@@ -35,50 +31,12 @@
35
31
  "main": "./dist/index.cjs",
36
32
  "module": "./dist/index.js",
37
33
  "types": "./dist/index.d.ts",
38
- "typesVersions": {
39
- "*": {
40
- "clients/axios": [
41
- "./dist/clients/axios.d.ts"
42
- ],
43
- "clients/fetch": [
44
- "./dist/clients/fetch.d.ts"
45
- ],
46
- "templates/config.source": [
47
- "./dist/templates/config.source.d.ts"
48
- ],
49
- "templates/clients/axios.source": [
50
- "./dist/templates/clients/axios.source.d.ts"
51
- ],
52
- "templates/clients/fetch.source": [
53
- "./dist/templates/clients/fetch.source.d.ts"
54
- ]
55
- }
56
- },
57
34
  "exports": {
58
35
  ".": {
36
+ "types": "./dist/index.d.ts",
59
37
  "import": "./dist/index.js",
60
38
  "require": "./dist/index.cjs"
61
39
  },
62
- "./clients/axios": {
63
- "import": "./dist/clients/axios.js",
64
- "require": "./dist/clients/axios.cjs"
65
- },
66
- "./clients/fetch": {
67
- "import": "./dist/clients/fetch.js",
68
- "require": "./dist/clients/fetch.cjs"
69
- },
70
- "./templates/clients/axios.source": {
71
- "import": "./dist/templates/clients/axios.source.js",
72
- "require": "./dist/templates/clients/axios.source.cjs"
73
- },
74
- "./templates/clients/fetch.source": {
75
- "import": "./dist/templates/clients/fetch.source.js",
76
- "require": "./dist/templates/clients/fetch.source.cjs"
77
- },
78
- "./templates/config.source": {
79
- "import": "./dist/templates/config.source.js",
80
- "require": "./dist/templates/config.source.cjs"
81
- },
82
40
  "./package.json": "./package.json"
83
41
  },
84
42
  "publishConfig": {
@@ -86,25 +44,17 @@
86
44
  "registry": "https://registry.npmjs.org/"
87
45
  },
88
46
  "dependencies": {
89
- "@kubb/ast": "5.0.0-beta.55",
90
- "@kubb/core": "5.0.0-beta.55",
91
- "@kubb/renderer-jsx": "5.0.0-beta.55",
92
- "@kubb/plugin-ts": "5.0.0-beta.56",
93
- "@kubb/plugin-zod": "5.0.0-beta.56"
47
+ "@kubb/plugin-ts": "^5.0.2",
48
+ "@kubb/plugin-zod": "^5.4.1"
94
49
  },
95
50
  "devDependencies": {
96
- "axios": "^1.17.0",
51
+ "@internals/client": "0.0.0",
97
52
  "@internals/shared": "0.0.0",
98
- "@internals/utils": "0.0.0"
53
+ "kubb": "^5.4.0",
54
+ "typescript": "^6.0.3"
99
55
  },
100
56
  "peerDependencies": {
101
- "@kubb/renderer-jsx": "5.0.0-beta.55",
102
- "axios": "^1.7.2"
103
- },
104
- "peerDependenciesMeta": {
105
- "axios": {
106
- "optional": true
107
- }
57
+ "kubb": "^5.4.0"
108
58
  },
109
59
  "engines": {
110
60
  "node": ">=22"
@@ -114,9 +64,6 @@
114
64
  "clean": "node -e \"require('node:fs').rmSync('./dist', {recursive:true,force:true})\"",
115
65
  "lint": "oxlint .",
116
66
  "lint:fix": "oxlint --fix .",
117
- "release": "pnpm publish --no-git-check",
118
- "release:canary": "bash ../../.github/canary.sh && node ../../scripts/build.js canary && pnpm publish --no-git-check",
119
- "release:stage": "pnpm stage publish --no-git-check",
120
67
  "start": "tsdown --watch",
121
68
  "test": "vitest --passWithNoTests",
122
69
  "typecheck": "tsc -p ./tsconfig.json --noEmit --emitDeclarationOnly false"
@@ -1,135 +1,9 @@
1
- import path from 'node:path'
2
- import { getOperationParameters, operationFileEntry, resolveOperationTypeNames } from '@internals/shared'
3
- import { ast, defineGenerator } from '@kubb/core'
4
- import { pluginTsName } from '@kubb/plugin-ts'
5
- import { pluginZodName } from '@kubb/plugin-zod'
6
- import { File, jsxRenderer } from '@kubb/renderer-jsx'
7
- import { Client } from '../components/Client'
8
- import { Url } from '../components/Url.tsx'
9
- import type { PluginClient } from '../types'
10
- import { isParserEnabled, resolveQueryParamsParser, resolveRequestParser, resolveResponseParser } from '../utils.ts'
1
+ import { createClientGenerator } from '@internals/client'
2
+ import type { PluginClient } from '../types.ts'
11
3
 
12
4
  /**
13
- * Built-in operation generator for `@kubb/plugin-client`. Emits one async
14
- * function per OpenAPI operation, plus the matching URL helper. Used when
15
- * `clientType: 'function'` (the default).
5
+ * Built-in operation generator for `@kubb/plugin-client`. Emits one async function per OpenAPI
6
+ * operation using the shared `Operation` component: a grouped `<Name>Request` type and a function
7
+ * that forwards a single `options` object to the user-provided client module and returns the `RequestResult`.
16
8
  */
17
- export const clientGenerator = defineGenerator<PluginClient>({
18
- name: 'client',
19
- renderer: jsxRenderer,
20
- operation(node, ctx) {
21
- if (!ast.isHttpOperationNode(node)) return null
22
- const { config, driver, resolver, root } = ctx
23
- const { output, urlType, dataReturnType, paramsCasing, paramsType, pathParamsType, parser, importPath, group } = ctx.options
24
- const baseURL = ctx.options.baseURL ?? ctx.meta.baseURL
25
-
26
- const pluginTs = driver.getPlugin(pluginTsName)
27
-
28
- if (!pluginTs) {
29
- return null
30
- }
31
-
32
- const tsResolver = driver.getResolver(pluginTsName)
33
-
34
- const pluginZod = isParserEnabled(parser) ? driver.getPlugin(pluginZodName) : null
35
- const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
36
-
37
- const importedTypeNames = resolveOperationTypeNames(node, tsResolver, { paramsCasing })
38
-
39
- const { query: queryParams } = getOperationParameters(node)
40
-
41
- const importedZodNames = zodResolver
42
- ? [
43
- resolveResponseParser(parser) === 'zod' ? zodResolver.resolveResponseName?.(node) : null,
44
- resolveRequestParser(parser) === 'zod' && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null,
45
- resolveQueryParamsParser(parser) === 'zod' && queryParams.length > 0 ? zodResolver.resolveQueryParamsName?.(node, queryParams[0]!) : null,
46
- ].filter((name): name is string => Boolean(name))
47
- : []
48
-
49
- const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.content?.[0]?.schema ? (zodResolver.resolveDataName?.(node) ?? null) : null
50
-
51
- const meta = {
52
- name: resolver.resolveName(node.operationId),
53
- urlName: resolver.resolveUrlName(node),
54
- file: resolver.resolveFile(operationFileEntry(node, node.operationId), { root, output, group: group ?? undefined }),
55
- fileTs: tsResolver.resolveFile(operationFileEntry(node, node.operationId), {
56
- root,
57
- output: pluginTs.options?.output ?? output,
58
- group: pluginTs.options?.group ?? undefined,
59
- }),
60
- fileZod:
61
- zodResolver && pluginZod?.options
62
- ? zodResolver.resolveFile(operationFileEntry(node, node.operationId), {
63
- root,
64
- output: pluginZod.options.output ?? output,
65
- group: pluginZod.options?.group ?? undefined,
66
- })
67
- : null,
68
- } as const
69
-
70
- const hasFormData = node.requestBody?.content?.some((e) => e.contentType === 'multipart/form-data') ?? false
71
-
72
- return (
73
- <File
74
- baseName={meta.file.baseName}
75
- path={meta.file.path}
76
- meta={meta.file.meta}
77
- banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
78
- footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
79
- >
80
- {importPath ? (
81
- <>
82
- <File.Import name={'client'} path={importPath} />
83
- <File.Import name={['Client', 'RequestConfig', 'ResponseErrorConfig']} path={importPath} isTypeOnly />
84
- </>
85
- ) : (
86
- <>
87
- <File.Import name={['client']} root={meta.file.path} path={path.resolve(root, '.kubb/client.ts')} />
88
- <File.Import
89
- name={['Client', 'RequestConfig', 'ResponseErrorConfig']}
90
- root={meta.file.path}
91
- path={path.resolve(root, '.kubb/client.ts')}
92
- isTypeOnly
93
- />
94
- </>
95
- )}
96
-
97
- {hasFormData && <File.Import name={['buildFormData']} root={meta.file.path} path={path.resolve(root, '.kubb/config.ts')} />}
98
-
99
- {zodRequestName && <File.Import name={['z']} path="zod" isTypeOnly />}
100
-
101
- {meta.fileZod && importedZodNames.length > 0 && <File.Import name={importedZodNames as Array<string>} root={meta.file.path} path={meta.fileZod.path} />}
102
-
103
- {meta.fileTs && importedTypeNames.length > 0 && (
104
- <File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
105
- )}
106
-
107
- <Url
108
- name={meta.urlName}
109
- baseURL={baseURL}
110
- pathParamsType={pathParamsType}
111
- paramsCasing={paramsCasing}
112
- paramsType={paramsType}
113
- node={node}
114
- tsResolver={tsResolver}
115
- isIndexable={urlType === 'export'}
116
- isExportable={urlType === 'export'}
117
- />
118
-
119
- <Client
120
- name={meta.name}
121
- urlName={meta.urlName}
122
- baseURL={baseURL}
123
- dataReturnType={dataReturnType}
124
- pathParamsType={pathParamsType}
125
- paramsCasing={paramsCasing}
126
- paramsType={paramsType}
127
- node={node}
128
- tsResolver={tsResolver}
129
- zodResolver={zodResolver}
130
- parser={parser}
131
- />
132
- </File>
133
- )
134
- },
135
- })
9
+ export const clientGenerator = createClientGenerator<PluginClient>('client', (options) => options.importPath)
package/src/index.ts CHANGED
@@ -1,10 +1,2 @@
1
- export { Client } from './components/Client.tsx'
2
- export { classClientGenerator } from './generators/classClientGenerator.tsx'
3
- export { clientGenerator } from './generators/clientGenerator.tsx'
4
- export { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'
5
- export { operationsGenerator } from './generators/operationsGenerator.ts'
6
- export { staticClassClientGenerator } from './generators/staticClassClientGenerator.tsx'
7
1
  export { default, pluginClient, pluginClientName } from './plugin.ts'
8
- export { resolverClient } from './resolvers/resolverClient.ts'
9
- export { isParserEnabled, resolveQueryParamsParser, resolveRequestParser, resolveResponseParser } from './utils.ts'
10
- export type { ClientImportPath, PluginClient, ResolverClient } from './types.ts'
2
+ export type { Options, PluginClient, ResolvedOptions, ResolverClient } from './types.ts'
package/src/plugin.ts CHANGED
@@ -1,158 +1,68 @@
1
- import path from 'node:path'
2
1
  import { createGroupConfig } from '@internals/shared'
3
-
4
- import { ast, definePlugin } from '@kubb/core'
2
+ import { definePlugin, Resolver } from 'kubb/kit'
5
3
  import { pluginTsName } from '@kubb/plugin-ts'
6
4
  import { pluginZodName } from '@kubb/plugin-zod'
7
- import { classClientGenerator } from './generators/classClientGenerator.tsx'
8
5
  import { clientGenerator } from './generators/clientGenerator.tsx'
9
- import { groupedClientGenerator } from './generators/groupedClientGenerator.tsx'
10
- import { operationsGenerator } from './generators/operationsGenerator.ts'
11
- import { staticClassClientGenerator } from './generators/staticClassClientGenerator.tsx'
12
- import { resolverClient } from './resolvers/resolverClient.ts'
13
- import { source as axiosClientSource } from './templates/clients/axios.source.ts'
14
- import { source as fetchClientSource } from './templates/clients/fetch.source.ts'
15
- import { source as configSource } from './templates/config.source.ts'
16
- import type { PluginClient } from './types.ts'
17
- import { isParserEnabled } from './utils.ts'
6
+ import { defaultMacros, resolverClient } from '@internals/client'
7
+ import type { PluginClient, ResolvedOptions, ResolverClient } from './types.ts'
18
8
 
19
9
  /**
20
- * Canonical plugin name for `@kubb/plugin-client`. Used for driver lookups and
21
- * cross-plugin dependency references.
10
+ * Canonical plugin name for `@kubb/plugin-client`.
22
11
  */
23
12
  export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
24
13
 
25
14
  /**
26
- * Generates one HTTP client function per OpenAPI operation. Each function has
27
- * typed path params, query params, body, and response, so callers use the API
28
- * like any other typed function. Ships with `axios` and `fetch` runtimes; bring
29
- * your own by setting `importPath`.
15
+ * Generates typed operations that call a client module owned by your application.
16
+ * The module at `importPath` must export the runtime helpers and types used by generated operations.
17
+ * Kubb does not copy or inject a transport implementation.
30
18
  *
31
19
  * @example
32
20
  * ```ts
33
- * import { defineConfig } from 'kubb'
34
- * import { pluginTs } from '@kubb/plugin-ts'
35
- * import { pluginClient } from '@kubb/plugin-client'
36
- *
37
- * export default defineConfig({
38
- * input: { path: './petStore.yaml' },
39
- * output: { path: './src/gen' },
40
- * plugins: [
41
- * pluginTs(),
42
- * pluginClient({
43
- * output: { path: './clients' },
44
- * client: 'fetch',
45
- * }),
46
- * ],
47
- * })
21
+ * pluginClient({ importPath: '../api/client' })
48
22
  * ```
49
23
  */
50
24
  export const pluginClient = definePlugin<PluginClient>((options) => {
51
25
  const {
52
26
  output = { path: 'clients', barrel: { type: 'named' } },
53
- group,
54
27
  exclude = [],
55
28
  include,
56
29
  override = [],
57
- urlType = false,
58
- dataReturnType = 'data',
59
- paramsType = 'inline',
60
- pathParamsType = paramsType === 'object' ? 'object' : options.pathParamsType || 'inline',
61
- operations = false,
62
- paramsCasing,
63
- clientType = options.sdk ? 'class' : 'function',
64
- parser = false,
65
- client = 'axios',
66
30
  importPath,
67
- bundle = false,
68
- sdk,
69
- baseURL,
31
+ throwOnErrorDefault = true,
32
+ validator = false,
33
+ group,
70
34
  resolver: userResolver,
71
- transformer: userTransformer,
72
35
  } = options
73
36
 
74
- const resolvedImportPath = importPath ?? (!bundle ? `@kubb/plugin-client/clients/${client}` : undefined)
75
-
76
- const selectedGenerators =
77
- options.generators ??
78
- [
79
- clientType === 'staticClass' ? staticClassClientGenerator : clientType === 'class' ? classClientGenerator : clientGenerator,
80
- group && clientType === 'function' ? groupedClientGenerator : null,
81
- operations ? operationsGenerator : null,
82
- ].filter((x): x is NonNullable<typeof x> => Boolean(x))
83
-
84
- const groupConfig = createGroupConfig(group)
37
+ const resolved: ResolvedOptions = {
38
+ output,
39
+ exclude,
40
+ include,
41
+ override,
42
+ group: createGroupConfig(group),
43
+ importPath,
44
+ baseURL: undefined,
45
+ throwOnErrorDefault,
46
+ validator,
47
+ returnType: 'plain',
48
+ sdk: undefined,
49
+ resolver: userResolver ? Resolver.merge<ResolverClient>(resolverClient, userResolver) : resolverClient,
50
+ }
85
51
 
86
52
  return {
87
53
  name: pluginClientName,
88
54
  options,
89
- dependencies: [pluginTsName, isParserEnabled(parser) ? pluginZodName : null].filter((dependency): dependency is string => Boolean(dependency)),
55
+ dependencies: [pluginTsName, pluginZodName],
90
56
  hooks: {
91
57
  'kubb:plugin:setup'(ctx) {
92
- const resolver = userResolver ? { ...resolverClient, ...userResolver } : resolverClient
93
-
94
- ctx.setOptions({
95
- client,
96
- clientType,
97
- bundle,
98
- output,
99
- exclude,
100
- include,
101
- override,
102
- group: groupConfig,
103
- parser,
104
- dataReturnType,
105
- importPath: resolvedImportPath,
106
- baseURL,
107
- paramsType,
108
- paramsCasing,
109
- pathParamsType,
110
- urlType,
111
- sdk,
112
- resolver,
113
- })
114
- ctx.setResolver(resolver)
115
- if (userTransformer) {
116
- ctx.setTransformer(userTransformer)
117
- }
118
- for (const gen of selectedGenerators) {
119
- ctx.addGenerator(gen)
120
- }
121
-
122
- const root = path.resolve(ctx.config.root, ctx.config.output.path)
123
-
124
- const isRelativePath = resolvedImportPath?.startsWith('.')
125
-
126
- if (!isRelativePath) {
127
- const isInlineSource = bundle && !resolvedImportPath
128
-
129
- ctx.injectFile({
130
- baseName: 'client.ts',
131
- path: path.resolve(root, '.kubb/client.ts'),
132
- sources: [
133
- ast.createSource({
134
- name: 'client',
135
- nodes: isInlineSource ? [ast.createText(client === 'fetch' ? fetchClientSource : axiosClientSource)] : [],
136
- isExportable: true,
137
- isIndexable: true,
138
- }),
139
- ],
140
- exports: !isInlineSource && resolvedImportPath ? [ast.createExport({ path: resolvedImportPath })] : [],
141
- })
58
+ if (validator && !ctx.config.plugins?.some((plugin) => plugin.name === pluginZodName)) {
59
+ throw new Error(`${pluginClientName}: \`validator\` needs ${pluginZodName}. Add pluginZod() to the plugins or set \`validator: false\`.`)
142
60
  }
143
61
 
144
- ctx.injectFile({
145
- baseName: 'config.ts',
146
- path: path.resolve(root, '.kubb/config.ts'),
147
- sources: [
148
- ast.createSource({
149
- name: 'config',
150
- nodes: [ast.createText(configSource)],
151
- isExportable: false,
152
- isIndexable: false,
153
- }),
154
- ],
155
- })
62
+ ctx.setOptions(resolved)
63
+ ctx.setResolver(resolved.resolver)
64
+ ctx.setMacros([...defaultMacros, ...(options.macros ?? [])])
65
+ ctx.addGenerator(clientGenerator)
156
66
  },
157
67
  },
158
68
  }