@kubb/plugin-client 3.0.0-alpha.3 → 3.0.0-alpha.30

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 (63) hide show
  1. package/README.md +13 -4
  2. package/client.ts +2 -2
  3. package/dist/chunk-ELPE4OER.cjs +183 -0
  4. package/dist/chunk-ELPE4OER.cjs.map +1 -0
  5. package/dist/chunk-RQSMSP6A.js +174 -0
  6. package/dist/chunk-RQSMSP6A.js.map +1 -0
  7. package/dist/chunk-X73EA6RA.cjs +133 -0
  8. package/dist/chunk-X73EA6RA.cjs.map +1 -0
  9. package/dist/chunk-YYBNZBCI.js +130 -0
  10. package/dist/chunk-YYBNZBCI.js.map +1 -0
  11. package/dist/client.cjs +16 -7
  12. package/dist/client.cjs.map +1 -1
  13. package/dist/client.d.cts +2 -2
  14. package/dist/client.d.ts +2 -2
  15. package/dist/client.js +5 -6
  16. package/dist/client.js.map +1 -1
  17. package/dist/components.cjs +11 -4
  18. package/dist/components.cjs.map +1 -1
  19. package/dist/components.d.cts +37 -7
  20. package/dist/components.d.ts +37 -7
  21. package/dist/components.js +2 -8
  22. package/dist/components.js.map +1 -1
  23. package/dist/generators.cjs +17 -0
  24. package/dist/generators.cjs.map +1 -0
  25. package/dist/generators.d.cts +9 -0
  26. package/dist/generators.d.ts +9 -0
  27. package/dist/generators.js +4 -0
  28. package/dist/generators.js.map +1 -0
  29. package/dist/index.cjs +10 -137
  30. package/dist/index.cjs.map +1 -1
  31. package/dist/index.d.cts +1 -6
  32. package/dist/index.d.ts +1 -6
  33. package/dist/index.js +3 -143
  34. package/dist/index.js.map +1 -1
  35. package/dist/types-Bc-y9yTU.d.cts +100 -0
  36. package/dist/types-Bc-y9yTU.d.ts +100 -0
  37. package/package.json +22 -17
  38. package/src/components/Client.tsx +111 -212
  39. package/src/components/Operations.tsx +9 -75
  40. package/src/generators/__snapshots__/deletePet.ts +13 -0
  41. package/src/generators/__snapshots__/deletePetObject.ts +15 -0
  42. package/src/generators/__snapshots__/findByTags.ts +13 -0
  43. package/src/generators/__snapshots__/findByTagsFull.ts +13 -0
  44. package/src/generators/__snapshots__/findByTagsWithZod.ts +13 -0
  45. package/src/generators/__snapshots__/findByTagsWithZodFull.ts +13 -0
  46. package/src/generators/__snapshots__/importPath.ts +13 -0
  47. package/src/generators/__snapshots__/operations.ts +82 -0
  48. package/src/generators/__snapshots__/updatePetById.ts +12 -0
  49. package/src/generators/clientGenerator.tsx +66 -0
  50. package/src/generators/index.ts +2 -0
  51. package/src/generators/operationsGenerator.tsx +26 -0
  52. package/src/plugin.ts +23 -42
  53. package/src/types.ts +27 -55
  54. package/dist/chunk-W57BRY5O.js +0 -201
  55. package/dist/chunk-W57BRY5O.js.map +0 -1
  56. package/dist/chunk-W7F5CMU6.cjs +0 -201
  57. package/dist/chunk-W7F5CMU6.cjs.map +0 -1
  58. package/dist/types-C14AAtNX.d.cts +0 -233
  59. package/dist/types-C14AAtNX.d.ts +0 -233
  60. package/src/OperationGenerator.tsx +0 -29
  61. package/src/components/__snapshots__/Client/showPetById.ts +0 -11
  62. package/src/components/__snapshots__/Operations/showPetById.ts +0 -6
  63. package/src/components/__snapshots__/Query/showPetById.ts +0 -15
