@kubb/swagger-ts 1.15.0-canary.20231112T135108 → 2.0.0-alpha.10

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
- }
143
- }
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);
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);
162
- }
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;
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
+ });
170
176
  }
171
- if (swagger.isReference(schema)) {
172
- return this.#getRefAlias(schema, baseName);
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));
173
183
  }
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
- })
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;
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
258
  }
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
- })
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);
266
- }
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
- }
289
- }
290
- if (schema.format === "binary") {
291
- return factory2.createTypeReferenceNode("Blob", []);
316
+ if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
317
+ return factory__namespace.createTypeReferenceNode(factory__namespace.createIdentifier("Date"));
292
318
  }
293
- if ("const" in schema && schema["const"] !== void 0 && typeof schema["const"] === "string") {
294
- return factory2.createLiteralTypeNode(factory2.createStringLiteral(schema["const"]));
319
+ if (schema.type in factory__namespace.keywordTypeNodes) {
320
+ return factory__namespace.keywordTypeNodes[schema.type];
295
321
  }
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,78 +382,87 @@ 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
  };
388
+ var _printCombinedSchema, printCombinedSchema_fn;
364
389
  var OperationGenerator = class extends swagger.OperationGenerator {
390
+ constructor() {
391
+ super(...arguments);
392
+ __privateAdd(this, _printCombinedSchema);
393
+ }
365
394
  resolve(operation) {
366
- const { pluginManager } = this.context;
395
+ const { pluginManager, plugin } = this.context;
367
396
  return swagger.resolve({
368
397
  operation,
369
398
  resolveName: pluginManager.resolveName,
370
399
  resolvePath: pluginManager.resolvePath,
371
- pluginName
400
+ pluginKey: plugin?.key
372
401
  });
373
402
  }
374
403
  async all() {
375
404
  return null;
376
405
  }
377
406
  async get(operation, schemas, options) {
378
- const { mode, enumType, dateType, optionalType } = options;
379
- const { pluginManager } = this.context;
407
+ const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
408
+ const { pluginManager, plugin } = this.context;
380
409
  const type = this.resolve(operation);
381
410
  const fileResolver = (name) => {
382
- const root = pluginManager.resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } });
411
+ const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
383
412
  const resolvedTypeId = pluginManager.resolvePath({
384
413
  baseName: `${name}.ts`,
385
- pluginName
414
+ pluginKey: plugin?.key
386
415
  });
387
- return core.getRelativePath(root, resolvedTypeId);
416
+ return utils.getRelativePath(root, resolvedTypeId);
388
417
  };
389
418
  const source = new TypeBuilder({
419
+ usedEnumNames,
390
420
  fileResolver: mode === "file" ? void 0 : fileResolver,
391
421
  withJSDocs: true,
392
- resolveName: pluginManager.resolveName,
422
+ resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
393
423
  enumType,
394
424
  optionalType,
395
425
  dateType
396
426
  }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.response).add(schemas.errors).configure().print();
427
+ const combinedSchemaSource = __privateMethod(this, _printCombinedSchema, printCombinedSchema_fn).call(this, type.name, operation, schemas);
397
428
  return {
398
429
  path: type.path,
399
430
  baseName: type.baseName,
400
- source,
431
+ source: [source, combinedSchemaSource].join("\n"),
401
432
  meta: {
402
- pluginName,
433
+ pluginKey: plugin.key,
403
434
  tag: operation.getTags()[0]?.name
404
435
  }
405
436
  };
406
437
  }
