@kubb/plugin-client 3.0.0-alpha.0
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 +21 -0
- package/README.md +44 -0
- package/client.ts +44 -0
- package/dist/chunk-W57BRY5O.js +201 -0
- package/dist/chunk-W57BRY5O.js.map +1 -0
- package/dist/chunk-W7F5CMU6.cjs +201 -0
- package/dist/chunk-W7F5CMU6.cjs.map +1 -0
- package/dist/client.cjs +19 -0
- package/dist/client.cjs.map +1 -0
- package/dist/client.d.cts +29 -0
- package/dist/client.d.ts +29 -0
- package/dist/client.js +19 -0
- package/dist/client.js.map +1 -0
- package/dist/components.cjs +9 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +8 -0
- package/dist/components.d.ts +8 -0
- package/dist/components.js +9 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +168 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +13 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +168 -0
- package/dist/index.js.map +1 -0
- package/dist/types-DWYt1NR3.d.cts +233 -0
- package/dist/types-DWYt1NR3.d.ts +233 -0
- package/globals.d.ts +24 -0
- package/package.json +112 -0
- package/src/OperationGenerator.tsx +65 -0
- package/src/components/Client.tsx +260 -0
- package/src/components/Operations.tsx +94 -0
- package/src/components/__snapshots__/Client/showPetById.ts +11 -0
- package/src/components/__snapshots__/Operations/showPetById.ts +6 -0
- package/src/components/__snapshots__/Query/showPetById.ts +15 -0
- package/src/components/index.ts +2 -0
- package/src/index.ts +2 -0
- package/src/plugin.ts +145 -0
- package/src/types.ts +129 -0
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import { URLPath } from '@kubb/core/utils'
|
|
2
|
+
import { Parser, File, Function, useApp } from '@kubb/react'
|
|
3
|
+
import { pluginTsName } from '@kubb/plugin-ts'
|
|
4
|
+
import { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'
|
|
5
|
+
import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
|
|
6
|
+
|
|
7
|
+
import { isOptional } from '@kubb/oas'
|
|
8
|
+
import type { HttpMethod } from '@kubb/oas'
|
|
9
|
+
import type { KubbNode, Params } from '@kubb/react'
|
|
10
|
+
import type { ComponentProps, ComponentType } from 'react'
|
|
11
|
+
import type { FileMeta, PluginClient } from '../types.ts'
|
|
12
|
+
|
|
13
|
+
type TemplateProps = {
|
|
14
|
+
/**
|
|
15
|
+
* Name of the function
|
|
16
|
+
*/
|
|
17
|
+
name: string
|
|
18
|
+
/**
|
|
19
|
+
* Parameters/options/props that need to be used
|
|
20
|
+
*/
|
|
21
|
+
params: Params
|
|
22
|
+
/**
|
|
23
|
+
* Generics that needs to be added for TypeScript
|
|
24
|
+
*/
|
|
25
|
+
generics?: string
|
|
26
|
+
/**
|
|
27
|
+
* ReturnType(see async for adding Promise type)
|
|
28
|
+
*/
|
|
29
|
+
returnType?: string
|
|
30
|
+
/**
|
|
31
|
+
* Options for JSdocs
|
|
32
|
+
*/
|
|
33
|
+
JSDoc?: {
|
|
34
|
+
comments: string[]
|
|
35
|
+
}
|
|
36
|
+
client: {
|
|
37
|
+
baseURL: string | undefined
|
|
38
|
+
generics: string | string[]
|
|
39
|
+
method: HttpMethod
|
|
40
|
+
path: URLPath
|
|
41
|
+
dataReturnType: PluginClient['options']['dataReturnType']
|
|
42
|
+
withQueryParams: boolean
|
|
43
|
+
withData: boolean
|
|
44
|
+
withHeaders: boolean
|
|
45
|
+
contentType: string
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function Template({ name, generics, returnType, params, JSDoc, client }: TemplateProps): KubbNode {
|
|
50
|
+
const isFormData = client.contentType === 'multipart/form-data'
|
|
51
|
+
const headers = [
|
|
52
|
+
client.contentType !== 'application/json' ? `'Content-Type': '${client.contentType}'` : undefined,
|
|
53
|
+
client.withHeaders ? '...headers' : undefined,
|
|
54
|
+
]
|
|
55
|
+
.filter(Boolean)
|
|
56
|
+
.join(', ')
|
|
57
|
+
const clientParams: Params = {
|
|
58
|
+
data: {
|
|
59
|
+
mode: 'object',
|
|
60
|
+
children: {
|
|
61
|
+
method: {
|
|
62
|
+
type: 'string',
|
|
63
|
+
value: JSON.stringify(client.method),
|
|
64
|
+
},
|
|
65
|
+
url: {
|
|
66
|
+
type: 'string',
|
|
67
|
+
value: client.path.template,
|
|
68
|
+
},
|
|
69
|
+
baseURL: client.baseURL
|
|
70
|
+
? {
|
|
71
|
+
type: 'string',
|
|
72
|
+
value: JSON.stringify(client.baseURL),
|
|
73
|
+
}
|
|
74
|
+
: undefined,
|
|
75
|
+
params: client.withQueryParams
|
|
76
|
+
? {
|
|
77
|
+
type: 'any',
|
|
78
|
+
}
|
|
79
|
+
: undefined,
|
|
80
|
+
data: client.withData
|
|
81
|
+
? {
|
|
82
|
+
type: 'any',
|
|
83
|
+
value: isFormData ? 'formData' : undefined,
|
|
84
|
+
}
|
|
85
|
+
: undefined,
|
|
86
|
+
headers: headers.length
|
|
87
|
+
? {
|
|
88
|
+
type: 'any',
|
|
89
|
+
value: headers.length ? `{ ${headers}, ...options.headers }` : undefined,
|
|
90
|
+
}
|
|
91
|
+
: undefined,
|
|
92
|
+
options: {
|
|
93
|
+
type: 'any',
|
|
94
|
+
mode: 'inlineSpread',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
const formData = isFormData
|
|
101
|
+
? `
|
|
102
|
+
const formData = new FormData()
|
|
103
|
+
if(data) {
|
|
104
|
+
Object.keys(data).forEach((key) => {
|
|
105
|
+
const value = data[key];
|
|
106
|
+
if (typeof key === "string" && (typeof value === "string" || value instanceof Blob)) {
|
|
107
|
+
formData.append(key, value);
|
|
108
|
+
}
|
|
109
|
+
})
|
|
110
|
+
}
|
|
111
|
+
`
|
|
112
|
+
: undefined
|
|
113
|
+
|
|
114
|
+
return (
|
|
115
|
+
<Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
|
|
116
|
+
{formData || ''}
|
|
117
|
+
<Function.Call name="res" to={<Function name="client" async generics={client.generics} params={clientParams} />} />
|
|
118
|
+
<Function.Return>{client.dataReturnType === 'data' ? 'res.data' : 'res'}</Function.Return>
|
|
119
|
+
</Function>
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
type RootTemplateProps = {
|
|
124
|
+
children?: React.ReactNode
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function RootTemplate({ children }: RootTemplateProps) {
|
|
128
|
+
const {
|
|
129
|
+
plugin: {
|
|
130
|
+
options: {
|
|
131
|
+
client: { importPath },
|
|
132
|
+
extName,
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
} = useApp<PluginClient>()
|
|
136
|
+
|
|
137
|
+
const { getSchemas, getFile } = useOperationManager()
|
|
138
|
+
const operation = useOperation()
|
|
139
|
+
|
|
140
|
+
const file = getFile(operation)
|
|
141
|
+
const fileType = getFile(operation, { pluginKey: [pluginTsName] })
|
|
142
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
143
|
+
|
|
144
|
+
return (
|
|
145
|
+
<Parser language="typescript">
|
|
146
|
+
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
|
|
147
|
+
<File.Import name={'client'} path={importPath} />
|
|
148
|
+
<File.Import name={['ResponseConfig']} path={importPath} isTypeOnly />
|
|
149
|
+
<File.Import
|
|
150
|
+
extName={extName}
|
|
151
|
+
name={[schemas.request?.name, schemas.response.name, schemas.pathParams?.name, schemas.queryParams?.name, schemas.headerParams?.name].filter(Boolean)}
|
|
152
|
+
root={file.path}
|
|
153
|
+
path={fileType.path}
|
|
154
|
+
isTypeOnly
|
|
155
|
+
/>
|
|
156
|
+
<File.Source>{children}</File.Source>
|
|
157
|
+
</File>
|
|
158
|
+
</Parser>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const defaultTemplates = { default: Template, root: RootTemplate } as const
|
|
163
|
+
|
|
164
|
+
type Templates = Partial<typeof defaultTemplates>
|
|
165
|
+
|
|
166
|
+
type ClientProps = {
|
|
167
|
+
baseURL: string | undefined
|
|
168
|
+
/**
|
|
169
|
+
* This will make it possible to override the default behaviour.
|
|
170
|
+
*/
|
|
171
|
+
Template?: ComponentType<ComponentProps<typeof Template>>
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export function Client({ baseURL, Template = defaultTemplates.default }: ClientProps): KubbNode {
|
|
175
|
+
const {
|
|
176
|
+
plugin: {
|
|
177
|
+
options: { client, dataReturnType, pathParamsType },
|
|
178
|
+
},
|
|
179
|
+
} = useApp<PluginClient>()
|
|
180
|
+
|
|
181
|
+
const { getSchemas, getName } = useOperationManager()
|
|
182
|
+
const operation = useOperation()
|
|
183
|
+
|
|
184
|
+
const contentType = operation.getContentType()
|
|
185
|
+
const name = getName(operation, { type: 'function' })
|
|
186
|
+
const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<Template
|
|
190
|
+
name={name}
|
|
191
|
+
params={{
|
|
192
|
+
pathParams: {
|
|
193
|
+
mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
|
|
194
|
+
children: getPathParams(schemas.pathParams, { typed: true }),
|
|
195
|
+
},
|
|
196
|
+
data: schemas.request?.name
|
|
197
|
+
? {
|
|
198
|
+
type: schemas.request?.name,
|
|
199
|
+
optional: isOptional(schemas.request?.schema),
|
|
200
|
+
}
|
|
201
|
+
: undefined,
|
|
202
|
+
params: schemas.queryParams?.name
|
|
203
|
+
? {
|
|
204
|
+
type: schemas.queryParams?.name,
|
|
205
|
+
optional: isOptional(schemas.queryParams?.schema),
|
|
206
|
+
}
|
|
207
|
+
: undefined,
|
|
208
|
+
headers: schemas.headerParams?.name
|
|
209
|
+
? {
|
|
210
|
+
type: schemas.headerParams?.name,
|
|
211
|
+
optional: isOptional(schemas.headerParams?.schema),
|
|
212
|
+
}
|
|
213
|
+
: undefined,
|
|
214
|
+
options: {
|
|
215
|
+
type: 'Partial<Parameters<typeof client>[0]>',
|
|
216
|
+
default: '{}',
|
|
217
|
+
},
|
|
218
|
+
}}
|
|
219
|
+
returnType={dataReturnType === 'data' ? `ResponseConfig<${schemas.response.name}>["data"]` : `ResponseConfig<${schemas.response.name}>`}
|
|
220
|
+
JSDoc={{
|
|
221
|
+
comments: getComments(operation),
|
|
222
|
+
}}
|
|
223
|
+
client={{
|
|
224
|
+
// only set baseURL from serverIndex(swagger) when no custom client(default) is used
|
|
225
|
+
baseURL: client.importPath === '@kubb/plugin-client/client' ? baseURL : undefined,
|
|
226
|
+
generics: [schemas.response.name, schemas.request?.name].filter(Boolean),
|
|
227
|
+
dataReturnType,
|
|
228
|
+
withQueryParams: !!schemas.queryParams?.name,
|
|
229
|
+
withData: !!schemas.request?.name,
|
|
230
|
+
withHeaders: !!schemas.headerParams?.name,
|
|
231
|
+
method: operation.method,
|
|
232
|
+
path: new URLPath(operation.path),
|
|
233
|
+
contentType,
|
|
234
|
+
}}
|
|
235
|
+
/>
|
|
236
|
+
)
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
type FileProps = {
|
|
240
|
+
baseURL: string | undefined
|
|
241
|
+
/**
|
|
242
|
+
* This will make it possible to override the default behaviour.
|
|
243
|
+
*/
|
|
244
|
+
templates?: Templates
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
Client.File = function ({ baseURL, ...props }: FileProps): KubbNode {
|
|
248
|
+
const templates = { ...defaultTemplates, ...props.templates }
|
|
249
|
+
|
|
250
|
+
const Template = templates.default
|
|
251
|
+
const RootTemplate = templates.root
|
|
252
|
+
|
|
253
|
+
return (
|
|
254
|
+
<RootTemplate>
|
|
255
|
+
<Client baseURL={baseURL} Template={Template} />
|
|
256
|
+
</RootTemplate>
|
|
257
|
+
)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
Client.templates = defaultTemplates
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { URLPath } from '@kubb/core/utils'
|
|
2
|
+
import { useOperations } from '@kubb/plugin-oas/hooks'
|
|
3
|
+
import { Const, File, Parser, useApp } from '@kubb/react'
|
|
4
|
+
|
|
5
|
+
import type { HttpMethod, Operation } from '@kubb/oas'
|
|
6
|
+
import type { KubbNode } from '@kubb/react'
|
|
7
|
+
import type { ComponentProps, ComponentType } from 'react'
|
|
8
|
+
import type { FileMeta, PluginClient } from '../types.ts'
|
|
9
|
+
|
|
10
|
+
type TemplateProps = {
|
|
11
|
+
/**
|
|
12
|
+
* Name of the function
|
|
13
|
+
*/
|
|
14
|
+
name: string
|
|
15
|
+
operations: Operation[]
|
|
16
|
+
baseURL: string | undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function Template({ name, operations }: TemplateProps): KubbNode {
|
|
20
|
+
const operationsObject: Record<string, { path: string; method: HttpMethod }> = {}
|
|
21
|
+
|
|
22
|
+
operations.forEach((operation) => {
|
|
23
|
+
operationsObject[operation.getOperationId()] = {
|
|
24
|
+
path: new URLPath(operation.path).URL,
|
|
25
|
+
method: operation.method,
|
|
26
|
+
}
|
|
27
|
+
})
|
|
28
|
+
return (
|
|
29
|
+
<Const name={name} export asConst>
|
|
30
|
+
{JSON.stringify(operationsObject, undefined, 2)}
|
|
31
|
+
</Const>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
type RootTemplateProps = {
|
|
36
|
+
children?: React.ReactNode
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function RootTemplate({ children }: RootTemplateProps) {
|
|
40
|
+
const {
|
|
41
|
+
pluginManager,
|
|
42
|
+
plugin: { key: pluginKey },
|
|
43
|
+
} = useApp<PluginClient>()
|
|
44
|
+
const file = pluginManager.getFile({ name: 'operations', extName: '.ts', pluginKey })
|
|
45
|
+
|
|
46
|
+
return (
|
|
47
|
+
<Parser language="typescript">
|
|
48
|
+
<File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta} exportable={false}>
|
|
49
|
+
<File.Source>{children}</File.Source>
|
|
50
|
+
</File>
|
|
51
|
+
</Parser>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const defaultTemplates = { default: Template, root: RootTemplate } as const
|
|
56
|
+
|
|
57
|
+
type Templates = Partial<typeof defaultTemplates>
|
|
58
|
+
|
|
59
|
+
type Props = {
|
|
60
|
+
baseURL: string | undefined
|
|
61
|
+
/**
|
|
62
|
+
* This will make it possible to override the default behaviour.
|
|
63
|
+
*/
|
|
64
|
+
Template?: ComponentType<ComponentProps<typeof Template>>
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function Operations({ baseURL, Template = defaultTemplates.default }: Props): KubbNode {
|
|
68
|
+
const operations = useOperations()
|
|
69
|
+
|
|
70
|
+
return <Template baseURL={baseURL} name="operations" operations={operations} />
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
type FileProps = {
|
|
74
|
+
baseURL: string | undefined
|
|
75
|
+
/**
|
|
76
|
+
* This will make it possible to override the default behaviour.
|
|
77
|
+
*/
|
|
78
|
+
templates?: Templates
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
Operations.File = function ({ baseURL, ...props }: FileProps): KubbNode {
|
|
82
|
+
const templates = { ...defaultTemplates, ...props.templates }
|
|
83
|
+
|
|
84
|
+
const Template = templates.default
|
|
85
|
+
const RootTemplate = templates.root
|
|
86
|
+
|
|
87
|
+
return (
|
|
88
|
+
<RootTemplate>
|
|
89
|
+
<Operations baseURL={baseURL} Template={Template} />
|
|
90
|
+
</RootTemplate>
|
|
91
|
+
)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
Operations.templates = defaultTemplates
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @summary Info for a specific pet
|
|
3
|
+
* @link /pets/:pet_id
|
|
4
|
+
*/
|
|
5
|
+
export async function showPetById(
|
|
6
|
+
{ petId, testId }: { petId: ShowPetByIdPathParams['pet_id']; testId: ShowPetByIdPathParams['testId'] },
|
|
7
|
+
options: Partial<Parameters<typeof client>[0]> = {},
|
|
8
|
+
): Promise<ResponseConfig<ShowPetByIdQueryResponse>['data']> {
|
|
9
|
+
const res = await client<ShowPetByIdQueryResponse>({ method: 'get', url: `/pets/${petId}`, ...options })
|
|
10
|
+
return res.data
|
|
11
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @summary Info for a specific pet
|
|
3
|
+
* @link /pets/:pet_id */
|
|
4
|
+
|
|
5
|
+
export async function showPetById(
|
|
6
|
+
{ petId, testId }: ShowPetByIdPathParams,
|
|
7
|
+
options: Partial<Parameters<typeof client>[0]> = {},
|
|
8
|
+
): Promise<ResponseConfig<ShowPetByIdQueryResponse>['data']> {
|
|
9
|
+
const res = await client<ShowPetByIdQueryResponse>({
|
|
10
|
+
method: 'get',
|
|
11
|
+
url: `/pets/${petId}`,
|
|
12
|
+
...options,
|
|
13
|
+
})
|
|
14
|
+
return res.data
|
|
15
|
+
}
|
package/src/index.ts
ADDED
package/src/plugin.ts
ADDED
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
|
|
3
|
+
import { FileManager, PluginManager, createPlugin } from '@kubb/core'
|
|
4
|
+
import { camelCase } from '@kubb/core/transformers'
|
|
5
|
+
import { renderTemplate } from '@kubb/core/utils'
|
|
6
|
+
import { pluginOasName } from '@kubb/plugin-oas'
|
|
7
|
+
import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
|
|
8
|
+
|
|
9
|
+
import { OperationGenerator } from './OperationGenerator.tsx'
|
|
10
|
+
import { Client, Operations } from './components/index.ts'
|
|
11
|
+
|
|
12
|
+
import type { Plugin } from '@kubb/core'
|
|
13
|
+
import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
|
|
14
|
+
import type { PluginClient } from './types.ts'
|
|
15
|
+
|
|
16
|
+
export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
|
|
17
|
+
|
|
18
|
+
export const pluginClient = createPlugin<PluginClient>((options) => {
|
|
19
|
+
const {
|
|
20
|
+
output = { path: 'clients' },
|
|
21
|
+
group,
|
|
22
|
+
exclude = [],
|
|
23
|
+
include,
|
|
24
|
+
override = [],
|
|
25
|
+
transformers = {},
|
|
26
|
+
dataReturnType = 'data',
|
|
27
|
+
pathParamsType = 'inline',
|
|
28
|
+
templates,
|
|
29
|
+
} = options
|
|
30
|
+
|
|
31
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
name: pluginClientName,
|
|
35
|
+
options: {
|
|
36
|
+
extName: output.extName,
|
|
37
|
+
dataReturnType,
|
|
38
|
+
client: {
|
|
39
|
+
importPath: '@kubb/plugin-client/client',
|
|
40
|
+
...options.client,
|
|
41
|
+
},
|
|
42
|
+
pathParamsType,
|
|
43
|
+
templates: {
|
|
44
|
+
operations: Operations.templates,
|
|
45
|
+
client: Client.templates,
|
|
46
|
+
...templates,
|
|
47
|
+
},
|
|
48
|
+
baseURL: undefined,
|
|
49
|
+
},
|
|
50
|
+
pre: [pluginOasName],
|
|
51
|
+
resolvePath(baseName, pathMode, options) {
|
|
52
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
53
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
|
|
54
|
+
|
|
55
|
+
if (mode === 'single') {
|
|
56
|
+
/**
|
|
57
|
+
* when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
|
|
58
|
+
* Other plugins then need to call addOrAppend instead of just add from the fileManager class
|
|
59
|
+
*/
|
|
60
|
+
return path.resolve(root, output.path)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (options?.tag && group?.type === 'tag') {
|
|
64
|
+
const tag = camelCase(options.tag)
|
|
65
|
+
|
|
66
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return path.resolve(root, output.path, baseName)
|
|
70
|
+
},
|
|
71
|
+
resolveName(name, type) {
|
|
72
|
+
const resolvedName = camelCase(name, { isFile: type === 'file' })
|
|
73
|
+
|
|
74
|
+
if (type) {
|
|
75
|
+
return transformers?.name?.(resolvedName, type) || resolvedName
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return resolvedName
|
|
79
|
+
},
|
|
80
|
+
async writeFile(path, source) {
|
|
81
|
+
if (!source) {
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return this.fileManager.write(path, source, { sanity: false })
|
|
86
|
+
},
|
|
87
|
+
async buildStart() {
|
|
88
|
+
const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
|
|
89
|
+
|
|
90
|
+
const oas = await swaggerPlugin.api.getOas()
|
|
91
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
92
|
+
const mode = FileManager.getMode(path.resolve(root, output.path))
|
|
93
|
+
const baseURL = await swaggerPlugin.api.getBaseURL()
|
|
94
|
+
|
|
95
|
+
const operationGenerator = new OperationGenerator(
|
|
96
|
+
{
|
|
97
|
+
...this.plugin.options,
|
|
98
|
+
baseURL,
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
oas,
|
|
102
|
+
pluginManager: this.pluginManager,
|
|
103
|
+
plugin: this.plugin,
|
|
104
|
+
contentType: swaggerPlugin.api.contentType,
|
|
105
|
+
exclude,
|
|
106
|
+
include,
|
|
107
|
+
override,
|
|
108
|
+
mode,
|
|
109
|
+
},
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
const files = await operationGenerator.build()
|
|
113
|
+
|
|
114
|
+
await this.addFile(...files)
|
|
115
|
+
},
|
|
116
|
+
async buildEnd() {
|
|
117
|
+
if (this.config.output.write === false) {
|
|
118
|
+
return
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const root = path.resolve(this.config.root, this.config.output.path)
|
|
122
|
+
|
|
123
|
+
if (group?.type === 'tag') {
|
|
124
|
+
const rootFiles = await getGroupedByTagFiles({
|
|
125
|
+
logger: this.logger,
|
|
126
|
+
files: this.fileManager.files,
|
|
127
|
+
plugin: this.plugin,
|
|
128
|
+
template,
|
|
129
|
+
exportAs: group.exportAs || '{{tag}}Service',
|
|
130
|
+
root,
|
|
131
|
+
output,
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
await this.addFile(...rootFiles)
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
await this.fileManager.addIndexes({
|
|
138
|
+
root,
|
|
139
|
+
output,
|
|
140
|
+
meta: { pluginKey: this.plugin.key },
|
|
141
|
+
logger: this.logger,
|
|
142
|
+
})
|
|
143
|
+
},
|
|
144
|
+
}
|
|
145
|
+
})
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
|
|
2
|
+
import type * as KubbFile from '@kubb/fs/types'
|
|
3
|
+
|
|
4
|
+
import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
|
|
5
|
+
import type { Client, Operations } from './components/index.ts'
|
|
6
|
+
|
|
7
|
+
type Templates = {
|
|
8
|
+
operations?: typeof Operations.templates | false
|
|
9
|
+
client?: typeof Client.templates | false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type Options = {
|
|
13
|
+
output?: {
|
|
14
|
+
/**
|
|
15
|
+
* Output to save the clients.
|
|
16
|
+
* @default `"clients"``
|
|
17
|
+
*/
|
|
18
|
+
path: string
|
|
19
|
+
/**
|
|
20
|
+
* Name to be used for the `export * as {{exportAs}} from './'`
|
|
21
|
+
*/
|
|
22
|
+
exportAs?: string
|
|
23
|
+
/**
|
|
24
|
+
* Add an extension to the generated imports and exports, default it will not use an extension
|
|
25
|
+
*/
|
|
26
|
+
extName?: KubbFile.Extname
|
|
27
|
+
/**
|
|
28
|
+
* Define what needs to exported, here you can also disable the export of barrel files
|
|
29
|
+
* @default `'barrel'`
|
|
30
|
+
*/
|
|
31
|
+
exportType?: 'barrel' | 'barrelNamed' | false
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Group the clients based on the provided name.
|
|
35
|
+
*/
|
|
36
|
+
group?: {
|
|
37
|
+
/**
|
|
38
|
+
* Tag will group based on the operation tag inside the Swagger file
|
|
39
|
+
*/
|
|
40
|
+
type: 'tag'
|
|
41
|
+
/**
|
|
42
|
+
* Relative path to save the grouped clients.
|
|
43
|
+
*
|
|
44
|
+
* `{{tag}}` will be replaced by the current tagName.
|
|
45
|
+
* @example `${output}/{{tag}}Controller` => `clients/PetController`
|
|
46
|
+
* @default `${output}/{{tag}}Controller`
|
|
47
|
+
*/
|
|
48
|
+
output?: string
|
|
49
|
+
/**
|
|
50
|
+
* Name to be used for the `export * as {{exportAs}} from './`
|
|
51
|
+
* @default `"{{tag}}Service"`
|
|
52
|
+
*/
|
|
53
|
+
exportAs?: string
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Array containing exclude parameters to exclude/skip tags/operations/methods/paths.
|
|
57
|
+
*/
|
|
58
|
+
exclude?: Array<Exclude>
|
|
59
|
+
/**
|
|
60
|
+
* Array containing include parameters to include tags/operations/methods/paths.
|
|
61
|
+
*/
|
|
62
|
+
include?: Array<Include>
|
|
63
|
+
/**
|
|
64
|
+
* Array containing override parameters to override `options` based on tags/operations/methods/paths.
|
|
65
|
+
*/
|
|
66
|
+
override?: Array<Override<ResolvedOptions>>
|
|
67
|
+
client?: {
|
|
68
|
+
/**
|
|
69
|
+
* Path to the client import path that will be used to do the API calls.
|
|
70
|
+
* It will be used as `import client from '${client.importPath}'`.
|
|
71
|
+
* It allows both relative and absolute path.
|
|
72
|
+
* the path will be applied as is, so relative path should be based on the file being generated.
|
|
73
|
+
* @default '@kubb/plugin-client/client'
|
|
74
|
+
*/
|
|
75
|
+
importPath?: string
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* ReturnType that needs to be used when calling client().
|
|
79
|
+
*
|
|
80
|
+
* `Data` will return ResponseConfig[data].
|
|
81
|
+
*
|
|
82
|
+
* `Full` will return ResponseConfig.
|
|
83
|
+
* @default `'data'`
|
|
84
|
+
* @private
|
|
85
|
+
*/
|
|
86
|
+
dataReturnType?: 'data' | 'full'
|
|
87
|
+
/**
|
|
88
|
+
* How to pass your pathParams.
|
|
89
|
+
*
|
|
90
|
+
* `object` will return the pathParams as an object.
|
|
91
|
+
*
|
|
92
|
+
* `inline` will return the pathParams as comma separated params.
|
|
93
|
+
* @default `'inline'`
|
|
94
|
+
* @private
|
|
95
|
+
*/
|
|
96
|
+
pathParamsType?: 'object' | 'inline'
|
|
97
|
+
transformers?: {
|
|
98
|
+
/**
|
|
99
|
+
* Customize the names based on the type that is provided by the plugin.
|
|
100
|
+
*/
|
|
101
|
+
name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Make it possible to override one of the templates
|
|
105
|
+
*/
|
|
106
|
+
templates?: Partial<Templates>
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
type ResolvedOptions = {
|
|
110
|
+
extName: KubbFile.Extname | undefined
|
|
111
|
+
baseURL: string | undefined
|
|
112
|
+
client: Required<NonNullable<Options['client']>>
|
|
113
|
+
dataReturnType: NonNullable<Options['dataReturnType']>
|
|
114
|
+
pathParamsType: NonNullable<Options['pathParamsType']>
|
|
115
|
+
templates: NonNullable<Templates>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export type FileMeta = {
|
|
119
|
+
pluginKey?: Plugin['key']
|
|
120
|
+
tag?: string
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>
|
|
124
|
+
|
|
125
|
+
declare module '@kubb/core' {
|
|
126
|
+
export interface _Register {
|
|
127
|
+
['@kubb/plugin-client']: PluginClient
|
|
128
|
+
}
|
|
129
|
+
}
|