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