@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.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,208 @@ 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
|
-
}
|
217
|
-
if (schema.enum && baseName) {
|
218
|
-
const enumName = getUniqueName(baseName, _TypeGenerator.usedEnumNames);
|
219
|
-
let enums = [...new Set(schema.enum)].map((key) => [key, key]);
|
220
|
-
if ("x-enumNames" in schema) {
|
221
|
-
enums = [...new Set(schema["x-enumNames"])].map((key, index) => {
|
222
|
-
return [key, schema.enum?.[index]];
|
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
234
|
}
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
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
|
258
|
-
}
|
259
|
-
if (schema.type) {
|
260
|
-
if (Array.isArray(schema.type)) {
|
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", []);
|
292
|
+
if (this.options.dateType === "date" && ["date", "date-time"].some((item) => item === schema.format)) {
|
293
|
+
return factory.createTypeReferenceNode(factory.createIdentifier("Date"));
|
284
294
|
}
|
285
|
-
if (
|
286
|
-
return
|
295
|
+
if (schema.type in factory.keywordTypeNodes) {
|
296
|
+
return factory.keywordTypeNodes[schema.type];
|
287
297
|
}
|
288
|
-
return keywordTypeNodes.any;
|
289
298
|
}
|
299
|
+
if (schema.format === "binary") {
|
300
|
+
return factory.createTypeReferenceNode("Blob", []);
|
301
|
+
}
|
302
|
+
return factory.keywordTypeNodes.any;
|
290
303
|
};
|
291
304
|
|
292
305
|
// 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
306
|
var TypeBuilder = class extends OasBuilder {
|
303
307
|
configure(options) {
|
304
308
|
if (options) {
|
@@ -311,8 +315,9 @@ var TypeBuilder = class extends OasBuilder {
|
|
311
315
|
}
|
312
316
|
print(name) {
|
313
317
|
const codes = [];
|
314
|
-
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(nameSorter).map((operationSchema) => {
|
318
|
+
const generated = this.items.filter((operationSchema) => name ? operationSchema.name === name : true).sort(transformers.nameSorter).map((operationSchema) => {
|
315
319
|
const generator = new TypeGenerator({
|
320
|
+
usedEnumNames: this.options.usedEnumNames,
|
316
321
|
withJSDocs: this.options.withJSDocs,
|
317
322
|
resolveName: this.options.resolveName,
|
318
323
|
enumType: this.options.enumType,
|
@@ -341,7 +346,7 @@ var TypeBuilder = class extends OasBuilder {
|
|
341
346
|
const importMeta = importsGenerator.build(generated.map((item) => item.import));
|
342
347
|
if (importMeta) {
|
343
348
|
const nodes = importMeta.map((item) => {
|
344
|
-
return createImportDeclaration({
|
349
|
+
return factory.createImportDeclaration({
|
345
350
|
name: [{ propertyName: item.ref.propertyName }],
|
346
351
|
path: item.path,
|
347
352
|
isTypeOnly: true
|
@@ -350,38 +355,39 @@ var TypeBuilder = class extends OasBuilder {
|
|
350
355
|
codes.unshift(print(nodes));
|
351
356
|
}
|
352
357
|
}
|
353
|
-
return combineCodes(codes);
|
358
|
+
return transformers.combineCodes(codes);
|
354
359
|
}
|
355
360
|
};
|
356
361
|
var OperationGenerator = class extends OperationGenerator$1 {
|
357
362
|
resolve(operation) {
|
358
|
-
const { pluginManager } = this.context;
|
359
|
-
return resolve
|
363
|
+
const { pluginManager, plugin } = this.context;
|
364
|
+
return resolve({
|
360
365
|
operation,
|
361
366
|
resolveName: pluginManager.resolveName,
|
362
367
|
resolvePath: pluginManager.resolvePath,
|
363
|
-
|
368
|
+
pluginKey: plugin?.key
|
364
369
|
});
|
365
370
|
}
|
366
371
|
async all() {
|
367
372
|
return null;
|
368
373
|
}
|
369
374
|
async get(operation, schemas, options) {
|
370
|
-
const { mode, enumType, dateType, optionalType } = options;
|
371
|
-
const { pluginManager } = this.context;
|
375
|
+
const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
|
376
|
+
const { pluginManager, plugin } = this.context;
|
372
377
|
const type = this.resolve(operation);
|
373
378
|
const fileResolver = (name) => {
|
374
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName,
|
379
|
+
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
|
375
380
|
const resolvedTypeId = pluginManager.resolvePath({
|
376
381
|
baseName: `${name}.ts`,
|
377
|
-
|
382
|
+
pluginKey: plugin?.key
|
378
383
|
});
|
379
384
|
return getRelativePath(root, resolvedTypeId);
|
380
385
|
};
|
381
386
|
const source = new TypeBuilder({
|
387
|
+
usedEnumNames,
|
382
388
|
fileResolver: mode === "file" ? void 0 : fileResolver,
|
383
389
|
withJSDocs: true,
|
384
|
-
resolveName: pluginManager.resolveName,
|
390
|
+
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
385
391
|
enumType,
|
386
392
|
optionalType,
|
387
393
|
dateType
|
@@ -391,27 +397,28 @@ var OperationGenerator = class extends OperationGenerator$1 {
|
|
391
397
|
baseName: type.baseName,
|
392
398
|
source,
|
393
399
|
meta: {
|
394
|
-
|
400
|
+
pluginKey: plugin.key,
|
395
401
|
tag: operation.getTags()[0]?.name
|
396
402
|
}
|
397
403
|
};
|
398
404
|
}
|
399
405
|
async post(operation, schemas, options) {
|
400
|
-
const { mode, enumType, dateType, optionalType } = options;
|
401
|
-
const { pluginManager } = this.context;
|
406
|
+
const { mode, enumType, dateType, optionalType, usedEnumNames } = options;
|
407
|
+
const { pluginManager, plugin } = this.context;
|
402
408
|
const type = this.resolve(operation);
|
403
409
|
const fileResolver = (name) => {
|
404
|
-
const root = pluginManager.resolvePath({ baseName: type.baseName,
|
410
|
+
const root = pluginManager.resolvePath({ baseName: type.baseName, pluginKey: plugin?.key, options: { tag: operation.getTags()[0]?.name } });
|
405
411
|
const resolvedTypeId = pluginManager.resolvePath({
|
406
412
|
baseName: `${name}.ts`,
|
407
|
-
|
413
|
+
pluginKey: plugin?.key
|
408
414
|
});
|
409
415
|
return getRelativePath(root, resolvedTypeId);
|
410
416
|
};
|
411
417
|
const source = new TypeBuilder({
|
418
|
+
usedEnumNames,
|
412
419
|
fileResolver: mode === "file" ? void 0 : fileResolver,
|
413
420
|
withJSDocs: true,
|
414
|
-
resolveName: pluginManager.resolveName,
|
421
|
+
resolveName: (params) => pluginManager.resolveName({ ...params, pluginKey: plugin?.key }),
|
415
422
|
enumType,
|
416
423
|
optionalType,
|
417
424
|
dateType
|
@@ -421,7 +428,7 @@ var OperationGenerator = class extends OperationGenerator$1 {
|
|
421
428
|
baseName: type.baseName,
|
422
429
|
source,
|
423
430
|
meta: {
|
424
|
-
|
431
|
+
pluginKey: plugin.key,
|
425
432
|
tag: operation.getTags()[0]?.name
|
426
433
|
}
|
427
434
|
};
|
@@ -439,6 +446,7 @@ var OperationGenerator = class extends OperationGenerator$1 {
|
|
439
446
|
|
440
447
|
// src/plugin.ts
|
441
448
|
var pluginName = "swagger-ts";
|
449
|
+
var pluginKey = ["schema", pluginName];
|
442
450
|
var definePlugin = createPlugin((options) => {
|
443
451
|
const {
|
444
452
|
output = "types",
|
@@ -448,7 +456,8 @@ var definePlugin = createPlugin((options) => {
|
|
448
456
|
enumType = "asConst",
|
449
457
|
dateType = "string",
|
450
458
|
optionalType = "questionToken",
|
451
|
-
transformers = {}
|
459
|
+
transformers: transformers2 = {},
|
460
|
+
exportAs
|
452
461
|
} = options;
|
453
462
|
const template = groupBy?.output ? groupBy.output : `${output}/{{tag}}Controller`;
|
454
463
|
let pluginsOptions;
|
@@ -457,46 +466,48 @@ var definePlugin = createPlugin((options) => {
|
|
457
466
|
options,
|
458
467
|
kind: "schema",
|
459
468
|
validate(plugins) {
|
460
|
-
pluginsOptions = getDependedPlugins(plugins, [pluginName$1]);
|
469
|
+
pluginsOptions = PluginManager.getDependedPlugins(plugins, [pluginName$1]);
|
461
470
|
return true;
|
462
471
|
},
|
463
472
|
resolvePath(baseName, directory, options2) {
|
464
|
-
const root =
|
465
|
-
const mode =
|
473
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
474
|
+
const mode = FileManager.getMode(path.resolve(root, output));
|
466
475
|
if (mode === "file") {
|
467
|
-
return
|
476
|
+
return path.resolve(root, output);
|
468
477
|
}
|
469
478
|
if (options2?.tag && groupBy?.type === "tag") {
|
470
479
|
const tag = camelCase(options2.tag, { delimiter: "", transform: camelCaseTransformMerge });
|
471
|
-
return
|
480
|
+
return path.resolve(root, renderTemplate(template, { tag }), baseName);
|
472
481
|
}
|
473
|
-
return
|
482
|
+
return path.resolve(root, output, baseName);
|
474
483
|
},
|
475
484
|
resolveName(name) {
|
476
485
|
const resolvedName = pascalCase(name, { delimiter: "", stripRegexp: /[^A-Z0-9$]/gi, transform: pascalCaseTransformMerge });
|
477
|
-
return
|
486
|
+
return transformers2?.name?.(resolvedName) || resolvedName;
|
478
487
|
},
|
479
|
-
async writeFile(source,
|
480
|
-
if (!
|
488
|
+
async writeFile(source, writePath) {
|
489
|
+
if (!writePath.endsWith(".ts") || !source) {
|
481
490
|
return;
|
482
491
|
}
|
483
|
-
|
492
|
+
return this.fileManager.write(source, writePath);
|
484
493
|
},
|
485
494
|
async buildStart() {
|
486
495
|
const [swaggerPlugin] = pluginsOptions;
|
487
496
|
const oas = await swaggerPlugin.api.getOas();
|
488
497
|
const schemas = await swaggerPlugin.api.getSchemas();
|
489
|
-
const root =
|
490
|
-
const mode =
|
498
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
499
|
+
const mode = FileManager.getMode(path.resolve(root, output));
|
500
|
+
const usedEnumNames = {};
|
491
501
|
if (mode === "directory") {
|
492
502
|
const builder = await new TypeBuilder({
|
493
|
-
|
503
|
+
usedEnumNames,
|
504
|
+
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
494
505
|
fileResolver: (name) => {
|
495
506
|
const resolvedTypeId = this.resolvePath({
|
496
507
|
baseName: `${name}.ts`,
|
497
|
-
|
508
|
+
pluginKey: this.plugin.key
|
498
509
|
});
|
499
|
-
const root2 = this.resolvePath({ baseName: ``,
|
510
|
+
const root2 = this.resolvePath({ baseName: ``, pluginKey: this.plugin.key });
|
500
511
|
return getRelativePath(root2, resolvedTypeId);
|
501
512
|
},
|
502
513
|
withJSDocs: true,
|
@@ -511,16 +522,16 @@ var definePlugin = createPlugin((options) => {
|
|
511
522
|
});
|
512
523
|
});
|
513
524
|
const mapFolderSchema = async ([name]) => {
|
514
|
-
const
|
515
|
-
if (!
|
525
|
+
const resolvedPath = this.resolvePath({ baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`, pluginKey: this.plugin.key });
|
526
|
+
if (!resolvedPath) {
|
516
527
|
return null;
|
517
528
|
}
|
518
529
|
return this.addFile({
|
519
|
-
path,
|
520
|
-
baseName: `${this.resolveName({ name,
|
530
|
+
path: resolvedPath,
|
531
|
+
baseName: `${this.resolveName({ name, pluginKey: this.plugin.key })}.ts`,
|
521
532
|
source: builder.print(name),
|
522
533
|
meta: {
|
523
|
-
|
534
|
+
pluginKey: this.plugin.key
|
524
535
|
}
|
525
536
|
});
|
526
537
|
};
|
@@ -529,7 +540,8 @@ var definePlugin = createPlugin((options) => {
|
|
529
540
|
}
|
530
541
|
if (mode === "file") {
|
531
542
|
const builder = new TypeBuilder({
|
532
|
-
|
543
|
+
usedEnumNames,
|
544
|
+
resolveName: (params) => this.resolveName({ pluginKey: this.plugin.key, ...params }),
|
533
545
|
withJSDocs: true,
|
534
546
|
enumType,
|
535
547
|
dateType,
|
@@ -541,17 +553,18 @@ var definePlugin = createPlugin((options) => {
|
|
541
553
|
name
|
542
554
|
});
|
543
555
|
});
|
544
|
-
const
|
545
|
-
if (!
|
556
|
+
const resolvedPath = this.resolvePath({ baseName: "", pluginKey: this.plugin.key });
|
557
|
+
if (!resolvedPath) {
|
546
558
|
return;
|
547
559
|
}
|
548
560
|
await this.addFile({
|
549
|
-
path,
|
550
|
-
baseName:
|
561
|
+
path: resolvedPath,
|
562
|
+
baseName: output,
|
551
563
|
source: builder.print(),
|
552
564
|
meta: {
|
553
|
-
|
554
|
-
}
|
565
|
+
pluginKey: this.plugin.key
|
566
|
+
},
|
567
|
+
validate: false
|
555
568
|
});
|
556
569
|
}
|
557
570
|
const operationGenerator = new OperationGenerator(
|
@@ -559,11 +572,13 @@ var definePlugin = createPlugin((options) => {
|
|
559
572
|
mode,
|
560
573
|
enumType,
|
561
574
|
dateType,
|
562
|
-
optionalType
|
575
|
+
optionalType,
|
576
|
+
usedEnumNames
|
563
577
|
},
|
564
578
|
{
|
565
579
|
oas,
|
566
580
|
pluginManager: this.pluginManager,
|
581
|
+
plugin: this.plugin,
|
567
582
|
contentType: swaggerPlugin.api.contentType,
|
568
583
|
skipBy,
|
569
584
|
overrideBy
|
@@ -576,21 +591,41 @@ var definePlugin = createPlugin((options) => {
|
|
576
591
|
if (this.config.output.write === false) {
|
577
592
|
return;
|
578
593
|
}
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
594
|
+
const root = path.resolve(this.config.root, this.config.output.path);
|
595
|
+
await this.fileManager.addIndexes({
|
596
|
+
root,
|
597
|
+
extName: ".ts",
|
598
|
+
meta: { pluginKey: this.plugin.key },
|
599
|
+
options: {
|
600
|
+
map: (file) => {
|
601
|
+
return {
|
602
|
+
...file,
|
603
|
+
exports: file.exports?.map((item) => {
|
604
|
+
if (exportAs) {
|
605
|
+
return {
|
606
|
+
...item,
|
607
|
+
name: exportAs,
|
608
|
+
asAlias: !!exportAs
|
609
|
+
};
|
610
|
+
}
|
611
|
+
return item;
|
612
|
+
})
|
613
|
+
};
|
614
|
+
},
|
615
|
+
output,
|
616
|
+
isTypeOnly: true
|
617
|
+
}
|
618
|
+
});
|
584
619
|
}
|
585
620
|
};
|
586
621
|
});
|
587
|
-
function
|
588
|
-
return
|
622
|
+
function resolve2(props) {
|
623
|
+
return resolve({ pluginKey, ...props });
|
589
624
|
}
|
590
625
|
|
591
626
|
// src/index.ts
|
592
627
|
var src_default = definePlugin;
|
593
628
|
|
594
|
-
export {
|
629
|
+
export { src_default as default, definePlugin, pluginKey, pluginName, resolve2 as resolve };
|
595
630
|
//# sourceMappingURL=out.js.map
|
596
631
|
//# sourceMappingURL=index.js.map
|