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