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