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