@@ -0,0 +1,66 @@
1
+ import { createReactGenerator } from '@kubb/plugin-oas'
2
+ import { useOperationManager } from '@kubb/plugin-oas/hooks'
3
+ import { pluginTsName } from '@kubb/plugin-ts'
4
+ import { pluginZodName } from '@kubb/plugin-zod'
5
+ import { File, useApp } from '@kubb/react'
6
+ import { Client } from '../components/Client'
7
+ import type { PluginClient } from '../types'
8
+
9
+ export const clientGenerator = createReactGenerator<PluginClient>({
10
+ name: 'client',
11
+ Operation({ options, operation }) {
12
+ const {
13
+ plugin: {
14
+ options: { output },
15
+ },
16
+ } = useApp<PluginClient>()
17
+ const { getSchemas, getName, getFile } = useOperationManager()
18
+
19
+ const client = {
20
+ name: getName(operation, { type: 'function' }),
21
+ file: getFile(operation),
22
+ }
23
+
24
+ const type = {
25
+ file: getFile(operation, { pluginKey: [pluginTsName] }),
26
+ schemas: getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' }),
27
+ }
28
+
29
+ const zod = {
30
+ file: getFile(operation, { pluginKey: [pluginZodName] }),
31
+ schemas: getSchemas(operation, { pluginKey: [pluginZodName], type: 'function' }),
32
+ }
33
+
34
+ return (
35
+ <File baseName={client.file.baseName} path={client.file.path} meta={client.file.meta} banner={output?.banner} footer={output?.footer}>
36
+ <File.Import name={'client'} path={options.importPath} />
37
+ <File.Import name={['RequestConfig']} path={options.importPath} isTypeOnly />
38
+ {options.parser === 'zod' && <File.Import name={[zod.schemas.response.name]} root={client.file.path} path={zod.file.path} />}
39
+ <File.Import
40
+ name={[
41
+ type.schemas.request?.name,
42
+ type.schemas.response.name,
43
+ type.schemas.pathParams?.name,
44
+ type.schemas.queryParams?.name,
45
+ type.schemas.headerParams?.name,
46
+ ...(type.schemas.statusCodes?.map((item) => item.name) || []),
47
+ ].filter(Boolean)}
48
+ root={client.file.path}
49
+ path={type.file.path}
50
+ isTypeOnly
51
+ />
52
+
53
+ <Client
54
+ name={client.name}
55
+ baseURL={options.baseURL}
56
+ dataReturnType={options.dataReturnType}
57
+ pathParamsType={options.pathParamsType}
58
+ typeSchemas={type.schemas}
59
+ operation={operation}
60
+ parser={options.parser}
61
+ zodSchemas={zod.schemas}
62
+ />
63
+ </File>
64
+ )
65
+ },
66
+ })
@@ -0,0 +1,2 @@
1
+ export { clientGenerator } from './clientGenerator.tsx'
2
+ export { operationsGenerator } from './operationsGenerator.tsx'
@@ -0,0 +1,26 @@
1
+ import { pluginClientName } from '@kubb/plugin-client'
2
+ import { createReactGenerator } from '@kubb/plugin-oas'
3
+ import { File, useApp } from '@kubb/react'
4
+ import { Operations } from '../components/Operations'
5
+ import type { PluginClient } from '../types'
6
+
7
+ export const operationsGenerator = createReactGenerator<PluginClient>({
8
+ name: 'client',
9
+ Operations({ operations }) {
10
+ const {
11
+ pluginManager,
12
+ plugin: {
13
+ options: { output },
14
+ },
15
+ } = useApp<PluginClient>()
16
+
17
+ const name = 'operations'
18
+ const file = pluginManager.getFile({ name, extname: '.ts', pluginKey: [pluginClientName] })
19
+
20
+ return (
21
+ <File baseName={file.baseName} path={file.path} meta={file.meta} banner={output?.banner} footer={output?.footer}>
22
+ <Operations name={name} operations={operations} />
23
+ </File>
24
+ )
25
+ },
26
+ })
package/src/plugin.ts CHANGED
@@ -4,20 +4,19 @@ import { FileManager, PluginManager, createPlugin } from '@kubb/core'
4
4
  import { camelCase } from '@kubb/core/transformers'
5
5
  import { renderTemplate } from '@kubb/core/utils'
6
6
  import { OperationGenerator, pluginOasName } from '@kubb/plugin-oas'
7
- import { getGroupedByTagFiles } from '@kubb/plugin-oas/utils'
8
-
9
- import { clientParser } from './OperationGenerator.tsx'
10
- import { Client, Operations } from './components/index.ts'
11
7
 
12
8
  import type { Plugin } from '@kubb/core'
13
9
  import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
10
+ import { pluginZodName } from '@kubb/plugin-zod'
11
+ import { operationsGenerator } from './generators'
12
+ import { clientGenerator } from './generators/clientGenerator.tsx'
14
13
  import type { PluginClient } from './types.ts'
15
14
 
16
15
  export const pluginClientName = 'plugin-client' satisfies PluginClient['name']
17
16
 
