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