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