18
17
  export const pluginClient = createPlugin<PluginClient>((options) => {
19
18
  const {
20
- output = { path: 'clients' },
19
+ output = { path: 'clients', barrelType: 'named' },
21
20
  group,
22
21
  exclude = [],
23
22
  include,
@@ -25,7 +24,10 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
25
24
  transformers = {},
26
25
  dataReturnType = 'data',
27
26
  pathParamsType = 'inline',
28
- templates,
27
+ operations = false,
28
+ generators = [clientGenerator, operations ? operationsGenerator : undefined].filter(Boolean),
29
+ importPath = '@kubb/plugin-client/client',
30
+ parser = 'client',
29
31
  } = options
30
32
 
31
33
  const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`
@@ -33,22 +35,16 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
33
35
  return {
34
36
  name: pluginClientName,
35
37
  options: {
36
- extName: output.extName,
37
- dataReturnType,
38
- client: {
39
- importPath: '@kubb/plugin-client/client',
40
- methods: ['get', 'post', 'delete', 'put'],
41
- ...options.client,
38
+ output: {
39
+ ...output,
42
40
  },
41
+ parser,
42
+ dataReturnType,
43
+ importPath,
43
44
  pathParamsType,
44
- templates: {
45
- operations: Operations.templates,
46
- client: Client.templates,
47
- ...templates,
48
- },
49
45
  baseURL: undefined,
50
46
  },
51
- pre: [pluginOasName],
47
+ pre: [pluginOasName, parser === 'zod' ? pluginZodName : undefined].filter(Boolean),
52
48
  resolvePath(baseName, pathMode, options) {
53
49
  const root = path.resolve(this.config.root, this.config.output.path)
54
50
  const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
@@ -103,37 +99,22 @@ export const pluginClient = createPlugin<PluginClient>((options) => {
103
99
  },
104
100
  )
105
101
 
106
- const files = await operationGenerator.build(clientParser)
102
+ const files = await operationGenerator.build(...generators)
107
103
 
108
104
  await this.addFile(...files)
109
- },
110
- async buildEnd() {
111
- if (this.config.output.write === false) {
112
- return
113
- }
114
-
115
- const root = path.resolve(this.config.root, this.config.output.path)
116
-
117
- if (group?.type === 'tag') {
118
- const rootFiles = await getGroupedByTagFiles({
119
- logger: this.logger,
120
- files: this.fileManager.files,
121
- plugin: this.plugin,
122
- template,
123
- exportAs: group.exportAs || '{{tag}}Service',
124
- root,
125
- output,
126
- })
127
105
 
128
- await this.addFile(...rootFiles)
129
- }
130
-
131
- await this.fileManager.addIndexes({
106
+ const barrelFiles = await this.fileManager.getBarrelFiles({
107
+ type: output.barrelType ?? 'named',
132
108
  root,
133
109
  output,
134
- meta: { pluginKey: this.plugin.key },
110
+ files: this.fileManager.files,
111
+ meta: {
112
+ pluginKey: this.plugin.key,
113
+ },
135
114
  logger: this.logger,
136
115
  })
116
+
117
+ await this.addFile(...barrelFiles)
137
118
  },
138
119
  }
139
120
  })
package/src/types.ts CHANGED
@@ -1,36 +1,9 @@
1
- import type { Plugin, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
2
- import type * as KubbFile from '@kubb/fs/types'
1
+ import type { Output, PluginFactoryOptions, ResolveNameParams } from '@kubb/core'
3
2
 
4
- import type { Exclude, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
5
- import type { Client, Operations } from './components/index.ts'
6
- import type { HttpMethod } from '@kubb/oas'
7
-
8
- type Templates = {
9
- operations?: typeof Operations.templates | false
10
- client?: typeof Client.templates | false
11
- }
3
+ import type { Exclude, Generator, Include, Override, ResolvePathOptions } from '@kubb/plugin-oas'
12
4
 
13
5
  export type Options = {
14
- output?: {
15
- /**
16
- * Output to save the clients.
17
- * @default `"clients"``
18
- */
19
- path: string
20
- /**
21
- * Name to be used for the `export * as {{exportAs}} from './'`
22
- */
23
- exportAs?: string
24
- /**
25
- * Add an extension to the generated imports and exports, default it will not use an extension
26
- */
27
- extName?: KubbFile.Extname
28
- /**
29
- * Define what needs to exported, here you can also disable the export of barrel files
30
- * @default `'barrel'`
31
- */
32
- exportType?: 'barrel' | 'barrelNamed' | false
33
- }
6
+ output?: Output
34
7
  /**
35
8
  * Group the clients based on the provided name.
36
9
  */
@@ -65,21 +38,19 @@ export type Options = {
65
38
  * Array containing override parameters to override `options` based on tags/operations/methods/paths.
66
39
  */
67
40
  override?: Array<Override<ResolvedOptions>>
68
- client?: {
69
- /**
70
- * Path to the client import path that will be used to do the API calls.
71
- * It will be used as `import client from '${client.importPath}'`.
72
- * It allows both relative and absolute path.
73
- * the path will be applied as is, so relative path should be based on the file being generated.
74
- * @default '@kubb/plugin-client/client'
75
- */
76
- importPath?: string
77
- /**
78
- * Define which HttpMethods can be used for queries
79
- * @default ['get', 'post', 'put', 'delete']
80
- */
81
- methods: Array<HttpMethod>
82
- }
41
+ /**
42
+ * Create `operations.ts` file with all operations grouped by methods.
43
+ * @default `false`
44
+ */
45
+ operations?: boolean
46
+ /**
47
+ * Path to the client import path that will be used to do the API calls.
48
+ * It will be used as `import client from '${client.importPath}'`.
49
+ * It allows both relative and absolute path.
50
+ * the path will be applied as is, so relative path should be based on the file being generated.
51
+ * @default '@kubb/plugin-client/client'
52
+ */
53
+ importPath?: string
83
54
  /**
84
55
  * ReturnType that needs to be used when calling client().
85
56
  *
@@ -100,6 +71,12 @@ export type Options = {
100
71
  * @private
101
72
  */
102
73
  pathParamsType?: 'object' | 'inline'
74
+ /**
75
+ * Which parser can be used before returning the data
76
+ * `'zod'` will use `@kubb/plugin-zod` to parse the data.
77
+ * @default 'client'
78
+ */
79
+ parser?: 'client' | 'zod'
103
80
  transformers?: {
104
81
  /**
105
82
  * Customize the names based on the type that is provided by the plugin.
@@ -107,23 +84,18 @@ export type Options = {
107
84
  name?: (name: ResolveNameParams['name'], type?: ResolveNameParams['type']) => string
108
85
  }
109
86
  /**
110
- * Make it possible to override one of the templates
87
+ * Define some generators next to the client generators
111
88
  */
112
- templates?: Partial<Templates>
89
+ generators?: Array<Generator<PluginClient>>
113
90
  }
114
91
 
115
92
  type ResolvedOptions = {
116
- extName: KubbFile.Extname | undefined
93
+ output: Output
117
94
  baseURL: string | undefined
118
- client: Required<NonNullable<Options['client']>>
95
+ parser: NonNullable<Options['parser']>
96
+ importPath: NonNullable<Options['importPath']>
119
97
  dataReturnType: NonNullable<Options['dataReturnType']>
120
98
  pathParamsType: NonNullable<Options['pathParamsType']>
121
- templates: NonNullable<Templates>
122
- }
123
-
124
- export type FileMeta = {
125
- pluginKey?: Plugin['key']
126
- tag?: string
127
99
  }
128
100
 
129
101
  export type PluginClient = PluginFactoryOptions<'plugin-client', Options, ResolvedOptions, never, ResolvePathOptions>
@@ -1,201 +0,0 @@
1
- // src/components/Client.tsx
2
- import { URLPath } from "@kubb/core/utils";
3
- import { Parser, File, Function, useApp } from "@kubb/react";
4
- import { pluginTsName } from "@kubb/plugin-ts";
5
- import { useOperation, useOperationManager } from "@kubb/plugin-oas/hooks";
6
- import { getComments, getPathParams } from "@kubb/plugin-oas/utils";
7
- import { isOptional } from "@kubb/oas";
8
- import { jsx, jsxs } from "@kubb/react/jsx-runtime";
9
- function Template({ name, generics, returnType, params, JSDoc, client }) {
10
- const isFormData = client.contentType === "multipart/form-data";
11
- const headers = [
12
- client.contentType !== "application/json" ? `'Content-Type': '${client.contentType}'` : void 0,
13
- client.withHeaders ? "...headers" : void 0
14
- ].filter(Boolean).join(", ");
15
- const clientParams = {
16
- data: {
17
- mode: "object",
18
- children: {
19
- method: {
20
- type: "string",
21
- value: JSON.stringify(client.method)
22
- },
23
- url: {
24
- type: "string",
25
- value: client.path.template
26
- },
27
- baseURL: client.baseURL ? {
28
- type: "string",
29
- value: JSON.stringify(client.baseURL)
30
- } : void 0,
31
- params: client.withQueryParams ? {
32
- type: "any"
33
- } : void 0,
34
- data: client.withData ? {
35
- type: "any",
36
- value: isFormData ? "formData" : void 0
37
- } : void 0,
38
- headers: headers.length ? {
39
- type: "any",
40
- value: headers.length ? `{ ${headers}, ...options.headers }` : void 0
41
- } : void 0,
42
- options: {
43
- type: "any",
44
- mode: "inlineSpread"
45
- }
46
- }
47
- }
48
- };
49
- const formData = isFormData ? `
50
- const formData = new FormData()
51
- if(data) {
52
- Object.keys(data).forEach((key) => {
53
- const value = data[key];
54
- if (typeof key === "string" && (typeof value === "string" || value instanceof Blob)) {
55
- formData.append(key, value);
56
- }
57
- })
58
- }
59
- ` : void 0;
60
- return /* @__PURE__ */ jsxs(Function, { name, async: true, export: true, generics, returnType, params, JSDoc, children: [
61
- formData || "",
62
- /* @__PURE__ */ jsx(Function.Call, { name: "res", to: /* @__PURE__ */ jsx(Function, { name: "client", async: true, generics: client.generics, params: clientParams }) }),
63
- /* @__PURE__ */ jsx(Function.Return, { children: client.dataReturnType === "data" ? "res.data" : "res" })
64
- ] });
65
- }
66
- function RootTemplate({ children }) {
67
- const {
68
- plugin: {
69
- options: {
70
- client: { importPath },
71
- extName
72
- }
73
- }
74
- } = useApp();
75
- const { getSchemas, getFile } = useOperationManager();
76
- const operation = useOperation();
77
- const file = getFile(operation);
78
- const fileType = getFile(operation, { pluginKey: [pluginTsName] });
79
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: "type" });
80
- return /* @__PURE__ */ jsx(Parser, { language: "typescript", children: /* @__PURE__ */ jsxs(File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
81
- /* @__PURE__ */ jsx(File.Import, { name: "client", path: importPath }),
82
- /* @__PURE__ */ jsx(File.Import, { name: ["ResponseConfig"], path: importPath, isTypeOnly: true }),
83
- /* @__PURE__ */ jsx(
84
- File.Import,
85
- {
86
- extName,
87
- name: [schemas.request?.name, schemas.response.name, schemas.pathParams?.name, schemas.queryParams?.name, schemas.headerParams?.name].filter(Boolean),
88
- root: file.path,
89
- path: fileType.path,
90
- isTypeOnly: true
91
- }
92
- ),
93
- /* @__PURE__ */ jsx(File.Source, { children })
94
- ] }) });
95
- }
96
- var defaultTemplates = { default: Template, root: RootTemplate };
97
- function Client({ baseURL, Template: Template3 = defaultTemplates.default }) {
98
- const {
99
- plugin: {
100
- options: { client, dataReturnType, pathParamsType }
101
- }
102
- } = useApp();
103
- const { getSchemas, getName } = useOperationManager();
104
- const operation = useOperation();
105
- const contentType = operation.getContentType();
106
- const name = getName(operation, { type: "function" });
107
- const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: "type" });
108
- return /* @__PURE__ */ jsx(
109
- Template3,
110
- {
111
- name,
112
- params: {
113
- pathParams: {
114
- mode: pathParamsType === "object" ? "object" : "inlineSpread",
115
- children: getPathParams(schemas.pathParams, { typed: true })
116
- },
117
- data: schemas.request?.name ? {
118
- type: schemas.request?.name,
119
- optional: isOptional(schemas.request?.schema)
120
- } : void 0,
121
- params: schemas.queryParams?.name ? {
122
- type: schemas.queryParams?.name,
123
- optional: isOptional(schemas.queryParams?.schema)
124
- } : void 0,
125
- headers: schemas.headerParams?.name ? {
126
- type: schemas.headerParams?.name,
127
- optional: isOptional(schemas.headerParams?.schema)
128
- } : void 0,
129
- options: {
130
- type: "Partial<Parameters<typeof client>[0]>",
131
- default: "{}"
132
- }
133
- },
134
- returnType: dataReturnType === "data" ? `ResponseConfig<${schemas.response.name}>["data"]` : `ResponseConfig<${schemas.response.name}>`,
135
- JSDoc: {
136
- comments: getComments(operation)
137
- },
138
- client: {
139
- // only set baseURL from serverIndex(swagger) when no custom client(default) is used
140
- baseURL: client.importPath === "@kubb/plugin-client/client" ? baseURL : void 0,
141
- generics: [schemas.response.name, schemas.request?.name].filter(Boolean),
142
- dataReturnType,
143
- withQueryParams: !!schemas.queryParams?.name,
144
- withData: !!schemas.request?.name,
145
- withHeaders: !!schemas.headerParams?.name,
146
- method: operation.method,
147
- path: new URLPath(operation.path),
148
- contentType
149
- }
150
- }
151
- );
152
- }
153
- Client.File = function({ baseURL, ...props }) {
154
- const templates = { ...defaultTemplates, ...props.templates };
155
- const Template3 = templates.default;
156
- const RootTemplate3 = templates.root;
157
- return /* @__PURE__ */ jsx(RootTemplate3, { children: /* @__PURE__ */ jsx(Client, { baseURL, Template: Template3 }) });
158
- };
159
- Client.templates = defaultTemplates;
160
-
161
- // src/components/Operations.tsx
162
- import { URLPath as URLPath2 } from "@kubb/core/utils";
163
- import { useOperations } from "@kubb/plugin-oas/hooks";
164
- import { Const, File as File2, Parser as Parser2, useApp as useApp2 } from "@kubb/react";
165
- import { jsx as jsx2 } from "@kubb/react/jsx-runtime";
166
- function Template2({ name, operations }) {
167
- const operationsObject = {};
168
- operations.forEach((operation) => {
169
- operationsObject[operation.getOperationId()] = {
170
- path: new URLPath2(operation.path).URL,
171
- method: operation.method
172
- };
173
- });
174
- return /* @__PURE__ */ jsx2(Const, { name, export: true, asConst: true, children: JSON.stringify(operationsObject, void 0, 2) });
175
- }
176
- function RootTemplate2({ children }) {
177
- const {
178
- pluginManager,
179
- plugin: { key: pluginKey }
180
- } = useApp2();
181
- const file = pluginManager.getFile({ name: "operations", extName: ".ts", pluginKey });
182
- return /* @__PURE__ */ jsx2(Parser2, { language: "typescript", children: /* @__PURE__ */ jsx2(File2, { baseName: file.baseName, path: file.path, meta: file.meta, exportable: false, children: /* @__PURE__ */ jsx2(File2.Source, { children }) }) });
183
- }
184
- var defaultTemplates2 = { default: Template2, root: RootTemplate2 };
185
- function Operations({ baseURL, Template: Template3 = defaultTemplates2.default }) {
186
- const operations = useOperations();
187
- return /* @__PURE__ */ jsx2(Template3, { baseURL, name: "operations", operations });
188
- }
189
- Operations.File = function({ baseURL, ...props }) {
190
- const templates = { ...defaultTemplates2, ...props.templates };
191
- const Template3 = templates.default;
192
- const RootTemplate3 = templates.root;
193
- return /* @__PURE__ */ jsx2(RootTemplate3, { children: /* @__PURE__ */ jsx2(Operations, { baseURL, Template: Template3 }) });
194
- };
195
- Operations.templates = defaultTemplates2;
196
-
197
- export {
198
- Client,
199
- Operations
200
- };
201
- //# sourceMappingURL=chunk-W57BRY5O.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/components/Client.tsx","../src/components/Operations.tsx"],"sourcesContent":["import { URLPath } from '@kubb/core/utils'\nimport { Parser, File, Function, useApp } from '@kubb/react'\nimport { pluginTsName } from '@kubb/plugin-ts'\nimport { useOperation, useOperationManager } from '@kubb/plugin-oas/hooks'\nimport { getComments, getPathParams } from '@kubb/plugin-oas/utils'\n\nimport { isOptional } from '@kubb/oas'\nimport type { HttpMethod } from '@kubb/oas'\nimport type { KubbNode, Params } from '@kubb/react'\nimport type { ComponentProps, ComponentType } from 'react'\nimport type { FileMeta, PluginClient } from '../types.ts'\n\ntype TemplateProps = {\n /**\n * Name of the function\n */\n name: string\n /**\n * Parameters/options/props that need to be used\n */\n params: Params\n /**\n * Generics that needs to be added for TypeScript\n */\n generics?: string\n /**\n * ReturnType(see async for adding Promise type)\n */\n returnType?: string\n /**\n * Options for JSdocs\n */\n JSDoc?: {\n comments: string[]\n }\n client: {\n baseURL: string | undefined\n generics: string | string[]\n method: HttpMethod\n path: URLPath\n dataReturnType: PluginClient['options']['dataReturnType']\n withQueryParams: boolean\n withData: boolean\n withHeaders: boolean\n contentType: string\n }\n}\n\nfunction Template({ name, generics, returnType, params, JSDoc, client }: TemplateProps): KubbNode {\n const isFormData = client.contentType === 'multipart/form-data'\n const headers = [\n client.contentType !== 'application/json' ? `'Content-Type': '${client.contentType}'` : undefined,\n client.withHeaders ? '...headers' : undefined,\n ]\n .filter(Boolean)\n .join(', ')\n const clientParams: Params = {\n data: {\n mode: 'object',\n children: {\n method: {\n type: 'string',\n value: JSON.stringify(client.method),\n },\n url: {\n type: 'string',\n value: client.path.template,\n },\n baseURL: client.baseURL\n ? {\n type: 'string',\n value: JSON.stringify(client.baseURL),\n }\n : undefined,\n params: client.withQueryParams\n ? {\n type: 'any',\n }\n : undefined,\n data: client.withData\n ? {\n type: 'any',\n value: isFormData ? 'formData' : undefined,\n }\n : undefined,\n headers: headers.length\n ? {\n type: 'any',\n value: headers.length ? `{ ${headers}, ...options.headers }` : undefined,\n }\n : undefined,\n options: {\n type: 'any',\n mode: 'inlineSpread',\n },\n },\n },\n }\n\n const formData = isFormData\n ? `\n const formData = new FormData()\n if(data) {\n Object.keys(data).forEach((key) => {\n const value = data[key];\n if (typeof key === \"string\" && (typeof value === \"string\" || value instanceof Blob)) {\n formData.append(key, value);\n }\n })\n }\n `\n : undefined\n\n return (\n <Function name={name} async export generics={generics} returnType={returnType} params={params} JSDoc={JSDoc}>\n {formData || ''}\n <Function.Call name=\"res\" to={<Function name=\"client\" async generics={client.generics} params={clientParams} />} />\n <Function.Return>{client.dataReturnType === 'data' ? 'res.data' : 'res'}</Function.Return>\n </Function>\n )\n}\n\ntype RootTemplateProps = {\n children?: React.ReactNode\n}\n\nfunction RootTemplate({ children }: RootTemplateProps) {\n const {\n plugin: {\n options: {\n client: { importPath },\n extName,\n },\n },\n } = useApp<PluginClient>()\n\n const { getSchemas, getFile } = useOperationManager()\n const operation = useOperation()\n\n const file = getFile(operation)\n const fileType = getFile(operation, { pluginKey: [pluginTsName] })\n const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })\n\n return (\n <Parser language=\"typescript\">\n <File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta}>\n <File.Import name={'client'} path={importPath} />\n <File.Import name={['ResponseConfig']} path={importPath} isTypeOnly />\n <File.Import\n extName={extName}\n name={[schemas.request?.name, schemas.response.name, schemas.pathParams?.name, schemas.queryParams?.name, schemas.headerParams?.name].filter(Boolean)}\n root={file.path}\n path={fileType.path}\n isTypeOnly\n />\n <File.Source>{children}</File.Source>\n </File>\n </Parser>\n )\n}\n\nconst defaultTemplates = { default: Template, root: RootTemplate } as const\n\ntype Templates = Partial<typeof defaultTemplates>\n\ntype ClientProps = {\n baseURL: string | undefined\n /**\n * This will make it possible to override the default behaviour.\n */\n Template?: ComponentType<ComponentProps<typeof Template>>\n}\n\nexport function Client({ baseURL, Template = defaultTemplates.default }: ClientProps): KubbNode {\n const {\n plugin: {\n options: { client, dataReturnType, pathParamsType },\n },\n } = useApp<PluginClient>()\n\n const { getSchemas, getName } = useOperationManager()\n const operation = useOperation()\n\n const contentType = operation.getContentType()\n const name = getName(operation, { type: 'function' })\n const schemas = getSchemas(operation, { pluginKey: [pluginTsName], type: 'type' })\n\n return (\n <Template\n name={name}\n params={{\n pathParams: {\n mode: pathParamsType === 'object' ? 'object' : 'inlineSpread',\n children: getPathParams(schemas.pathParams, { typed: true }),\n },\n data: schemas.request?.name\n ? {\n type: schemas.request?.name,\n optional: isOptional(schemas.request?.schema),\n }\n : undefined,\n params: schemas.queryParams?.name\n ? {\n type: schemas.queryParams?.name,\n optional: isOptional(schemas.queryParams?.schema),\n }\n : undefined,\n headers: schemas.headerParams?.name\n ? {\n type: schemas.headerParams?.name,\n optional: isOptional(schemas.headerParams?.schema),\n }\n : undefined,\n options: {\n type: 'Partial<Parameters<typeof client>[0]>',\n default: '{}',\n },\n }}\n returnType={dataReturnType === 'data' ? `ResponseConfig<${schemas.response.name}>[\"data\"]` : `ResponseConfig<${schemas.response.name}>`}\n JSDoc={{\n comments: getComments(operation),\n }}\n client={{\n // only set baseURL from serverIndex(swagger) when no custom client(default) is used\n baseURL: client.importPath === '@kubb/plugin-client/client' ? baseURL : undefined,\n generics: [schemas.response.name, schemas.request?.name].filter(Boolean),\n dataReturnType,\n withQueryParams: !!schemas.queryParams?.name,\n withData: !!schemas.request?.name,\n withHeaders: !!schemas.headerParams?.name,\n method: operation.method,\n path: new URLPath(operation.path),\n contentType,\n }}\n />\n )\n}\n\ntype FileProps = {\n baseURL: string | undefined\n /**\n * This will make it possible to override the default behaviour.\n */\n templates?: Templates\n}\n\nClient.File = function ({ baseURL, ...props }: FileProps): KubbNode {\n const templates = { ...defaultTemplates, ...props.templates }\n\n const Template = templates.default\n const RootTemplate = templates.root\n\n return (\n <RootTemplate>\n <Client baseURL={baseURL} Template={Template} />\n </RootTemplate>\n )\n}\n\nClient.templates = defaultTemplates\n","import { URLPath } from '@kubb/core/utils'\nimport { useOperations } from '@kubb/plugin-oas/hooks'\nimport { Const, File, Parser, useApp } from '@kubb/react'\n\nimport type { HttpMethod, Operation } from '@kubb/oas'\nimport type { KubbNode } from '@kubb/react'\nimport type { ComponentProps, ComponentType } from 'react'\nimport type { FileMeta, PluginClient } from '../types.ts'\n\ntype TemplateProps = {\n /**\n * Name of the function\n */\n name: string\n operations: Operation[]\n baseURL: string | undefined\n}\n\nfunction Template({ name, operations }: TemplateProps): KubbNode {\n const operationsObject: Record<string, { path: string; method: HttpMethod }> = {}\n\n operations.forEach((operation) => {\n operationsObject[operation.getOperationId()] = {\n path: new URLPath(operation.path).URL,\n method: operation.method,\n }\n })\n return (\n <Const name={name} export asConst>\n {JSON.stringify(operationsObject, undefined, 2)}\n </Const>\n )\n}\n\ntype RootTemplateProps = {\n children?: React.ReactNode\n}\n\nfunction RootTemplate({ children }: RootTemplateProps) {\n const {\n pluginManager,\n plugin: { key: pluginKey },\n } = useApp<PluginClient>()\n const file = pluginManager.getFile({ name: 'operations', extName: '.ts', pluginKey })\n\n return (\n <Parser language=\"typescript\">\n <File<FileMeta> baseName={file.baseName} path={file.path} meta={file.meta} exportable={false}>\n <File.Source>{children}</File.Source>\n </File>\n </Parser>\n )\n}\n\nconst defaultTemplates = { default: Template, root: RootTemplate } as const\n\ntype Templates = Partial<typeof defaultTemplates>\n\ntype Props = {\n baseURL: string | undefined\n /**\n * This will make it possible to override the default behaviour.\n */\n Template?: ComponentType<ComponentProps<typeof Template>>\n}\n\nexport function Operations({ baseURL, Template = defaultTemplates.default }: Props): KubbNode {\n const operations = useOperations()\n\n return <Template baseURL={baseURL} name=\"operations\" operations={operations} />\n}\n\ntype FileProps = {\n baseURL: string | undefined\n /**\n * This will make it possible to override the default behaviour.\n */\n templates?: Templates\n}\n\nOperations.File = function ({ baseURL, ...props }: FileProps): KubbNode {\n const templates = { ...defaultTemplates, ...props.templates }\n\n const Template = templates.default\n const RootTemplate = templates.root\n\n return (\n <RootTemplate>\n <Operations baseURL={baseURL} Template={Template} />\n </RootTemplate>\n )\n}\n\nOperations.templates = defaultTemplates\n"],"mappings":";AAAA,SAAS,eAAe;AACxB,SAAS,QAAQ,MAAM,UAAU,cAAc;AAC/C,SAAS,oBAAoB;AAC7B,SAAS,cAAc,2BAA2B;AAClD,SAAS,aAAa,qBAAqB;AAE3C,SAAS,kBAAkB;AA4GvB,SAEgC,KAFhC;AAlEJ,SAAS,SAAS,EAAE,MAAM,UAAU,YAAY,QAAQ,OAAO,OAAO,GAA4B;AAChG,QAAM,aAAa,OAAO,gBAAgB;AAC1C,QAAM,UAAU;AAAA,IACd,OAAO,gBAAgB,qBAAqB,oBAAoB,OAAO,WAAW,MAAM;AAAA,IACxF,OAAO,cAAc,eAAe;AAAA,EACtC,EACG,OAAO,OAAO,EACd,KAAK,IAAI;AACZ,QAAM,eAAuB;AAAA,IAC3B,MAAM;AAAA,MACJ,MAAM;AAAA,MACN,UAAU;AAAA,QACR,QAAQ;AAAA,UACN,MAAM;AAAA,UACN,OAAO,KAAK,UAAU,OAAO,MAAM;AAAA,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