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