@kubb/swagger-ts 1.15.0-canary.20231027T175035 → 1.15.0-canary.20231027T185644

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,94 +2,50 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var path = require('path');
5
+ var pathParser = require('path');
6
6
  var core = require('@kubb/core');
7
- var utils = require('@kubb/core/utils');
8
7
  var swagger = require('@kubb/swagger');
9
8
  var changeCase = require('change-case');
10
9
  var parser = require('@kubb/parser');
11
- var factory = require('@kubb/parser/factory');
12
- var utils$1 = require('@kubb/swagger/utils');
10
+ var ts = require('typescript');
13
11
 
14
12
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
15
13
 
16
- 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);
32
- }
33
-
34
- var path__default = /*#__PURE__*/_interopDefault(path);
35
- var factory__namespace = /*#__PURE__*/_interopNamespace(factory);
14
+ var pathParser__default = /*#__PURE__*/_interopDefault(pathParser);
15
+ var ts__default = /*#__PURE__*/_interopDefault(ts);
36
16
 
37
- 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;
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))
53
28
  };
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
- }) {
29
+ function resolve(props) {
30
+ return swagger.resolve({ ...props, pluginName });
31
+ }
32
+
33
+ // src/generators/TypeGenerator.ts
34
+ var { factory: factory2 } = ts__default.default;
35
+ var TypeGenerator = class _TypeGenerator extends core.SchemaGenerator {
36
+ // Collect the types of all referenced schemas so we can export them later
37
+ static usedEnumNames = {};
38
+ 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" }) {
64
48
  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
- });
93
49
  return this;
94
50
  }
95
51
  build({
@@ -99,18 +55,18 @@ var TypeGenerator = class extends core.SchemaGenerator {
99
55
  keysToOmit
100
56
  }) {
101
57
  const nodes = [];
102
- const type = __privateMethod(this, _getTypeFromSchema, getTypeFromSchema_fn).call(this, schema, baseName);
58
+ const type = this.#getTypeFromSchema(schema, baseName);
103
59
  if (!type) {
104
60
  return this.extraNodes;
105
61
  }
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
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
110
66
  });
