@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
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Stijn Van Hulle
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<!-- <img src="assets/logo.png" alt="logo" width="200" height="auto" /> -->
|
|
4
|
+
<h1>@kubb/plugin-client</h1>
|
|
5
|
+
|
|
6
|
+
<p>
|
|
7
|
+
Swagger integration for Axios to generate all the different clients based on an OpenAPI specification.
|
|
8
|
+
</p>
|
|
9
|
+
<img src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/banner.png" alt="logo" height="auto" />
|
|
10
|
+
|
|
11
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
12
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
13
|
+
[![Coverage][coverage-src]][coverage-href]
|
|
14
|
+
[![License][license-src]][license-href]
|
|
15
|
+
|
|
16
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
17
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
<h4>
|
|
21
|
+
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/alpha/examples/typescript" target="_blank">View Demo</a>
|
|
22
|
+
<span> · </span>
|
|
23
|
+
<a href="https://kubb.dev/" target="_blank">Documentation</a>
|
|
24
|
+
<span> · </span>
|
|
25
|
+
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
|
|
26
|
+
<span> · </span>
|
|
27
|
+
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
|
|
28
|
+
</h4>
|
|
29
|
+
</div>
|
|
30
|
+
|
|
31
|
+
<!-- Badges -->
|
|
32
|
+
|
|
33
|
+
[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-client?flat&colorA=18181B&colorB=f58517
|
|
34
|
+
[npm-version-href]: https://npmjs.com/package/@kubb/plugin-client
|
|
35
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/plugin-client?flat&colorA=18181B&colorB=f58517
|
|
36
|
+
[npm-downloads-href]: https://npmjs.com/package/@kubb/plugin-client
|
|
37
|
+
[license-src]: https://img.shields.io/github/license/kubb-labs/kubb.svg?flat&colorA=18181B&colorB=f58517
|
|
38
|
+
[license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
|
|
39
|
+
[build-src]: https://img.shields.io/github/actions/workflow/status/kubb-labs/kubb/ci.yaml?style=flat&colorA=18181B&colorB=f58517
|
|
40
|
+
[build-href]: https://www.npmjs.com/package/@kubb/plugin-client
|
|
41
|
+
[minified-src]: https://img.shields.io/bundlephobia/min/@kubb/plugin-client?style=flat&colorA=18181B&colorB=f58517
|
|
42
|
+
[minified-href]: https://www.npmjs.com/package/@kubb/plugin-client
|
|
43
|
+
[coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
|
|
44
|
+
[coverage-href]: https://www.npmjs.com/package/@kubb/plugin-client
|
package/client.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
|
|
3
|
+
import type { AxiosError, AxiosHeaders, AxiosRequestConfig, AxiosResponse } from 'axios'
|
|
4
|
+
|
|
5
|
+
declare const AXIOS_BASE: string
|
|
6
|
+
declare const AXIOS_HEADERS: string
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Subset of AxiosRequestConfig
|
|
10
|
+
*/
|
|
11
|
+
export type RequestConfig<TData = unknown> = {
|
|
12
|
+
baseURL?: string
|
|
13
|
+
url?: string
|
|
14
|
+
method: 'get' | 'put' | 'patch' | 'post' | 'delete'
|
|
15
|
+
params?: unknown
|
|
16
|
+
data?: TData
|
|
17
|
+
responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'
|
|
18
|
+
signal?: AbortSignal
|
|
19
|
+
headers?: AxiosRequestConfig['headers']
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Subset of AxiosResponse
|
|
23
|
+
*/
|
|
24
|
+
export type ResponseConfig<TData = unknown> = {
|
|
25
|
+
data: TData
|
|
26
|
+
status: number
|
|
27
|
+
statusText: string
|
|
28
|
+
headers?: AxiosResponse['headers']
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export const axiosInstance = axios.create({
|
|
32
|
+
baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,
|
|
33
|
+
headers: typeof AXIOS_HEADERS !== 'undefined' ? (JSON.parse(AXIOS_HEADERS) as AxiosHeaders) : undefined,
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
export const axiosClient = async <TData, TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>): Promise<ResponseConfig<TData>> => {
|
|
37
|
+
const promise = axiosInstance.request<TData, ResponseConfig<TData>>(config).catch((e: AxiosError<TError>) => {
|
|
38
|
+
throw e
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
return promise
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export default axiosClient
|
|
@@ -0,0 +1,201 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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,QACrC;AAAA,QACA,KAAK;AAAA,UACH,MAAM;AAAA,UACN,OAAO,OAAO,KAAK;AAAA,QACrB;AAAA,QACA,SAAS,OAAO,UACZ;AAAA,UACE,MAAM;AAAA,UACN,OAAO,KAAK,UAAU,OAAO,OAAO;AAAA,QACtC,IACA;AAAA,QACJ,QAAQ,OAAO,kBACX;AAAA,UACE,MAAM;AAAA,QACR,IACA;AAAA,QACJ,MAAM,OAAO,WACT;AAAA,UACE,MAAM;AAAA,UACN,OAAO,aAAa,aAAa;AAAA,QACnC,IACA;AAAA,QACJ,SAAS,QAAQ,SACb;AAAA,UACE,MAAM;AAAA,UACN,OAAO,QAAQ,SAAS,KAAK,OAAO,2BAA2B;AAAA,QACjE,I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+1,201 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/Client.tsx
|
|
2
|
+
var _utils = require('@kubb/core/utils');
|
|
3
|
+
var _react = require('@kubb/react');
|
|
4
|
+
var _plugints = require('@kubb/plugin-ts');
|
|
5
|
+
var _hooks = require('@kubb/plugin-oas/hooks');
|
|
6
|
+
var _utils3 = require('@kubb/plugin-oas/utils');
|
|
7
|
+
var _oas = require('@kubb/oas');
|
|
8
|
+
var _jsxruntime = require('@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__ */ _jsxruntime.jsxs.call(void 0, _react.Function, { name, async: true, export: true, generics, returnType, params, JSDoc, children: [
|
|
61
|
+
formData || "",
|
|
62
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Function.Call, { name: "res", to: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Function, { name: "client", async: true, generics: client.generics, params: clientParams }) }),
|
|
63
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.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
|
+
} = _react.useApp.call(void 0, );
|
|
75
|
+
const { getSchemas, getFile } = _hooks.useOperationManager.call(void 0, );
|
|
76
|
+
const operation = _hooks.useOperation.call(void 0, );
|
|
77
|
+
const file = getFile(operation);
|
|
78
|
+
const fileType = getFile(operation, { pluginKey: [_plugints.pluginTsName] });
|
|
79
|
+
const schemas = getSchemas(operation, { pluginKey: [_plugints.pluginTsName], type: "type" });
|
|
80
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
81
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: "client", path: importPath }),
|
|
82
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Import, { name: ["ResponseConfig"], path: importPath, isTypeOnly: true }),
|
|
83
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
84
|
+
_react.File.Import,
|
|
85
|
+
{
|
|
86
|
+
extName,
|
|
87
|
+
name: [_optionalChain([schemas, 'access', _ => _.request, 'optionalAccess', _2 => _2.name]), schemas.response.name, _optionalChain([schemas, 'access', _3 => _3.pathParams, 'optionalAccess', _4 => _4.name]), _optionalChain([schemas, 'access', _5 => _5.queryParams, 'optionalAccess', _6 => _6.name]), _optionalChain([schemas, 'access', _7 => _7.headerParams, 'optionalAccess', _8 => _8.name])].filter(Boolean),
|
|
88
|
+
root: file.path,
|
|
89
|
+
path: fileType.path,
|
|
90
|
+
isTypeOnly: true
|
|
91
|
+
}
|
|
92
|
+
),
|
|
93
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.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
|
+
} = _react.useApp.call(void 0, );
|
|
103
|
+
const { getSchemas, getName } = _hooks.useOperationManager.call(void 0, );
|
|
104
|
+
const operation = _hooks.useOperation.call(void 0, );
|
|
105
|
+
const contentType = operation.getContentType();
|
|
106
|
+
const name = getName(operation, { type: "function" });
|
|
107
|
+
const schemas = getSchemas(operation, { pluginKey: [_plugints.pluginTsName], type: "type" });
|
|
108
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
109
|
+
Template3,
|
|
110
|
+
{
|
|
111
|
+
name,
|
|
112
|
+
params: {
|
|
113
|
+
pathParams: {
|
|
114
|
+
mode: pathParamsType === "object" ? "object" : "inlineSpread",
|
|
115
|
+
children: _utils3.getPathParams.call(void 0, schemas.pathParams, { typed: true })
|
|
116
|
+
},
|
|
117
|
+
data: _optionalChain([schemas, 'access', _9 => _9.request, 'optionalAccess', _10 => _10.name]) ? {
|
|
118
|
+
type: _optionalChain([schemas, 'access', _11 => _11.request, 'optionalAccess', _12 => _12.name]),
|
|
119
|
+
optional: _oas.isOptional.call(void 0, _optionalChain([schemas, 'access', _13 => _13.request, 'optionalAccess', _14 => _14.schema]))
|
|
120
|
+
} : void 0,
|
|
121
|
+
params: _optionalChain([schemas, 'access', _15 => _15.queryParams, 'optionalAccess', _16 => _16.name]) ? {
|
|
122
|
+
type: _optionalChain([schemas, 'access', _17 => _17.queryParams, 'optionalAccess', _18 => _18.name]),
|
|
123
|
+
optional: _oas.isOptional.call(void 0, _optionalChain([schemas, 'access', _19 => _19.queryParams, 'optionalAccess', _20 => _20.schema]))
|
|
124
|
+
} : void 0,
|
|
125
|
+
headers: _optionalChain([schemas, 'access', _21 => _21.headerParams, 'optionalAccess', _22 => _22.name]) ? {
|
|
126
|
+
type: _optionalChain([schemas, 'access', _23 => _23.headerParams, 'optionalAccess', _24 => _24.name]),
|
|
127
|
+
optional: _oas.isOptional.call(void 0, _optionalChain([schemas, 'access', _25 => _25.headerParams, 'optionalAccess', _26 => _26.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: _utils3.getComments.call(void 0, 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, _optionalChain([schemas, 'access', _27 => _27.request, 'optionalAccess', _28 => _28.name])].filter(Boolean),
|
|
142
|
+
dataReturnType,
|
|
143
|
+
withQueryParams: !!_optionalChain([schemas, 'access', _29 => _29.queryParams, 'optionalAccess', _30 => _30.name]),
|
|
144
|
+
withData: !!_optionalChain([schemas, 'access', _31 => _31.request, 'optionalAccess', _32 => _32.name]),
|
|
145
|
+
withHeaders: !!_optionalChain([schemas, 'access', _33 => _33.headerParams, 'optionalAccess', _34 => _34.name]),
|
|
146
|
+
method: operation.method,
|
|
147
|
+
path: new (0, _utils.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__ */ _jsxruntime.jsx.call(void 0, RootTemplate3, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Client, { baseURL, Template: Template3 }) });
|
|
158
|
+
};
|
|
159
|
+
Client.templates = defaultTemplates;
|
|
160
|
+
|
|
161
|
+
// src/components/Operations.tsx
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
function Template2({ name, operations }) {
|
|
167
|
+
const operationsObject = {};
|
|
168
|
+
operations.forEach((operation) => {
|
|
169
|
+
operationsObject[operation.getOperationId()] = {
|
|
170
|
+
path: new (0, _utils.URLPath)(operation.path).URL,
|
|
171
|
+
method: operation.method
|
|
172
|
+
};
|
|
173
|
+
});
|
|
174
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.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
|
+
} = _react.useApp.call(void 0, );
|
|
181
|
+
const file = pluginManager.getFile({ name: "operations", extName: ".ts", pluginKey });
|
|
182
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.Parser, { language: "typescript", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File, { baseName: file.baseName, path: file.path, meta: file.meta, exportable: false, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _react.File.Source, { children }) }) });
|
|
183
|
+
}
|
|
184
|
+
var defaultTemplates2 = { default: Template2, root: RootTemplate2 };
|
|
185
|
+
function Operations({ baseURL, Template: Template3 = defaultTemplates2.default }) {
|
|
186
|
+
const operations = _hooks.useOperations.call(void 0, );
|
|
187
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, 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__ */ _jsxruntime.jsx.call(void 0, RootTemplate3, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, Operations, { baseURL, Template: Template3 }) });
|
|
194
|
+
};
|
|
195
|
+
Operations.templates = defaultTemplates2;
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
200
|
+
exports.Client = Client; exports.Operations = Operations;
|
|
201
|
+
//# sourceMappingURL=chunk-W7F5CMU6.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/kubb/kubb/packages/plugin-client/dist/chunk-W7F5CMU6.cjs","../src/components/Client.tsx","../src/components/Operations.tsx"],"names":["Template","RootTemplate","URLPath","jsx","useApp","Parser","File","defaultTemplates"],"mappings":"AAAA;ACAA,yCAAwB;AACxB,oCAA+C;AAC/C,2CAA6B;AAC7B,+CAAkD;AAClD,gDAA2C;AAE3C,gCAA2B;AA4GvB,qDAAA;AAlEJ,SAAS,QAAA,CAAS,EAAE,IAAA,EAAM,QAAA,EAAU,UAAA,EAAY,MAAA,EAAQ,KAAA,EAAO,OAAO,CAAA,EAA4B;AAChG,EAAA,MAAM,WAAA,EAAa,MAAA,CAAO,YAAA,IAAgB,qBAAA;AAC1C,EAAA,MAAM,QAAA,EAAU;AAAA,IACd,MAAA,CAAO,YAAA,IAAgB,mBAAA,EAAqB,CAAA,iBAAA,EAAoB,MAAA,CAAO,WAAW,CAAA,CAAA,EAAA,EAAM,KAAA,CAAA;AAAA,IACxF,MAAA,CAAO,YAAA,EAAc,aAAA,EAAe,KAAA;AAAA,EACtC,CAAA,CACG,MAAA,CAAO,OAAO,CAAA,CACd,IAAA,CAAK,IAAI,CAAA;AACZ,EAAA,MAAM,aAAA,EAAuB;AAAA,IAC3B,IAAA,EAAM;AAAA,MACJ,IAAA,EAAM,QAAA;AAAA,MACN,QAAA,EAAU;AAAA,QACR,MAAA,EAAQ;AAAA,UACN,IAAA,EAAM,QAAA;AAAA,UACN,KAAA,EAAO,IAAA,CAAK,SAAA,CAAU,MAAA,CAAO,MAAM;AAAA,QACrC,CAAA;AAAA,QACA,GAAA,EAAK;AAAA,UACH,IAAA,EAAM,QAAA;AAAA,UACN,KAAA,EAAO,MAAA,C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F,yDAAC","file":"/home/runner/work/kubb/kubb/packages/plugin-client/dist/chunk-W7F5CMU6.cjs","sourcesContent":[null,"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"]}
|
package/dist/client.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// client.ts
|
|
2
|
+
var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
|
|
3
|
+
var axiosInstance = _axios2.default.create({
|
|
4
|
+
baseURL: typeof AXIOS_BASE !== "undefined" ? AXIOS_BASE : void 0,
|
|
5
|
+
headers: typeof AXIOS_HEADERS !== "undefined" ? JSON.parse(AXIOS_HEADERS) : void 0
|
|
6
|
+
});
|
|
7
|
+
var axiosClient = async (config) => {
|
|
8
|
+
const promise = axiosInstance.request(config).catch((e) => {
|
|
9
|
+
throw e;
|
|
10
|
+
});
|
|
11
|
+
return promise;
|
|
12
|
+
};
|
|
13
|
+
var client_default = axiosClient;
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
exports.axiosClient = axiosClient; exports.axiosInstance = axiosInstance; exports.default = client_default;
|
|
19
|
+
//# sourceMappingURL=client.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/home/runner/work/kubb/kubb/packages/plugin-client/dist/client.cjs","../client.ts"],"names":[],"mappings":"AAAA;ACAA,4EAAkB;AA8BX,IAAM,cAAA,EAAgB,eAAA,CAAM,MAAA,CAAO;AAAA,EACxC,OAAA,EAAS,OAAO,WAAA,IAAe,YAAA,EAAc,WAAA,EAAa,KAAA,CAAA;AAAA,EAC1D,OAAA,EAAS,OAAO,cAAA,IAAkB,YAAA,EAAe,IAAA,CAAK,KAAA,CAAM,aAAa,EAAA,EAAqB,KAAA;AAChG,CAAC,CAAA;AAEM,IAAM,YAAA,EAAc,MAAA,CAAsD,MAAA,EAAA,GAAsE;AACrJ,EAAA,MAAM,QAAA,EAAU,aAAA,CAAc,OAAA,CAAsC,MAAM,CAAA,CAAE,KAAA,CAAM,CAAC,CAAA,EAAA,GAA0B;AAC3G,IAAA,MAAM,CAAA;AAAA,EACR,CAAC,CAAA;AAED,EAAA,OAAO,OAAA;AACT,CAAA;AAEA,IAAO,eAAA,EAAQ,WAAA;AD9Bf;AACE;AACA;AACA;AACF,2GAAC","file":"/home/runner/work/kubb/kubb/packages/plugin-client/dist/client.cjs","sourcesContent":[null,"import axios from 'axios'\n\nimport type { AxiosError, AxiosHeaders, AxiosRequestConfig, AxiosResponse } from 'axios'\n\ndeclare const AXIOS_BASE: string\ndeclare const AXIOS_HEADERS: string\n\n/**\n * Subset of AxiosRequestConfig\n */\nexport type RequestConfig<TData = unknown> = {\n baseURL?: string\n url?: string\n method: 'get' | 'put' | 'patch' | 'post' | 'delete'\n params?: unknown\n data?: TData\n responseType?: 'arraybuffer' | 'blob' | 'document' | 'json' | 'text' | 'stream'\n signal?: AbortSignal\n headers?: AxiosRequestConfig['headers']\n}\n/**\n * Subset of AxiosResponse\n */\nexport type ResponseConfig<TData = unknown> = {\n data: TData\n status: number\n statusText: string\n headers?: AxiosResponse['headers']\n}\n\nexport const axiosInstance = axios.create({\n baseURL: typeof AXIOS_BASE !== 'undefined' ? AXIOS_BASE : undefined,\n headers: typeof AXIOS_HEADERS !== 'undefined' ? (JSON.parse(AXIOS_HEADERS) as AxiosHeaders) : undefined,\n})\n\nexport const axiosClient = async <TData, TError = unknown, TVariables = unknown>(config: RequestConfig<TVariables>): Promise<ResponseConfig<TData>> => {\n const promise = axiosInstance.request<TData, ResponseConfig<TData>>(config).catch((e: AxiosError<TError>) => {\n throw e\n })\n\n return promise\n}\n\nexport default axiosClient\n"]}
|