@kubb/plugin-client 3.0.0-alpha.7 → 3.0.0-alpha.9

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 (48) hide show
  1. package/client.ts +1 -1
  2. package/dist/chunk-AZDWYBLW.cjs +1 -0
  3. package/dist/chunk-AZDWYBLW.cjs.map +1 -0
  4. package/dist/chunk-GHHJWXKQ.cjs +61 -0
  5. package/dist/chunk-GHHJWXKQ.cjs.map +1 -0
  6. package/dist/chunk-OODSLPAP.js +61 -0
  7. package/dist/chunk-OODSLPAP.js.map +1 -0
  8. package/dist/chunk-W256NILH.js +128 -0
  9. package/dist/chunk-W256NILH.js.map +1 -0
  10. package/dist/chunk-YGRM4AA5.cjs +128 -0
  11. package/dist/chunk-YGRM4AA5.cjs.map +1 -0
  12. package/dist/chunk-YJOVLRSC.js +1 -0
  13. package/dist/chunk-YJOVLRSC.js.map +1 -0
  14. package/dist/client.cjs.map +1 -1
  15. package/dist/client.d.cts +1 -1
  16. package/dist/client.d.ts +1 -1
  17. package/dist/client.js.map +1 -1
  18. package/dist/components.cjs +4 -3
  19. package/dist/components.cjs.map +1 -1
  20. package/dist/components.d.cts +1 -3
  21. package/dist/components.d.ts +1 -3
  22. package/dist/components.js +2 -1
  23. package/dist/generators.cjs +8 -0
  24. package/dist/generators.cjs.map +1 -0
  25. package/dist/generators.d.cts +10 -0
  26. package/dist/generators.d.ts +10 -0
  27. package/dist/generators.js +8 -0
  28. package/dist/generators.js.map +1 -0
  29. package/dist/index.cjs +9 -29
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +1 -3
  32. package/dist/index.d.ts +1 -3
  33. package/dist/index.js +8 -28
  34. package/dist/index.js.map +1 -1
  35. package/dist/{types-C_-LImV-.d.cts → types-DETYKDFZ.d.cts} +13 -107
  36. package/dist/{types-C_-LImV-.d.ts → types-DETYKDFZ.d.ts} +13 -107
  37. package/package.json +20 -12
  38. package/src/components/Client.tsx +74 -193
  39. package/src/components/Operations.tsx +6 -72
  40. package/src/generators/axiosGenerator.tsx +63 -0
  41. package/src/generators/index.ts +1 -0
  42. package/src/plugin.ts +4 -4
  43. package/src/types.ts +2 -2
  44. package/dist/chunk-BEM4SQUR.cjs +0 -199
  45. package/dist/chunk-BEM4SQUR.cjs.map +0 -1
  46. package/dist/chunk-IQZOOJUP.js +0 -199
  47. package/dist/chunk-IQZOOJUP.js.map +0 -1
  48. package/src/OperationGenerator.tsx +0 -29
@@ -1,83 +1,85 @@
1
1
  import { URLPath } from '@kubb/core/utils'
