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