111
67
  if (description) {
112
68
  nodes.push(
113
- factory__namespace.appendJSDocToNode({
69
+ parser.appendJSDocToNode({
114
70
  node,
115
71
  comments: [`@description ${description}`]
116
72
  })
@@ -125,211 +81,232 @@ var TypeGenerator = class extends core.SchemaGenerator {
125
81
  );
126
82
  return [...this.extraNodes, ...filterdNodes];
127
83
  }
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}` }));
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);
152
91
  if (!type) {
153
92
  return null;
154
93
  }
155
- if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
156
- type = factory__namespace.createUnionDeclaration({ nodes: [type, factory__namespace.keywordTypeNodes.undefined] });
94
+ if (schema && !schema.nullable) {
95
+ return type;
157
96
  }
158
- const propertySignature = factory__namespace.createPropertySignature({
159
- questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
160
- name,
161
- type,
162
- readOnly: schema.readOnly
163
- });
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)
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
175
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;
137
+ });
138
+ if (additionalProperties) {
139
+ const type = additionalProperties === true ? keywordTypeNodes.any : this.#getTypeFromSchema(additionalProperties);
140
+ if (type) {
141
+ members.push(parser.createIndexSignature(type));
142
+ }
176
143
  }
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));
183
- }
184
- }
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;
144
+ return factory2.createTypeLiteralNode(members.filter(Boolean));
206
145
  }
207
- if (utils$1.isReference(schema)) {
208
- return __privateMethod(this, _getRefAlias, getRefAlias_fn).call(this, schema, baseName);
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);
154
+ }
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);
209
162
  }
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)
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
+ })
223
183
  });
184
+ if (schemaWithoutOneOf.properties) {
185
+ return parser.createIntersectionDeclaration({
186
+ nodes: [this.#getBaseTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean)
187
+ });
188
+ }
189
+ return union;
224
190
  }
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)
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
+ })
240
200
  });
201
+ if (schemaWithoutAnyOf.properties) {
202
+ return parser.createIntersectionDeclaration({
203
+ nodes: [this.#getBaseTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean)
204
+ });
205
+ }
206
+ return union;
241
207
  }
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)
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
+ })
257
217
  });
218
+ if (schemaWithoutAllOf.properties) {
219
+ return parser.createIntersectionDeclaration({
220
+ nodes: [this.#getBaseTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean)
221
+ });
222
+ }
223
+ return and;
258
224
  }
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]];
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
+ }
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
+ })
267
248
  });
268
249
  }
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);
250
+ if ("items" in schema) {
251
+ const node = this.#getTypeFromSchema(schema.items, baseName);
252
+ if (node) {
253
+ return factory2.createArrayTypeNode(node);
254
+ }
290
255
  }
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)
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
+ })
314
262
  });
315
263
  }
316
- if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
317
- return factory__namespace.createTypeReferenceNode(factory__namespace.createIdentifier("Date"));
264
+ if (schema.properties || schema.additionalProperties) {
265
+ return this.#getTypeFromProperties(schema, baseName);
318
266
  }
319
- if (schema.type in factory__namespace.keywordTypeNodes) {
320
- return factory__namespace.keywordTypeNodes[schema.type];
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
+ }
321
289
  }
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;
322
297
  }
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;
330
298
  };
331
299
 
332
300
  // src/builders/TypeBuilder.ts
301
+ function refsSorter(a, b) {
302
+ if (Object.keys(a.import.refs)?.length < Object.keys(b.import.refs)?.length) {
303
+ return -1;
304
+ }
305
+ if (Object.keys(a.import.refs)?.length > Object.keys(b.import.refs)?.length) {
306
+ return 1;
307
+ }
308
+ return 0;
309
+ }
333
310
  var TypeBuilder = class extends swagger.OasBuilder {
334
311
  configure(options) {
335
312
  if (options) {
@@ -342,9 +319,8 @@ var TypeBuilder = class extends swagger.OasBuilder {
342
319
  }
343
320
  print(name) {
344
321
  const codes = [];
345
- const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(utils.transformers.nameSorter).map((operationSchema) => {
322
+ const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(core.nameSorter).map((operationSchema) => {
346
323
  const generator = new TypeGenerator({
347
- usedEnumNames: this.options.usedEnumNames,
348
324
  withJSDocs: this.options.withJSDocs,
349
325
  resolveName: this.options.resolveName,
350
326
  enumType: this.options.enumType,
@@ -364,7 +340,7 @@ var TypeBuilder = class extends swagger.OasBuilder {
364
340
  },
365
341
  sources
366
342
  };
367
- }).sort(utils$1.refsSorter);
343
+ }).sort(refsSorter);
368
344
  generated.forEach((item) => {
369
345
  codes.push(parser.print(item.sources));
370
346
  });
@@ -373,7 +349,7 @@ var TypeBuilder = class extends swagger.OasBuilder {
373
349
  const importMeta = importsGenerator.build(generated.map((item) => item.import));
374
350
  if (importMeta) {
375
351
  const nodes = importMeta.map((item) => {
376
- return factory__namespace.createImportDeclaration({
352
+ return parser.createImportDeclaration({
377
353
  name: [{ propertyName: item.ref.propertyName }],
378
354
  path: item.path,
379
355
  isTypeOnly: true
@@ -382,39 +358,38 @@ var TypeBuilder = class extends swagger.OasBuilder {
382
358
  codes.unshift(parser.print(nodes));
383
359
  }
384
360
  }
385
- return utils.transformers.combineCodes(codes);
361
+ return core.combineCodes(codes);
386
362
  }
387
363
  };
388
364
  var OperationGenerator = class extends swagger.OperationGenerator {
389
365
  resolve(operation) {
390
- const { pluginManager, plugin } = this.context;
366
+ const { pluginManager } = this.context;
391
367
  return swagger.resolve({
392
368
  operation,
393
369
  resolveName: pluginManager.resolveName,
394
370
  resolvePath: pluginManager.resolvePath,
395
- pluginKey: plugin?.key
371
+ pluginName
396
372
  });
397
373
  }
398
374
  async all() {
399
375
  return null;
400
376
  }
401
377
  async get(operation, schemas, options) {
402
- const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
403
- const { pluginManager, plugin } = this.context;
378
+ const { mode, enumType, dateType, optionalType } = options;
379
+ const { pluginManager } = this.context;
404
380
  const type = this.resolve(operation);
405
381
  const fileResolver = (name) => {
406
- const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
382
+ const root = pluginManager.resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } });
407
383
  const resolvedTypeId = pluginManager.resolvePath({
408
384
  baseName: `${name}.ts`,
409
- pluginKey: plugin?.key
385
+ pluginName
410
386
  });
411
- return utils.getRelativePath(root, resolvedTypeId);
387
+ return core.getRelativePath(root, resolvedTypeId);
412
388
  };
413
389
  const source = new TypeBuilder({
414
- usedEnumNames,
415
390
  fileResolver: mode === "file" ? void 0 : fileResolver,
416
391
  withJSDocs: true,
417
- resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
392
+ resolveName: pluginManager.resolveName,
418
393
  enumType,
419
394
  optionalType,
420
395
  dateType
@@ -424,28 +399,27 @@ var OperationGenerator = class extends swagger.OperationGenerator {
424
399
  baseName: type.baseName,
425
400
  source,
426
401
  meta: {
427
- pluginKey: plugin.key,
402
+ pluginName,
428
403
  tag: operation.getTags()[0]?.name
429
404
  }
430
405
  };
431
406
  }
432
407
  async post(operation, schemas, options) {
433
- const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
434
- const { pluginManager, plugin } = this.context;
408
+ const { mode, enumType, dateType, optionalType } = options;
409
+ const { pluginManager } = this.context;
435
410
  const type = this.resolve(operation);
436
411
  const fileResolver = (name) => {
437
- const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
412
+ const root = pluginManager.resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } });
438
413
  const resolvedTypeId = pluginManager.resolvePath({
439
414
  baseName: `${name}.ts`,
440
- pluginKey: plugin?.key
415
+ pluginName
441
416
  });
442
- return utils.getRelativePath(root, resolvedTypeId);
417
+ return core.getRelativePath(root, resolvedTypeId);
443
418
  };
444
419
  const source = new TypeBuilder({
445
- usedEnumNames,
446
420
  fileResolver: mode === "file" ? void 0 : fileResolver,
447
421
  withJSDocs: true,
448
- resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
422
+ resolveName: pluginManager.resolveName,
449
423
  enumType,
450
424
  optionalType,
451
425
  dateType
@@ -455,7 +429,7 @@ var OperationGenerator = class extends swagger.OperationGenerator {
455
429
  baseName: type.baseName,
456
430
  source,
457
431
  meta: {
458
- pluginKey: plugin.key,
432
+ pluginName,
459
433
  tag: operation.getTags()[0]?.name
460
434
  }
461
435
  };
@@ -473,7 +447,6 @@ var OperationGenerator = class extends swagger.OperationGenerator {
473
447
 
474
448
  // src/plugin.ts
475
449
  var pluginName = "swagger-ts";
476
- var pluginKey = ["schema", pluginName];
477
450
  var definePlugin = core.createPlugin((options) => {
478
451
  const {
479
452
  output = "types",
@@ -483,8 +456,7 @@ var definePlugin = core.createPlugin((options) => {
483
456
  enumType = "asConst",
484
457
  dateType = "string",
485
458
  optionalType = "questionToken",
486
- transformers: transformers2 = {},
487
- exportAs
459
+ transformers = {}
488
460
  } = options;
489
461
  const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller`;
490
462
  let pluginsOptions;
@@ -493,49 +465,47 @@ var definePlugin = core.createPlugin((options) => {
493
465
  options,
494
466
  kind: "schema",
495
467
  validate(plugins) {
496
- pluginsOptions = core.PluginManager.getDependedPlugins(plugins, [swagger.pluginName]);
468
+ pluginsOptions = core.getDependedPlugins(plugins, [swagger.pluginName]);
497
469
  return true;
498
470
  },
499
471
  resolvePath(baseName, directory, options2) {
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));
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));
502
474
  if (mode === "file") {
503
- return path__default.default.resolve(root, output);
475
+ return pathParser__default.default.resolve(root, output);
504
476
  }
505
477
  if (options2?.tag && groupBy?.type === "tag") {
506
478
  const tag = changeCase.camelCase(options2.tag, { delimiter: "", transform: changeCase.camelCaseTransformMerge });
507
- return path__default.default.resolve(root, utils.renderTemplate(template, { tag }), baseName);
479
+ return pathParser__default.default.resolve(root, core.renderTemplate(template, { tag }), baseName);
508
480
  }
509
- return path__default.default.resolve(root, output, baseName);
481
+ return pathParser__default.default.resolve(root, output, baseName);
510
482
  },
