@kubb/plugin-client 5.0.0-beta.42 → 5.0.0-beta.64

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 (39) hide show
  1. package/dist/clients/axios.cjs.map +1 -1
  2. package/dist/clients/axios.js.map +1 -1
  3. package/dist/clients/fetch.cjs.map +1 -1
  4. package/dist/clients/fetch.js.map +1 -1
  5. package/dist/index.cjs +439 -383
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.ts +60 -25
  8. package/dist/index.js +435 -383
  9. package/dist/index.js.map +1 -1
  10. package/dist/templates/clients/axios.source.cjs.map +1 -1
  11. package/dist/templates/clients/axios.source.js.map +1 -1
  12. package/dist/templates/clients/fetch.source.cjs.map +1 -1
  13. package/dist/templates/clients/fetch.source.js.map +1 -1
  14. package/dist/templates/config.source.cjs.map +1 -1
  15. package/dist/templates/config.source.js.map +1 -1
  16. package/package.json +10 -18
  17. package/extension.yaml +0 -1267
  18. package/src/clients/axios.ts +0 -113
  19. package/src/clients/fetch.ts +0 -201
  20. package/src/components/ClassClient.tsx +0 -137
  21. package/src/components/Client.tsx +0 -273
  22. package/src/components/Operations.tsx +0 -29
  23. package/src/components/StaticClassClient.tsx +0 -129
  24. package/src/components/Url.tsx +0 -91
  25. package/src/components/WrapperClient.tsx +0 -33
  26. package/src/functionParams.ts +0 -118
  27. package/src/generators/classClientGenerator.tsx +0 -253
  28. package/src/generators/clientGenerator.tsx +0 -127
  29. package/src/generators/groupedClientGenerator.tsx +0 -82
  30. package/src/generators/operationsGenerator.tsx +0 -34
  31. package/src/generators/staticClassClientGenerator.tsx +0 -232
  32. package/src/index.ts +0 -9
  33. package/src/plugin.ts +0 -160
  34. package/src/resolvers/resolverClient.ts +0 -45
  35. package/src/templates/clients/axios.source.ts +0 -4
  36. package/src/templates/clients/fetch.source.ts +0 -4
  37. package/src/templates/config.source.ts +0 -4
  38. package/src/types.ts +0 -268
  39. package/src/utils.ts +0 -159
