@kubb/swagger-ts 1.15.0-canary.20231026T165055 → 1.15.0-canary.20231027T155054

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