511
483
  resolveName(name) {
512
484
  const resolvedName = changeCase.pascalCase(name, { delimiter: "", stripRegexp: /[^A-Z0-9$]/gi, transform: changeCase.pascalCaseTransformMerge });
513
- return transformers2?.name?.(resolvedName) || resolvedName;
485
+ return transformers?.name?.(resolvedName) || resolvedName;
514
486
  },
515
- async writeFile(source, writePath) {
516
- if (!writePath.endsWith(".ts") || !source) {
487
+ async writeFile(source, path) {
488
+ if (!path.endsWith(".ts") || !source) {
517
489
  return;
518
490
  }
519
- return this.fileManager.write(source, writePath);
491
+ await this.fileManager.write(source, path);
520
492
  },
521
493
  async buildStart() {
522
494
  const [swaggerPlugin] = pluginsOptions;
523
495
  const oas = await swaggerPlugin.api.getOas();
524
496
  const schemas = await swaggerPlugin.api.getSchemas();
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 = {};
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));
528
499
  if (mode === "directory") {
529
500
  const builder = await new TypeBuilder({
530
- usedEnumNames,
531
- resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
501
+ resolveName: (params) => this.resolveName({ pluginName, ...params }),
532
502
  fileResolver: (name) => {
533
503
  const resolvedTypeId = this.resolvePath({
534
504
  baseName: `${name}.ts`,
535
- pluginKey: this.plugin.key
505
+ pluginName
536
506
  });
537
- const root2 = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key });
538
- return utils.getRelativePath(root2, resolvedTypeId);
507
+ const root2 = this.resolvePath({ baseName: ``, pluginName });
508
+ return core.getRelativePath(root2, resolvedTypeId);
539
509
  },
540
510
  withJSDocs: true,
541
511
  enumType,
@@ -549,16 +519,16 @@ var definePlugin = core.createPlugin((options) => {
549
519
  });
550
520
  });
