@kubb/swagger-ts 1.15.0-canary.20231027T201109 → 2.0.0-alpha.1
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 +656 -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 +634 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.cjs +354 -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 +334 -296
- package/dist/index.js.map +1 -1
- package/package.json +11 -6
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,211 @@ 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
|
-
}
|
319
|
+
if (schema.type in factory__namespace.keywordTypeNodes) {
|
320
|
+
return factory__namespace.keywordTypeNodes[schema.type];
|
289
321
|
}
|
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"]));
|
295
|
-
}
|
296
|
-
return keywordTypeNodes.any;
|
297
322
|
}
|
323
|
+
if (schema.format === "binary") {
|
324
|
+
return factory__namespace.createTypeReferenceNode("Blob", []);
|
325
|
+
}
|
326
|
+
if ("const" in schema && schema["const"] !== void 0 && typeof schema["const"] === "string") {
|
327
|
+
return factory__namespace.createLiteralTypeNode(factory__namespace.createStringLiteral(schema["const"]));
|
328
|
+
}
|
329
|
+
return factory__namespace.keywordTypeNodes.any;
|
298
330
|
};
|
299
331
|
|
300
332
|
// 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
333
|
var TypeBuilder = class extends swagger.OasBuilder {
|
311
334
|
configure(options) {
|
312
335
|
if (options) {
|
@@ -319,8 +342,9 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
319
342
|
}
|
320
343
|
print(name) {
|
321
344
|
const codes = [];
|
322
|
-
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(
|
345
|
+
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(utils.transformers.nameSorter).map((operationSchema) => {
|
323
346
|
const generator = new TypeGenerator({
|
347
|
+
usedEnumNames: this.options.usedEnumNames,
|
324
348
|
withJSDocs: this.options.withJSDocs,
|
325
349
|
resolveName: this.options.resolveName,
|
326
350
|
enumType: this.options.enumType,
|
@@ -340,7 +364,7 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
340
364
|
},
|
341
365
|
sources
|
342
366
|
};
|
343
|
-
}).sort(refsSorter);
|
367
|
+
}).sort(utils$1.refsSorter);
|
344
368
|
generated.forEach((item) => {
|
345
369
|
codes.push(parser.print(item.sources));
|
346
370
|
});
|
@@ -349,7 +373,7 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
349
373
|
const importMeta = importsGenerator.build(generated.map((item) => item.import));
|
350
374
|
if (importMeta) {
|
351
375
|
const nodes = importMeta.map((item) => {
|
352
|
-
return
|
376
|
+
return factory__namespace.createImportDeclaration({
|
353
377
|
name: [{ propertyName: item.ref.propertyName }],
|
354
378
|
path: item.path,
|
355
379
|
isTypeOnly: true
|
@@ -358,38 +382,39 @@ var TypeBuilder = class extends swagger.OasBuilder {
|
|
358
382
|
codes.unshift(parser.print(nodes));
|
359
383
|
}
|
360
384
|
}
|
361
|
-
return
|
385
|
+
return utils.transformers.combineCodes(codes);
|
362
386
|
}
|
363
387
|
};
|
364
388
|
var OperationGenerator = class extends swagger.OperationGenerator {
|
365
389
|
resolve(operation) {
|
366
|
-
const { pluginManager } = this.context;
|
390
|
+
const { pluginManager, plugin } = this.context;
|
367
391
|
return swagger.resolve({
|
368
392
|
operation,
|
369
393
|
resolveName: pluginManager.resolveName,
|
370
394
|
resolvePath: pluginManager.resolvePath,
|
371
|
-
|
395
|
+
pluginKey: plugin?.key
|
372
396
|
});
|
373
397
|
}
|
374
398
|
async all() {
|
375
399
|
return null;
|
376
400
|
}
|
377
401
|
async get(operation, schemas, options) {
|
378
|
-
const { mode, enumType, dateType, optionalType } = options;
|
379
|
-
const { pluginManager } = this.context;
|
402
|
+
const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
|
403
|
+
const { pluginManager, plugin } = this.context;
|
380
404
|
const type = this.resolve(operation);
|
381
405
|
const fileResolver = (name) => {
|
382
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName,
|
406
|
+
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
|
383
407
|
const resolvedTypeId = pluginManager.resolvePath({
|
384
408
|
baseName: `${name}.ts`,
|
385
|
-
|
409
|
+
pluginKey: plugin?.key
|
386
410
|
});
|
387
|
-
return
|
411
|
+
return utils.getRelativePath(root, resolvedTypeId);
|
388
412
|
};
|
389
413
|
const source = new TypeBuilder({
|
414
|
+
usedEnumNames,
|
390
415
|
fileResolver: mode === "file" ? void 0 : fileResolver,
|
391
416
|
withJSDocs: true,
|
392
|
-
resolveName: pluginManager.resolveName,
|
417
|
+
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
393
418
|
enumType,
|
394
419
|
optionalType,
|
395
420
|
dateType
|
@@ -399,27 +424,28 @@ var OperationGenerator = class extends swagger.OperationGenerator {
|
|
399
424
|
baseName: type.baseName,
|
400
425
|
source,
|
401
426
|
meta: {
|
402
|
-
|
427
|
+
pluginKey: plugin.key,
|
403
428
|
tag: operation.getTags()[0]?.name
|
404
429
|
}
|
405
430
|
};
|
406
431
|
}
|
407
432
|
async post(operation, schemas, options) {
|
408
|
-
const { mode, enumType, dateType, optionalType } = options;
|
409
|
-
const { pluginManager } = this.context;
|
433
|
+
const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
|
434
|
+
const { pluginManager, plugin } = this.context;
|
410
435
|
const type = this.resolve(operation);
|
411
436
|
const fileResolver = (name) => {
|
412
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName,
|
437
|
+
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
|
413
438
|
const resolvedTypeId = pluginManager.resolvePath({
|
414
439
|
baseName: `${name}.ts`,
|
415
|
-
|
440
|
+
pluginKey: plugin?.key
|
416
441
|
});
|
417
|
-
return
|
442
|
+
return utils.getRelativePath(root, resolvedTypeId);
|
418
443
|
};
|
419
444
|
const source = new TypeBuilder({
|
445
|
+
usedEnumNames,
|
420
446
|
fileResolver: mode === "file" ? void 0 : fileResolver,
|
421
447
|
withJSDocs: true,
|
422
|
-
resolveName: pluginManager.resolveName,
|
448
|
+
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
423
449
|
enumType,
|
424
450
|
optionalType,
|
425
451
|
dateType
|
@@ -429,7 +455,7 @@ var OperationGenerator = class extends swagger.OperationGenerator {
|
|
429
455
|
baseName: type.baseName,
|
430
456
|
source,
|
431
457
|
meta: {
|
432
|
-
|
458
|
+
pluginKey: plugin.key,
|
433
459
|
tag: operation.getTags()[0]?.name
|
434
460
|
}
|
435
461
|
};
|
@@ -447,6 +473,7 @@ var OperationGenerator = class extends swagger.OperationGenerator {
|
|
447
473
|
|
448
474
|
// src/plugin.ts
|
449
475
|
var pluginName = "swagger-ts";
|
476
|
+
var pluginKey = ["schema", pluginName];
|
450
477
|
var definePlugin = core.createPlugin((options) => {
|
451
478
|
const {
|
452
479
|
output = "types",
|
@@ -456,7 +483,8 @@ var definePlugin = core.createPlugin((options) => {
|
|
456
483
|
enumType = "asConst",
|
457
484
|
dateType = "string",
|
458
485
|
optionalType = "questionToken",
|
459
|
-
transformers = {}
|
486
|
+
transformers: transformers2 = {},
|
487
|
+
exportAs
|
460
488
|
} = options;
|
461
489
|
const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller`;
|
462
490
|
let pluginsOptions;
|
@@ -465,47 +493,49 @@ var definePlugin = core.createPlugin((options) => {
|
|
465
493
|
options,
|
466
494
|
kind: "schema",
|
467
495
|
validate(plugins) {
|
468
|
-
pluginsOptions = core.getDependedPlugins(plugins, [swagger.pluginName]);
|
496
|
+
pluginsOptions = core.PluginManager.getDependedPlugins(plugins, [swagger.pluginName]);
|
469
497
|
return true;
|
470
498
|
},
|
471
499
|
resolvePath(baseName, directory, options2) {
|
472
|
-
const root =
|
473
|
-
const mode = core.
|
500
|
+
const root = path__default.default.resolve(this.config.root, this.config.output.path);
|
501
|
+
const mode = core.FileManager.getMode(path__default.default.resolve(root, output));
|
474
502
|
if (mode === "file") {
|
475
|
-
return
|
503
|
+
return path__default.default.resolve(root, output);
|
476
504
|
}
|
477
505
|
if (options2?.tag && groupBy?.type === "tag") {
|
478
506
|
const tag = changeCase.camelCase(options2.tag, { delimiter: "", transform: changeCase.camelCaseTransformMerge });
|
479
|
-
return
|
507
|
+
return path__default.default.resolve(root, utils.renderTemplate(template, { tag }), baseName);
|
480
508
|
}
|
481
|
-
return
|
509
|
+
return path__default.default.resolve(root, output, baseName);
|
482
510
|
},
|
483
511
|
resolveName(name) {
|
484
512
|
const resolvedName = changeCase.pascalCase(name, { delimiter: "", stripRegexp: /[^A-Z0-9$]/gi, transform: changeCase.pascalCaseTransformMerge });
|
485
|
-
return
|
513
|
+
return transformers2?.name?.(resolvedName) || resolvedName;
|
486
514
|
},
|
487
|
-
async writeFile(source,
|
488
|
-
if (!
|
515
|
+
async writeFile(source, writePath) {
|
516
|
+
if (!writePath.endsWith(".ts") || !source) {
|
489
517
|
return;
|
490
518
|
}
|
491
|
-
|
519
|
+
return this.fileManager.write(source, writePath);
|
492
520
|
},
|
493
521
|
async buildStart() {
|
494
522
|
const [swaggerPlugin] = pluginsOptions;
|
495
523
|
const oas = await swaggerPlugin.api.getOas();
|
496
524
|
const schemas = await swaggerPlugin.api.getSchemas();
|
497
|
-
const root =
|
498
|
-
const mode = core.
|
525
|
+
const root = path__default.default.resolve(this.config.root, this.config.output.path);
|
526
|
+
const mode = core.FileManager.getMode(path__default.default.resolve(root, output));
|
527
|
+
const usedEnumNames = {};
|
499
528
|
if (mode === "directory") {
|
500
529
|
const builder = await new TypeBuilder({
|
501
|
-
|
530
|
+
usedEnumNames,
|
531
|
+
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
502
532
|
fileResolver: (name) => {
|
503
533
|
const resolvedTypeId = this.resolvePath({
|
504
534
|
baseName: `${name}.ts`,
|
505
|
-
|
535
|
+
pluginKey: this.plugin.key
|
506
536
|
});
|
507
|
-
const root2 = this.resolvePath({ baseName: ``,
|
508
|
-
return
|
537
|
+
const root2 = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key });
|
538
|
+
return utils.getRelativePath(root2, resolvedTypeId);
|
509
539
|
},
|
510
540
|
withJSDocs: true,
|
511
541
|
enumType,
|
@@ -519,16 +549,16 @@ var definePlugin = core.createPlugin((options) => {
|
|
519
549
|
});
|
520
550
|
});
|
521
551
|
const mapFolderSchema = async ([name]) => {
|
522
|
-
const
|
523
|
-
if (!
|
552
|
+
const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key });
|
553
|
+
if (!resolvedPath) {
|
524
554
|
return null;
|
525
555
|
}
|
526
556
|
return this.addFile({
|
527
|
-
path,
|
528
|
-
baseName: `${this.resolveName({ name,
|
557
|
+
path: resolvedPath,
|
558
|
+
baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
|
529
559
|
source: builder.print(name),
|
530
560
|
meta: {
|
531
|
-
|
561
|
+
pluginKey: this.plugin.key
|
532
562
|
}
|
533
563
|
});
|
534
564
|
};
|
@@ -537,7 +567,8 @@ var definePlugin = core.createPlugin((options) => {
|
|
537
567
|
}
|
538
568
|
if (mode === "file") {
|
539
569
|
const builder = new TypeBuilder({
|
540
|
-
|
570
|
+
usedEnumNames,
|
571
|
+
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
541
572
|
withJSDocs: true,
|
542
573
|
enumType,
|
543
574
|
dateType,
|
@@ -549,17 +580,18 @@ var definePlugin = core.createPlugin((options) => {
|
|
549
580
|
name
|
550
581
|
});
|
551
582
|
});
|
552
|
-
const
|
553
|
-
if (!
|
583
|
+
const resolvedPath = this.resolvePath({ baseName: "", pluginKey: this.plugin.key });
|
584
|
+
if (!resolvedPath) {
|
554
585
|
return;
|
555
586
|
}
|
556
587
|
await this.addFile({
|
557
|
-
path,
|
558
|
-
baseName:
|
588
|
+
path: resolvedPath,
|
589
|
+
baseName: output,
|
559
590
|
source: builder.print(),
|
560
591
|
meta: {
|
561
|
-
|
562
|
-
}
|
592
|
+
pluginKey: this.plugin.key
|
593
|
+
},
|
594
|
+
validate: false
|
563
595
|
});
|
564
596
|
}
|
565
597
|
const operationGenerator = new OperationGenerator(
|
@@ -567,11 +599,13 @@ var definePlugin = core.createPlugin((options) => {
|
|
567
599
|
mode,
|
568
600
|
enumType,
|
569
601
|
dateType,
|
570
|
-
optionalType
|
602
|
+
optionalType,
|
603
|
+
usedEnumNames
|
571
604
|
},
|
572
605
|
{
|
573
606
|
oas,
|
574
607
|
pluginManager: this.pluginManager,
|
608
|
+
plugin: this.plugin,
|
575
609
|
contentType: swaggerPlugin.api.contentType,
|
576
610
|
skipBy,
|
577
611
|
overrideBy
|
@@ -584,29 +618,45 @@ var definePlugin = core.createPlugin((options) => {
|
|
584
618
|
if (this.config.output.write === false) {
|
585
619
|
return;
|
586
620
|
}
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
621
|
+
const root = path__default.default.resolve(this.config.root, this.config.output.path);
|
622
|
+
await this.fileManager.addIndexes({
|
623
|
+
root,
|
624
|
+
extName: ".ts",
|
625
|
+
meta: { pluginKey: this.plugin.key },
|
626
|
+
options: {
|
627
|
+
map: (file) => {
|
628
|
+
return {
|
629
|
+
...file,
|
630
|
+
exports: file.exports?.map((item) => {
|
631
|
+
if (exportAs) {
|
632
|
+
return {
|
633
|
+
...item,
|
634
|
+
name: exportAs,
|
635
|
+
asAlias: !!exportAs
|
636
|
+
};
|
637
|
+
}
|
638
|
+
return item;
|
639
|
+
})
|
640
|
+
};
|
641
|
+
},
|
642
|
+
output,
|
643
|
+
isTypeOnly: true
|
644
|
+
}
|
645
|
+
});
|
592
646
|
}
|
593
647
|
};
|
594
648
|
});
|
595
|
-
function
|
596
|
-
return swagger.
|
649
|
+
function resolve2(props) {
|
650
|
+
return swagger.resolve({ pluginKey, ...props });
|
597
651
|
}
|
598
652
|
|
599
653
|
// src/index.ts
|
600
654
|
var src_default = definePlugin;
|
601
655
|
|
602
|
-
exports.OperationGenerator = OperationGenerator;
|
603
|
-
exports.TypeBuilder = TypeBuilder;
|
604
|
-
exports.TypeGenerator = TypeGenerator;
|
605
656
|
exports.default = src_default;
|
606
657
|
exports.definePlugin = definePlugin;
|
607
|
-
exports.
|
658
|
+
exports.pluginKey = pluginKey;
|
608
659
|
exports.pluginName = pluginName;
|
609
|
-
exports.resolve =
|
610
|
-
exports.useResolve = useResolve;
|
660
|
+
exports.resolve = resolve2;
|
611
661
|
//# sourceMappingURL=out.js.map
|
612
662
|
//# sourceMappingURL=index.cjs.map
|