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