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

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