407
438
  async post(operation, schemas, options) {
408
- const { mode, enumType, dateType, optionalType } = options;
409
- const { pluginManager } = this.context;
439
+ const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
440
+ const { pluginManager, plugin } = this.context;
410
441
  const type = this.resolve(operation);
411
442
  const fileResolver = (name) => {
412
- const root = pluginManager.resolvePath({ baseName: type.baseName, pluginName, options: { tag: operation.getTags()[0]?.name } });
443
+ const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
413
444
  const resolvedTypeId = pluginManager.resolvePath({
414
445
  baseName: `${name}.ts`,
415
- pluginName
446
+ pluginKey: plugin?.key
416
447
  });
417
- return core.getRelativePath(root, resolvedTypeId);
448
+ return utils.getRelativePath(root, resolvedTypeId);
418
449
  };
419
450
  const source = new TypeBuilder({
451
+ usedEnumNames,
420
452
  fileResolver: mode === "file" ? void 0 : fileResolver,
421
453
  withJSDocs: true,
422
- resolveName: pluginManager.resolveName,
454
+ resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
423
455
  enumType,
424
456
  optionalType,
425
457
  dateType
426
458
  }).add(schemas.pathParams).add(schemas.queryParams).add(schemas.headerParams).add(schemas.request).add(schemas.response).add(schemas.errors).configure().print();
459
+ const combinedSchemaSource = __privateMethod(this, _printCombinedSchema, printCombinedSchema_fn).call(this, type.name, operation, schemas);
427
460
  return {
428
461
  path: type.path,
429
462
  baseName: type.baseName,
430
- source,
463
+ source: [source, combinedSchemaSource].join("\n"),
431
464
  meta: {
432
- pluginName,
465
+ pluginKey: plugin.key,
433
466
  tag: operation.getTags()[0]?.name
434
467
  }
435
468
  };
@@ -444,68 +477,134 @@ var OperationGenerator = class extends swagger.OperationGenerator {
444
477
  return this.post(operation, schemas, options);
445
478
  }
446
479
  };
480
+ _printCombinedSchema = new WeakSet();
481
+ printCombinedSchema_fn = function(name, operation, schemas) {
482
+ const properties = {
483
+ "response": factory__namespace.createTypeReferenceNode(
484
+ factory__namespace.createIdentifier(schemas.response.name),
485
+ void 0
486
+ )
487
+ };
488
+ if (schemas.request) {
489
+ properties["request"] = factory__namespace.createTypeReferenceNode(
490
+ factory__namespace.createIdentifier(schemas.request.name),
491
+ void 0
492
+ );
493
+ }
494
+ if (schemas.pathParams) {
495
+ properties["pathParams"] = factory__namespace.createTypeReferenceNode(
496
+ factory__namespace.createIdentifier(schemas.pathParams.name),
497
+ void 0
498
+ );
499
+ }
500
+ if (schemas.queryParams) {
501
+ properties["queryParams"] = factory__namespace.createTypeReferenceNode(
502
+ factory__namespace.createIdentifier(schemas.queryParams.name),
503
+ void 0
504
+ );
505
+ }
506
+ if (schemas.headerParams) {
507
+ properties["headerParams"] = factory__namespace.createTypeReferenceNode(
508
+ factory__namespace.createIdentifier(schemas.headerParams.name),
509
+ void 0
510
+ );
511
+ }
512
+ if (schemas.errors) {
513
+ properties["errors"] = factory__namespace.createUnionDeclaration({
514
+ nodes: schemas.errors.map((error) => {
515
+ return factory__namespace.createTypeReferenceNode(
516
+ factory__namespace.createIdentifier(error.name),
517
+ void 0
518
+ );
519
+ })
520
+ });
521
+ }
522
+ const namespaceNode = factory__namespace.createNamespaceDeclaration({
523
+ name: operation.method === "get" ? `${name}Query` : `${name}Mutation`,
524
+ statements: Object.keys(properties).map((key) => {
525
+ const type = properties[key];
526
+ if (!type) {
527
+ return void 0;
528
+ }
529
+ return factory__namespace.createTypeAliasDeclaration({
530
+ modifiers: [factory__namespace.modifiers.export],
531
+ name: changeCase.pascalCase(key),
532
+ type
533
+ });
534
+ }).filter(Boolean)
535
+ });
536
+ return parser.print(namespaceNode);
537
+ };
447
538
 
448
539
  // src/plugin.ts
449
540
  var pluginName = "swagger-ts";
