@kubb/plugin-client 5.0.0-alpha.9 → 5.0.0-beta.10

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 (62) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +27 -7
  3. package/dist/clients/axios.cjs +10 -3
  4. package/dist/clients/axios.cjs.map +1 -1
  5. package/dist/clients/axios.d.ts +5 -4
  6. package/dist/clients/axios.js +9 -2
  7. package/dist/clients/axios.js.map +1 -1
  8. package/dist/clients/fetch.cjs +5 -2
  9. package/dist/clients/fetch.cjs.map +1 -1
  10. package/dist/clients/fetch.d.ts +3 -2
  11. package/dist/clients/fetch.js +5 -2
  12. package/dist/clients/fetch.js.map +1 -1
  13. package/dist/index.cjs +1818 -97
  14. package/dist/index.cjs.map +1 -1
  15. package/dist/index.d.ts +330 -4
  16. package/dist/index.js +1804 -95
  17. package/dist/index.js.map +1 -1
  18. package/dist/templates/clients/axios.source.cjs +1 -1
  19. package/dist/templates/clients/axios.source.js +1 -1
  20. package/dist/templates/clients/fetch.source.cjs +1 -1
  21. package/dist/templates/clients/fetch.source.js +1 -1
  22. package/extension.yaml +776 -0
  23. package/package.json +62 -85
  24. package/src/clients/axios.ts +19 -4
  25. package/src/clients/fetch.ts +10 -2
  26. package/src/components/ClassClient.tsx +46 -145
  27. package/src/components/Client.tsx +109 -139
  28. package/src/components/Operations.tsx +10 -10
  29. package/src/components/StaticClassClient.tsx +46 -142
  30. package/src/components/Url.tsx +38 -50
  31. package/src/components/WrapperClient.tsx +11 -7
  32. package/src/functionParams.ts +118 -0
  33. package/src/generators/classClientGenerator.tsx +143 -172
  34. package/src/generators/clientGenerator.tsx +83 -81
  35. package/src/generators/groupedClientGenerator.tsx +50 -52
  36. package/src/generators/operationsGenerator.tsx +11 -18
  37. package/src/generators/staticClassClientGenerator.tsx +172 -184
  38. package/src/index.ts +9 -2
  39. package/src/plugin.ts +115 -145
  40. package/src/resolvers/resolverClient.ts +38 -0
  41. package/src/types.ts +128 -44
  42. package/src/utils.ts +156 -0
  43. package/dist/StaticClassClient-By-aMAe4.cjs +0 -677
  44. package/dist/StaticClassClient-By-aMAe4.cjs.map +0 -1
  45. package/dist/StaticClassClient-CCn9g9eF.js +0 -636
  46. package/dist/StaticClassClient-CCn9g9eF.js.map +0 -1
  47. package/dist/components.cjs +0 -7
  48. package/dist/components.d.ts +0 -216
  49. package/dist/components.js +0 -2
  50. package/dist/generators-BYUJaeZP.js +0 -723
  51. package/dist/generators-BYUJaeZP.js.map +0 -1
  52. package/dist/generators-DTxD9FDY.cjs +0 -753
  53. package/dist/generators-DTxD9FDY.cjs.map +0 -1
  54. package/dist/generators.cjs +0 -7
  55. package/dist/generators.d.ts +0 -517
  56. package/dist/generators.js +0 -2
  57. package/dist/types-DBQdg-BV.d.ts +0 -169
  58. package/src/components/index.ts +0 -5
  59. package/src/generators/index.ts +0 -5
  60. package/templates/clients/axios.ts +0 -70
  61. package/templates/clients/fetch.ts +0 -93
  62. package/templates/config.ts +0 -43
@@ -1,15 +1,13 @@
1
+ import { buildParamsMapping, getOperationParameters } from '@internals/shared'
1
2
  import { isValidVarName, URLPath } from '@internals/utils'
2
- import { getDefaultValue, type Operation } from '@kubb/oas'
3
- import type { OperationSchemas } from '@kubb/plugin-oas'
4
- import { getParamsMapping, getPathParams } from '@kubb/plugin-oas/utils'
5
- import { Const, File, Function, FunctionParams } from '@kubb/react-fabric'
6
- import type { FabricReactNode } from '@kubb/react-fabric/types'
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
- typeSchemas: OperationSchemas
22
- operation: Operation
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
- typeSchemas: OperationSchemas
27
+ node: ast.OperationNode
28
+ tsResolver: ResolverTs
30
29
  }
31
30
 
32
- function getParams({ paramsType, paramsCasing, pathParamsType, typeSchemas }: GetParamsProps) {
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
- return FunctionParams.factory({
40
- data: {
41
- mode: 'object',
42
- children: {
43
- ...pathParams,
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 FunctionParams.factory({
50
- pathParams: typeSchemas.pathParams?.name
51
- ? {
52
- mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
53
- children: getPathParams(typeSchemas.pathParams, {
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
- operation,
73
- }: Props): FabricReactNode {
74
- const path = new URLPath(operation.path)
75
- const params = getParams({
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
- typeSchemas,
66
+ node,
67
+ tsResolver,
80
68
  })
69
+ const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
81
70
 
82
- // Generate pathParams mapping when paramsCasing is used
83
- const pathParamsMapping = paramsCasing ? getParamsMapping(typeSchemas.pathParams, { casing: paramsCasing }) : undefined
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={params.toConstructor()}>
77
+ <Function name={name} export={isExportable} params={paramsSignature}>
88
78
  {pathParamsMapping &&
89
79
  Object.entries(pathParamsMapping)
90
- .filter(([originalName, camelCaseName]) => originalName !== camelCaseName && isValidVarName(originalName))
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: '${operation.method.toUpperCase()}', url: ${path.toTemplateString({ prefix: baseURL })} as const }`}</Const>
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 { camelCase } from '@internals/utils'
2
- import { File } from '@kubb/react-fabric'
3
- import type { FabricReactNode } from '@kubb/react-fabric/types'
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
- classNames: Array<string>
11
+ controllers: Array<ClientController>
8
12
  isExportable?: boolean
9
13
  isIndexable?: boolean
10
14
  }
11
15
 
12
- export function WrapperClient({ name, classNames, isExportable = true, isIndexable = true }: Props): FabricReactNode {
13
- const properties = classNames.map((className) => ` readonly ${camelCase(className)}: ${className}`).join('\n')
14
- const assignments = classNames.map((className) => ` this.${camelCase(className)} = new ${className}(config)`).join('\n')
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
+ }