551
521
  const mapFolderSchema = async ([name]) => {
552
- const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key });
553
- if (!resolvedPath) {
522
+ const path = this.resolvePath({ baseName: `${this.resolveName({ name, pluginName })}.ts`, pluginName });
523
+ if (!path) {
554
524
  return null;
555
525
  }
556
526
  return this.addFile({
557
- path: resolvedPath,
558
- baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
527
+ path,
528
+ baseName: `${this.resolveName({ name, pluginName })}.ts`,
559
529
  source: builder.print(name),
560
530
  meta: {
561
- pluginKey: this.plugin.key
531
+ pluginName
562
532
  }
563
533
  });
564
534
  };
@@ -567,8 +537,7 @@ var definePlugin = core.createPlugin((options) => {
567
537
  }
568
538
  if (mode === "file") {
569
539
  const builder = new TypeBuilder({
570
- usedEnumNames,
571
- resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
540
+ resolveName: (params) => this.resolveName({ pluginName, ...params }),
572
541
  withJSDocs: true,
573
542
  enumType,
574
543
  dateType,
@@ -580,18 +549,17 @@ var definePlugin = core.createPlugin((options) => {
580
549
  name
581
550
  });
582
551
  });
583
- const resolvedPath = this.resolvePath({ baseName: "", pluginKey: this.plugin.key });
584
- if (!resolvedPath) {
552
+ const path = this.resolvePath({ baseName: "", pluginName });
553
+ if (!path) {
585
554
  return;
586
555
  }
587
556
  await this.addFile({
588
- path: resolvedPath,
589
- baseName: output,
557
+ path,
558
+ baseName: `${this.resolveName({ name: output, pluginName })}.ts`,
590
559
  source: builder.print(),
591
560
  meta: {
592
- pluginKey: this.plugin.key
593
- },
594
- validate: false
561
+ pluginName
562
+ }
595
563
  });
596
564
  }
597
565
  const operationGenerator = new OperationGenerator(
@@ -599,13 +567,11 @@ var definePlugin = core.createPlugin((options) => {
599
567
  mode,
600
568
  enumType,
601
569
  dateType,
602
- optionalType,
603
- usedEnumNames
570
+ optionalType
604
571
  },
605
572
  {
606
573
  oas,
607
574
  pluginManager: this.pluginManager,
608
- plugin: this.plugin,
609
575
  contentType: swaggerPlugin.api.contentType,
610
576
  skipBy,
611
577
  overrideBy
@@ -618,45 +584,29 @@ var definePlugin = core.createPlugin((options) => {
618
584
  if (this.config.output.write === false) {
619
585
  return;
620
586
  }
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
- });
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");
646
592
  }
647
593
  };
648
594
  });
649
- function resolve2(props) {
650
- return swagger.resolve({ pluginKey, ...props });
595
+ function useResolve(props = {}) {
596
+ return swagger.useResolve({ pluginName, ...props });
651
597
  }
652
598
 
653
599
  // src/index.ts
654
600
  var src_default = definePlugin;
655
601
 
602
+ exports.OperationGenerator = OperationGenerator;
603
+ exports.TypeBuilder = TypeBuilder;
604
+ exports.TypeGenerator = TypeGenerator;
656
605
  exports.default = src_default;
657
606
  exports.definePlugin = definePlugin;
658
- exports.pluginKey = pluginKey;
607
+ exports.keywordTypeNodes = keywordTypeNodes;
659
608
  exports.pluginName = pluginName;
660
- exports.resolve = resolve2;
609
+ exports.resolve = resolve;
610
+ exports.useResolve = useResolve;
661
611
  //# sourceMappingURL=out.js.map
662
612
  //# sourceMappingURL=index.cjs.map