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