@kubb/swagger-ts 1.15.0-canary.20231026T132100 → 1.15.0-canary.20231026T182535

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
@@ -9,16 +9,26 @@ import * as factory from '@kubb/parser/factory';
9
9
  import { refsSorter, isReference } from '@kubb/swagger/utils';
10
10
 
11
11
  createRequire(import.meta.url);
12
+
13
+ var __accessCheck = (obj, member, msg) => {
14
+ if (!member.has(obj))
15
+ throw TypeError("Cannot " + msg);
16
+ };
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;
12
31
  var TypeGenerator = class extends SchemaGenerator {
13
- refs = {};
14
- extraNodes = [];
15
- aliases = [];
16
- // Keep track of already used type aliases
17
- #usedAliasNames = {};
18
- #caseOptions = {
19
- delimiter: "",
20
- stripRegexp: /[^A-Z0-9$]/gi
21
- };
22
32
  constructor(options = {
23
33
  usedEnumNames: {},
24
34
  withJSDocs: true,
@@ -28,6 +38,34 @@ var TypeGenerator = class extends SchemaGenerator {
28
38
  optionalType: "questionToken"
29
39
  }) {
30
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
+ });
31
69
  return this;
32
70
  }
33
71
  build({
@@ -37,7 +75,7 @@ var TypeGenerator = class extends SchemaGenerator {
37
75
  keysToOmit
38
76
  }) {
39
77
  const nodes = [];
40
- const type = this.#getTypeFromSchema(schema, baseName);
78
+ const type = __privateMethod(this, _getTypeFromSchema, getTypeFromSchema_fn).call(this, schema, baseName);
41
79
  if (!type) {
42
80
  return this.extraNodes;
43
81
  }
@@ -63,217 +101,205 @@ var TypeGenerator = class extends SchemaGenerator {
63
101
  );
64
102
  return [...this.extraNodes, ...filterdNodes];
65
103
  }
66
- /**
67
- * Creates a type node from a given schema.
68
- * Delegates to getBaseTypeFromSchema internally and
69
- * optionally adds a union with null.
70
- */
71
- #getTypeFromSchema(schema, name) {
72
- const type = this.#getBaseTypeFromSchema(schema, name);
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}` }));
73
128
  if (!type) {
74
129
  return null;
75
130
  }
76
- if (schema && !schema.nullable) {
77
- return type;
131
+ if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
132
+ type = factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.undefined] });
78
133
  }
79
- return factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.null] });
80
- }
81
- /**
82
- * Recursively creates a type literal with the given props.
83
- */
84
- #getTypeFromProperties(baseSchema, baseName) {
85
- const { optionalType } = this.options;
86
- const properties = baseSchema?.properties || {};
87
- const required = baseSchema?.required;
88
- const additionalProperties = baseSchema?.additionalProperties;
89
- const members = Object.keys(properties).map((name) => {
90
- const schema = properties[name];
91
- const isRequired = required && required.includes(name);
92
- let type = this.#getTypeFromSchema(schema, this.options.resolveName({ name: `${baseName || ""} ${name}` }));
93
- if (!type) {
94
- return null;
95
- }
96
- if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
97
- type = factory.createUnionDeclaration({ nodes: [type, factory.keywordTypeNodes.undefined] });
98
- }
99
- const propertySignature = factory.createPropertySignature({
100
- questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
101
- name,
102
- type,
103
- readOnly: schema.readOnly
104
- });
105
- if (this.options.withJSDocs) {
106
- return factory.appendJSDocToNode({
107
- node: propertySignature,
108
- comments: [
109
- schema.description ? `@description ${schema.description}` : void 0,
110
- schema.type ? `@type ${schema.type}${isRequired ? "" : " | undefined"} ${schema.format || ""}` : void 0,
111
- schema.example ? `@example ${schema.example}` : void 0,
112
- schema.deprecated ? `@deprecated` : void 0,
113
- schema.default !== void 0 && typeof schema.default === "string" ? `@default '${schema.default}'` : void 0,
114
- schema.default !== void 0 && typeof schema.default !== "string" ? `@default ${schema.default}` : void 0
115
- ].filter(Boolean)
116
- });
117
- }
118
- return propertySignature;
134
+ const propertySignature = factory.createPropertySignature({
135
+ questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
136
+ name,
137
+ type,
138
+ readOnly: schema.readOnly
119
139
  });
120
- if (additionalProperties) {
121
- const type = additionalProperties === true ? factory.keywordTypeNodes.any : this.#getTypeFromSchema(additionalProperties);
122
- if (type) {
123
- members.push(factory.createIndexSignature(type));
124
- }
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)
151
+ });
125
152
  }
126
- return factory.createTypeLiteralNode(members.filter(Boolean));
127
- }
128
- /**
129
- * Create a type alias for the schema referenced by the given ReferenceObject
130
- */
131
- #getRefAlias(obj, _baseName) {
132
- const { $ref } = obj;
133
- let ref = this.refs[$ref];
134
- if (ref) {
135
- return factory.createTypeReferenceNode(ref.propertyName, void 0);
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));
136
159
  }
137
- const originalName = getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
138
- const propertyName = this.options.resolveName({ name: originalName }) || originalName;
139
- ref = this.refs[$ref] = {
140
- propertyName,
141
- originalName
142
- };
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) {
143
168
  return factory.createTypeReferenceNode(ref.propertyName, void 0);
144
169
  }
145
- /**
146
- * This is the very core of the OpenAPI to TS conversion - it takes a
147
- * schema and returns the appropriate type.
148
- */
149
- #getBaseTypeFromSchema(schema, baseName) {
150
- if (!schema) {
151
- return factory.keywordTypeNodes.any;
152
- }
153
- if (isReference(schema)) {
154
- return this.#getRefAlias(schema, baseName);
155
- }
156
- if (schema.oneOf) {
157
- const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
158
- const union = factory.createUnionDeclaration({
159
- withParentheses: true,
160
- nodes: schema.oneOf.map((item) => {
161
- return this.#getBaseTypeFromSchema(item);
162
- }).filter((item) => {
163
- return item && item !== factory.keywordTypeNodes.any;
164
- })
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;
182
+ }
183
+ if (isReference(schema)) {
184
+ return __privateMethod(this, _getRefAlias, getRefAlias_fn).call(this, schema, baseName);
185
+ }
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)
165
199
  });
166
- if (schemaWithoutOneOf.properties) {
167
- return factory.createIntersectionDeclaration({
168
- nodes: [this.#getBaseTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean)
169
- });
170
- }
171
- return union;
172
200
  }
173
- if (schema.anyOf) {
174
- const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
175
- const union = factory.createUnionDeclaration({
176
- withParentheses: true,
177
- nodes: schema.anyOf.map((item) => {
178
- return this.#getBaseTypeFromSchema(item);
179
- }).filter((item) => {
180
- return item && item !== factory.keywordTypeNodes.any;
181
- })
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)
182
216
  });
183
- if (schemaWithoutAnyOf.properties) {
184
- return factory.createIntersectionDeclaration({
185
- nodes: [this.#getBaseTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean)
186
- });
187
- }
188
- return union;
189
217
  }
190
- if (schema.allOf) {
191
- const schemaWithoutAllOf = { ...schema, allOf: void 0 };
192
- const and = factory.createIntersectionDeclaration({
193
- withParentheses: true,
194
- nodes: schema.allOf.map((item) => {
195
- return this.#getBaseTypeFromSchema(item);
196
- }).filter((item) => {
197
- return item && item !== factory.keywordTypeNodes.any;
198
- })
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)
199
233
  });
200
- if (schemaWithoutAllOf.properties) {
201
- return factory.createIntersectionDeclaration({
202
- nodes: [this.#getBaseTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean)
203
- });
204
- }
205
- return and;
206
234
  }
207
- if (schema.enum && baseName) {
208
- const enumName = getUniqueName(baseName, this.options.usedEnumNames);
209
- let enums = [...new Set(schema.enum)].map((key) => [key, key]);
210
- if ("x-enumNames" in schema) {
211
- enums = [...new Set(schema["x-enumNames"])].map((key, index) => {
212
- return [key, schema.enum?.[index]];
213
- });
214
- }
215
- this.extraNodes.push(
216
- ...factory.createEnumDeclaration({
217
- name: camelCase(enumName, this.#caseOptions),
218
- typeName: this.options.resolveName({ name: enumName }),
219
- enums,
220
- type: this.options.enumType
221
- })
222
- );
223
- return factory.createTypeReferenceNode(this.options.resolveName({ name: enumName }), void 0);
224
- }
225
- if (schema.enum) {
226
- return factory.createUnionDeclaration({
227
- nodes: schema.enum.map((name) => {
228
- return factory.createLiteralTypeNode(typeof name === "number" ? factory.createNumericLiteral(name) : factory.createStringLiteral(`${name}`));
229
- })
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]];
230
243
  });
231
244
  }
232
- if ("items" in schema) {
233
- const node = this.#getTypeFromSchema(schema.items, baseName);
234
- if (node) {
235
- return factory.createArrayTypeNode(node);
236
- }
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);
237
266
  }
238
- if ("prefixItems" in schema) {
239
- const prefixItems = schema.prefixItems;
240
- return factory.createTupleDeclaration({
241
- nodes: prefixItems.map((item) => {
242
- return this.#getBaseTypeFromSchema(item, void 0);
243
- })
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)
244
290
  });
245
291
  }
246
- if (schema.properties || schema.additionalProperties) {
247
- return this.#getTypeFromProperties(schema, baseName);
248
- }
249
- if (schema.type) {
250
- if (Array.isArray(schema.type)) {
251
- const [type, nullable] = schema.type;
252
- return factory.createUnionDeclaration({
253
- nodes: [
254
- this.#getBaseTypeFromSchema(
255
- {
256
- ...schema,
257
- type
258
- },
259
- baseName
260
- ),
261
- nullable ? factory.createLiteralTypeNode(factory.createNull()) : void 0
262
- ].filter(Boolean)
263
- });
264
- }
265
- if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
266
- return factory.createTypeReferenceNode(factory.createIdentifier("Date"));
267
- }
268
- if (schema.type in factory.keywordTypeNodes) {
269
- return factory.keywordTypeNodes[schema.type];
270
- }
292
+ if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
293
+ return factory.createTypeReferenceNode(factory.createIdentifier("Date"));
271
294
  }
272
- if (schema.format === "binary") {
273
- return factory.createTypeReferenceNode("Blob", []);
295
+ if (schema.type in factory.keywordTypeNodes) {
296
+ return factory.keywordTypeNodes[schema.type];
274
297
  }
275
- return factory.keywordTypeNodes.any;
276
298
  }
299
+ if (schema.format === "binary") {
300
+ return factory.createTypeReferenceNode("Blob", []);
301
+ }
302
+ return factory.keywordTypeNodes.any;
277
303
  };
278
304
 
279
305
  // src/builders/TypeBuilder.ts