@kubb/swagger-ts 1.15.0-canary.20231025T223956 → 1.15.0-canary.20231026T165055
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/index.cjs +141 -170
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -20
- package/dist/index.d.ts +28 -20
- package/dist/index.js +138 -149
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
@@ -2,56 +2,49 @@
|
|
2
2
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
4
4
|
|
5
|
-
var
|
5
|
+
var pathParser = require('path');
|
6
6
|
var core = require('@kubb/core');
|
7
|
-
var utils = require('@kubb/core/utils');
|
8
7
|
var swagger = require('@kubb/swagger');
|
9
8
|
var changeCase = require('change-case');
|
10
9
|
var parser = require('@kubb/parser');
|
11
|
-
var
|
10
|
+
var ts = require('typescript');
|
12
11
|
|
13
12
|
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
var n = Object.create(null);
|
18
|
-
if (e) {
|
19
|
-
Object.keys(e).forEach(function (k) {
|
20
|
-
if (k !== 'default') {
|
21
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
22
|
-
Object.defineProperty(n, k, d.get ? d : {
|
23
|
-
enumerable: true,
|
24
|
-
get: function () { return e[k]; }
|
25
|
-
});
|
26
|
-
}
|
27
|
-
});
|
28
|
-
}
|
29
|
-
n.default = e;
|
30
|
-
return Object.freeze(n);
|
31
|
-
}
|
32
|
-
|
33
|
-
var path__default = /*#__PURE__*/_interopDefault(path);
|
34
|
-
var factory__namespace = /*#__PURE__*/_interopNamespace(factory);
|
14
|
+
var pathParser__default = /*#__PURE__*/_interopDefault(pathParser);
|
15
|
+
var ts__default = /*#__PURE__*/_interopDefault(ts);
|
35
16
|
|
36
17
|
// src/plugin.ts
|
37
|
-
var
|
18
|
+
var { factory } = ts__default.default;
|
19
|
+
var keywordTypeNodes = {
|
20
|
+
any: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.AnyKeyword),
|
21
|
+
number: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.NumberKeyword),
|
22
|
+
integer: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.NumberKeyword),
|
23
|
+
object: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.ObjectKeyword),
|
24
|
+
string: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.StringKeyword),
|
25
|
+
boolean: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.BooleanKeyword),
|
26
|
+
undefined: factory.createKeywordTypeNode(ts__default.default.SyntaxKind.UndefinedKeyword),
|
27
|
+
null: factory.createLiteralTypeNode(factory.createToken(ts__default.default.SyntaxKind.NullKeyword))
|
28
|
+
};
|
29
|
+
function resolve(props) {
|
30
|
+
return swagger.resolve({ ...props, pluginName });
|
31
|
+
}
|
32
|
+
|
33
|
+
// src/generators/TypeGenerator.ts
|
34
|
+
var { factory: factory2 } = ts__default.default;
|
35
|
+
var TypeGenerator = class _TypeGenerator extends core.SchemaGenerator {
|
36
|
+
// Collect the types of all referenced schemas so we can export them later
|
37
|
+
static usedEnumNames = {};
|
38
38
|
refs = {};
|
39
39
|
extraNodes = [];
|
40
40
|
aliases = [];
|
41
41
|
// Keep track of already used type aliases
|
42
|
-
|
43
|
-
|
42
|
+
usedAliasNames = {};
|
43
|
+
caseOptions = {
|
44
44
|
delimiter: "",
|
45
45
|
stripRegexp: /[^A-Z0-9$]/gi
|
46
46
|
};
|
47
|
-
constructor(options = {
|
48
|
-
usedEnumNames: {},
|
49
|
-
withJSDocs: true,
|
50
|
-
resolveName: ({ name }) => name,
|
51
|
-
enumType: "asConst",
|
52
|
-
dateType: "string",
|
53
|
-
optionalType: "questionToken"
|
54
|
-
}) {
|
47
|
+
constructor(options = { withJSDocs: true, resolveName: ({ name }) => name, enumType: "asConst", dateType: "string", optionalType: "questionToken" }) {
|
55
48
|
super(options);
|
56
49
|
return this;
|
57
50
|
}
|
@@ -66,14 +59,14 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
66
59
|
if (!type) {
|
67
60
|
return this.extraNodes;
|
68
61
|
}
|
69
|
-
const node =
|
70
|
-
modifiers: [
|
71
|
-
name: this.options.resolveName({ name: baseName }) || baseName,
|
72
|
-
type: keysToOmit?.length ?
|
62
|
+
const node = parser.createTypeAliasDeclaration({
|
63
|
+
modifiers: [parser.modifiers.export],
|
64
|
+
name: this.options.resolveName({ name: baseName, pluginName }) || baseName,
|
65
|
+
type: keysToOmit?.length ? parser.createOmitDeclaration({ keys: keysToOmit, type, nonNullable: true }) : type
|
73
66
|
});
|
74
67
|
if (description) {
|
75
68
|
nodes.push(
|
76
|
-
|
69
|
+
parser.appendJSDocToNode({
|
77
70
|
node,
|
78
71
|
comments: [`@description ${description}`]
|
79
72
|
})
|
@@ -101,7 +94,7 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
101
94
|
if (schema && !schema.nullable) {
|
102
95
|
return type;
|
103
96
|
}
|
104
|
-
return
|
97
|
+
return parser.createUnionDeclaration({ nodes: [type, keywordTypeNodes.null] });
|
105
98
|
}
|
106
99
|
/**
|
107
100
|
* Recursively creates a type literal with the given props.
|
@@ -114,21 +107,21 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
114
107
|
const members = Object.keys(properties).map((name) => {
|
115
108
|
const schema = properties[name];
|
116
109
|
const isRequired = required && required.includes(name);
|
117
|
-
let type = this.#getTypeFromSchema(schema, this.options.resolveName({ name: `${baseName || ""} ${name}
|
110
|
+
let type = this.#getTypeFromSchema(schema, this.options.resolveName({ name: `${baseName || ""} ${name}`, pluginName }));
|
118
111
|
if (!type) {
|
119
112
|
return null;
|
120
113
|
}
|
121
114
|
if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
|
122
|
-
type =
|
115
|
+
type = parser.createUnionDeclaration({ nodes: [type, keywordTypeNodes.undefined] });
|
123
116
|
}
|
124
|
-
const propertySignature =
|
117
|
+
const propertySignature = parser.createPropertySignature({
|
125
118
|
questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
|
126
119
|
name,
|
127
120
|
type,
|
128
121
|
readOnly: schema.readOnly
|
129
122
|
});
|
130
123
|
if (this.options.withJSDocs) {
|
131
|
-
return
|
124
|
+
return parser.appendJSDocToNode({
|
132
125
|
node: propertySignature,
|
133
126
|
comments: [
|
134
127
|
schema.description ? `@description ${schema.description}` : void 0,
|
@@ -143,29 +136,29 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
143
136
|
return propertySignature;
|
144
137
|
});
|
145
138
|
if (additionalProperties) {
|
146
|
-
const type = additionalProperties === true ?
|
139
|
+
const type = additionalProperties === true ? keywordTypeNodes.any : this.#getTypeFromSchema(additionalProperties);
|
147
140
|
if (type) {
|
148
|
-
members.push(
|
141
|
+
members.push(parser.createIndexSignature(type));
|
149
142
|
}
|
150
143
|
}
|
151
|
-
return
|
144
|
+
return factory2.createTypeLiteralNode(members.filter(Boolean));
|
152
145
|
}
|
153
146
|
/**
|
154
147
|
* Create a type alias for the schema referenced by the given ReferenceObject
|
155
148
|
*/
|
156
|
-
#getRefAlias(obj,
|
149
|
+
#getRefAlias(obj, baseName) {
|
157
150
|
const { $ref } = obj;
|
158
151
|
let ref = this.refs[$ref];
|
159
152
|
if (ref) {
|
160
|
-
return
|
153
|
+
return factory2.createTypeReferenceNode(ref.propertyName, void 0);
|
161
154
|
}
|
162
|
-
const originalName =
|
163
|
-
const propertyName = this.options.resolveName({ name: originalName }) || originalName;
|
155
|
+
const originalName = core.getUniqueName($ref.replace(/.+\//, ""), this.usedAliasNames);
|
156
|
+
const propertyName = this.options.resolveName({ name: originalName, pluginName }) || originalName;
|
164
157
|
ref = this.refs[$ref] = {
|
165
158
|
propertyName,
|
166
159
|
originalName
|
167
160
|
};
|
168
|
-
return
|
161
|
+
return factory2.createTypeReferenceNode(ref.propertyName, void 0);
|
169
162
|
}
|
170
163
|
/**
|
171
164
|
* This is the very core of the OpenAPI to TS conversion - it takes a
|
@@ -173,23 +166,23 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
173
166
|
*/
|
174
167
|
#getBaseTypeFromSchema(schema, baseName) {
|
175
168
|
if (!schema) {
|
176
|
-
return
|
169
|
+
return keywordTypeNodes.any;
|
177
170
|
}
|
178
171
|
if (swagger.isReference(schema)) {
|
179
172
|
return this.#getRefAlias(schema, baseName);
|
180
173
|
}
|
181
174
|
if (schema.oneOf) {
|
182
175
|
const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
|
183
|
-
const union =
|
176
|
+
const union = parser.createUnionDeclaration({
|
184
177
|
withParentheses: true,
|
185
178
|
nodes: schema.oneOf.map((item) => {
|
186
179
|
return this.#getBaseTypeFromSchema(item);
|
187
180
|
}).filter((item) => {
|
188
|
-
return item && item !==
|
181
|
+
return item && item !== keywordTypeNodes.any;
|
189
182
|
})
|
190
183
|
});
|
191
184
|
if (schemaWithoutOneOf.properties) {
|
192
|
-
return
|
185
|
+
return parser.createIntersectionDeclaration({
|
193
186
|
nodes: [this.#getBaseTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean)
|
194
187
|
});
|
195
188
|
}
|
@@ -197,16 +190,16 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
197
190
|
}
|
198
191
|
if (schema.anyOf) {
|
199
192
|
const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
|
200
|
-
const union =
|
193
|
+
const union = parser.createUnionDeclaration({
|
201
194
|
withParentheses: true,
|
202
195
|
nodes: schema.anyOf.map((item) => {
|
203
196
|
return this.#getBaseTypeFromSchema(item);
|
204
197
|
}).filter((item) => {
|
205
|
-
return item && item !==
|
198
|
+
return item && item !== keywordTypeNodes.any;
|
206
199
|
})
|
207
200
|
});
|
208
201
|
if (schemaWithoutAnyOf.properties) {
|
209
|
-
return
|
202
|
+
return parser.createIntersectionDeclaration({
|
210
203
|
nodes: [this.#getBaseTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean)
|
211
204
|
});
|
212
205
|
}
|
@@ -214,23 +207,23 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
214
207
|
}
|
215
208
|
if (schema.allOf) {
|
216
209
|
const schemaWithoutAllOf = { ...schema, allOf: void 0 };
|
217
|
-
const and =
|
210
|
+
const and = parser.createIntersectionDeclaration({
|
218
211
|
withParentheses: true,
|
219
212
|
nodes: schema.allOf.map((item) => {
|
220
213
|
return this.#getBaseTypeFromSchema(item);
|
221
214
|
}).filter((item) => {
|
222
|
-
return item && item !==
|
215
|
+
return item && item !== keywordTypeNodes.any;
|
223
216
|
})
|
224
217
|
});
|
225
218
|
if (schemaWithoutAllOf.properties) {
|
226
|
-
return
|
219
|
+
return parser.createIntersectionDeclaration({
|
227
220
|
nodes: [this.#getBaseTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean)
|
228
221
|
});
|
229
222
|
}
|
230
223
|
return and;
|
231
224
|
}
|
232
225
|
if (schema.enum && baseName) {
|
233
|
-
const enumName =
|
226
|
+
const enumName = core.getUniqueName(baseName, _TypeGenerator.usedEnumNames);
|
234
227
|
let enums = [...new Set(schema.enum)].map((key) => [key, key]);
|
235
228
|
if ("x-enumNames" in schema) {
|
236
229
|
enums = [...new Set(schema["x-enumNames"])].map((key, index) => {
|
@@ -238,31 +231,31 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
238
231
|
});
|
239
232
|
}
|
240
233
|
this.extraNodes.push(
|
241
|
-
...
|
242
|
-
name: changeCase.camelCase(enumName, this
|
243
|
-
typeName: this.options.resolveName({ name: enumName }),
|
234
|
+
...parser.createEnumDeclaration({
|
235
|
+
name: changeCase.camelCase(enumName, this.caseOptions),
|
236
|
+
typeName: this.options.resolveName({ name: enumName, pluginName }),
|
244
237
|
enums,
|
245
238
|
type: this.options.enumType
|
246
239
|
})
|
247
240
|
);
|
248
|
-
return
|
241
|
+
return factory2.createTypeReferenceNode(this.options.resolveName({ name: enumName, pluginName }), void 0);
|
249
242
|
}
|
250
243
|
if (schema.enum) {
|
251
|
-
return
|
244
|
+
return parser.createUnionDeclaration({
|
252
245
|
nodes: schema.enum.map((name) => {
|
253
|
-
return
|
246
|
+
return factory2.createLiteralTypeNode(typeof name === "number" ? factory2.createNumericLiteral(name) : factory2.createStringLiteral(`${name}`));
|
254
247
|
})
|
255
248
|
});
|
256
249
|
}
|
257
250
|
if ("items" in schema) {
|
258
251
|
const node = this.#getTypeFromSchema(schema.items, baseName);
|
259
252
|
if (node) {
|
260
|
-
return
|
253
|
+
return factory2.createArrayTypeNode(node);
|
261
254
|
}
|
262
255
|
}
|
263
256
|
if ("prefixItems" in schema) {
|
264
257
|
const prefixItems = schema.prefixItems;
|
265
|
-
return
|
258
|
+
return parser.createTupleDeclaration({
|
266
259
|
nodes: prefixItems.map((item) => {
|
267
260
|
return this.#getBaseTypeFromSchema(item, void 0);
|
268
261
|
})
|
@@ -274,7 +267,7 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
274
267
|
if (schema.type) {
|
275
268
|
if (Array.isArray(schema.type)) {
|
276
269
|
const [type, nullable] = schema.type;
|
277
|
-
return
|
270
|
+
return parser.createUnionDeclaration({
|
278
271
|
nodes: [
|
279
272
|
this.#getBaseTypeFromSchema(
|
280
273
|
{
|
@@ -283,25 +276,37 @@ var TypeGenerator = class extends core.SchemaGenerator {
|
|
283
276
|
},
|
284
277
|
baseName
|
285
278
|
),
|
286
|
-
nullable ?
|
279
|
+
nullable ? factory2.createLiteralTypeNode(factory2.createNull()) : void 0
|
287
280
|
].filter(Boolean)
|
288
281
|
});
|
289
282
|
}
|
290
283
|
if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
|
291
|
-
return
|
284
|
+
return factory2.createTypeReferenceNode(factory2.createIdentifier("Date"));
|
292
285
|
}
|
293
|
-
if (schema.type in
|
294
|
-
return
|
286
|
+
if (schema.type in keywordTypeNodes) {
|
287
|
+
return keywordTypeNodes[schema.type];
|
295
288
|
}
|
296
289
|
}
|
297
290
|
if (schema.format === "binary") {
|
298
|
-
return
|
291
|
+
return factory2.createTypeReferenceNode("Blob", []);
|
292
|
+
}
|
293
|
+
if ("const" in schema && schema["const"] !== void 0 && typeof schema["const"] === "string") {
|
294
|
+
return factory2.createLiteralTypeNode(factory2.createStringLiteral(schema["const"]));
|
299
295
|
}
|
300
|
-
return
|
296
|
+
return keywordTypeNodes.any;
|
301
297
|
}
|
302
298
|
};
|
303
299
|
|
304
300
|
// src/builders/TypeBuilder.ts
|
301
|
+
function refsSorter(a, b) {
|
302
|
+
if (Object.keys(a.import.refs)?.length < Object.keys(b.import.refs)?.length) {
|
303
|
+
return -1;
|
304
|
+
}
|
305
|
+
if (Object.keys(a.import.refs)?.length > Object.keys(b.import.refs)?.length) {
|
306
|
+
return 1;
|
307
|
+
}
|
308
|
+
return 0;
|
309
|
+
}
|
305
310
|
var TypeBuilder = class extends swagger.OasBuilder {
|
306
311
|
configure(options) {
|
307
312
|
if (options) {
|
@@ -314,9 +319,8 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
314
319
|
}
|
315
320
|
print(name) {
|
316
321
|
const codes = [];
|
317
|
-
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(
|
322
|
+
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(core.nameSorter).map((operationSchema) => {
|
318
323
|
const generator = new TypeGenerator({
|
319
|
-
usedEnumNames: this.options.usedEnumNames,
|
320
324
|
withJSDocs: this.options.withJSDocs,
|
321
325
|
resolveName: this.options.resolveName,
|
322
326
|
enumType: this.options.enumType,
|
@@ -336,7 +340,7 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
336
340
|
},
|
337
341
|
sources
|
338
342
|
};
|
339
|
-
}).sort(
|
343
|
+
}).sort(refsSorter);
|
340
344
|
generated.forEach((item) => {
|
341
345
|
codes.push(parser.print(item.sources));
|
342
346
|
});
|
@@ -345,7 +349,7 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
345
349
|
const importMeta = importsGenerator.build(generated.map((item) => item.import));
|
346
350
|
if (importMeta) {
|
347
351
|
const nodes = importMeta.map((item) => {
|
348
|
-
return
|
352
|
+
return parser.createImportDeclaration({
|
349
353
|
name: [{ propertyName: item.ref.propertyName }],
|
350
354
|
path: item.path,
|
351
355
|
isTypeOnly: true
|
@@ -354,39 +358,38 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
354
358
|
codes.unshift(parser.print(nodes));
|
355
359
|
}
|
356
360
|
}
|
357
|
-
return
|
361
|
+
return core.combineCodes(codes);
|
358
362
|
}
|
359
363
|
};
|
360
364
|
var OperationGenerator = class extends swagger.OperationGenerator {
|
361
365
|
resolve(operation) {
|
362
|
-
const { pluginManager
|
366
|
+
const { pluginManager } = this.context;
|
363
367
|
return swagger.resolve({
|
364
368
|
operation,
|
365
369
|
resolveName: pluginManager.resolveName,
|
366
370
|
resolvePath: pluginManager.resolvePath,
|
367
|
-
|
371
|
+
pluginName
|
368
372
|
});
|
369
373
|
}
|
370
374
|
async all() {
|
371
375
|
return null;
|
372
376
|
}
|
373
377
|
async get(operation, schemas, options) {
|
374
|
-
const { mode, enumType, dateType, optionalType
|
375
|
-
const { pluginManager
|
378
|
+
const { mode, enumType, dateType, optionalType } = options;
|
379
|
+
const { pluginManager } = this.context;
|
376
380
|
const type = this.resolve(operation);
|
377
381
|
const fileResolver = (name) => {
|
378
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName,
|
382
|
+
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } });
|
379
383
|
const resolvedTypeId = pluginManager.resolvePath({
|
380
384
|
baseName: `${name}.ts`,
|
381
|
-
|
385
|
+
pluginName
|
382
386
|
});
|
383
|
-
return
|
387
|
+
return core.getRelativePath(root, resolvedTypeId);
|
384
388
|
};
|
385
389
|
const source = new TypeBuilder({
|
386
|
-
usedEnumNames,
|
387
390
|
fileResolver: mode === "file" ? void 0 : fileResolver,
|
388
391
|
withJSDocs: true,
|
389
|
-
resolveName:
|
392
|
+
resolveName: pluginManager.resolveName,
|
390
393
|
enumType,
|
391
394
|
optionalType,
|
392
395
|
dateType
|
@@ -396,28 +399,27 @@ var OperationGenerator = class extends swagger.OperationGenerator {
|
|
396
399
|
baseName: type.baseName,
|
397
400
|
source,
|
398
401
|
meta: {
|
399
|
-
|
402
|
+
pluginName,
|
400
403
|
tag: operation.getTags()[0]?.name
|
401
404
|
}
|
402
405
|
};
|
403
406
|
}
|
404
407
|
async post(operation, schemas, options) {
|
405
|
-
const { mode, enumType, dateType, optionalType
|
406
|
-
const { pluginManager
|
408
|
+
const { mode, enumType, dateType, optionalType } = options;
|
409
|
+
const { pluginManager } = this.context;
|
407
410
|
const type = this.resolve(operation);
|
408
411
|
const fileResolver = (name) => {
|
409
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName,
|
412
|
+
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } });
|
410
413
|
const resolvedTypeId = pluginManager.resolvePath({
|
411
414
|
baseName: `${name}.ts`,
|
412
|
-
|
415
|
+
pluginName
|
413
416
|
});
|
414
|
-
return
|
417
|
+
return core.getRelativePath(root, resolvedTypeId);
|
415
418
|
};
|
416
419
|
const source = new TypeBuilder({
|
417
|
-
usedEnumNames,
|
418
420
|
fileResolver: mode === "file" ? void 0 : fileResolver,
|
419
421
|
withJSDocs: true,
|
420
|
-
resolveName:
|
422
|
+
resolveName: pluginManager.resolveName,
|
421
423
|
enumType,
|
422
424
|
optionalType,
|
423
425
|
dateType
|
@@ -427,7 +429,7 @@ var OperationGenerator = class extends swagger.OperationGenerator {
|
|
427
429
|
baseName: type.baseName,
|
428
430
|
source,
|
429
431
|
meta: {
|
430
|
-
|
432
|
+
pluginName,
|
431
433
|
tag: operation.getTags()[0]?.name
|
432
434
|
}
|
433
435
|
};
|
@@ -445,7 +447,6 @@ var OperationGenerator = class extends swagger.OperationGenerator {
|
|
445
447
|
|
446
448
|
// src/plugin.ts
|
447
449
|
var pluginName = "swagger-ts";
|
448
|
-
var pluginKey = ["schema", pluginName];
|
449
450
|
var definePlugin = core.createPlugin((options) => {
|
450
451
|
const {
|
451
452
|
output = "types",
|
@@ -455,8 +456,7 @@ var definePlugin = core.createPlugin((options) => {
|
|
455
456
|
enumType = "asConst",
|
456
457
|
dateType = "string",
|
457
458
|
optionalType = "questionToken",
|
458
|
-
transformers
|
459
|
-
exportAs
|
459
|
+
transformers = {}
|
460
460
|
} = options;
|
461
461
|
const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller`;
|
462
462
|
let pluginsOptions;
|
@@ -465,49 +465,47 @@ var definePlugin = core.createPlugin((options) => {
|
|
465
465
|
options,
|
466
466
|
kind: "schema",
|
467
467
|
validate(plugins) {
|
468
|
-
pluginsOptions = core.
|
468
|
+
pluginsOptions = core.getDependedPlugins(plugins, [swagger.pluginName]);
|
469
469
|
return true;
|
470
470
|
},
|
471
471
|
resolvePath(baseName, directory, options2) {
|
472
|
-
const root =
|
473
|
-
const mode = core.
|
472
|
+
const root = pathParser__default.default.resolve(this.config.root, this.config.output.path);
|
473
|
+
const mode = core.getPathMode(pathParser__default.default.resolve(root, output));
|
474
474
|
if (mode === "file") {
|
475
|
-
return
|
475
|
+
return pathParser__default.default.resolve(root, output);
|
476
476
|
}
|
477
477
|
if (options2?.tag && groupBy?.type === "tag") {
|
478
478
|
const tag = changeCase.camelCase(options2.tag, { delimiter: "", transform: changeCase.camelCaseTransformMerge });
|
479
|
-
return
|
479
|
+
return pathParser__default.default.resolve(root, core.renderTemplate(template, { tag }), baseName);
|
480
480
|
}
|
481
|
-
return
|
481
|
+
return pathParser__default.default.resolve(root, output, baseName);
|
482
482
|
},
|
483
483
|
resolveName(name) {
|
484
484
|
const resolvedName = changeCase.pascalCase(name, { delimiter: "", stripRegexp: /[^A-Z0-9$]/gi, transform: changeCase.pascalCaseTransformMerge });
|
485
|
-
return
|
485
|
+
return transformers?.name?.(resolvedName) || resolvedName;
|
486
486
|
},
|
487
|
-
async writeFile(source,
|
488
|
-
if (!
|
487
|
+
async writeFile(source, path) {
|
488
|
+
if (!path.endsWith(".ts") || !source) {
|
489
489
|
return;
|
490
490
|
}
|
491
|
-
|
491
|
+
await this.fileManager.write(source, path);
|
492
492
|
},
|
493
493
|
async buildStart() {
|
494
494
|
const [swaggerPlugin] = pluginsOptions;
|
495
495
|
const oas = await swaggerPlugin.api.getOas();
|
496
496
|
const schemas = await swaggerPlugin.api.getSchemas();
|
497
|
-
const root =
|
498
|
-
const mode = core.
|
499
|
-
const usedEnumNames = {};
|
497
|
+
const root = pathParser__default.default.resolve(this.config.root, this.config.output.path);
|
498
|
+
const mode = core.getPathMode(pathParser__default.default.resolve(root, output));
|
500
499
|
if (mode === "directory") {
|
501
500
|
const builder = await new TypeBuilder({
|
502
|
-
|
503
|
-
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
501
|
+
resolveName: (params) => this.resolveName({ pluginName, ...params }),
|
504
502
|
fileResolver: (name) => {
|
505
503
|
const resolvedTypeId = this.resolvePath({
|
506
504
|
baseName: `${name}.ts`,
|
507
|
-
|
505
|
+
pluginName
|
508
506
|
});
|
509
|
-
const root2 = this.resolvePath({ baseName: ``,
|
510
|
-
return
|
507
|
+
const root2 = this.resolvePath({ baseName: ``, pluginName });
|
508
|
+
return core.getRelativePath(root2, resolvedTypeId);
|
511
509
|
},
|
512
510
|
withJSDocs: true,
|
513
511
|
enumType,
|
@@ -521,16 +519,16 @@ var definePlugin = core.createPlugin((options) => {
|
|
521
519
|
});
|
522
520
|
});
|
523
521
|
const mapFolderSchema = async ([name]) => {
|
524
|
-
const
|
525
|
-
if (!
|
522
|
+
const path = this.resolvePath({ baseName: `${this.resolveName({ name, pluginName })}.ts`, pluginName });
|
523
|
+
if (!path) {
|
526
524
|
return null;
|
527
525
|
}
|
528
526
|
return this.addFile({
|
529
|
-
path
|
530
|
-
baseName: `${this.resolveName({ name,
|
527
|
+
path,
|
528
|
+
baseName: `${this.resolveName({ name, pluginName })}.ts`,
|
531
529
|
source: builder.print(name),
|
532
530
|
meta: {
|
533
|
-
|
531
|
+
pluginName
|
534
532
|
}
|
535
533
|
});
|
536
534
|
};
|
@@ -539,8 +537,7 @@ var definePlugin = core.createPlugin((options) => {
|
|
539
537
|
}
|
540
538
|
if (mode === "file") {
|
541
539
|
const builder = new TypeBuilder({
|
542
|
-
|
543
|
-
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
540
|
+
resolveName: (params) => this.resolveName({ pluginName, ...params }),
|
544
541
|
withJSDocs: true,
|
545
542
|
enumType,
|
546
543
|
dateType,
|
@@ -552,18 +549,17 @@ var definePlugin = core.createPlugin((options) => {
|
|
552
549
|
name
|
553
550
|
});
|
554
551
|
});
|
555
|
-
const
|
556
|
-
if (!
|
552
|
+
const path = this.resolvePath({ baseName: "", pluginName });
|
553
|
+
if (!path) {
|
557
554
|
return;
|
558
555
|
}
|
559
556
|
await this.addFile({
|
560
|
-
path
|
561
|
-
baseName: output,
|
557
|
+
path,
|
558
|
+
baseName: `${this.resolveName({ name: output, pluginName })}.ts`,
|
562
559
|
source: builder.print(),
|
563
560
|
meta: {
|
564
|
-
|
565
|
-
}
|
566
|
-
validate: false
|
561
|
+
pluginName
|
562
|
+
}
|
567
563
|
});
|
568
564
|
}
|
569
565
|
const operationGenerator = new OperationGenerator(
|
@@ -571,13 +567,11 @@ var definePlugin = core.createPlugin((options) => {
|
|
571
567
|
mode,
|
572
568
|
enumType,
|
573
569
|
dateType,
|
574
|
-
optionalType
|
575
|
-
usedEnumNames
|
570
|
+
optionalType
|
576
571
|
},
|
577
572
|
{
|
578
573
|
oas,
|
579
574
|
pluginManager: this.pluginManager,
|
580
|
-
plugin: this.plugin,
|
581
575
|
contentType: swaggerPlugin.api.contentType,
|
582
576
|
skipBy,
|
583
577
|
overrideBy
|
@@ -590,39 +584,16 @@ var definePlugin = core.createPlugin((options) => {
|
|
590
584
|
if (this.config.output.write === false) {
|
591
585
|
return;
|
592
586
|
}
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
options: {
|
599
|
-
map: (file) => {
|
600
|
-
return {
|
601
|
-
...file,
|
602
|
-
exports: file.exports?.map((item) => {
|
603
|
-
if (exportAs) {
|
604
|
-
return {
|
605
|
-
...item,
|
606
|
-
name: exportAs,
|
607
|
-
asAlias: !!exportAs
|
608
|
-
};
|
609
|
-
}
|
610
|
-
return item;
|
611
|
-
})
|
612
|
-
};
|
613
|
-
},
|
614
|
-
output,
|
615
|
-
isTypeOnly: true
|
616
|
-
}
|
617
|
-
});
|
587
|
+
while (this.fileManager.isExecuting) {
|
588
|
+
await core.timeout(100);
|
589
|
+
}
|
590
|
+
const root = pathParser__default.default.resolve(this.config.root, this.config.output.path);
|
591
|
+
await this.fileManager.addIndexes(root, ".ts");
|
618
592
|
}
|
619
593
|
};
|
620
594
|
});
|
621
595
|
function useResolve(props = {}) {
|
622
|
-
return swagger.useResolve({
|
623
|
-
}
|
624
|
-
function resolve2(props) {
|
625
|
-
return swagger.resolve({ pluginKey, ...props });
|
596
|
+
return swagger.useResolve({ pluginName, ...props });
|
626
597
|
}
|
627
598
|
|
628
599
|
// src/index.ts
|
@@ -633,9 +604,9 @@ exports.TypeBuilder = TypeBuilder;
|
|
633
604
|
exports.TypeGenerator = TypeGenerator;
|
634
605
|
exports.default = src_default;
|
635
606
|
exports.definePlugin = definePlugin;
|
636
|
-
exports.
|
607
|
+
exports.keywordTypeNodes = keywordTypeNodes;
|
637
608
|
exports.pluginName = pluginName;
|
638
|
-
exports.resolve =
|
609
|
+
exports.resolve = resolve;
|
639
610
|
exports.useResolve = useResolve;
|
640
611
|
//# sourceMappingURL=out.js.map
|
641
612
|
//# sourceMappingURL=index.cjs.map
|