2
- import { 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
2
 
7
- import { isOptional } from '@kubb/oas'
8
- import type { HttpMethod } from '@kubb/oas'
3
+ import { type Operation, isOptional } from '@kubb/oas'
4
+ import type { OperationSchemas } from '@kubb/plugin-oas'
5
+ import { getComments, getPathParams } from '@kubb/plugin-oas/utils'
6
+ import { File, Function, createParams } from '@kubb/react'
9
7
  import type { KubbNode, Params } from '@kubb/react/types'
10
- import type { ComponentProps, ComponentType } from 'react'
11
- import type { FileMeta, PluginClient } from '../types.ts'
8
+ import type { PluginClient } from '../types.ts'
12
9
 
13
- type TemplateProps = {
10
+ type Props = {
14
11
  /**
15
12
  * Name of the function
16
13
  */
17
14
  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
- }
15
+ options: PluginClient['resolvedOptions']
16
+ typedSchemas: OperationSchemas
17
+ operation: Operation
47
18
  }
48
19
 
49
- function Template({ name, generics, returnType, params, JSDoc, client }: TemplateProps): KubbNode {
50
- const isFormData = client.contentType === 'multipart/form-data'
20
+ export function Client({ name, options, typedSchemas, operation }: Props): KubbNode {
21
+ const contentType = operation.getContentType()
22
+ const baseURL = options.client.importPath === '@kubb/plugin-client/client' ? options.baseURL : undefined
23
+ const path = new URLPath(operation.path)
24
+ const isFormData = contentType === 'multipart/form-data'
51
25
  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 = {
26
+ contentType !== 'application/json' ? `'Content-Type': '${contentType}'` : undefined,
27
+ typedSchemas.headerParams?.name ? '...headers' : undefined,
28
+ ].filter(Boolean)
29
+
30
+ const params = createParams({
31
+ pathParams: {
32
+ mode: options.pathParamsType === 'object' ? 'object' : 'inlineSpread',
33
+ children: getPathParams(typedSchemas.pathParams, { typed: true }),
34
+ },
35
+ data: typedSchemas.request?.name
36
+ ? {
37
+ type: typedSchemas.request?.name,
38
+ optional: isOptional(typedSchemas.request?.schema),
39
+ }
40
+ : undefined,
41
+ params: typedSchemas.queryParams?.name
42
+ ? {
43
+ type: typedSchemas.queryParams?.name,
44
+ optional: isOptional(typedSchemas.queryParams?.schema),
45
+ }
46
+ : undefined,
47
+ headers: typedSchemas.headerParams?.name
48
+ ? {
49
+ type: typedSchemas.headerParams?.name,
50
+ optional: isOptional(typedSchemas.headerParams?.schema),
51
+ }
52
+ : undefined,
53
+ options: {
54
+ type: 'Partial<Parameters<typeof client>[0]>',
55
+ default: '{}',
56
+ },
57
+ })
58
+
59
+ const clientParams = createParams({
58
60
  data: {
59
61
  mode: 'object',
60
62
  children: {
61
63
  method: {
62
64
  type: 'string',
63
- value: JSON.stringify(client.method),
65
+ value: JSON.stringify(operation.method),
64
66
  },
65
67
  url: {
66
68
  type: 'string',
67
- value: client.path.template,
69
+ value: path.template,
68
70
  },
69
- baseURL: client.baseURL
71
+ baseURL: baseURL
70
72
  ? {
71
73
  type: 'string',
72
- value: JSON.stringify(client.baseURL),
74
+ value: JSON.stringify(baseURL),
73
75
  }
74
76
  : undefined,
75
- params: client.withQueryParams
77
+ params: typedSchemas.queryParams?.name
76
78
  ? {
77
79
  type: 'any',
78
80
  }
79
81
  : undefined,
80
- data: client.withData
82
+ data: typedSchemas.request?.name
81
83
  ? {
82
84
  type: 'any',
83
85
  value: isFormData ? 'formData' : undefined,
@@ -86,7 +88,7 @@ function Template({ name, generics, returnType, params, JSDoc, client }: Templat
86
88
  headers: headers.length
87
89
  ? {
88
90
  type: 'any',
89
- value: headers.length ? `{ ${headers}, ...options.headers }` : undefined,
91
+ value: headers.length ? `{ ${headers.join(', ')}, ...options.headers }` : undefined,
90
92
  }
91
93
  : undefined,
92
94
  options: {
@@ -95,7 +97,7 @@ function Template({ name, generics, returnType, params, JSDoc, client }: Templat
95
97
  },
96
98
  },
97
99
  },
98
- }
100
+ })
99
101
 
100
102
  const formData = isFormData
101
103
  ? `
@@ -112,147 +114,26 @@ function Template({ name, generics, returnType, params, JSDoc, client }: Templat
112
114
  : undefined
113
115
 
114
116
  return (
115
- <File.Source name={name} isExportable>
116
- <Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>
117
+ <File.Source name={name} isExportable isIndexable>
118
+ <Function
119
+ name={name}
120
+ async
121
+ export
122
+ returnType={
123
+ options.dataReturnType === 'data' ? `ResponseConfig<${typedSchemas.response.name}>["data"]` : `ResponseConfig<${typedSchemas.response.name}>`
124
+ }
125
+ params={params}
126
+ JSDoc={{
127
+ comments: getComments(operation),
128
+ }}
129
+ >
117
130
  {formData || ''}
118
- <Function.Call name="res" to={<Function name="client" async generics={client.generics} params={clientParams} />} />
119
- <Function.Return>{client.dataReturnType === 'data' ? 'res.data' : 'res'}</Function.Return>
131
+ <Function.Call
132
+ name="res"
133
+ to={<Function name="client" async generics={[typedSchemas.response.name, typedSchemas.request?.name].filter(Boolean)} params={clientParams} />}
134
+ />
135
+ <Function.Return>{options.dataReturnType === 'data' ? 'res.data' : 'res'}</Function.Return>
120
136
  </Function>
121
137
  </File.Source>
122
138
  )
123
139
  }
124
-
125
- type RootTemplateProps = {
126
- children?: React.ReactNode
127
- }
128
-
129
- function RootTemplate({ children }: RootTemplateProps) {
130
- const {
131
- plugin: {
132
- options: {
133
- client: { importPath },
134
- },
135
- },
136
- } = useApp<PluginClient>()
137
-
138
- const { getSchemas, getFile } = useOperationManager()
139
- const operation = useOperation()
140
-
141
- const file = getFile(operation)
142
- const fileType = getFile(operation, { pluginKey: [pluginTsName] })
143
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
144
-
145
- return (
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
- name={[schemas.request?.name, schemas.response.name, schemas.pathParams?.name, schemas.queryParams?.name, schemas.headerParams?.name].filter(Boolean)}
151
- root={file.path}
152
- path={fileType.path}
153
- isTypeOnly
154
- />
155
- {children}
156
- </File>
157
- )
158
- }
159
-
160
- const defaultTemplates = { default: Template, root: RootTemplate } as const
161
-
162
- type Templates = Partial<typeof defaultTemplates>
163
-
164
- type ClientProps = {
165
- baseURL: string | undefined
166
- /**
167
- * This will make it possible to override the default behaviour.
168
- */
169
- Template?: ComponentType<ComponentProps<typeof Template>>
170
- }
171
-
172
- export function Client({ baseURL, Template = defaultTemplates.default }: ClientProps): KubbNode {
173
- const {
174
- plugin: {
175
- options: { client, dataReturnType, pathParamsType },
176
- },
177
- } = useApp<PluginClient>()
178
-
179
- const { getSchemas, getName } = useOperationManager()
180
- const operation = useOperation()
181
-
182
- const contentType = operation.getContentType()
183
- const name = getName(operation, { type: 'function' })
184
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
185
-
186
- return (
187
- <Template
188
- name={name}
189
- params={{
190
- pathParams: {
191
- mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',
192
- children: getPathParams(schemas.pathParams, { typed: true }),
193
- },
194
- data: schemas.request?.name
195
- ? {
196
- type: schemas.request?.name,
197
- optional: isOptional(schemas.request?.schema),
198
- }
199
- : undefined,
200
- params: schemas.queryParams?.name
201
- ? {
202
- type: schemas.queryParams?.name,
203
- optional: isOptional(schemas.queryParams?.schema),
204
- }
205
- : undefined,
206
- headers: schemas.headerParams?.name
207
- ? {
208
- type: schemas.headerParams?.name,
209
- optional: isOptional(schemas.headerParams?.schema),
210
- }
211
- : undefined,
212
- options: {
213
- type: 'Partial<Parameters<typeof client>[0]>',
214
- default: '{}',
215
- },
216
- }}
217
- returnType={dataReturnType === 'data' ? `ResponseConfig<${schemas.response.name}>["data"]` : `ResponseConfig<${schemas.response.name}>`}
218
- JSDoc={{
219
- comments: getComments(operation),
220
- }}
221
- client={{
222
- // only set baseURL from serverIndex(swagger) when no custom client(default) is used
223
- baseURL: client.importPath === '@kubb/plugin-client/client' ? baseURL : undefined,
224
- generics: [schemas.response.name, schemas.request?.name].filter(Boolean),
225
- dataReturnType,
226
- withQueryParams: !!schemas.queryParams?.name,
227
- withData: !!schemas.request?.name,
228
- withHeaders: !!schemas.headerParams?.name,
229
- method: operation.method,
230
- path: new URLPath(operation.path),
231
- contentType,
232
- }}
233
- />
234
- )
235
- }
236
-
237
- type FileProps = {
238
- baseURL: string | undefined
239
- /**
240
- * This will make it possible to override the default behaviour.
241
- */
242
- templates?: Templates
243
- }
244
-
245
- Client.File = function ({ baseURL, ...props }: FileProps): KubbNode {
246
- const templates = { ...defaultTemplates, ...props.templates }
247
-
248
- const Template = templates.default
249
- const RootTemplate = templates.root
250
-
251
- return (
252
- <RootTemplate>
253
- <Client baseURL={baseURL} Template={Template} />
254
- </RootTemplate>
255
- )
256
- }
257
-
258
- Client.templates = defaultTemplates
@@ -1,22 +1,14 @@
1
1
  import { URLPath } from '@kubb/core/utils'
2
- import { useOperations } from '@kubb/plugin-oas/hooks'
3
- import { Const, File, useApp } from '@kubb/react'
2
+ import { Const, File } from '@kubb/react'
4
3
 
5
4
  import type { HttpMethod, Operation } from '@kubb/oas'
6
- import type { KubbNode } from '@kubb/react/types'
7
- import type { ComponentProps, ComponentType } from 'react'
8
- import type { FileMeta, PluginClient } from '../types.ts'
9
5
 
10
- type TemplateProps = {
11
- /**
12
- * Name of the function
13
- */
6
+ type OperationsProps = {
14
7
  name: string
15
- operations: Operation[]
16
- baseURL: string | undefined
8
+ operations: Array<Operation>
17
9
  }
18
10
 
19
- function Template({ name, operations }: TemplateProps): KubbNode {
11
+ export function Operations({ name, operations }: OperationsProps) {
20
12
  const operationsObject: Record<string, { path: string; method: HttpMethod }> = {}
21
13
 
22
14
  operations.forEach((operation) => {
@@ -25,70 +17,12 @@ function Template({ name, operations }: TemplateProps): KubbNode {
25
17
  method: operation.method,
26
18
  }
27
19
  })
20
+
28
21
  return (
29
- <File.Source name={name} isExportable>
22
+ <File.Source name={name} isExportable isIndexable>
30
23
  <Const name={name} export asConst>
31
24
  {JSON.stringify(operationsObject, undefined, 2)}
32
25
  </Const>
33
26
  </File.Source>
34
27
  )
35
28
  }
36
-
37
- type RootTemplateProps = {
38
- children?: React.ReactNode
39
- }
40
-
41
- function RootTemplate({ children }: RootTemplateProps) {
42
- const {
43
- pluginManager,
44
- plugin: { key: pluginKey },
45
- } = useApp<PluginClient>()
46
- const file = pluginManager.getFile({ name: 'operations', extName: '.ts', pluginKey })
47
-
48
- return (
49
- <File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>
50
- {children}
51
- </File>
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,63 @@
1
+ import { createReactGenerator } from '@kubb/plugin-oas'
2
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
3
+ import { pluginTsName } from '@kubb/plugin-ts'
4
+ import { File, useApp } from '@kubb/react'
5
+ import { Client } from '../components/Client'
6
+ import { Operations } from '../components/Operations'
7
+ import type { PluginClient } from '../types'
8
+
9
+ export const axiosGenerator = createReactGenerator<PluginClient>({
10
+ name: 'plugin-client',
11
+ Operations({ options, operations }) {
12
+ const { pluginManager } = useApp<PluginClient>()
13
+
14
+ if (!options.templates.operations) {
15
+ return null
16
+ }
17
+
18
+ const Template = options.templates.operations || Operations
19
+ const name = 'operations'
20
+ const file = pluginManager.getFile({ name, extName: '.ts', pluginKey: ['plugin-client'] })
21
+
22
+ return (
23
+ <File baseName={file.baseName} path={file.path} meta={file.meta}>
24
+ <Template name={name} operations={operations} />
25
+ </File>
26
+ )
27
+ },
28
+ Operation({ options, operation }) {
29
+ const { getSchemas, getName, getFile } = useOperationManager()
30
+
31
+ const name = getName(operation, { type: 'function' })
32
+ const typedSchemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })
33
+ const file = getFile(operation)
34
+ const fileType = getFile(operation, { pluginKey: [pluginTsName] })
35
+
36
+ if (!options.templates.client) {
37
+ return null
38
+ }
39
+
40
+ const Template = options.templates.client || Client
41
+
42
+ return (
43
+ <File baseName={file.baseName} path={file.path} meta={file.meta}>
44
+ <File.Import name={'client'} path={options.client.importPath} />
45
+ <File.Import name={['ResponseConfig']} path={options.client.importPath} isTypeOnly />
46
+ <File.Import
47
+ extName={options.extName}
48
+ name={[
49
+ typedSchemas.request?.name,
50
+ typedSchemas.response.name,
51
+ typedSchemas.pathParams?.name,
52
+ typedSchemas.queryParams?.name,
53
+ typedSchemas.headerParams?.name,
54
+ ].filter(Boolean)}
55
+ root={file.path}
56
+ path={fileType.path}
57
+ isTypeOnly
58
+ />
59
+ <Template name={name} options={options} typedSchemas={typedSchemas} operation={operation} />
60
+ </File>
61
+ )
62
+ },
63
+ })
@@ -0,0 +1 @@
1
+ export { axiosGenerator } from './axiosGenerator.tsx'
package/src/plugin.ts CHANGED
@@ -5,11 +5,11 @@ import { camelCase } from '@kubb/core/transformers'
5
5
  import { renderTemplate } from '@kubb/core/utils'
6
6
  import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
7
7
 
8
- import { clientParser } from './OperationGenerator.tsx'
9
8
  import { Client, Operations } from './components/index.ts'
10
9
 
11
10
  import type { Plugin } from '@kubb/core'
12
11
  import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
12
+ import { axiosGenerator } from './generators/axiosGenerator.tsx'
13
13
  import type { PluginClient } from './types.ts'
14
14
 
15
15
  export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
@@ -45,8 +45,8 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
45
45
  },
46
46
  pathParamsType,
47
47
  templates: {
48
- operations: Operations.templates,
49
- client: Client.templates,
48
+ operations: Operations,
49
+ client: Client,
50
50
  ...templates,
51
51
  },
52
52
  baseURL: undefined,
@@ -106,7 +106,7 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
106
106
  },
107
107
  )
108
108
 
109
- const files = await operationGenerator.build(clientParser)
109
+ const files = await operationGenerator.build(axiosGenerator)
110
110
 
111
111
  await this.addFile(...files)
112
112
 
package/src/types.ts CHANGED
@@ -6,8 +6,8 @@ import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugi
6
6
  import type { Client, Operations } from './components/index.ts'
7
7
 
8
8
  type Templates = {
9
- operations?: typeof Operations.templates | false
10
- client?: typeof Client.templates | false
9
+ operations?: typeof Operations | false
10
+ client?: typeof Client | false
11
11
  }
12
12
 
13
13
  export type Options = {