@@ -1,273 +0,0 @@
1
- import {
2
- buildOperationComments,
3
- buildParamsMapping,
4
- buildRequestConfigType,
5
- getContentTypeInfo,
6
- getOperationParameters,
7
- getResponseType,
8
- resolveSuccessNames,
9
- } from '@internals/shared'
10
- import { isValidVarName, URLPath } from '@internals/utils'
11
- import { ast } from '@kubb/core'
12
- import type { ResolverTs } from '@kubb/plugin-ts'
13
- import { functionPrinter } from '@kubb/plugin-ts'
14
- import type { ResolverZod } from '@kubb/plugin-zod'
15
- import { File, Function } from '@kubb/renderer-jsx'
16
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
17
- import { createFunctionParams } from '../functionParams.ts'
18
- import type { PluginClient } from '../types.ts'
19
- import { buildUrlParamsNode } from './Url.tsx'
20
-
21
- type Props = {
22
- name: string
23
- urlName?: string
24
- isExportable?: boolean
25
- isIndexable?: boolean
26
- isConfigurable?: boolean
27
- returnType?: string
28
-
29
- baseURL: string | null | undefined
30
- dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
31
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
32
- paramsType: PluginClient['resolvedOptions']['pathParamsType']
33
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
34
- parser: PluginClient['resolvedOptions']['parser'] | undefined
35
- node: ast.OperationNode
36
- tsResolver: ResolverTs
37
- zodResolver?: ResolverZod | null
38
- children?: KubbReactNode
39
- }
40
-
41
- type GetParamsProps = {
42
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
43
- paramsType: PluginClient['resolvedOptions']['paramsType']
44
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
45
- node: ast.OperationNode
46
- tsResolver: ResolverTs
47
- isConfigurable: boolean
48
- }
49
-
50
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
51
-
52
- export function buildClientParamsNode({
53
- paramsType,
54
- paramsCasing,
55
- pathParamsType,
56
- node,
57
- tsResolver,
58
- isConfigurable,
59
- }: GetParamsProps): ast.FunctionParametersNode {
60
- return ast.createOperationParams(node, {
61
- paramsType,
62
- pathParamsType: paramsType === 'object' ? 'object' : pathParamsType === 'object' ? 'object' : 'inline',
63
- paramsCasing,
64
- resolver: tsResolver,
65
- extraParams: [
66
- ...(isConfigurable
67
- ? [
68
- ast.createFunctionParameter({
69
- name: 'config',
70
- type: ast.createParamsType({
71
- variant: 'reference',
72
- name: buildRequestConfigType(node, tsResolver),
73
- }),
74
- default: '{}',
75
- }),
76
- ]
77
- : []),
78
- ],
79
- })
80
- }
81
-
82
- export function Client({
83
- name,
84
- isExportable = true,
85
- isIndexable = true,
86
- returnType,
87
- baseURL,
88
- dataReturnType,
89
- parser,
90
- paramsType,
91
- paramsCasing,
92
- pathParamsType,
93
- node,
94
- tsResolver,
95
- zodResolver,
96
- urlName,
97
- children,
98
- isConfigurable = true,
99
- }: Props): KubbReactNode {
100
- if (!ast.isHttpOperationNode(node)) return null
101
- const path = new URLPath(node.path)
102
- const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node)
103
- const isFormData = !isMultipleContentTypes && contentType === 'multipart/form-data'
104
- const responseType = getResponseType(node)
105
-
106
- const { path: originalPathParams, query: originalQueryParams, header: originalHeaderParams } = getOperationParameters(node)
107
- const { path: casedPathParams, query: casedQueryParams, header: casedHeaderParams } = getOperationParameters(node, { paramsCasing })
108
-
109
- const pathParamsMapping = paramsCasing && !urlName ? buildParamsMapping(originalPathParams, casedPathParams) : null
110
- const queryParamsMapping = paramsCasing ? buildParamsMapping(originalQueryParams, casedQueryParams) : null
111
- const headerParamsMapping = paramsCasing ? buildParamsMapping(originalHeaderParams, casedHeaderParams) : null
112
-
113
- const requestName = node.requestBody?.content?.[0]?.schema ? tsResolver.resolveDataName(node) : null
114
- const successNames = resolveSuccessNames(node, tsResolver)
115
- const responseName = successNames.length > 0 ? successNames.join(' | ') : tsResolver.resolveResponseName(node)
116
- const queryParamsName = originalQueryParams.length > 0 ? tsResolver.resolveQueryParamsName(node, originalQueryParams[0]!) : null
117
- const headerParamsName = originalHeaderParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, originalHeaderParams[0]!) : null
118
-
119
- const zodResponseName = zodResolver && parser === 'zod' ? zodResolver.resolveResponseName?.(node) : null
120
- const zodRequestName = zodResolver && parser === 'zod' && node.requestBody?.content?.[0]?.schema ? zodResolver.resolveDataName?.(node) : null
121
-
122
- const errorNames = node.responses
123
- .filter((r) => {
124
- const code = Number.parseInt(r.statusCode, 10)
125
- return code >= 400
126
- })
127
- .map((r) => tsResolver.resolveResponseStatusName(node, r.statusCode))
128
-
129
- const headers = [
130
- !isMultipleContentTypes && contentType !== 'application/json' && contentType !== 'multipart/form-data' ? `'Content-Type': '${contentType}'` : null,
131
- headerParamsName ? (headerParamsMapping ? '...mappedHeaders' : '...headers') : null,
132
- ].filter(Boolean)
133
-
134
- const TError = `ResponseErrorConfig<${errorNames.length > 0 ? errorNames.join(' | ') : 'Error'}>`
135
-
136
- const generics = [responseName, TError, requestName || 'unknown'].filter(Boolean)
137
- const paramsNode = buildClientParamsNode({
138
- paramsType,
139
- paramsCasing,
140
- pathParamsType,
141
- node,
142
- tsResolver,
143
- isConfigurable,
144
- })
145
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
146
-
147
- const urlParamsNode = buildUrlParamsNode({
148
- paramsType,
149
- paramsCasing,
150
- pathParamsType,
151
- node,
152
- tsResolver,
153
- })
154
- const callPrinter = functionPrinter({ mode: 'call' })
155
- const urlParamsCall = callPrinter.print(urlParamsNode) ?? ''
156
-
157
- const clientParams = createFunctionParams({
158
- config: {
159
- mode: 'object',
160
- children: {
161
- method: {
162
- value: JSON.stringify(node.method.toUpperCase()),
163
- },
164
- url: {
165
- value: urlName ? `${urlName}(${urlParamsCall}).url.toString()` : path.template,
166
- },
167
- baseURL:
168
- baseURL && !urlName
169
- ? {
170
- value: `\`${baseURL}\``,
171
- }
172
- : null,
173
- params: queryParamsName ? (queryParamsMapping ? { value: 'mappedParams' } : {}) : null,
174
- data: requestName
175
- ? {
176
- value:
177
- isMultipleContentTypes && hasFormData
178
- ? "contentType === 'multipart/form-data' ? formData as FormData : requestData"
179
- : isFormData
180
- ? 'formData as FormData'
181
- : 'requestData',
182
- }
183
- : null,
184
- contentType: isConfigurable && isMultipleContentTypes ? {} : null,
185
- responseType: responseType ? { value: JSON.stringify(responseType) } : null,
186
- requestConfig: isConfigurable
187
- ? {
188
- mode: 'inlineSpread',
189
- }
190
- : null,
191
- headers: headers.length
192
- ? {
193
- value: isConfigurable ? `{ ${headers.join(', ')}, ...requestConfig.headers }` : `{ ${headers.join(', ')} }`,
194
- }
195
- : null,
196
- },
197
- },
198
- })
199
-
200
- const childrenElement = children ? (
201
- children
202
- ) : (
203
- <>
204
- {dataReturnType === 'full' && parser === 'zod' && zodResponseName && `return {...res, data: ${zodResponseName}.parse(res.data)}`}
205
- {dataReturnType === 'data' && parser === 'zod' && zodResponseName && `return ${zodResponseName}.parse(res.data)`}
206
- {dataReturnType === 'full' && parser !== 'zod' && 'return res'}
207
- {dataReturnType === 'data' && parser !== 'zod' && 'return res.data'}
208
- </>
209
- )
210
-
211
- return (
212
- <>
213
- <br />
214
-
215
- <File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
216
- <Function
217
- name={name}
218
- async
219
- export={isExportable}
220
- params={paramsSignature}
221
- JSDoc={{
222
- comments: buildOperationComments(node, { link: 'urlPath', linkPosition: 'beforeDeprecated', splitLines: true }),
223
- }}
224
- returnType={returnType}
225
- >
226
- {isConfigurable
227
- ? `const { client: request = client, ${isMultipleContentTypes ? `contentType = ${JSON.stringify(contentType)}, ` : ''}...requestConfig } = config`
228
- : ''}
229
- <br />
230
- <br />
231
- {pathParamsMapping &&
232
- Object.entries(pathParamsMapping)
233
- .filter(([originalName, camelCaseName]) => isValidVarName(originalName) && originalName !== camelCaseName)
234
- .map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`)
235
- .join('\n')}
236
- {pathParamsMapping && (
237
- <>
238
- <br />
239
- <br />
240
- </>
241
- )}
242
- {queryParamsMapping && queryParamsName && (
243
- <>
244
- {`const mappedParams = params ? { ${Object.entries(queryParamsMapping)
245
- .map(([originalName, camelCaseName]) => `"${originalName}": params.${camelCaseName}`)
246
- .join(', ')} } : undefined`}
247
- <br />
248
- <br />
249
- </>
250
- )}
251
- {headerParamsMapping && headerParamsName && (
252
- <>
253
- {`const mappedHeaders = headers ? { ${Object.entries(headerParamsMapping)
254
- .map(([originalName, camelCaseName]) => `"${originalName}": headers.${camelCaseName}`)
255
- .join(', ')} } : undefined`}
256
- <br />
257
- <br />
258
- </>
259
- )}
260
- {parser === 'zod' && zodRequestName ? `const requestData = ${zodRequestName}.parse(data)` : requestName && 'const requestData = data'}
261
- <br />
262
- {(isFormData || (isMultipleContentTypes && hasFormData)) && requestName && 'const formData = buildFormData(requestData)'}
263
- <br />
264
- {isConfigurable
265
- ? `const res = await request<${generics.join(', ')}>(${clientParams.toCall()})`
266
- : `const res = await client<${generics.join(', ')}>(${clientParams.toCall()})`}
267
- <br />
268
- {childrenElement}
269
- </Function>
270
- </File.Source>
271
- </>
272
- )
273
- }
@@ -1,29 +0,0 @@
1
- import { URLPath } from '@internals/utils'
2
- import { ast } from '@kubb/core'
3
- import { Const, File } from '@kubb/renderer-jsx'
4
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
5
-
6
- type OperationsProps = {
7
- name: string
8
- nodes: Array<ast.OperationNode>
9
- }
10
-
11
- export function Operations({ name, nodes }: OperationsProps): KubbReactNode {
12
- const operationsObject: Record<string, { path: string; method: string }> = {}
13
-
14
- nodes.forEach((node) => {
15
- if (!ast.isHttpOperationNode(node)) return
16
- operationsObject[node.operationId] = {
17
- path: new URLPath(node.path).URL,
18
- method: node.method.toLowerCase(),
19
- }
20
- })
21
-
22
- return (
23
- <File.Source name={name} isExportable isIndexable>
24
- <Const name={name} export>
25
- {JSON.stringify(operationsObject, undefined, 2)}
26
- </Const>
27
- </File.Source>
28
- )
29
- }
@@ -1,129 +0,0 @@
1
- import { buildOperationComments, getContentTypeInfo, getOperationParameters } from '@internals/shared'
2
- import { buildJSDoc, URLPath } from '@internals/utils'
3
- import { ast } from '@kubb/core'
4
- import type { ResolverTs } from '@kubb/plugin-ts'
5
- import { functionPrinter } from '@kubb/plugin-ts'
6
- import type { ResolverZod } from '@kubb/plugin-zod'
7
- import { File } from '@kubb/renderer-jsx'
8
- import type { KubbReactNode } from '@kubb/renderer-jsx/types'
9
- import type { PluginClient } from '../types.ts'
10
- import { buildClassClientParams, buildFormDataLine, buildGenerics, buildHeaders, buildRequestDataLine, buildReturnStatement } from '../utils.ts'
11
- import { buildClientParamsNode } from './Client.tsx'
12
-
13
- type OperationData = {
14
- node: ast.OperationNode
15
- name: string
16
- tsResolver: ResolverTs
17
- zodResolver?: ResolverZod | null
18
- }
19
-
20
- type Props = {
21
- name: string
22
- isExportable?: boolean
23
- isIndexable?: boolean
24
- operations: Array<OperationData>
25
- baseURL: string | null | undefined
26
- dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
27
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
28
- paramsType: PluginClient['resolvedOptions']['pathParamsType']
29
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
30
- parser: PluginClient['resolvedOptions']['parser'] | undefined
31
- children?: KubbReactNode
32
- }
33
-
34
- type GenerateMethodProps = {
35
- node: ast.OperationNode
36
- name: string
37
- tsResolver: ResolverTs
38
- zodResolver?: ResolverZod | null
39
- baseURL: string | null | undefined
40
- dataReturnType: PluginClient['resolvedOptions']['dataReturnType']
41
- parser: PluginClient['resolvedOptions']['parser'] | undefined
42
- paramsType: PluginClient['resolvedOptions']['paramsType']
43
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
44
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
45
- }
46
-
47
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
48
-
49
- function generateMethod({
50
- node,
51
- name,
52
- tsResolver,
53
- zodResolver,
54
- baseURL,
55
- dataReturnType,
56
- parser,
57
- paramsType,
58
- paramsCasing,
59
- pathParamsType,
60
- }: GenerateMethodProps): string {
61
- if (!ast.isHttpOperationNode(node)) return ''
62
- const path = new URLPath(node.path, { casing: paramsCasing })
63
- const { defaultContentType: contentType, isMultipleContentTypes, hasFormData } = getContentTypeInfo(node)
64
- const isFormData = !isMultipleContentTypes && contentType === 'multipart/form-data'
65
- const { header: headerParams } = getOperationParameters(node)
66
- const headerParamsName = headerParams.length > 0 ? tsResolver.resolveHeaderParamsName(node, headerParams[0]!) : null
67
- const headers = isMultipleContentTypes ? (headerParamsName ? ['...headers'] : []) : buildHeaders(contentType, !!headerParamsName)
68
- const generics = buildGenerics(node, tsResolver)
69
- const paramsNode = buildClientParamsNode({ paramsType, paramsCasing, pathParamsType, node, tsResolver, isConfigurable: true })
70
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
71
- const clientParams = buildClassClientParams({ node, path, baseURL, tsResolver, isFormData, isMultipleContentTypes, hasFormData, headers })
72
- const jsdoc = buildJSDoc(buildOperationComments(node, { link: 'urlPath', linkPosition: 'beforeDeprecated', splitLines: true }))
73
-
74
- const requestDataLine = buildRequestDataLine({ parser, node, zodResolver })
75
- const formDataLine = buildFormDataLine(isFormData || (isMultipleContentTypes && hasFormData), !!node.requestBody?.content?.[0]?.schema)
76
- const returnStatement = buildReturnStatement({ dataReturnType, parser, node, zodResolver })
77
-
78
- const methodBody = [
79
- `const { client: request = client, ${isMultipleContentTypes ? `contentType = ${JSON.stringify(contentType)}, ` : ''}...requestConfig } = mergeConfig(this.#config, config)`,
80
- '',
81
- requestDataLine,
82
- formDataLine,
83
- `const res = await request<${generics.join(', ')}>(${clientParams.toCall()})`,
84
- returnStatement,
85
- ]
86
- .filter(Boolean)
87
- .map((line) => ` ${line}`)
88
- .join('\n')
89
-
90
- return `${jsdoc} static async ${name}(${paramsSignature}) {\n${methodBody}\n }`
91
- }
92
-
93
- export function StaticClassClient({
94
- name,
95
- isExportable = true,
96
- isIndexable = true,
97
- operations,
98
- baseURL,
99
- dataReturnType,
100
- parser,
101
- paramsType,
102
- paramsCasing,
103
- pathParamsType,
104
- children,
105
- }: Props): KubbReactNode {
106
- const methods = operations.map(({ node, name: methodName, tsResolver, zodResolver }) =>
107
- generateMethod({
108
- node,
109
- name: methodName,
110
- tsResolver,
111
- zodResolver,
112
- baseURL,
113
- dataReturnType,
114
- parser,
115
- paramsType,
116
- paramsCasing,
117
- pathParamsType,
118
- }),
119
- )
120
-
121
- const classCode = `export class ${name} {\n static #config: Partial<RequestConfig> & { client?: Client } = {}\n\n${methods.join('\n\n')}\n}`
122
-
123
- return (
124
- <File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
125
- {classCode}
126
- {children}
127
- </File.Source>
128
- )
129
- }
@@ -1,91 +0,0 @@
1
- import { buildParamsMapping, getOperationParameters } from '@internals/shared'
2
- import { isValidVarName, URLPath } from '@internals/utils'
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'
8
- import type { PluginClient } from '../types.ts'
9
-
10
- type Props = {
11
- name: string
12
- isExportable?: boolean
13
- isIndexable?: boolean
14
-
15
- baseURL: string | null | undefined
16
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
17
- paramsType: PluginClient['resolvedOptions']['pathParamsType']
18
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
19
- node: ast.OperationNode
20
- tsResolver: ResolverTs
21
- }
22
-
23
- type GetParamsProps = {
24
- paramsCasing: PluginClient['resolvedOptions']['paramsCasing']
25
- paramsType: PluginClient['resolvedOptions']['paramsType']
26
- pathParamsType: PluginClient['resolvedOptions']['pathParamsType']
27
- node: ast.OperationNode
28
- tsResolver: ResolverTs
29
- }
30
-
31
- const declarationPrinter = functionPrinter({ mode: 'declaration' })
32
-
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,
39
- }
40
-
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,
46
- })
47
- }
48
-
49
- export function Url({
50
- name,
51
- isExportable = true,
52
- isIndexable = true,
53
- baseURL,
54
- paramsType,
55
- paramsCasing,
56
- pathParamsType,
57
- node,
58
- tsResolver,
59
- }: Props): KubbReactNode {
60
- if (!ast.isHttpOperationNode(node)) return null
61
- const path = new URLPath(node.path)
62
-
63
- const paramsNode = buildUrlParamsNode({
64
- paramsType,
65
- paramsCasing,
66
- pathParamsType,
67
- node,
68
- tsResolver,
69
- })
70
- const paramsSignature = declarationPrinter.print(paramsNode) ?? ''
71
-
72
- const { path: originalPathParams } = getOperationParameters(node)
73
- const { path: casedPathParams } = getOperationParameters(node, { paramsCasing })
74
- const pathParamsMapping = paramsCasing ? buildParamsMapping(originalPathParams, casedPathParams) : null
75
-
76
- return (
77
- <File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
78
- <Function name={name} export={isExportable} params={paramsSignature}>
79
- {pathParamsMapping &&
80
- Object.entries(pathParamsMapping)
81
- .filter(([originalName, camelCaseName]) => isValidVarName(originalName) && originalName !== camelCaseName)
82
- .map(([originalName, camelCaseName]) => `const ${originalName} = ${camelCaseName}`)
83
- .join('\n')}
84
- {pathParamsMapping && Object.keys(pathParamsMapping).length > 0 && <br />}
85
- <Const name={'res'}>{`{ method: '${node.method.toUpperCase()}', url: ${path.toTemplateString({ prefix: baseURL })} as const }`}</Const>
86
- <br />
87
- return res
88
- </Function>
89
- </File.Source>
90
- )
91
- }
@@ -1,33 +0,0 @@
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
- }
8
-
9
- type Props = {
10
- name: string
11
- controllers: Array<ClientController>
12
- isExportable?: boolean
13
- isIndexable?: boolean
14
- }
15
-
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')
19
-
20
- const classCode = `export class ${name} {
21
- ${properties}
22
-
23
- constructor(config: Partial<RequestConfig> & { client?: Client } = {}) {
24
- ${assignments}
25
- }
26
- }`
27
-
28
- return (
29
- <File.Source name={name} isExportable={isExportable} isIndexable={isIndexable}>
30
- {classCode}
31
- </File.Source>
32
- )
33
- }
@@ -1,118 +0,0 @@
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 | null | 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 }) : null
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) ?? undefined,
36
- default: spec.default,
37
- rest: spec.mode === 'inlineSpread',
38
- })
39
- }
40
-
41
- return ast.createFunctionParameter({
42
- name,
43
- type: createType(spec.type) ?? undefined,
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 != null)
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 != null)
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 | null | undefined>) {
96
- const entries = Object.entries(params).filter(([, spec]) => spec != null) 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
- }