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