541
+ var pluginKey = ["schema", pluginName];
450
542
  var definePlugin = core.createPlugin((options) => {
451
543
  const {
452
544
  output = "types",
453
- groupBy,
454
- skipBy = [],
455
- overrideBy = [],
545
+ group,
546
+ exclude = [],
547
+ include,
548
+ override = [],
456
549
  enumType = "asConst",
457
550
  dateType = "string",
458
551
  optionalType = "questionToken",
459
- transformers = {}
552
+ transformers: transformers2 = {},
553
+ exportAs
460
554
  } = options;
461
- const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller`;
555
+ const template = group?.output ? group.output : `${output}/{{tag}}Controller`;
462
556
  let pluginsOptions;
463
557
  return {
464
558
  name: pluginName,
465
559
  options,
466
560
  kind: "schema",
467
561
  validate(plugins) {
468
- pluginsOptions = core.getDependedPlugins(plugins, [swagger.pluginName]);
562
+ pluginsOptions = core.PluginManager.getDependedPlugins(plugins, [swagger.pluginName]);
469
563
  return true;
470
564
  },
471
565
  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));
566
+ const root = path__default.default.resolve(this.config.root, this.config.output.path);
567
+ const mode = core.FileManager.getMode(path__default.default.resolve(root, output));
474
568
  if (mode === "file") {
475
- return pathParser__default.default.resolve(root, output);
569
+ return path__default.default.resolve(root, output);
476
570
  }
477
- if (options2?.tag && groupBy?.type === "tag") {
571
+ if (options2?.tag && group?.type === "tag") {
478
572
  const tag = changeCase.camelCase(options2.tag, { delimiter: "", transform: changeCase.camelCaseTransformMerge });
479
- return pathParser__default.default.resolve(root, core.renderTemplate(template, { tag }), baseName);
573
+ return path__default.default.resolve(root, utils.renderTemplate(template, { tag }), baseName);
480
574
  }
481
- return pathParser__default.default.resolve(root, output, baseName);
575
+ return path__default.default.resolve(root, output, baseName);
482
576
  },
483
- resolveName(name) {
577
+ resolveName(name, type) {
484
578
  const resolvedName = changeCase.pascalCase(name, { delimiter: "", stripRegexp: /[^A-Z0-9$]/gi, transform: changeCase.pascalCaseTransformMerge });
485
- return transformers?.name?.(resolvedName) || resolvedName;
579
+ if (type) {
580
+ return transformers2?.name?.(resolvedName, type) || resolvedName;
581
+ }
582
+ return resolvedName;
486
583
  },
487
- async writeFile(source, path) {
488
- if (!path.endsWith(".ts") || !source) {
584
+ async writeFile(source, writePath) {
585
+ if (!writePath.endsWith(".ts") || !source) {
489
586
  return;
490
587
  }
491
- await this.fileManager.write(source, path);
588
+ return this.fileManager.write(source, writePath);
492
589
  },
493
590
  async buildStart() {
494
591
  const [swaggerPlugin] = pluginsOptions;
495
592
  const oas = await swaggerPlugin.api.getOas();
496
593
  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));
594
+ const root = path__default.default.resolve(this.config.root, this.config.output.path);
595
+ const mode = core.FileManager.getMode(path__default.default.resolve(root, output));
596
+ const usedEnumNames = {};
499
597
  if (mode === "directory") {
500
598
  const builder = await new TypeBuilder({
501
- resolveName: (params) => this.resolveName({ pluginName, ...params }),
599
+ usedEnumNames,
600
+ resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
502
601
  fileResolver: (name) => {
503
602
  const resolvedTypeId = this.resolvePath({
504
603
  baseName: `${name}.ts`,
505
- pluginName
604
+ pluginKey: this.plugin.key
506
605
  });
507
- const root2 = this.resolvePath({ baseName: ``, pluginName });
508
- return core.getRelativePath(root2, resolvedTypeId);
606
+ const root2 = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key });
607
+ return utils.getRelativePath(root2, resolvedTypeId);
509
608
  },
510
609
  withJSDocs: true,
511
610
  enumType,
@@ -519,16 +618,16 @@ var definePlugin = core.createPlugin((options) => {
519
618
  });
520
619
  });
521
620
  const mapFolderSchema = async ([name]) => {
522
- const path = this.resolvePath({ baseName: `${this.resolveName({ name, pluginName })}.ts`, pluginName });
523
- if (!path) {
621
+ const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key });
622
+ if (!resolvedPath) {
524
623
  return null;
525
624
  }
526
625
  return this.addFile({
527
- path,
528
- baseName: `${this.resolveName({ name, pluginName })}.ts`,
626
+ path: resolvedPath,
627
+ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
529
628
  source: builder.print(name),
530
629
  meta: {
531
- pluginName
630
+ pluginKey: this.plugin.key
532
631
  }
533
632
  });
534
633
  };
@@ -537,7 +636,8 @@ var definePlugin = core.createPlugin((options) => {
537
636
  }
538
637
  if (mode === "file") {
539
638
  const builder = new TypeBuilder({
540
- resolveName: (params) => this.resolveName({ pluginName, ...params }),
639
+ usedEnumNames,
640
+ resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
541
641
  withJSDocs: true,
542
642
  enumType,
543
643
  dateType,
@@ -549,17 +649,18 @@ var definePlugin = core.createPlugin((options) => {
549
649
  name
550
650
  });
551
651
  });
552
- const path = this.resolvePath({ baseName: "", pluginName });
553
- if (!path) {
652
+ const resolvedPath = this.resolvePath({ baseName: "", pluginKey: this.plugin.key });
653
+ if (!resolvedPath) {
554
654
  return;
555
655
  }
556
656
  await this.addFile({
557
- path,
558
- baseName: `${this.resolveName({ name: output, pluginName })}.ts`,
657
+ path: resolvedPath,
658
+ baseName: output,
559
659
  source: builder.print(),
560
660
  meta: {
561
- pluginName
562
- }
661
+ pluginKey: this.plugin.key
662
+ },
663
+ validate: false
563
664
  });
564
665
  }
565
666
  const operationGenerator = new OperationGenerator(
@@ -567,14 +668,17 @@ var definePlugin = core.createPlugin((options) => {
567
668
  mode,
568
669
  enumType,
569
670
  dateType,
570
- optionalType
671
+ optionalType,
672
+ usedEnumNames
571
673
  },
572
674
  {
573
675
  oas,
574
676
  pluginManager: this.pluginManager,
677
+ plugin: this.plugin,
575
678
  contentType: swaggerPlugin.api.contentType,
576
- skipBy,
577
- overrideBy
679
+ exclude,
680
+ include,
681
+ override
578
682
  }
579
683
  );
580
684
  const files = await operationGenerator.build();
@@ -584,29 +688,41 @@ var definePlugin = core.createPlugin((options) => {
584
688
  if (this.config.output.write === false) {
585
689
  return;
586
690
  }
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");
691
+ const root = path__default.default.resolve(this.config.root, this.config.output.path);
692
+ await this.fileManager.addIndexes({
693
+ root,
694
+ extName: ".ts",
695
+ meta: { pluginKey: this.plugin.key },
696
+ options: {
697
+ map: (file) => {
698
+ return {
699
+ ...file,
700
+ exports: file.exports?.map((item) => {
701
+ if (exportAs) {
702
+ return {
703
+ ...item,
704
+ name: exportAs,
705
+ asAlias: !!exportAs
706
+ };
707
+ }
708
+ return item;
709
+ })
710
+ };
711
+ },
712
+ output,
713
+ isTypeOnly: true
714
+ }
715
+ });
592
716
  }
593
717
  };
594
718
  });
595
- function useResolve(props = {}) {
596
- return swagger.useResolve({ pluginName, ...props });
597
- }
598
719
 
599
720
  // src/index.ts
600
721
  var src_default = definePlugin;
601
722
 
602
- exports.OperationGenerator = OperationGenerator;
603
- exports.TypeBuilder = TypeBuilder;
604
- exports.TypeGenerator = TypeGenerator;
605
723
  exports.default = src_default;
606
724
  exports.definePlugin = definePlugin;
607
- exports.keywordTypeNodes = keywordTypeNodes;
725
+ exports.pluginKey = pluginKey;
608
726
  exports.pluginName = pluginName;
609
- exports.resolve = resolve;
610
- exports.useResolve = useResolve;
611
727
  //# sourceMappingURL=out.js.map
612
728
  //# sourceMappingURL=index.cjs.map