@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.cjs CHANGED
@@ -34,17 +34,25 @@ function _interopNamespace(e) {
34
34
  var path__default = /*#__PURE__*/_interopDefault(path);
35
35
  var factory__namespace = /*#__PURE__*/_interopNamespace(factory);
36
36
 
37
- // src/plugin.ts
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;
38
55
  var TypeGenerator = class extends core.SchemaGenerator {
39
- refs = {};
40
- extraNodes = [];
41
- aliases = [];
42
- // Keep track of already used type aliases
43
- #usedAliasNames = {};
44
- #caseOptions = {
45
- delimiter: "",
46
- stripRegexp: /[^A-Z0-9$]/gi
47
- };
48
56
  constructor(options = {
49
57
  usedEnumNames: {},
50
58
  withJSDocs: true,
@@ -54,6 +62,34 @@ var TypeGenerator = class extends core.SchemaGenerator {
54
62
  optionalType: "questionToken"
55
63
  }) {
56
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
+ });
57
93
  return this;
58
94
  }
59
95
  build({
@@ -63,7 +99,7 @@ var TypeGenerator = class extends core.SchemaGenerator {
63
99
  keysToOmit
64
100
  }) {
65
101
  const nodes = [];
66
- const type = this.#getTypeFromSchema(schema, baseName);
102
+ const type = __privateMethod(this, _getTypeFromSchema, getTypeFromSchema_fn).call(this, schema, baseName);
67
103
  if (!type) {
68
104
  return this.extraNodes;
69
105
  }
@@ -89,217 +125,205 @@ var TypeGenerator = class extends core.SchemaGenerator {
89
125
  );
90
126
  return [...this.extraNodes, ...filterdNodes];
91
127
  }
92
- /**
93
- * Creates a type node from a given schema.
94
- * Delegates to getBaseTypeFromSchema internally and
95
- * optionally adds a union with null.
96
- */
97
- #getTypeFromSchema(schema, name) {
98
- 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}` }));
99
152
  if (!type) {
100
153
  return null;
101
154
  }
102
- if (schema && !schema.nullable) {
103
- return type;
155
+ if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
156
+ type = factory__namespace.createUnionDeclaration({ nodes: [type, factory__namespace.keywordTypeNodes.undefined] });
104
157
  }
105
- return factory__namespace.createUnionDeclaration({ nodes: [type, factory__namespace.keywordTypeNodes.null] });
106
- }
107
- /**
108
- * Recursively creates a type literal with the given props.
109
- */
110
- #getTypeFromProperties(baseSchema, baseName) {
111
- const { optionalType } = this.options;
112
- const properties = baseSchema?.properties || {};
113
- const required = baseSchema?.required;
114
- const additionalProperties = baseSchema?.additionalProperties;
115
- const members = Object.keys(properties).map((name) => {
116
- const schema = properties[name];
117
- const isRequired = required && required.includes(name);
118
- let type = this.#getTypeFromSchema(schema, this.options.resolveName({ name: `${baseName || ""} ${name}` }));
119
- if (!type) {
120
- return null;
121
- }
122
- if (!isRequired && ["undefined", "questionTokenAndUndefined"].includes(optionalType)) {
123
- type = factory__namespace.createUnionDeclaration({ nodes: [type, factory__namespace.keywordTypeNodes.undefined] });
124
- }
125
- const propertySignature = factory__namespace.createPropertySignature({
126
- questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
127
- name,
128
- type,
129
- readOnly: schema.readOnly
130
- });
131
- if (this.options.withJSDocs) {
132
- return factory__namespace.appendJSDocToNode({
133
- node: propertySignature,
134
- comments: [
135
- schema.description ? `@description ${schema.description}` : void 0,
136
- schema.type ? `@type ${schema.type}${isRequired ? "" : " | undefined"} ${schema.format || ""}` : void 0,
137
- schema.example ? `@example ${schema.example}` : void 0,
138
- schema.deprecated ? `@deprecated` : void 0,
139
- schema.default !== void 0 && typeof schema.default === "string" ? `@default '${schema.default}'` : void 0,
140
- schema.default !== void 0 && typeof schema.default !== "string" ? `@default ${schema.default}` : void 0
141
- ].filter(Boolean)
142
- });
143
- }
144
- return propertySignature;
158
+ const propertySignature = factory__namespace.createPropertySignature({
159
+ questionToken: ["questionToken", "questionTokenAndUndefined"].includes(optionalType) && !isRequired,
160
+ name,
161
+ type,
162
+ readOnly: schema.readOnly
145
163
  });
146
- if (additionalProperties) {
147
- const type = additionalProperties === true ? factory__namespace.keywordTypeNodes.any : this.#getTypeFromSchema(additionalProperties);
148
- if (type) {
149
- members.push(factory__namespace.createIndexSignature(type));
150
- }
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
+ });
151
176
  }
152
- return factory__namespace.createTypeLiteralNode(members.filter(Boolean));
153
- }
154
- /**
155
- * Create a type alias for the schema referenced by the given ReferenceObject
156
- */
157
- #getRefAlias(obj, _baseName) {
158
- const { $ref } = obj;
159
- let ref = this.refs[$ref];
160
- if (ref) {
161
- return factory__namespace.createTypeReferenceNode(ref.propertyName, void 0);
177
+ return propertySignature;
178
+ });
179
+ if (additionalProperties) {
180
+ const type = additionalProperties === true ? factory__namespace.keywordTypeNodes.any : __privateMethod(this, _getTypeFromSchema, getTypeFromSchema_fn).call(this, additionalProperties);
181
+ if (type) {
182
+ members.push(factory__namespace.createIndexSignature(type));
162
183
  }
163
- const originalName = utils.getUniqueName($ref.replace(/.+\//, ""), this.#usedAliasNames);
164
- const propertyName = this.options.resolveName({ name: originalName }) || originalName;
165
- ref = this.refs[$ref] = {
166
- propertyName,
167
- originalName
168
- };
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) {
169
192
  return factory__namespace.createTypeReferenceNode(ref.propertyName, void 0);
170
193
  }
171
- /**
172
- * This is the very core of the OpenAPI to TS conversion - it takes a
173
- * schema and returns the appropriate type.
174
- */
175
- #getBaseTypeFromSchema(schema, baseName) {
176
- if (!schema) {
177
- return factory__namespace.keywordTypeNodes.any;
178
- }
179
- if (utils$1.isReference(schema)) {
180
- return this.#getRefAlias(schema, baseName);
181
- }
182
- if (schema.oneOf) {
183
- const schemaWithoutOneOf = { ...schema, oneOf: void 0 };
184
- const union = factory__namespace.createUnionDeclaration({
185
- withParentheses: true,
186
- nodes: schema.oneOf.map((item) => {
187
- return this.#getBaseTypeFromSchema(item);
188
- }).filter((item) => {
189
- return item && item !== factory__namespace.keywordTypeNodes.any;
190
- })
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)
191
223
  });
192
- if (schemaWithoutOneOf.properties) {
193
- return factory__namespace.createIntersectionDeclaration({
194
- nodes: [this.#getBaseTypeFromSchema(schemaWithoutOneOf, baseName), union].filter(Boolean)
195
- });
196
- }
197
- return union;
198
224
  }
199
- if (schema.anyOf) {
200
- const schemaWithoutAnyOf = { ...schema, anyOf: void 0 };
201
- const union = factory__namespace.createUnionDeclaration({
202
- withParentheses: true,
203
- nodes: schema.anyOf.map((item) => {
204
- return this.#getBaseTypeFromSchema(item);
205
- }).filter((item) => {
206
- return item && item !== factory__namespace.keywordTypeNodes.any;
207
- })
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)
208
240
  });
209
- if (schemaWithoutAnyOf.properties) {
210
- return factory__namespace.createIntersectionDeclaration({
211
- nodes: [this.#getBaseTypeFromSchema(schemaWithoutAnyOf, baseName), union].filter(Boolean)
212
- });
213
- }
214
- return union;
215
241
  }
216
- if (schema.allOf) {
217
- const schemaWithoutAllOf = { ...schema, allOf: void 0 };
218
- const and = factory__namespace.createIntersectionDeclaration({
219
- withParentheses: true,
220
- nodes: schema.allOf.map((item) => {
221
- return this.#getBaseTypeFromSchema(item);
222
- }).filter((item) => {
223
- return item && item !== factory__namespace.keywordTypeNodes.any;
224
- })
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)
225
257
  });
226
- if (schemaWithoutAllOf.properties) {
227
- return factory__namespace.createIntersectionDeclaration({
228
- nodes: [this.#getBaseTypeFromSchema(schemaWithoutAllOf, baseName), and].filter(Boolean)
229
- });
230
- }
231
- return and;
232
258
  }
233
- if (schema.enum && baseName) {
234
- const enumName = utils.getUniqueName(baseName, this.options.usedEnumNames);
235
- let enums = [...new Set(schema.enum)].map((key) => [key, key]);
236
- if ("x-enumNames" in schema) {
237
- enums = [...new Set(schema["x-enumNames"])].map((key, index) => {
238
- return [key, schema.enum?.[index]];
239
- });
240
- }
241
- this.extraNodes.push(
242
- ...factory__namespace.createEnumDeclaration({
243
- name: changeCase.camelCase(enumName, this.#caseOptions),
244
- typeName: this.options.resolveName({ name: enumName }),
245
- enums,
246
- type: this.options.enumType
247
- })
248
- );
249
- return factory__namespace.createTypeReferenceNode(this.options.resolveName({ name: enumName }), void 0);
250
- }
251
- if (schema.enum) {
252
- return factory__namespace.createUnionDeclaration({
253
- nodes: schema.enum.map((name) => {
254
- return factory__namespace.createLiteralTypeNode(typeof name === "number" ? factory__namespace.createNumericLiteral(name) : factory__namespace.createStringLiteral(`${name}`));
255
- })
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]];
256
267
  });
257
268
  }
258
- if ("items" in schema) {
259
- const node = this.#getTypeFromSchema(schema.items, baseName);
260
- if (node) {
261
- return factory__namespace.createArrayTypeNode(node);
262
- }
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);
263
290
  }
264
- if ("prefixItems" in schema) {
265
- const prefixItems = schema.prefixItems;
266
- return factory__namespace.createTupleDeclaration({
267
- nodes: prefixItems.map((item) => {
268
- return this.#getBaseTypeFromSchema(item, void 0);
269
- })
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)
270
314
  });
271
315
  }
272
- if (schema.properties || schema.additionalProperties) {
273
- return this.#getTypeFromProperties(schema, baseName);
274
- }
275
- if (schema.type) {
276
- if (Array.isArray(schema.type)) {
277
- const [type, nullable] = schema.type;
278
- return factory__namespace.createUnionDeclaration({
279
- nodes: [
280
- this.#getBaseTypeFromSchema(
281
- {
282
- ...schema,
283
- type
284
- },
285
- baseName
286
- ),
287
- nullable ? factory__namespace.createLiteralTypeNode(factory__namespace.createNull()) : void 0
288
- ].filter(Boolean)
289
- });
290
- }
291
- if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
292
- return factory__namespace.createTypeReferenceNode(factory__namespace.createIdentifier("Date"));
293
- }
294
- if (schema.type in factory__namespace.keywordTypeNodes) {
295
- return factory__namespace.keywordTypeNodes[schema.type];
296
- }
316
+ if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
317
+ return factory__namespace.createTypeReferenceNode(factory__namespace.createIdentifier("Date"));
297
318
  }
298
- if (schema.format === "binary") {
299
- return factory__namespace.createTypeReferenceNode("Blob", []);
319
+ if (schema.type in factory__namespace.keywordTypeNodes) {
320
+ return factory__namespace.keywordTypeNodes[schema.type];
300
321
  }
301
- return factory__namespace.keywordTypeNodes.any;
302
322
  }
323
+ if (schema.format === "binary") {
324
+ return factory__namespace.createTypeReferenceNode("Blob", []);
325
+ }
326
+ return factory__namespace.keywordTypeNodes.any;
303
327
  };
304
328
 
305
329
  // src/builders/TypeBuilder.ts