@kubb/plugin-axios 5.0.0-beta.77 → 5.0.0-beta.80
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/dist/index.cjs +276 -88
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +276 -88
- package/dist/index.js.map +1 -1
- package/package.json +7 -7
- package/src/generators/clientGenerator.tsx +24 -11
- package/src/plugin.ts +19 -5
- package/src/templates.ts +9 -4
- package/templates/axios.ts +288 -173
- package/templates/serializers.ts +424 -0
- package/templates/standardSchema.ts +54 -0
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
-
import {
|
|
3
|
-
|
|
2
|
+
import {
|
|
3
|
+
buildZodErrorParse,
|
|
4
|
+
getOperationSecurity,
|
|
5
|
+
Operation,
|
|
6
|
+
resolveRequestValidator,
|
|
7
|
+
resolveResponseValidator,
|
|
8
|
+
type SecurityDocument,
|
|
9
|
+
} from '@internals/client'
|
|
10
|
+
import { isEventStream, operationFileEntry } from '@internals/shared'
|
|
4
11
|
import { ast, defineGenerator } from '@kubb/core'
|
|
5
12
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
13
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
@@ -19,15 +26,15 @@ export const clientGenerator = defineGenerator<PluginAxios>({
|
|
|
19
26
|
if (!ast.isHttpOperationNode(node)) return null
|
|
20
27
|
|
|
21
28
|
const { config, driver, resolver, root } = ctx
|
|
22
|
-
const { output,
|
|
29
|
+
const { output, validator, group } = ctx.options
|
|
23
30
|
|
|
24
31
|
const pluginTs = driver.getPlugin(pluginTsName)
|
|
25
32
|
if (!pluginTs) return null
|
|
26
33
|
|
|
27
34
|
const tsResolver = driver.getResolver(pluginTsName)
|
|
28
35
|
|
|
29
|
-
const
|
|
30
|
-
const pluginZod =
|
|
36
|
+
const validatorEnabled = resolveResponseValidator(validator) === 'zod' || resolveRequestValidator(validator) === 'zod'
|
|
37
|
+
const pluginZod = validatorEnabled ? driver.getPlugin(pluginZodName) : null
|
|
31
38
|
const zodResolver = pluginZod ? driver.getResolver(pluginZodName) : null
|
|
32
39
|
|
|
33
40
|
const hasRequestBody = Boolean(node.requestBody?.content?.[0]?.schema)
|
|
@@ -35,9 +42,9 @@ export const clientGenerator = defineGenerator<PluginAxios>({
|
|
|
35
42
|
|
|
36
43
|
const importedZodNames = zodResolver
|
|
37
44
|
? [
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
45
|
+
resolveResponseValidator(validator) === 'zod' ? zodResolver.resolveResponseName?.(node) : null,
|
|
46
|
+
resolveResponseValidator(validator) === 'zod' ? (buildZodErrorParse(node, zodResolver)?.expression ?? null) : null,
|
|
47
|
+
resolveRequestValidator(validator) === 'zod' && hasRequestBody ? zodResolver.resolveDataName?.(node) : null,
|
|
41
48
|
].filter((name): name is string => Boolean(name))
|
|
42
49
|
: []
|
|
43
50
|
|
|
@@ -66,6 +73,7 @@ export const clientGenerator = defineGenerator<PluginAxios>({
|
|
|
66
73
|
})
|
|
67
74
|
|
|
68
75
|
const clientPath = path.resolve(root, '.kubb/client.ts')
|
|
76
|
+
const eventStream = isEventStream(node)
|
|
69
77
|
|
|
70
78
|
return (
|
|
71
79
|
<File
|
|
@@ -75,8 +83,13 @@ export const clientGenerator = defineGenerator<PluginAxios>({
|
|
|
75
83
|
banner={resolver.resolveBanner(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
|
|
76
84
|
footer={resolver.resolveFooter(ctx.meta, { output, config, file: { path: meta.file.path, baseName: meta.file.baseName } })}
|
|
77
85
|
>
|
|
78
|
-
<File.Import name={['client']} root={meta.file.path} path={clientPath} />
|
|
79
|
-
<File.Import
|
|
86
|
+
<File.Import name={eventStream ? ['client', 'toEventStream'] : ['client']} root={meta.file.path} path={clientPath} />
|
|
87
|
+
<File.Import
|
|
88
|
+
name={eventStream ? ['Options', 'EventStreamResult', 'SuccessOf'] : ['Options', 'RequestResult']}
|
|
89
|
+
root={meta.file.path}
|
|
90
|
+
path={clientPath}
|
|
91
|
+
isTypeOnly
|
|
92
|
+
/>
|
|
80
93
|
|
|
81
94
|
{meta.fileTs && importedTypeNames.length > 0 && (
|
|
82
95
|
<File.Import name={Array.from(new Set(importedTypeNames))} root={meta.file.path} path={meta.fileTs.path} isTypeOnly />
|
|
@@ -84,7 +97,7 @@ export const clientGenerator = defineGenerator<PluginAxios>({
|
|
|
84
97
|
|
|
85
98
|
{meta.fileZod && importedZodNames.length > 0 && <File.Import name={importedZodNames} root={meta.file.path} path={meta.fileZod.path} />}
|
|
86
99
|
|
|
87
|
-
<Operation name={meta.name} node={node} tsResolver={tsResolver} zodResolver={zodResolver}
|
|
100
|
+
<Operation name={meta.name} node={node} tsResolver={tsResolver} zodResolver={zodResolver} validator={validator} security={security} />
|
|
88
101
|
</File>
|
|
89
102
|
)
|
|
90
103
|
},
|
package/src/plugin.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import path from 'node:path'
|
|
2
|
-
import { createSdkGenerator, defaultMacros,
|
|
2
|
+
import { createSdkGenerator, defaultMacros, isValidatorEnabled, resolverClient } from '@internals/client'
|
|
3
3
|
import { createGroupConfig } from '@internals/shared'
|
|
4
4
|
import { definePlugin } from '@kubb/core'
|
|
5
5
|
import { pluginTsName } from '@kubb/plugin-ts'
|
|
6
6
|
import { pluginZodName } from '@kubb/plugin-zod'
|
|
7
7
|
import { clientGenerator } from './generators/clientGenerator.tsx'
|
|
8
|
-
import { axiosClientTemplatePath } from './templates.ts'
|
|
8
|
+
import { axiosClientTemplatePath, axiosSerializersTemplatePath, standardSchemaTemplatePath } from './templates.ts'
|
|
9
9
|
import type { PluginAxios, ResolvedOptions } from './types.ts'
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -43,7 +43,7 @@ export const pluginAxios = definePlugin<PluginAxios>((options) => {
|
|
|
43
43
|
include,
|
|
44
44
|
override = [],
|
|
45
45
|
baseURL,
|
|
46
|
-
|
|
46
|
+
validator = false,
|
|
47
47
|
group,
|
|
48
48
|
sdk,
|
|
49
49
|
resolver: userResolver,
|
|
@@ -56,7 +56,7 @@ export const pluginAxios = definePlugin<PluginAxios>((options) => {
|
|
|
56
56
|
override,
|
|
57
57
|
group: createGroupConfig(group),
|
|
58
58
|
baseURL,
|
|
59
|
-
|
|
59
|
+
validator,
|
|
60
60
|
sdk: sdk ? { mode: sdk.mode ?? 'tag', name: sdk.name } : undefined,
|
|
61
61
|
resolver: userResolver ? { ...resolverClient, ...userResolver } : resolverClient,
|
|
62
62
|
}
|
|
@@ -68,7 +68,9 @@ export const pluginAxios = definePlugin<PluginAxios>((options) => {
|
|
|
68
68
|
return {
|
|
69
69
|
name: pluginAxiosName,
|
|
70
70
|
options,
|
|
71
|
-
dependencies: [pluginTsName,
|
|
71
|
+
dependencies: [pluginTsName, isValidatorEnabled(resolved.validator) ? pluginZodName : null].filter((dependency): dependency is string =>
|
|
72
|
+
Boolean(dependency),
|
|
73
|
+
),
|
|
72
74
|
hooks: {
|
|
73
75
|
'kubb:plugin:setup'(ctx) {
|
|
74
76
|
ctx.setOptions(resolved)
|
|
@@ -79,12 +81,24 @@ export const pluginAxios = definePlugin<PluginAxios>((options) => {
|
|
|
79
81
|
|
|
80
82
|
const root = path.resolve(ctx.config.root, ctx.config.output.path)
|
|
81
83
|
|
|
84
|
+
ctx.injectFile({
|
|
85
|
+
baseName: 'serializers.ts',
|
|
86
|
+
path: path.resolve(root, '.kubb/serializers.ts'),
|
|
87
|
+
copy: axiosSerializersTemplatePath,
|
|
88
|
+
})
|
|
89
|
+
|
|
82
90
|
ctx.injectFile({
|
|
83
91
|
baseName: 'client.ts',
|
|
84
92
|
path: path.resolve(root, '.kubb/client.ts'),
|
|
85
93
|
copy: axiosClientTemplatePath,
|
|
86
94
|
footer: baseURL ? `client.setConfig({ baseURL: ${JSON.stringify(baseURL)} })` : undefined,
|
|
87
95
|
})
|
|
96
|
+
|
|
97
|
+
ctx.injectFile({
|
|
98
|
+
baseName: 'standardSchema.ts',
|
|
99
|
+
path: path.resolve(root, '.kubb/standardSchema.ts'),
|
|
100
|
+
copy: standardSchemaTemplatePath,
|
|
101
|
+
})
|
|
88
102
|
},
|
|
89
103
|
},
|
|
90
104
|
}
|
package/src/templates.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
import { fileURLToPath } from 'node:url'
|
|
2
2
|
|
|
3
|
+
/** Absolute path to the axios client template, copied into `.kubb/client.ts`. */
|
|
4
|
+
export const axiosClientTemplatePath = fileURLToPath(new URL('../templates/axios.ts', import.meta.url))
|
|
5
|
+
|
|
6
|
+
/** Absolute path to the axios serializers template, copied into `.kubb/serializers.ts`. */
|
|
7
|
+
export const axiosSerializersTemplatePath = fileURLToPath(new URL('../templates/serializers.ts', import.meta.url))
|
|
8
|
+
|
|
3
9
|
/**
|
|
4
|
-
* Absolute path to the
|
|
5
|
-
*
|
|
6
|
-
* field to emit the runtime into the generated `.kubb/client.ts` verbatim.
|
|
10
|
+
* Absolute path to the Standard Schema runtime template. Pass it to a file node's `copy` field to
|
|
11
|
+
* emit the helper into the generated `.kubb/standardSchema.ts` verbatim.
|
|
7
12
|
*/
|
|
8
|
-
export const
|
|
13
|
+
export const standardSchemaTemplatePath = fileURLToPath(new URL('../templates/standardSchema.ts', import.meta.url))
|