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