@kubb/swagger-ts 2.18.4 → 2.18.6
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-H35DL3CM.cjs → chunk-DRW5SRC6.cjs} +278 -243
- package/dist/chunk-DRW5SRC6.cjs.map +1 -0
- package/dist/{chunk-NOXQZTV4.js → chunk-UJR52JIE.js} +288 -253
- package/dist/chunk-UJR52JIE.js.map +1 -0
- package/dist/components.cjs +4 -2
- package/dist/components.d.cts +20 -7
- package/dist/components.d.ts +20 -7
- package/dist/components.js +5 -3
- package/dist/index.cjs +3 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +4 -7
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
- package/src/OperationGenerator.tsx +1 -19
- package/src/SchemaGenerator.tsx +10 -48
- package/src/components/OperationSchema.tsx +62 -20
- package/src/components/Schema.tsx +135 -0
- package/src/components/__snapshots__/Schema/pets.ts +0 -4
- package/src/components/__snapshots__/Schema/showPetById.ts +0 -4
- package/src/components/index.ts +1 -0
- package/src/{typeParser.ts → parser/index.ts} +8 -90
- package/src/plugin.ts +3 -3
- package/src/types.ts +2 -1
- package/dist/chunk-H35DL3CM.cjs.map +0 -1
- package/dist/chunk-NOXQZTV4.js.map +0 -1
@@ -1,3 +1,15 @@
|
|
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
|
+
|
1
13
|
// src/components/OasType.tsx
|
2
14
|
import { Parser, File, Type, useApp } from "@kubb/react";
|
3
15
|
import { useOas } from "@kubb/plugin-oas/hooks";
|
@@ -29,176 +41,31 @@ OasType.File = function({ name, typeName, templates = defaultTemplates }) {
|
|
29
41
|
OasType.templates = defaultTemplates;
|
30
42
|
|
31
43
|
// src/components/OperationSchema.tsx
|
32
|
-
import
|
44
|
+
import transformers3 from "@kubb/core/transformers";
|
33
45
|
import { print as print2 } from "@kubb/parser-ts";
|
34
|
-
import * as
|
35
|
-
import { Parser as Parser2, File as File2, useApp as useApp2 } from "@kubb/react";
|
46
|
+
import * as factory3 from "@kubb/parser-ts/factory";
|
36
47
|
import { Oas as Oas3 } from "@kubb/plugin-oas/components";
|
37
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";
|
38
50
|
|
39
51
|
// src/SchemaGenerator.tsx
|
40
|
-
import {
|
41
|
-
import { SchemaGenerator as Generator2 } from "@kubb/plugin-oas";
|
52
|
+
import { SchemaGenerator as Generator } from "@kubb/plugin-oas";
|
42
53
|
import { Oas as Oas2 } from "@kubb/plugin-oas/components";
|
43
|
-
|
44
|
-
// src/plugin.ts
|
45
|
-
import path from "path";
|
46
|
-
import { FileManager, PluginManager, createPlugin } from "@kubb/core";
|
47
|
-
import { camelCase, pascalCase } from "@kubb/core/transformers";
|
48
|
-
import { renderTemplate } from "@kubb/core/utils";
|
49
|
-
import { pluginOasName } from "@kubb/plugin-oas";
|
50
|
-
|
51
|
-
// src/OperationGenerator.tsx
|
52
54
|
import { App, createRoot } from "@kubb/react";
|
53
|
-
import { OperationGenerator as Generator } from "@kubb/plugin-oas";
|
54
|
-
import { Oas } from "@kubb/plugin-oas/components";
|
55
|
-
import { jsx as jsx2 } from "@kubb/react/jsx-runtime";
|
56
|
-
var OperationGenerator = class extends Generator {
|
57
|
-
async all(operations) {
|
58
|
-
const { oas, pluginManager, plugin, mode } = this.context;
|
59
|
-
const root = createRoot({
|
60
|
-
logger: pluginManager.logger
|
61
|
-
});
|
62
|
-
root.render(
|
63
|
-
/* @__PURE__ */ jsx2(App, { pluginManager, plugin, mode, children: /* @__PURE__ */ jsx2(Oas, { oas, operations, generator: this, children: plugin.options.oasType && /* @__PURE__ */ jsx2(OasType.File, { name: "oas", typeName: "Oas" }) }) })
|
64
|
-
);
|
65
|
-
return root.files;
|
66
|
-
}
|
67
|
-
async operation(operation, options) {
|
68
|
-
const { oas, pluginManager, plugin, mode } = this.context;
|
69
|
-
const root = createRoot({
|
70
|
-
logger: pluginManager.logger
|
71
|
-
});
|
72
|
-
root.render(
|
73
|
-
/* @__PURE__ */ jsx2(App, { pluginManager, plugin: { ...plugin, options }, mode, children: /* @__PURE__ */ jsx2(Oas, { oas, operations: [operation], generator: this, children: /* @__PURE__ */ jsx2(Oas.Operation, { operation, children: /* @__PURE__ */ jsx2(OperationSchema.File, {}) }) }) })
|
74
|
-
);
|
75
|
-
return root.files;
|
76
|
-
}
|
77
|
-
async get() {
|
78
|
-
return null;
|
79
|
-
}
|
80
|
-
async post() {
|
81
|
-
return null;
|
82
|
-
}
|
83
|
-
async put() {
|
84
|
-
return null;
|
85
|
-
}
|
86
|
-
async patch() {
|
87
|
-
return null;
|
88
|
-
}
|
89
|
-
async delete() {
|
90
|
-
return null;
|
91
|
-
}
|
92
|
-
};
|
93
55
|
|
94
|
-
// src/
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
override = [],
|
103
|
-
enumType = "asConst",
|
104
|
-
enumSuffix = "",
|
105
|
-
dateType = "string",
|
106
|
-
unknownType = "any",
|
107
|
-
optionalType = "questionToken",
|
108
|
-
transformers: transformers3 = {},
|
109
|
-
oasType = false
|
110
|
-
} = options;
|
111
|
-
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
|
112
|
-
return {
|
113
|
-
name: pluginTsName,
|
114
|
-
options: {
|
115
|
-
transformers: transformers3,
|
116
|
-
dateType,
|
117
|
-
optionalType,
|
118
|
-
oasType,
|
119
|
-
enumType,
|
120
|
-
enumSuffix,
|
121
|
-
// keep the used enumnames between SchemaGenerator and OperationGenerator per plugin(pluginKey)
|
122
|
-
usedEnumNames: {},
|
123
|
-
unknownType,
|
124
|
-
override
|
125
|
-
},
|
126
|
-
pre: [pluginOasName],
|
127
|
-
resolvePath(baseName, pathMode, options2) {
|
128
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
129
|
-
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
|
130
|
-
if (mode === "single") {
|
131
|
-
return path.resolve(root, output.path);
|
132
|
-
}
|
133
|
-
if (options2?.tag && group?.type === "tag") {
|
134
|
-
const tag = camelCase(options2.tag);
|
135
|
-
return path.resolve(root, renderTemplate(template, { tag }), baseName);
|
136
|
-
}
|
137
|
-
return path.resolve(root, output.path, baseName);
|
138
|
-
},
|
139
|
-
resolveName(name, type) {
|
140
|
-
const resolvedName = pascalCase(name, { isFile: type === "file" });
|
141
|
-
if (type) {
|
142
|
-
return transformers3?.name?.(resolvedName, type) || resolvedName;
|
143
|
-
}
|
144
|
-
return resolvedName;
|
145
|
-
},
|
146
|
-
async writeFile(source, writePath) {
|
147
|
-
if (!writePath.endsWith(".ts") || !source) {
|
148
|
-
return;
|
149
|
-
}
|
150
|
-
return this.fileManager.write(source, writePath, { sanity: false });
|
151
|
-
},
|
152
|
-
async buildStart() {
|
153
|
-
const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]);
|
154
|
-
const oas = await swaggerPlugin.api.getOas();
|
155
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
156
|
-
const mode = FileManager.getMode(path.resolve(root, output.path));
|
157
|
-
const schemaGenerator = new SchemaGenerator(this.plugin.options, {
|
158
|
-
oas,
|
159
|
-
pluginManager: this.pluginManager,
|
160
|
-
plugin: this.plugin,
|
161
|
-
contentType: swaggerPlugin.api.contentType,
|
162
|
-
include: void 0,
|
163
|
-
override,
|
164
|
-
mode,
|
165
|
-
output: output.path
|
166
|
-
});
|
167
|
-
const schemaFiles = await schemaGenerator.build();
|
168
|
-
await this.addFile(...schemaFiles);
|
169
|
-
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
170
|
-
oas,
|
171
|
-
pluginManager: this.pluginManager,
|
172
|
-
plugin: this.plugin,
|
173
|
-
contentType: swaggerPlugin.api.contentType,
|
174
|
-
exclude,
|
175
|
-
include,
|
176
|
-
override,
|
177
|
-
mode
|
178
|
-
});
|
179
|
-
const operationFiles = await operationGenerator.build();
|
180
|
-
await this.addFile(...operationFiles);
|
181
|
-
},
|
182
|
-
async buildEnd() {
|
183
|
-
if (this.config.output.write === false) {
|
184
|
-
return;
|
185
|
-
}
|
186
|
-
const root = path.resolve(this.config.root, this.config.output.path);
|
187
|
-
await this.fileManager.addIndexes({
|
188
|
-
root,
|
189
|
-
output,
|
190
|
-
meta: { pluginKey: this.plugin.key },
|
191
|
-
logger: this.logger
|
192
|
-
});
|
193
|
-
}
|
194
|
-
};
|
195
|
-
});
|
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";
|
196
64
|
|
197
|
-
// src/
|
65
|
+
// src/parser/index.ts
|
198
66
|
import transformers from "@kubb/core/transformers";
|
199
|
-
import { print } from "@kubb/parser-ts";
|
200
67
|
import * as factory from "@kubb/parser-ts/factory";
|
201
|
-
import {
|
68
|
+
import { isKeyword, schemaKeywords } from "@kubb/plugin-oas";
|
202
69
|
var typeKeywordMapper = {
|
203
70
|
any: () => factory.keywordTypeNodes.any,
|
204
71
|
unknown: () => factory.keywordTypeNodes.unknown,
|
@@ -292,19 +159,19 @@ var typeKeywordMapper = {
|
|
292
159
|
catchall: void 0,
|
293
160
|
name: void 0
|
294
161
|
};
|
295
|
-
function
|
162
|
+
function parse(parent, current, options) {
|
296
163
|
const value = typeKeywordMapper[current.keyword];
|
297
164
|
if (!value) {
|
298
165
|
return void 0;
|
299
166
|
}
|
300
167
|
if (isKeyword(current, schemaKeywords.union)) {
|
301
|
-
return typeKeywordMapper.union(current.args.map((schema) =>
|
168
|
+
return typeKeywordMapper.union(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
|
302
169
|
}
|
303
170
|
if (isKeyword(current, schemaKeywords.and)) {
|
304
|
-
return typeKeywordMapper.and(current.args.map((schema) =>
|
171
|
+
return typeKeywordMapper.and(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
|
305
172
|
}
|
306
173
|
if (isKeyword(current, schemaKeywords.array)) {
|
307
|
-
return typeKeywordMapper.array(current.args.items.map((schema) =>
|
174
|
+
return typeKeywordMapper.array(current.args.items.map((schema) => parse(current, schema, options)).filter(Boolean));
|
308
175
|
}
|
309
176
|
if (isKeyword(current, schemaKeywords.enum)) {
|
310
177
|
return typeKeywordMapper.enum(current.args.typeName);
|
@@ -316,7 +183,7 @@ function parseTypeMeta(parent, current, options) {
|
|
316
183
|
return value();
|
317
184
|
}
|
318
185
|
if (isKeyword(current, schemaKeywords.tuple)) {
|
319
|
-
return typeKeywordMapper.tuple(current.args.map((schema) =>
|
186
|
+
return typeKeywordMapper.tuple(current.args.map((schema) => parse(current, schema, options)).filter(Boolean));
|
320
187
|
}
|
321
188
|
if (isKeyword(current, schemaKeywords.const)) {
|
322
189
|
return typeKeywordMapper.const(current.args.name, current.args.format);
|
@@ -340,7 +207,7 @@ function parseTypeMeta(parent, current, options) {
|
|
340
207
|
const defaultSchema = schemas.find((schema) => schema.keyword === schemaKeywords.default);
|
341
208
|
const exampleSchema = schemas.find((schema) => schema.keyword === schemaKeywords.example);
|
342
209
|
const schemaSchema = schemas.find((schema) => schema.keyword === schemaKeywords.schema);
|
343
|
-
let type = schemas.map((schema) =>
|
210
|
+
let type = schemas.map((schema) => parse(current, schema, options)).filter(Boolean)[0];
|
344
211
|
if (isNullable) {
|
345
212
|
type = factory.createUnionDeclaration({
|
346
213
|
nodes: [type, factory.keywordTypeNodes.null]
|
@@ -374,7 +241,7 @@ function parseTypeMeta(parent, current, options) {
|
|
374
241
|
});
|
375
242
|
});
|
376
243
|
const additionalProperties = current.args?.additionalProperties?.length ? factory.createIndexSignature(
|
377
|
-
current.args.additionalProperties.map((schema) =>
|
244
|
+
current.args.additionalProperties.map((schema) => parse(current, schema, options)).filter(Boolean).at(0)
|
378
245
|
) : void 0;
|
379
246
|
return typeKeywordMapper.object([...properties, additionalProperties].filter(Boolean));
|
380
247
|
}
|
@@ -392,57 +259,78 @@ function parseTypeMeta(parent, current, options) {
|
|
392
259
|
}
|
393
260
|
return void 0;
|
394
261
|
}
|
395
|
-
|
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: { 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
|
+
});
|
396
284
|
const nodes = [];
|
397
285
|
const extraNodes = [];
|
398
|
-
if (!
|
286
|
+
if (!tree.length) {
|
399
287
|
return "";
|
400
288
|
}
|
401
|
-
const isNullish =
|
402
|
-
const isNullable =
|
403
|
-
const isOptional =
|
404
|
-
let type =
|
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 })).filter(Boolean).at(0) || typeKeywordMapper.undefined();
|
405
293
|
if (isNullable) {
|
406
|
-
type =
|
407
|
-
nodes: [type,
|
294
|
+
type = factory2.createUnionDeclaration({
|
295
|
+
nodes: [type, factory2.keywordTypeNodes.null]
|
408
296
|
});
|
409
297
|
}
|
410
|
-
if (isNullish && ["undefined", "questionTokenAndUndefined"].includes(
|
411
|
-
type =
|
412
|
-
nodes: [type,
|
298
|
+
if (isNullish && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
|
299
|
+
type = factory2.createUnionDeclaration({
|
300
|
+
nodes: [type, factory2.keywordTypeNodes.undefined]
|
413
301
|
});
|
414
302
|
}
|
415
|
-
if (isOptional && ["undefined", "questionTokenAndUndefined"].includes(
|
416
|
-
type =
|
417
|
-
nodes: [type,
|
303
|
+
if (isOptional && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
|
304
|
+
type = factory2.createUnionDeclaration({
|
305
|
+
nodes: [type, factory2.keywordTypeNodes.undefined]
|
418
306
|
});
|
419
307
|
}
|
420
|
-
const node =
|
421
|
-
modifiers: [
|
422
|
-
name:
|
423
|
-
type:
|
424
|
-
keys:
|
308
|
+
const node = factory2.createTypeAliasDeclaration({
|
309
|
+
modifiers: [factory2.modifiers.export],
|
310
|
+
name: resolvedName,
|
311
|
+
type: keysToOmit?.length ? factory2.createOmitDeclaration({
|
312
|
+
keys: keysToOmit,
|
425
313
|
type,
|
426
314
|
nonNullable: true
|
427
315
|
}) : type
|
428
316
|
});
|
429
|
-
const enumSchemas =
|
317
|
+
const enumSchemas = SchemaGenerator.deepSearch(tree, schemaKeywords2.enum);
|
430
318
|
if (enumSchemas) {
|
431
319
|
enumSchemas.forEach((enumSchema) => {
|
432
320
|
extraNodes.push(
|
433
|
-
...
|
434
|
-
name:
|
321
|
+
...factory2.createEnumDeclaration({
|
322
|
+
name: transformers2.camelCase(enumSchema.args.name),
|
435
323
|
typeName: enumSchema.args.typeName,
|
436
|
-
enums: enumSchema.args.items.map((item) => item.value === void 0 ? void 0 : [
|
437
|
-
type:
|
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
|
438
326
|
})
|
439
327
|
);
|
440
328
|
});
|
441
329
|
}
|
442
330
|
nodes.push(
|
443
|
-
|
331
|
+
factory2.appendJSDocToNode({
|
444
332
|
node,
|
445
|
-
comments: [
|
333
|
+
comments: [description ? `@description ${transformers2.jsStringEscape(description)}` : void 0].filter(Boolean)
|
446
334
|
})
|
447
335
|
);
|
448
336
|
const filterdNodes = nodes.filter(
|
@@ -452,109 +340,119 @@ function typeParser(schemas, options) {
|
|
452
340
|
);
|
453
341
|
return print([...extraNodes, ...filterdNodes]);
|
454
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
|
+
};
|
455
348
|
|
456
349
|
// src/SchemaGenerator.tsx
|
457
350
|
import { jsx as jsx3 } from "@kubb/react/jsx-runtime";
|
458
|
-
var
|
459
|
-
async schema(name,
|
460
|
-
const { oas, pluginManager,
|
461
|
-
const root =
|
351
|
+
var SchemaGenerator2 = class extends Generator {
|
352
|
+
async schema(name, schema) {
|
353
|
+
const { oas, pluginManager, plugin, mode, output } = this.context;
|
354
|
+
const root = createRoot({
|
462
355
|
logger: pluginManager.logger
|
463
356
|
});
|
357
|
+
const tree = this.parse({ schema, name });
|
464
358
|
root.render(
|
465
|
-
/* @__PURE__ */ jsx3(
|
359
|
+
/* @__PURE__ */ jsx3(App, { pluginManager, plugin, mode, children: /* @__PURE__ */ jsx3(Oas2, { oas, children: /* @__PURE__ */ jsx3(Oas2.Schema, { name, value: schema, tree, children: /* @__PURE__ */ jsx3(Schema.File, {}) }) }) })
|
466
360
|
);
|
467
361
|
return root.files;
|
468
362
|
}
|
469
|
-
// TODO convert to a react component called `Schema.Parser` with props parser as part of the SchemaContext
|
470
|
-
getSource(name, schemas, { keysToOmit, description } = {}) {
|
471
|
-
const texts = [];
|
472
|
-
const resolvedName = this.context.pluginManager.resolveName({
|
473
|
-
name,
|
474
|
-
pluginKey: [pluginTsName],
|
475
|
-
type: "function"
|
476
|
-
});
|
477
|
-
const resolvedTypeName = this.context.pluginManager.resolveName({
|
478
|
-
name,
|
479
|
-
pluginKey: [pluginTsName],
|
480
|
-
type: "type"
|
481
|
-
});
|
482
|
-
const typeOutput = typeParser(schemas, {
|
483
|
-
name: resolvedName,
|
484
|
-
typeName: resolvedTypeName,
|
485
|
-
description,
|
486
|
-
enumType: this.options.enumType || "asConst",
|
487
|
-
optionalType: this.options.optionalType,
|
488
|
-
keysToOmit
|
489
|
-
});
|
490
|
-
texts.push(typeOutput);
|
491
|
-
return texts;
|
492
|
-
}
|
493
|
-
/**
|
494
|
-
* @deprecated only used for testing
|
495
|
-
*/
|
496
|
-
buildSource(name, schema, options = {}) {
|
497
|
-
const schemas = this.buildSchemas({ schema, name });
|
498
|
-
return this.getSource(name, schemas, options);
|
499
|
-
}
|
500
363
|
};
|
501
364
|
|
502
365
|
// src/components/OperationSchema.tsx
|
503
|
-
import {
|
504
|
-
function printCombinedSchema(
|
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
|
+
}) {
|
505
373
|
const properties = {};
|
506
374
|
if (schemas.response) {
|
507
|
-
|
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);
|
508
381
|
}
|
509
382
|
if (schemas.request) {
|
510
|
-
|
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);
|
511
389
|
}
|
512
390
|
if (schemas.pathParams) {
|
513
|
-
|
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);
|
514
397
|
}
|
515
398
|
if (schemas.queryParams) {
|
516
|
-
|
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);
|
517
405
|
}
|
518
406
|
if (schemas.headerParams) {
|
519
|
-
|
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);
|
520
413
|
}
|
521
414
|
if (schemas.errors) {
|
522
|
-
properties["errors"] =
|
415
|
+
properties["errors"] = factory3.createUnionDeclaration({
|
523
416
|
nodes: schemas.errors.map((error) => {
|
524
|
-
|
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);
|
525
423
|
})
|
526
424
|
});
|
527
425
|
}
|
528
|
-
const namespaceNode =
|
426
|
+
const namespaceNode = factory3.createTypeAliasDeclaration({
|
529
427
|
name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
|
530
|
-
type:
|
428
|
+
type: factory3.createTypeLiteralNode(
|
531
429
|
Object.keys(properties).map((key) => {
|
532
430
|
const type = properties[key];
|
533
431
|
if (!type) {
|
534
432
|
return void 0;
|
535
433
|
}
|
536
|
-
return
|
537
|
-
name:
|
434
|
+
return factory3.createPropertySignature({
|
435
|
+
name: transformers3.pascalCase(key),
|
538
436
|
type
|
539
437
|
});
|
540
438
|
}).filter(Boolean)
|
541
439
|
),
|
542
|
-
modifiers: [
|
440
|
+
modifiers: [factory3.modifiers.export]
|
543
441
|
});
|
544
442
|
return print2(namespaceNode);
|
545
443
|
}
|
546
|
-
function OperationSchema({}) {
|
547
|
-
return /* @__PURE__ */ jsx4(
|
444
|
+
function OperationSchema({ keysToOmit, description }) {
|
445
|
+
return /* @__PURE__ */ jsx4(Schema, { keysToOmit, description });
|
548
446
|
}
|
549
447
|
OperationSchema.File = function({}) {
|
550
|
-
const { pluginManager, plugin, mode, fileManager } =
|
448
|
+
const { pluginManager, plugin, mode, fileManager } = useApp3();
|
551
449
|
const oas = useOas2();
|
552
450
|
const { getSchemas, getFile, getName } = useOperationManager();
|
553
451
|
const operation = useOperation();
|
554
452
|
const file = getFile(operation);
|
555
453
|
const schemas = getSchemas(operation);
|
556
454
|
const factoryName = getName(operation, { type: "type" });
|
557
|
-
const generator = new
|
455
|
+
const generator = new SchemaGenerator2(plugin.options, {
|
558
456
|
oas,
|
559
457
|
plugin,
|
560
458
|
pluginManager,
|
@@ -562,22 +460,159 @@ OperationSchema.File = function({}) {
|
|
562
460
|
override: plugin.options.override
|
563
461
|
});
|
564
462
|
const items = [schemas.pathParams, schemas.queryParams, schemas.headerParams, schemas.statusCodes, schemas.request, schemas.response].flat().filter(Boolean);
|
565
|
-
const mapItem = ({ name, schema
|
566
|
-
|
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: [
|
567
466
|
mode === "split" && /* @__PURE__ */ jsx4(Oas3.Schema.Imports, { isTypeOnly: true }),
|
568
|
-
/* @__PURE__ */ jsx4(
|
467
|
+
/* @__PURE__ */ jsx4(File3.Source, { children: /* @__PURE__ */ jsx4(OperationSchema, { description, keysToOmit }) })
|
569
468
|
] }, i);
|
570
469
|
};
|
571
|
-
return /* @__PURE__ */ jsx4(Parser2, { language: "typescript", children: /* @__PURE__ */ jsxs2(
|
470
|
+
return /* @__PURE__ */ jsx4(Parser2, { language: "typescript", children: /* @__PURE__ */ jsxs2(File3, { baseName: file.baseName, path: file.path, meta: file.meta, children: [
|
572
471
|
items.map(mapItem),
|
573
|
-
/* @__PURE__ */ jsx4(
|
472
|
+
/* @__PURE__ */ jsx4(File3.Source, { children: printCombinedSchema({ name: factoryName, operation, schemas, pluginManager }) })
|
574
473
|
] }) });
|
575
474
|
};
|
576
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
|
+
} = options;
|
518
|
+
const template = group?.output ? group.output : `${output.path}/{{tag}}Controller`;
|
519
|
+
return {
|
520
|
+
name: pluginTsName,
|
521
|
+
options: {
|
522
|
+
transformers: transformers4,
|
523
|
+
dateType,
|
524
|
+
optionalType,
|
525
|
+
oasType,
|
526
|
+
enumType,
|
527
|
+
enumSuffix,
|
528
|
+
// keep the used enumnames between SchemaGenerator and OperationGenerator per plugin(pluginKey)
|
529
|
+
usedEnumNames: {},
|
530
|
+
unknownType,
|
531
|
+
override
|
532
|
+
},
|
533
|
+
pre: [pluginOasName],
|
534
|
+
resolvePath(baseName, pathMode, options2) {
|
535
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
536
|
+
const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path));
|
537
|
+
if (mode === "single") {
|
538
|
+
return path.resolve(root, output.path);
|
539
|
+
}
|
540
|
+
if (options2?.tag && group?.type === "tag") {
|
541
|
+
const tag = camelCase(options2.tag);
|
542
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName);
|
543
|
+
}
|
544
|
+
return path.resolve(root, output.path, baseName);
|
545
|
+
},
|
546
|
+
resolveName(name, type) {
|
547
|
+
const resolvedName = pascalCase(name, { isFile: type === "file" });
|
548
|
+
if (type) {
|
549
|
+
return transformers4?.name?.(resolvedName, type) || resolvedName;
|
550
|
+
}
|
551
|
+
return resolvedName;
|
552
|
+
},
|
553
|
+
async writeFile(path2, source) {
|
554
|
+
if (!path2.endsWith(".ts") || !source) {
|
555
|
+
return;
|
556
|
+
}
|
557
|
+
return this.fileManager.write(path2, source, { sanity: false });
|
558
|
+
},
|
559
|
+
async buildStart() {
|
560
|
+
const [swaggerPlugin] = PluginManager.getDependedPlugins(this.plugins, [pluginOasName]);
|
561
|
+
const oas = await swaggerPlugin.api.getOas();
|
562
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
563
|
+
const mode = FileManager.getMode(path.resolve(root, output.path));
|
564
|
+
const schemaGenerator = new SchemaGenerator2(this.plugin.options, {
|
565
|
+
oas,
|
566
|
+
pluginManager: this.pluginManager,
|
567
|
+
plugin: this.plugin,
|
568
|
+
contentType: swaggerPlugin.api.contentType,
|
569
|
+
include: void 0,
|
570
|
+
override,
|
571
|
+
mode,
|
572
|
+
output: output.path
|
573
|
+
});
|
574
|
+
const schemaFiles = await schemaGenerator.build();
|
575
|
+
await this.addFile(...schemaFiles);
|
576
|
+
const operationGenerator = new OperationGenerator(this.plugin.options, {
|
577
|
+
oas,
|
578
|
+
pluginManager: this.pluginManager,
|
579
|
+
plugin: this.plugin,
|
580
|
+
contentType: swaggerPlugin.api.contentType,
|
581
|
+
exclude,
|
582
|
+
include,
|
583
|
+
override,
|
584
|
+
mode
|
585
|
+
});
|
586
|
+
const operationFiles = await operationGenerator.build();
|
587
|
+
await this.addFile(...operationFiles);
|
588
|
+
},
|
589
|
+
async buildEnd() {
|
590
|
+
if (this.config.output.write === false) {
|
591
|
+
return;
|
592
|
+
}
|
593
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
594
|
+
await this.fileManager.addIndexes({
|
595
|
+
root,
|
596
|
+
output,
|
597
|
+
meta: { pluginKey: this.plugin.key },
|
598
|
+
logger: this.logger
|
599
|
+
});
|
600
|
+
}
|
601
|
+
};
|
602
|
+
});
|
603
|
+
|
604
|
+
// src/index.ts
|
605
|
+
var definePluginDefault = pluginTs;
|
606
|
+
var definePlugin = pluginTs;
|
607
|
+
var src_default = definePluginDefault;
|
608
|
+
|
577
609
|
export {
|
578
610
|
OasType,
|
611
|
+
Schema,
|
579
612
|
OperationSchema,
|
580
613
|
pluginTsName,
|
581
|
-
pluginTs
|
614
|
+
pluginTs,
|
615
|
+
definePlugin,
|
616
|
+
src_default
|
582
617
|
};
|
583
|
-
//# sourceMappingURL=chunk-
|
618
|
+
//# sourceMappingURL=chunk-UJR52JIE.js.map
|