@kubb/plugin-client 5.0.0-alpha.9 → 5.0.0-beta.15
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/LICENSE +17 -10
- package/README.md +27 -7
- package/dist/clients/axios.cjs +10 -3
- package/dist/clients/axios.cjs.map +1 -1
- package/dist/clients/axios.d.ts +5 -4
- package/dist/clients/axios.js +9 -2
- package/dist/clients/axios.js.map +1 -1
- package/dist/clients/fetch.cjs +5 -2
- package/dist/clients/fetch.cjs.map +1 -1
- package/dist/clients/fetch.d.ts +3 -2
- package/dist/clients/fetch.js +5 -2
- package/dist/clients/fetch.js.map +1 -1
- package/dist/index.cjs +1830 -97
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +330 -4
- package/dist/index.js +1816 -95
- package/dist/index.js.map +1 -1
- package/dist/templates/clients/axios.source.cjs +1 -1
- package/dist/templates/clients/axios.source.js +1 -1
- package/dist/templates/clients/fetch.source.cjs +1 -1
- package/dist/templates/clients/fetch.source.js +1 -1
- package/extension.yaml +779 -0
- package/package.json +62 -85
- package/src/clients/axios.ts +19 -4
- package/src/clients/fetch.ts +10 -2
- package/src/components/ClassClient.tsx +46 -145
- package/src/components/Client.tsx +109 -139
- package/src/components/Operations.tsx +10 -10
- package/src/components/StaticClassClient.tsx +46 -142
- package/src/components/Url.tsx +38 -50
- package/src/components/WrapperClient.tsx +11 -7
- package/src/functionParams.ts +118 -0
- package/src/generators/classClientGenerator.tsx +143 -172
- package/src/generators/clientGenerator.tsx +83 -81
- package/src/generators/groupedClientGenerator.tsx +50 -52
- package/src/generators/operationsGenerator.tsx +11 -18
- package/src/generators/staticClassClientGenerator.tsx +172 -184
- package/src/index.ts +9 -2
- package/src/plugin.ts +115 -145
- package/src/resolvers/resolverClient.ts +38 -0
- package/src/types.ts +128 -44
- package/src/utils.ts +156 -0
- package/dist/StaticClassClient-By-aMAe4.cjs +0 -677
- package/dist/StaticClassClient-By-aMAe4.cjs.map +0 -1
- package/dist/StaticClassClient-CCn9g9eF.js +0 -636
- package/dist/StaticClassClient-CCn9g9eF.js.map +0 -1
- package/dist/components.cjs +0 -7
- package/dist/components.d.ts +0 -216
- package/dist/components.js +0 -2
- package/dist/generators-BYUJaeZP.js +0 -723
- package/dist/generators-BYUJaeZP.js.map +0 -1
- package/dist/generators-DTxD9FDY.cjs +0 -753
- package/dist/generators-DTxD9FDY.cjs.map +0 -1
- package/dist/generators.cjs +0 -7
- package/dist/generators.d.ts +0 -517
- package/dist/generators.js +0 -2
- package/dist/types-DBQdg-BV.d.ts +0 -169
- package/src/components/index.ts +0 -5
- package/src/generators/index.ts +0 -5
- package/templates/clients/axios.ts +0 -70
- package/templates/clients/fetch.ts +0 -93
- package/templates/config.ts +0 -43
package/src/components/Url.tsx
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
+
import { buildParamsMapping, getOperationParameters } from '@internals/shared'
|
|
1
2
|
import { isValidVarName, URLPath } from '@internals/utils'
|
|
2
|
-
import {
|
|
3
|
-
import type {
|
|
4
|
-
import {
|
|
5
|
-
import { Const, File, Function
|
|
6
|
-
import type {
|
|
3
|
+
import { ast } from '@kubb/core'
|
|
4
|
+
import type { ResolverTs } from '@kubb/plugin-ts'
|
|
5
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
6
|
+
import { Const, File, Function } from '@kubb/renderer-jsx'
|
|
7
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
7
8
|
import type { PluginClient } from '../types.ts'
|
|
8
9
|
|
|
9
10
|
type Props = {
|
|
10
|
-
/**
|
|
11
|
-
* Name of the function
|
|
12
|
-
*/
|
|
13
11
|
name: string
|
|
14
12
|
isExportable?: boolean
|
|
15
13
|
isIndexable?: boolean
|
|
@@ -18,45 +16,33 @@ type Props = {
|
|
|
18
16
|
paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
|
|
19
17
|
paramsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
20
18
|
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
node: ast.OperationNode
|
|
20
|
+
tsResolver: ResolverTs
|
|
23
21
|
}
|
|
24
22
|
|
|
25
23
|
type GetParamsProps = {
|
|
26
24
|
paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
|
|
27
25
|
paramsType: PluginClient['resolvedOptions']['paramsType']
|
|
28
26
|
pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
|
|
29
|
-
|
|
27
|
+
node: ast.OperationNode
|
|
28
|
+
tsResolver: ResolverTs
|
|
30
29
|
}
|
|
31
30
|
|
|
32
|
-
|
|
33
|
-
if (paramsType === 'object') {
|
|
34
|
-
const pathParams = getPathParams(typeSchemas.pathParams, {
|
|
35
|
-
typed: true,
|
|
36
|
-
casing: paramsCasing,
|
|
37
|
-
})
|
|
31
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
},
|
|
46
|
-
})
|
|
33
|
+
export function buildUrlParamsNode({ paramsType, paramsCasing, pathParamsType, node, tsResolver }: GetParamsProps): ast.FunctionParametersNode {
|
|
34
|
+
// Build a URL-only node with only path params (no body, query, header)
|
|
35
|
+
const urlNode: ast.OperationNode = {
|
|
36
|
+
...node,
|
|
37
|
+
parameters: node.parameters.filter((p) => p.in === 'path'),
|
|
38
|
+
requestBody: undefined,
|
|
47
39
|
}
|
|
48
40
|
|
|
49
|
-
return
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
typed: true,
|
|
55
|
-
casing: paramsCasing,
|
|
56
|
-
}),
|
|
57
|
-
default: getDefaultValue(typeSchemas.pathParams?.schema),
|
|
58
|
-
}
|
|
59
|
-
: undefined,
|
|
41
|
+
return ast.createOperationParams(urlNode, {
|
|
42
|
+
paramsType: paramsType === 'object' ? 'object' : 'inline',
|
|
43
|
+
pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
|
|
44
|
+
paramsCasing,
|
|
45
|
+
resolver: tsResolver,
|
|
60
46
|
})
|
|
61
47
|
}
|
|
62
48
|
|
|
@@ -64,39 +50,41 @@ export function Url({
|
|
|
64
50
|
name,
|
|
65
51
|
isExportable = true,
|
|
66
52
|
isIndexable = true,
|
|
67
|
-
typeSchemas,
|
|
68
53
|
baseURL,
|
|
69
54
|
paramsType,
|
|
70
55
|
paramsCasing,
|
|
71
56
|
pathParamsType,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
const
|
|
57
|
+
node,
|
|
58
|
+
tsResolver,
|
|
59
|
+
}: Props): KubbReactNode {
|
|
60
|
+
const path = new URLPath(node.path)
|
|
61
|
+
|
|
62
|
+
const paramsNode = buildUrlParamsNode({
|
|
76
63
|
paramsType,
|
|
77
64
|
paramsCasing,
|
|
78
65
|
pathParamsType,
|
|
79
|
-
|
|
66
|
+
node,
|
|
67
|
+
tsResolver,
|
|
80
68
|
})
|
|
69
|
+
const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
|
|
81
70
|
|
|
82
|
-
|
|
83
|
-
const
|
|
71
|
+
const { path: originalPathParams } = getOperationParameters(node)
|
|
72
|
+
const { path: casedPathParams } = getOperationParameters(node, { paramsCasing })
|
|
73
|
+
const pathParamsMapping = paramsCasing ? buildParamsMapping(originalPathParams, casedPathParams) : undefined
|
|
84
74
|
|
|
85
75
|
return (
|
|
86
76
|
<File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
|
|
87
|
-
<Function name={name} export={isExportable} params={
|
|
77
|
+
<Function name={name} export={isExportable} params={paramsSignature}>
|
|
88
78
|
{pathParamsMapping &&
|
|
89
79
|
Object.entries(pathParamsMapping)
|
|
90
|
-
.filter(([originalName, camelCaseName]) => originalName !== camelCaseName
|
|
80
|
+
.filter(([originalName, camelCaseName]) => isValidVarName(originalName) && originalName !== camelCaseName)
|
|
91
81
|
.map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`)
|
|
92
82
|
.join('\n')}
|
|
93
|
-
{pathParamsMapping && <br />}
|
|
94
|
-
<Const name={'res'}>{`{ method: '${
|
|
83
|
+
{pathParamsMapping && Object.keys(pathParamsMapping).length > 0 && <br />}
|
|
84
|
+
<Const name={'res'}>{`{ method: '${node.method.toUpperCase()}', url: ${path.toTemplateString({ prefix: baseURL })} as const }`}</Const>
|
|
95
85
|
<br />
|
|
96
86
|
return res
|
|
97
87
|
</Function>
|
|
98
88
|
</File.Source>
|
|
99
89
|
)
|
|
100
90
|
}
|
|
101
|
-
|
|
102
|
-
Url.getParams = getParams
|
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { File } from '@kubb/renderer-jsx'
|
|
2
|
+
import type { KubbReactNode } from '@kubb/renderer-jsx/types'
|
|
3
|
+
|
|
4
|
+
type ClientController = {
|
|
5
|
+
className: string
|
|
6
|
+
propertyName: string
|
|
7
|
+
}
|
|
4
8
|
|
|
5
9
|
type Props = {
|
|
6
10
|
name: string
|
|
7
|
-
|
|
11
|
+
controllers: Array<ClientController>
|
|
8
12
|
isExportable?: boolean
|
|
9
13
|
isIndexable?: boolean
|
|
10
14
|
}
|
|
11
15
|
|
|
12
|
-
export function WrapperClient({ name,
|
|
13
|
-
const properties =
|
|
14
|
-
const assignments =
|
|
16
|
+
export function WrapperClient({ name, controllers, isExportable = true, isIndexable = true }: Props): KubbReactNode {
|
|
17
|
+
const properties = controllers.map(({ className, propertyName }) => ` readonly ${propertyName}: ${className}`).join('\n')
|
|
18
|
+
const assignments = controllers.map(({ className, propertyName }) => ` this.${propertyName} = new ${className}(config)`).join('\n')
|
|
15
19
|
|
|
16
20
|
const classCode = `export class ${name} {
|
|
17
21
|
${properties}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { ast } from '@kubb/core'
|
|
2
|
+
import { functionPrinter } from '@kubb/plugin-ts'
|
|
3
|
+
|
|
4
|
+
type ParamLeaf = {
|
|
5
|
+
type?: string
|
|
6
|
+
optional?: boolean
|
|
7
|
+
default?: string
|
|
8
|
+
value?: string
|
|
9
|
+
mode?: 'inlineSpread'
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
type ParamGroup = {
|
|
13
|
+
mode: 'object' | 'inlineSpread'
|
|
14
|
+
children: Record<string, ParamLeaf | undefined>
|
|
15
|
+
default?: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type ParamSpec = ParamLeaf | ParamGroup
|
|
19
|
+
|
|
20
|
+
const declarationPrinter = functionPrinter({ mode: 'declaration' })
|
|
21
|
+
const callPrinter = functionPrinter({ mode: 'call' })
|
|
22
|
+
|
|
23
|
+
function isGroup(spec: ParamSpec): spec is ParamGroup {
|
|
24
|
+
return 'children' in spec
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function createType(type?: string) {
|
|
28
|
+
return type ? ast.createParamsType({ variant: 'reference', name: type }) : undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function createDeclarationLeaf(name: string, spec: ParamLeaf): ast.FunctionParameterNode {
|
|
32
|
+
if (spec.default !== undefined) {
|
|
33
|
+
return ast.createFunctionParameter({
|
|
34
|
+
name,
|
|
35
|
+
type: createType(spec.type),
|
|
36
|
+
default: spec.default,
|
|
37
|
+
rest: spec.mode === 'inlineSpread',
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return ast.createFunctionParameter({
|
|
42
|
+
name,
|
|
43
|
+
type: createType(spec.type),
|
|
44
|
+
optional: !!spec.optional,
|
|
45
|
+
rest: spec.mode === 'inlineSpread',
|
|
46
|
+
})
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function createDeclarationParam(name: string, spec: ParamSpec): ast.FunctionParameterNode | ast.ParameterGroupNode {
|
|
50
|
+
if (isGroup(spec)) {
|
|
51
|
+
return ast.createParameterGroup({
|
|
52
|
+
inline: spec.mode === 'inlineSpread',
|
|
53
|
+
default: spec.default,
|
|
54
|
+
properties: Object.entries(spec.children)
|
|
55
|
+
.filter(([, child]) => child !== undefined)
|
|
56
|
+
.map(([childName, child]) => createDeclarationLeaf(childName, child!)),
|
|
57
|
+
})
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return createDeclarationLeaf(name, spec)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function createCallParam(name: string, spec: ParamSpec): ast.FunctionParameterNode | ast.ParameterGroupNode {
|
|
64
|
+
if (isGroup(spec)) {
|
|
65
|
+
return ast.createParameterGroup({
|
|
66
|
+
inline: spec.mode === 'inlineSpread',
|
|
67
|
+
properties: Object.entries(spec.children)
|
|
68
|
+
.filter(([, child]) => child !== undefined)
|
|
69
|
+
.map(([childName, child]) =>
|
|
70
|
+
ast.createFunctionParameter({
|
|
71
|
+
name:
|
|
72
|
+
child?.mode === 'inlineSpread'
|
|
73
|
+
? spec.mode === 'inlineSpread'
|
|
74
|
+
? (child.value ?? childName)
|
|
75
|
+
: `...${child.value ?? childName}`
|
|
76
|
+
: child?.value
|
|
77
|
+
? `${childName}: ${child.value}`
|
|
78
|
+
: childName,
|
|
79
|
+
rest: spec.mode === 'inlineSpread' && child?.mode === 'inlineSpread',
|
|
80
|
+
}),
|
|
81
|
+
),
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return ast.createFunctionParameter({
|
|
86
|
+
name: spec.value ?? name,
|
|
87
|
+
rest: spec.mode === 'inlineSpread',
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Creates function parameter builders for generating function signatures and calls.
|
|
93
|
+
* Returns utilities to output constructor signatures (`toConstructor()`) or call expressions (`toCall()`).
|
|
94
|
+
*/
|
|
95
|
+
export function createFunctionParams(params: Record<string, ParamSpec | undefined>) {
|
|
96
|
+
const entries = Object.entries(params).filter(([, spec]) => spec !== undefined) as Array<[string, ParamSpec]>
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
toConstructor() {
|
|
100
|
+
return (
|
|
101
|
+
declarationPrinter.print(
|
|
102
|
+
ast.createFunctionParameters({
|
|
103
|
+
params: entries.map(([name, spec]) => createDeclarationParam(name, spec)),
|
|
104
|
+
}),
|
|
105
|
+
) ?? ''
|
|
106
|
+
)
|
|
107
|
+
},
|
|
108
|
+
toCall() {
|
|
109
|
+
return (
|
|
110
|
+
callPrinter.print(
|
|
111
|
+
ast.createFunctionParameters({
|
|
112
|
+
params: entries.map(([name, spec]) => createCallParam(name, spec)),
|
|
113
|
+
}),
|
|
114
|
+
) ?? ''
|
|
115
|
+
)
|
|
116
|
+
},
|
|
117
|
+
}
|
|
118
|
+
}
|