@kubb/plugin-ts 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 +45 -0
- package/dist/chunk-WOQIHEJC.js +621 -0
- package/dist/chunk-WOQIHEJC.js.map +1 -0
- package/dist/chunk-XCNZFEPR.cjs +621 -0
- package/dist/chunk-XCNZFEPR.cjs.map +1 -0
- package/dist/components.cjs +11 -0
- package/dist/components.cjs.map +1 -0
- package/dist/components.d.cts +60 -0
- package/dist/components.d.ts +60 -0
- package/dist/components.js +11 -0
- package/dist/components.js.map +1 -0
- package/dist/index.cjs +13 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +141 -0
- package/dist/index.d.ts +141 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/oas.cjs +1 -0
- package/dist/oas.cjs.map +1 -0
- package/dist/oas.d.cts +1 -0
- package/dist/oas.d.ts +1 -0
- package/dist/oas.js +1 -0
- package/dist/oas.js.map +1 -0
- package/package.json +91 -0
- package/src/OperationGenerator.tsx +49 -0
- package/src/SchemaGenerator.tsx +31 -0
- package/src/components/OasType.tsx +76 -0
- package/src/components/OperationSchema.tsx +161 -0
- package/src/components/Schema.tsx +135 -0
- package/src/components/__snapshots__/Schema/pets.ts +23 -0
- package/src/components/__snapshots__/Schema/showPetById.ts +28 -0
- package/src/components/index.ts +3 -0
- package/src/index.ts +15 -0
- package/src/oas/index.ts +1 -0
- package/src/parser/index.ts +263 -0
- package/src/plugin.ts +138 -0
- package/src/types.ts +138 -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,45 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
<!-- <img src="assets/logo.png" alt="logo" width="200" height="auto" /> -->
|
|
4
|
+
<h1>@kubb/plugin-ts</h1>
|
|
5
|
+
|
|
6
|
+
<p>
|
|
7
|
+
Swagger integration for TypeScript to generate all the different types based on an OpenAPI specification.
|
|
8
|
+
</p>
|
|
9
|
+
|
|
10
|
+
<img src="https://raw.githubusercontent.com/kubb-labs/kubb/main/assets/banner.png" alt="logo" height="auto" />
|
|
11
|
+
|
|
12
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
13
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
14
|
+
[![Coverage][coverage-src]][coverage-href]
|
|
15
|
+
[![License][license-src]][license-href]
|
|
16
|
+
|
|
17
|
+
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
18
|
+
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
19
|
+
</p>
|
|
20
|
+
|
|
21
|
+
<h4>
|
|
22
|
+
<a href="https://codesandbox.io/s/github/kubb-labs/kubb/tree/alpha/examples/typescript" target="_blank">View Demo</a>
|
|
23
|
+
<span> · </span>
|
|
24
|
+
<a href="https://kubb.dev/" target="_blank">Documentation</a>
|
|
25
|
+
<span> · </span>
|
|
26
|
+
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
|
|
27
|
+
<span> · </span>
|
|
28
|
+
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Request Feature</a>
|
|
29
|
+
</h4>
|
|
30
|
+
</div>
|
|
31
|
+
|
|
32
|
+
<!-- Badges -->
|
|
33
|
+
|
|
34
|
+
[npm-version-src]: https://img.shields.io/npm/v/@kubb/plugin-ts?flat&colorA=18181B&colorB=f58517
|
|
35
|
+
[npm-version-href]: https://npmjs.com/package/@kubb/plugin-ts
|
|
36
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@kubb/plugin-ts?flat&colorA=18181B&colorB=f58517
|
|
37
|
+
[npm-downloads-href]: https://npmjs.com/package/@kubb/plugin-ts
|
|
38
|
+
[license-src]: https://img.shields.io/github/license/kubb-labs/kubb.svg?flat&colorA=18181B&colorB=f58517
|
|
39
|
+
[license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
|
|
40
|
+
[build-src]: https://img.shields.io/github/actions/workflow/status/kubb-labs/kubb/ci.yaml?style=flat&colorA=18181B&colorB=f58517
|
|
41
|
+
[build-href]: https://www.npmjs.com/package/@kubb/plugin-ts
|
|
42
|
+
[minified-src]: https://img.shields.io/bundlephobia/min/@kubb/plugin-ts?style=flat&colorA=18181B&colorB=f58517
|
|
43
|
+
[minified-href]: https://www.npmjs.com/package/@kubb/plugin-ts
|
|
44
|
+
[coverage-src]: https://img.shields.io/codecov/c/github/kubb-labs/kubb?style=flat&colorA=18181B&colorB=f58517
|
|
45
|
+
[coverage-href]: https://www.npmjs.com/package/@kubb/plugin-ts
|
|
@@ -0,0 +1,621 @@
|
|
|
1
|
+
// src/plugin.ts
|
|
2
|
+
import path from "path";
|
|
3
|
+
import { FileManager, PluginManager, createPlugin } from "@kubb/core";
|
|
4
|
+
import { camelCase, pascalCase } from "@kubb/core/transformers";
|
|
5
|
+
import { renderTemplate } from "@kubb/core/utils";
|
|
6
|
+
import { pluginOasName } from "@kubb/plugin-oas";
|
|
7
|
+
|
|
8
|
+
// src/OperationGenerator.tsx
|
|
9
|
+
import { OperationGenerator as Generator2 } from "@kubb/plugin-oas";
|
|
10
|
+
import { Oas as Oas4 } from "@kubb/plugin-oas/components";
|
|
11
|
+
import { App as App2, createRoot as createRoot2 } from "@kubb/react";
|
|
12
|
+
|
|
13
|
+
// src/components/OasType.tsx
|
|
14
|
+
import { Parser, File, Type, useApp } from "@kubb/react";
|
|
15
|
+
import { useOas } from "@kubb/plugin-oas/hooks";
|
|
16
|
+
import { Fragment, jsx, jsxs } from "@kubb/react/jsx-runtime";
|
|
17
|
+
function Template({ name, typeName, api }) {
|
|
18
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
19
|
+
`export const ${name} = ${JSON.stringify(api, void 0, 2)} as const`,
|
|
20
|
+
/* @__PURE__ */ jsx("br", {}),
|
|
21
|
+
/* @__PURE__ */ jsx(Type, { name: typeName, export: true, children: `Infer<typeof ${name}>` })
|
|
22
|
+
] });
|
|
23
|
+
}
|
|
24
|
+
var defaultTemplates = { default: Template };
|
|
25
|
+
function OasType({ name, typeName, Template: Template2 = defaultTemplates.default }) {
|
|
26
|
+
const oas = useOas();
|
|
27
|
+
return /* @__PURE__ */ jsx(Template2, { name, typeName, api: oas.api });
|
|
28
|
+
}
|
|
29
|
+
OasType.File = function({ name, typeName, templates = defaultTemplates }) {
|
|
30
|
+
const {
|
|
31
|
+
pluginManager,
|
|
32
|
+
plugin: { key: pluginKey }
|
|
33
|
+
} = useApp();
|
|
34
|
+
const file = pluginManager.getFile({ name, extName: ".ts", pluginKey });
|
|
35
|
+
const Template2 = templates.default;
|
|
36
|
+
return /* @__PURE__ */ jsx(Parser, { language: "typescript", children: /* @__PURE__ */ jsxs(File, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
37
|
+
/* @__PURE__ */ jsx(File.Import, { name: ["Infer"], path: "@kubb/oas", isTypeOnly: true }),
|
|
38
|
+
/* @__PURE__ */ jsx(File.Source, { children: /* @__PURE__ */ jsx(OasType, { Template: Template2, name, typeName }) })
|
|
39
|
+
] }) });
|
|
40
|
+
};
|
|
41
|
+
OasType.templates = defaultTemplates;
|
|
42
|
+
|
|
43
|
+
// src/components/OperationSchema.tsx
|
|
44
|
+
import transformers3 from "@kubb/core/transformers";
|
|
45
|
+
import { print as print2 } from "@kubb/parser-ts";
|
|
46
|
+
import * as factory3 from "@kubb/parser-ts/factory";
|
|
47
|
+
import { Oas as Oas3 } from "@kubb/plugin-oas/components";
|
|
48
|
+
import { useOas as useOas2, useOperation, useOperationManager } from "@kubb/plugin-oas/hooks";
|
|
49
|
+
import { File as File3, Parser as Parser2, useApp as useApp3 } from "@kubb/react";
|
|
50
|
+
|
|
51
|
+
// src/SchemaGenerator.tsx
|
|
52
|
+
import { SchemaGenerator as Generator } from "@kubb/plugin-oas";
|
|
53
|
+
import { Oas as Oas2 } from "@kubb/plugin-oas/components";
|
|
54
|
+
import { App, createRoot } from "@kubb/react";
|
|
55
|
+
|
|
56
|
+
// src/components/Schema.tsx
|
|
57
|
+
import { Oas } from "@kubb/plugin-oas/components";
|
|
58
|
+
import { File as File2, useApp as useApp2 } from "@kubb/react";
|
|
59
|
+
import transformers2 from "@kubb/core/transformers";
|
|
60
|
+
import { print } from "@kubb/parser-ts";
|
|
61
|
+
import * as factory2 from "@kubb/parser-ts/factory";
|
|
62
|
+
import { SchemaGenerator, schemaKeywords as schemaKeywords2 } from "@kubb/plugin-oas";
|
|
63
|
+
import { useSchema } from "@kubb/plugin-oas/hooks";
|
|
64
|
+
|
|
65
|
+
// src/parser/index.ts
|
|
66
|
+
import transformers from "@kubb/core/transformers";
|
|
67
|
+
import * as factory from "@kubb/parser-ts/factory";
|
|
68
|
+
import { isKeyword, schemaKeywords } from "@kubb/plugin-oas";
|
|
69
|
+
var typeKeywordMapper = {
|
|
70
|
+
any: () => factory.keywordTypeNodes.any,
|
|
71
|
+
unknown: () => factory.keywordTypeNodes.unknown,
|
|
72
|
+
number: () => factory.keywordTypeNodes.number,
|
|
73
|
+
integer: () => factory.keywordTypeNodes.number,
|
|
74
|
+
object: (nodes) => {
|
|
75
|
+
if (!nodes || !nodes.length) {
|
|
76
|
+
return factory.keywordTypeNodes.object;
|
|
77
|
+
}
|
|
78
|
+
return factory.createTypeLiteralNode(nodes);
|
|
79
|
+
},
|
|
80
|
+
string: () => factory.keywordTypeNodes.string,
|
|
81
|
+
boolean: () => factory.keywordTypeNodes.boolean,
|
|
82
|
+
undefined: () => factory.keywordTypeNodes.undefined,
|
|
83
|
+
nullable: void 0,
|
|
84
|
+
null: () => factory.keywordTypeNodes.null,
|
|
85
|
+
nullish: void 0,
|
|
86
|
+
array: (nodes) => {
|
|
87
|
+
if (!nodes) {
|
|
88
|
+
return void 0;
|
|
89
|
+
}
|
|
90
|
+
return factory.createArrayDeclaration({ nodes });
|
|
91
|
+
},
|
|
92
|
+
tuple: (nodes) => {
|
|
93
|
+
if (!nodes) {
|
|
94
|
+
return void 0;
|
|
95
|
+
}
|
|
96
|
+
return factory.createTupleTypeNode(nodes);
|
|
97
|
+
},
|
|
98
|
+
enum: (name) => {
|
|
99
|
+
if (!name) {
|
|
100
|
+
return void 0;
|
|
101
|
+
}
|
|
102
|
+
return factory.createTypeReferenceNode(name, void 0);
|
|
103
|
+
},
|
|
104
|
+
union: (nodes) => {
|
|
105
|
+
if (!nodes) {
|
|
106
|
+
return void 0;
|
|
107
|
+
}
|
|
108
|
+
return factory.createUnionDeclaration({
|
|
109
|
+
withParentheses: true,
|
|
110
|
+
nodes
|
|
111
|
+
});
|
|
112
|
+
},
|
|
113
|
+
const: (name, format) => {
|
|
114
|
+
if (!name) {
|
|
115
|
+
return void 0;
|
|
116
|
+
}
|
|
117
|
+
if (format === "number") {
|
|
118
|
+
return factory.createLiteralTypeNode(factory.createNumericLiteral(name));
|
|
119
|
+
}
|
|
120
|
+
return factory.createLiteralTypeNode(factory.createStringLiteral(name.toString()));
|
|
121
|
+
},
|
|
122
|
+
datetime: () => factory.keywordTypeNodes.string,
|
|
123
|
+
date: (type = "string") => type === "string" ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier("Date")),
|
|
124
|
+
time: (type = "string") => type === "string" ? factory.keywordTypeNodes.string : factory.createTypeReferenceNode(factory.createIdentifier("Date")),
|
|
125
|
+
uuid: void 0,
|
|
126
|
+
url: void 0,
|
|
127
|
+
strict: void 0,
|
|
128
|
+
default: void 0,
|
|
129
|
+
and: (nodes) => {
|
|
130
|
+
if (!nodes) {
|
|
131
|
+
return void 0;
|
|
132
|
+
}
|
|
133
|
+
return factory.createIntersectionDeclaration({
|
|
134
|
+
withParentheses: true,
|
|
135
|
+
nodes
|
|
136
|
+
});
|
|
137
|
+
},
|
|
138
|
+
describe: void 0,
|
|
139
|
+
min: void 0,
|
|
140
|
+
max: void 0,
|
|
141
|
+
optional: void 0,
|
|
142
|
+
matches: void 0,
|
|
143
|
+
email: void 0,
|
|
144
|
+
firstName: void 0,
|
|
145
|
+
lastName: void 0,
|
|
146
|
+
password: void 0,
|
|
147
|
+
phone: void 0,
|
|
148
|
+
readOnly: void 0,
|
|
149
|
+
ref: (propertyName) => {
|
|
150
|
+
if (!propertyName) {
|
|
151
|
+
return void 0;
|
|
152
|
+
}
|
|
153
|
+
return factory.createTypeReferenceNode(propertyName, void 0);
|
|
154
|
+
},
|
|
155
|
+
blob: () => factory.createTypeReferenceNode("Blob", []),
|
|
156
|
+
deprecated: void 0,
|
|
157
|
+
example: void 0,
|
|
158
|
+
schema: void 0,
|
|
159
|
+
catchall: void 0,
|
|
160
|
+
name: void 0
|
|
161
|
+
};
|
|
162
|
+
function parse(parent, current, options) {
|
|
163
|
+
const value = typeKeywordMapper[current.keyword];
|
|
164
|
+
if (!value) {
|
|
165
|
+
return void 0;
|
|
166
|
+
}
|
|
167
|
+
if (isKeyword(current, schemaKeywords.union)) {
|
|
168
|
+
return typeKeywordMapper.union(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
|
|
169
|
+
}
|
|
170
|
+
if (isKeyword(current, schemaKeywords.and)) {
|
|
171
|
+
return typeKeywordMapper.and(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
|
|
172
|
+
}
|
|
173
|
+
if (isKeyword(current, schemaKeywords.array)) {
|
|
174
|
+
return typeKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
|
|
175
|
+
}
|
|
176
|
+
if (isKeyword(current, schemaKeywords.enum)) {
|
|
177
|
+
return typeKeywordMapper.enum(current.args.typeName);
|
|
178
|
+
}
|
|
179
|
+
if (isKeyword(current, schemaKeywords.ref)) {
|
|
180
|
+
return typeKeywordMapper.ref(current.args.name);
|
|
181
|
+
}
|
|
182
|
+
if (isKeyword(current, schemaKeywords.blob)) {
|
|
183
|
+
return value();
|
|
184
|
+
}
|
|
185
|
+
if (isKeyword(current, schemaKeywords.tuple)) {
|
|
186
|
+
return typeKeywordMapper.tuple(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
|
|
187
|
+
}
|
|
188
|
+
if (isKeyword(current, schemaKeywords.const)) {
|
|
189
|
+
return typeKeywordMapper.const(current.args.name, current.args.format);
|
|
190
|
+
}
|
|
191
|
+
if (isKeyword(current, schemaKeywords.object)) {
|
|
192
|
+
const properties = Object.entries(current.args?.properties || {}).filter((item) => {
|
|
193
|
+
const schemas = item[1];
|
|
194
|
+
return schemas && typeof schemas.map === "function";
|
|
195
|
+
}).map(([name, schemas]) => {
|
|
196
|
+
const nameSchema = schemas.find((schema) => schema.keyword === schemaKeywords.name);
|
|
197
|
+
const mappedName = nameSchema?.args || name;
|
|
198
|
+
if (options.mapper?.[mappedName]) {
|
|
199
|
+
return options.mapper?.[mappedName];
|
|
200
|
+
}
|
|
201
|
+
const isNullish = schemas.some((schema) => schema.keyword === schemaKeywords.nullish);
|
|
202
|
+
const isNullable = schemas.some((schema) => schema.keyword === schemaKeywords.nullable);
|
|
203
|
+
const isOptional = schemas.some((schema) => schema.keyword === schemaKeywords.optional);
|
|
204
|
+
const isReadonly = schemas.some((schema) => schema.keyword === schemaKeywords.readOnly);
|
|
205
|
+
const describeSchema = schemas.find((schema) => schema.keyword === schemaKeywords.describe);
|
|
206
|
+
const deprecatedSchema = schemas.find((schema) => schema.keyword === schemaKeywords.deprecated);
|
|
207
|
+
const defaultSchema = schemas.find((schema) => schema.keyword === schemaKeywords.default);
|
|
208
|
+
const exampleSchema = schemas.find((schema) => schema.keyword === schemaKeywords.example);
|
|
209
|
+
const schemaSchema = schemas.find((schema) => schema.keyword === schemaKeywords.schema);
|
|
210
|
+
let type = schemas.map((schema) => parse(current, schema, options)).filter(Boolean)[0];
|
|
211
|
+
if (isNullable) {
|
|
212
|
+
type = factory.createUnionDeclaration({
|
|
213
|
+
nodes: [type, factory.keywordTypeNodes.null]
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
if (isNullish && ["undefined", "questionTokenAndUndefined"].includes(options.optionalType)) {
|
|
217
|
+
type = factory.createUnionDeclaration({
|
|
218
|
+
nodes: [type, factory.keywordTypeNodes.undefined]
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
if (isOptional && ["undefined", "questionTokenAndUndefined"].includes(options.optionalType)) {
|
|
222
|
+
type = factory.createUnionDeclaration({
|
|
223
|
+
nodes: [type, factory.keywordTypeNodes.undefined]
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
const propertySignature = factory.createPropertySignature({
|
|
227
|
+
questionToken: isOptional || isNullish ? ["questionToken", "questionTokenAndUndefined"].includes(options.optionalType) : false,
|
|
228
|
+
name: mappedName,
|
|
229
|
+
type,
|
|
230
|
+
readOnly: isReadonly
|
|
231
|
+
});
|
|
232
|
+
return factory.appendJSDocToNode({
|
|
233
|
+
node: propertySignature,
|
|
234
|
+
comments: [
|
|
235
|
+
describeSchema ? `@description ${transformers.jsStringEscape(describeSchema.args)}` : void 0,
|
|
236
|
+
deprecatedSchema ? "@deprecated" : void 0,
|
|
237
|
+
defaultSchema ? `@default ${defaultSchema.args}` : void 0,
|
|
238
|
+
exampleSchema ? `@example ${exampleSchema.args}` : void 0,
|
|
239
|
+
schemaSchema?.args?.type || schemaSchema?.args?.format ? [`@type ${schemaSchema?.args?.type || "unknown"}${!isOptional ? "" : " | undefined"}`, schemaSchema?.args?.format].filter(Boolean).join(", ") : void 0
|
|
240
|
+
].filter(Boolean)
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
const additionalProperties = current.args?.additionalProperties?.length ? factory.createIndexSignature(
|
|
244
|
+
current.args.additionalProperties.map((schema) => parse(current, schema, options)).filter(Boolean).at(0)
|
|
245
|
+
) : void 0;
|
|
246
|
+
return typeKeywordMapper.object([...properties, additionalProperties].filter(Boolean));
|
|
247
|
+
}
|
|
248
|
+
if (isKeyword(current, schemaKeywords.datetime)) {
|
|
249
|
+
return typeKeywordMapper.datetime();
|
|
250
|
+
}
|
|
251
|
+
if (isKeyword(current, schemaKeywords.date)) {
|
|
252
|
+
return typeKeywordMapper.date(current.args.type);
|
|
253
|
+
}
|
|
254
|
+
if (isKeyword(current, schemaKeywords.time)) {
|
|
255
|
+
return typeKeywordMapper.time(current.args.type);
|
|
256
|
+
}
|
|
257
|
+
if (current.keyword in typeKeywordMapper) {
|
|
258
|
+
return value();
|
|
259
|
+
}
|
|
260
|
+
return void 0;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// src/components/Schema.tsx
|
|
264
|
+
import { jsx as jsx2 } from "@kubb/react/jsx-runtime";
|
|
265
|
+
function Schema(props) {
|
|
266
|
+
const { keysToOmit, description } = props;
|
|
267
|
+
const { tree, name } = useSchema();
|
|
268
|
+
const {
|
|
269
|
+
pluginManager,
|
|
270
|
+
plugin: {
|
|
271
|
+
options: { mapper, enumType, optionalType }
|
|
272
|
+
}
|
|
273
|
+
} = useApp2();
|
|
274
|
+
const resolvedName = pluginManager.resolveName({
|
|
275
|
+
name,
|
|
276
|
+
pluginKey: [pluginTsName],
|
|
277
|
+
type: "function"
|
|
278
|
+
});
|
|
279
|
+
const typeName = pluginManager.resolveName({
|
|
280
|
+
name,
|
|
281
|
+
pluginKey: [pluginTsName],
|
|
282
|
+
type: "type"
|
|
283
|
+
});
|
|
284
|
+
const nodes = [];
|
|
285
|
+
const extraNodes = [];
|
|
286
|
+
if (!tree.length) {
|
|
287
|
+
return "";
|
|
288
|
+
}
|
|
289
|
+
const isNullish = tree.some((item) => item.keyword === schemaKeywords2.nullish);
|
|
290
|
+
const isNullable = tree.some((item) => item.keyword === schemaKeywords2.nullable);
|
|
291
|
+
const isOptional = tree.some((item) => item.keyword === schemaKeywords2.optional);
|
|
292
|
+
let type = tree.map((schema) => parse(void 0, schema, { name: resolvedName, typeName, description, keysToOmit, optionalType, enumType, mapper })).filter(Boolean).at(0) || typeKeywordMapper.undefined();
|
|
293
|
+
if (isNullable) {
|
|
294
|
+
type = factory2.createUnionDeclaration({
|
|
295
|
+
nodes: [type, factory2.keywordTypeNodes.null]
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
if (isNullish && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
|
|
299
|
+
type = factory2.createUnionDeclaration({
|
|
300
|
+
nodes: [type, factory2.keywordTypeNodes.undefined]
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
if (isOptional && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
|
|
304
|
+
type = factory2.createUnionDeclaration({
|
|
305
|
+
nodes: [type, factory2.keywordTypeNodes.undefined]
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
const node = factory2.createTypeAliasDeclaration({
|
|
309
|
+
modifiers: [factory2.modifiers.export],
|
|
310
|
+
name: resolvedName,
|
|
311
|
+
type: keysToOmit?.length ? factory2.createOmitDeclaration({
|
|
312
|
+
keys: keysToOmit,
|
|
313
|
+
type,
|
|
314
|
+
nonNullable: true
|
|
315
|
+
}) : type
|
|
316
|
+
});
|
|
317
|
+
const enumSchemas = SchemaGenerator.deepSearch(tree, schemaKeywords2.enum);
|
|
318
|
+
if (enumSchemas) {
|
|
319
|
+
enumSchemas.forEach((enumSchema) => {
|
|
320
|
+
extraNodes.push(
|
|
321
|
+
...factory2.createEnumDeclaration({
|
|
322
|
+
name: transformers2.camelCase(enumSchema.args.name),
|
|
323
|
+
typeName: enumSchema.args.typeName,
|
|
324
|
+
enums: enumSchema.args.items.map((item) => item.value === void 0 ? void 0 : [transformers2.trimQuotes(item.name?.toString()), item.value]).filter(Boolean),
|
|
325
|
+
type: enumType
|
|
326
|
+
})
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
nodes.push(
|
|
331
|
+
factory2.appendJSDocToNode({
|
|
332
|
+
node,
|
|
333
|
+
comments: [description ? `@description ${transformers2.jsStringEscape(description)}` : void 0].filter(Boolean)
|
|
334
|
+
})
|
|
335
|
+
);
|
|
336
|
+
const filterdNodes = nodes.filter(
|
|
337
|
+
(node2) => !extraNodes.some(
|
|
338
|
+
(extraNode) => extraNode?.name?.escapedText === node2?.name?.escapedText
|
|
339
|
+
)
|
|
340
|
+
);
|
|
341
|
+
return print([...extraNodes, ...filterdNodes]);
|
|
342
|
+
}
|
|
343
|
+
Schema.File = function({}) {
|
|
344
|
+
const { pluginManager } = useApp2();
|
|
345
|
+
const { schema } = useSchema();
|
|
346
|
+
return /* @__PURE__ */ jsx2(Oas.Schema.File, { output: pluginManager.config.output.path, children: /* @__PURE__ */ jsx2(File2.Source, { children: /* @__PURE__ */ jsx2(Schema, { description: schema?.description }) }) });
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
// src/SchemaGenerator.tsx
|
|
350
|
+
import { jsx as jsx3 } from "@kubb/react/jsx-runtime";
|
|
351
|
+
var SchemaGenerator2 = class extends Generator {
|
|
352
|
+
async schema(name, schema, options) {
|
|
353
|
+
const { oas, pluginManager, plugin, mode, output } = this.context;
|
|
354
|
+
const root = createRoot({
|
|
355
|
+
logger: pluginManager.logger
|
|
356
|
+
});
|
|
357
|
+
const tree = this.parse({ schema, name });
|
|
358
|
+
root.render(
|
|
359
|
+
/* @__PURE__ */ jsx3(App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx3(Oas2, { oas, children: /* @__PURE__ */ jsx3(Oas2.Schema, { name, value: schema, tree, children: /* @__PURE__ */ jsx3(Schema.File, {}) }) }) })
|
|
360
|
+
);
|
|
361
|
+
return root.files;
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
// src/components/OperationSchema.tsx
|
|
366
|
+
import { jsx as jsx4, jsxs as jsxs2 } from "@kubb/react/jsx-runtime";
|
|
367
|
+
function printCombinedSchema({
|
|
368
|
+
name,
|
|
369
|
+
operation,
|
|
370
|
+
schemas,
|
|
371
|
+
pluginManager
|
|
372
|
+
}) {
|
|
373
|
+
const properties = {};
|
|
374
|
+
if (schemas.response) {
|
|
375
|
+
const identifier = pluginManager.resolveName({
|
|
376
|
+
name: schemas.response.name,
|
|
377
|
+
pluginKey: [pluginTsName],
|
|
378
|
+
type: "function"
|
|
379
|
+
});
|
|
380
|
+
properties["response"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
|
|
381
|
+
}
|
|
382
|
+
if (schemas.request) {
|
|
383
|
+
const identifier = pluginManager.resolveName({
|
|
384
|
+
name: schemas.request.name,
|
|
385
|
+
pluginKey: [pluginTsName],
|
|
386
|
+
type: "function"
|
|
387
|
+
});
|
|
388
|
+
properties["request"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
|
|
389
|
+
}
|
|
390
|
+
if (schemas.pathParams) {
|
|
391
|
+
const identifier = pluginManager.resolveName({
|
|
392
|
+
name: schemas.pathParams.name,
|
|
393
|
+
pluginKey: [pluginTsName],
|
|
394
|
+
type: "function"
|
|
395
|
+
});
|
|
396
|
+
properties["pathParams"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
|
|
397
|
+
}
|
|
398
|
+
if (schemas.queryParams) {
|
|
399
|
+
const identifier = pluginManager.resolveName({
|
|
400
|
+
name: schemas.queryParams.name,
|
|
401
|
+
pluginKey: [pluginTsName],
|
|
402
|
+
type: "function"
|
|
403
|
+
});
|
|
404
|
+
properties["queryParams"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
|
|
405
|
+
}
|
|
406
|
+
if (schemas.headerParams) {
|
|
407
|
+
const identifier = pluginManager.resolveName({
|
|
408
|
+
name: schemas.headerParams.name,
|
|
409
|
+
pluginKey: [pluginTsName],
|
|
410
|
+
type: "function"
|
|
411
|
+
});
|
|
412
|
+
properties["headerParams"] = factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
|
|
413
|
+
}
|
|
414
|
+
if (schemas.errors) {
|
|
415
|
+
properties["errors"] = factory3.createUnionDeclaration({
|
|
416
|
+
nodes: schemas.errors.map((error) => {
|
|
417
|
+
const identifier = pluginManager.resolveName({
|
|
418
|
+
name: error.name,
|
|
419
|
+
pluginKey: [pluginTsName],
|
|
420
|
+
type: "function"
|
|
421
|
+
});
|
|
422
|
+
return factory3.createTypeReferenceNode(factory3.createIdentifier(identifier), void 0);
|
|
423
|
+
})
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
const namespaceNode = factory3.createTypeAliasDeclaration({
|
|
427
|
+
name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
|
|
428
|
+
type: factory3.createTypeLiteralNode(
|
|
429
|
+
Object.keys(properties).map((key) => {
|
|
430
|
+
const type = properties[key];
|
|
431
|
+
if (!type) {
|
|
432
|
+
return void 0;
|
|
433
|
+
}
|
|
434
|
+
return factory3.createPropertySignature({
|
|
435
|
+
name: transformers3.pascalCase(key),
|
|
436
|
+
type
|
|
437
|
+
});
|
|
438
|
+
}).filter(Boolean)
|
|
439
|
+
),
|
|
440
|
+
modifiers: [factory3.modifiers.export]
|
|
441
|
+
});
|
|
442
|
+
return print2(namespaceNode);
|
|
443
|
+
}
|
|
444
|
+
function OperationSchema({ keysToOmit, description }) {
|
|
445
|
+
return /* @__PURE__ */ jsx4(Schema, { keysToOmit, description });
|
|
446
|
+
}
|
|
447
|
+
OperationSchema.File = function({}) {
|
|
448
|
+
const { pluginManager, plugin, mode, fileManager } = useApp3();
|
|
449
|
+
const oas = useOas2();
|
|
450
|
+
const { getSchemas, getFile, getName } = useOperationManager();
|
|
451
|
+
const operation = useOperation();
|
|
452
|
+
const file = getFile(operation);
|
|
453
|
+
const schemas = getSchemas(operation);
|
|
454
|
+
const factoryName = getName(operation, { type: "type" });
|
|
455
|
+
const generator = new SchemaGenerator2(plugin.options, {
|
|
456
|
+
oas,
|
|
457
|
+
plugin,
|
|
458
|
+
pluginManager,
|
|
459
|
+
mode,
|
|
460
|
+
override: plugin.options.override
|
|
461
|
+
});
|
|
462
|
+
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
|
|
463
|
+
const mapItem = ({ name, schema, description, keysToOmit, ...options }, i) => {
|
|
464
|
+
const tree = generator.parse({ schema, name });
|
|
465
|
+
return /* @__PURE__ */ jsxs2(Oas3.Schema, { name, value: schema, tree, children: [
|
|
466
|
+
mode === "split" && /* @__PURE__ */ jsx4(Oas3.Schema.Imports, { extName: plugin.options.extName, isTypeOnly: true }),
|
|
467
|
+
/* @__PURE__ */ jsx4(File3.Source, { children: /* @__PURE__ */ jsx4(OperationSchema, { description, keysToOmit }) })
|
|
468
|
+
] }, i);
|
|
469
|
+
};
|
|
470
|
+
return /* @__PURE__ */ jsx4(Parser2, { language: "typescript", children: /* @__PURE__ */ jsxs2(File3, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
|
471
|
+
items.map(mapItem),
|
|
472
|
+
/* @__PURE__ */ jsx4(File3.Source, { children: printCombinedSchema({ name: factoryName, operation, schemas, pluginManager }) })
|
|
473
|
+
] }) });
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
// src/OperationGenerator.tsx
|
|
477
|
+
import { jsx as jsx5 } from "@kubb/react/jsx-runtime";
|
|
478
|
+
var OperationGenerator = class extends Generator2 {
|
|
479
|
+
async all(operations) {
|
|
480
|
+
const { oas, pluginManager, plugin, mode } = this.context;
|
|
481
|
+
const root = createRoot2({
|
|
482
|
+
logger: pluginManager.logger
|
|
483
|
+
});
|
|
484
|
+
root.render(
|
|
485
|
+
/* @__PURE__ */ jsx5(App2, { pluginManager, plugin, mode, children: /* @__PURE__ */ jsx5(Oas4, { oas, operations, generator: this, children: plugin.options.oasType && /* @__PURE__ */ jsx5(OasType.File, { name: "oas", typeName: "Oas" }) }) })
|
|
486
|
+
);
|
|
487
|
+
return root.files;
|
|
488
|
+
}
|
|
489
|
+
async operation(operation, options) {
|
|
490
|
+
const { oas, pluginManager, plugin, mode } = this.context;
|
|
491
|
+
const root = createRoot2({
|
|
492
|
+
logger: pluginManager.logger
|
|
493
|
+
});
|
|
494
|
+
root.render(
|
|
495
|
+
/* @__PURE__ */ jsx5(App2, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx5(Oas4, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ jsx5(Oas4.Operation, { operation, children: /* @__PURE__ */ jsx5(OperationSchema.File, {}) }) }) })
|
|
496
|
+
);
|
|
497
|
+
return root.files;
|
|
498
|
+
}
|
|
499
|
+
};
|
|
500
|
+
|
|
501
|
+
// src/plugin.ts
|
|
502
|
+
var pluginTsName = "plugin-ts";
|
|
503
|
+
var pluginTs = createPlugin((options) => {
|
|
504
|
+
const {
|
|
505
|
+
output = { path: "types" },
|
|
506
|
+
group,
|
|
507
|
+
exclude = [],
|
|
508
|
+
include,
|
|
509
|
+
override = [],
|
|
510
|
+
enumType = "asConst",
|
|
511
|
+
enumSuffix = "",
|
|
512
|
+
dateType = "string",
|
|
513
|
+
unknownType = "any",
|
|
514
|
+
optionalType = "questionToken",
|
|
515
|
+
transformers: transformers4 = {},
|
|
516
|
+
oasType = false,
|
|
517
|
+
mapper = {}
|
|
518
|
+
} = options;
|
|
519
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
|
|
520
|
+
return {
|
|
521
|
+
name: pluginTsName,
|
|
522
|
+
options: {
|
|
523
|
+
extName: output.extName,
|
|
524
|
+
transformers: transformers4,
|
|
525
|
+
dateType,
|
|
526
|
+
optionalType,
|
|
527
|
+
oasType,
|
|
528
|
+
enumType,
|
|
529
|
+
enumSuffix,
|
|
530
|
+
// keep the used enumnames between SchemaGenerator and OperationGenerator per plugin(pluginKey)
|
|
531
|
+
usedEnumNames: {},
|
|
532
|
+
unknownType,
|
|
533
|
+
override,
|
|
534
|
+
mapper
|
|
535
|
+
},
|
|
536
|
+
pre: [pluginOasName],
|
|
537
|
+
resolvePath(baseName, pathMode, options2) {
|
|
538
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
|
539
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
|
|
540
|
+
if (mode === "single") {
|
|
541
|
+
return path.resolve(root, output.path);
|
|
542
|
+
}
|
|
543
|
+
if (options2?.tag && group?.type === "tag") {
|
|
544
|
+
const tag = camelCase(options2.tag);
|
|
545
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName);
|
|
546
|
+
}
|
|
547
|
+
return path.resolve(root, output.path, baseName);
|
|
548
|
+
},
|
|
549
|
+
resolveName(name, type) {
|
|
550
|
+
const resolvedName = pascalCase(name, { isFile: type === "file" });
|
|
551
|
+
if (type) {
|
|
552
|
+
return transformers4?.name?.(resolvedName, type) || resolvedName;
|
|
553
|
+
}
|
|
554
|
+
return resolvedName;
|
|
555
|
+
},
|
|
556
|
+
async writeFile(path2, source) {
|
|
557
|
+
if (!path2.endsWith(".ts") || !source) {
|
|
558
|
+
return;
|
|
559
|
+
}
|
|
560
|
+
return this.fileManager.write(path2, source, { sanity: false });
|
|
561
|
+
},
|
|
562
|
+
async buildStart() {
|
|
563
|
+
const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]);
|
|
564
|
+
const oas = await swaggerPlugin.api.getOas();
|
|
565
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
|
566
|
+
const mode = FileManager.getMode(path.resolve(root, output.path));
|
|
567
|
+
const schemaGenerator = new SchemaGenerator2(this.plugin.options, {
|
|
568
|
+
oas,
|
|
569
|
+
pluginManager: this.pluginManager,
|
|
570
|
+
plugin: this.plugin,
|
|
571
|
+
contentType: swaggerPlugin.api.contentType,
|
|
572
|
+
include: void 0,
|
|
573
|
+
override,
|
|
574
|
+
mode,
|
|
575
|
+
output: output.path
|
|
576
|
+
});
|
|
577
|
+
const schemaFiles = await schemaGenerator.build();
|
|
578
|
+
await this.addFile(...schemaFiles);
|
|
579
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
|
580
|
+
oas,
|
|
581
|
+
pluginManager: this.pluginManager,
|
|
582
|
+
plugin: this.plugin,
|
|
583
|
+
contentType: swaggerPlugin.api.contentType,
|
|
584
|
+
exclude,
|
|
585
|
+
include,
|
|
586
|
+
override,
|
|
587
|
+
mode
|
|
588
|
+
});
|
|
589
|
+
const operationFiles = await operationGenerator.build();
|
|
590
|
+
await this.addFile(...operationFiles);
|
|
591
|
+
},
|
|
592
|
+
async buildEnd() {
|
|
593
|
+
if (this.config.output.write === false) {
|
|
594
|
+
return;
|
|
595
|
+
}
|
|
596
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
|
597
|
+
await this.fileManager.addIndexes({
|
|
598
|
+
root,
|
|
599
|
+
output,
|
|
600
|
+
meta: { pluginKey: this.plugin.key },
|
|
601
|
+
logger: this.logger
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
});
|
|
606
|
+
|
|
607
|
+
// src/index.ts
|
|
608
|
+
var definePluginDefault = pluginTs;
|
|
609
|
+
var definePlugin = pluginTs;
|
|
610
|
+
var src_default = definePluginDefault;
|
|
611
|
+
|
|
612
|
+
export {
|
|
613
|
+
OasType,
|
|
614
|
+
Schema,
|
|
615
|
+
OperationSchema,
|
|
616
|
+
pluginTsName,
|
|
617
|
+
pluginTs,
|
|
618
|
+
definePlugin,
|
|
619
|
+
src_default
|
|
620
|
+
};
|
|
621
|
+
//# sourceMappingURL=chunk-WOQIHEJC.js.map
|