openapi-ts-request 1.11.0 → 1.12.0

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.
@@ -0,0 +1,532 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resolveArray = resolveArray;
4
+ exports.resolveProperties = resolveProperties;
5
+ exports.resolveEnumObject = resolveEnumObject;
6
+ exports.resolveAllOfObject = resolveAllOfObject;
7
+ exports.getProps = getProps;
8
+ exports.resolveParameterRef = resolveParameterRef;
9
+ exports.resolveRefObject = resolveRefObject;
10
+ exports.getResponsesType = getResponsesType;
11
+ exports.parseResponseEntries = parseResponseEntries;
12
+ exports.getResponseTypeFromContent = getResponseTypeFromContent;
13
+ exports.getParamsTP = getParamsTP;
14
+ exports.analyzeTypeReferences = analyzeTypeReferences;
15
+ exports.getModuleImports = getModuleImports;
16
+ exports.moveCommonTypeDependenciesToCommon = moveCommonTypeDependenciesToCommon;
17
+ exports.resolveFileTP = resolveFileTP;
18
+ const lodash_1 = require("lodash");
19
+ const config_1 = require("../config");
20
+ const config_2 = require("./config");
21
+ const util_1 = require("./util");
22
+ /**
23
+ * 解析数组类型
24
+ */
25
+ function resolveArray(params) {
26
+ var _a;
27
+ const { schemaObject, namespace, getType } = params;
28
+ if ((0, util_1.isReferenceObject)(schemaObject.items)) {
29
+ const refName = (0, util_1.getRefName)(schemaObject.items);
30
+ return {
31
+ type: `${refName}[]`,
32
+ };
33
+ }
34
+ else if ((_a = schemaObject.items) === null || _a === void 0 ? void 0 : _a.enum) {
35
+ return {
36
+ type: getType(schemaObject, namespace),
37
+ };
38
+ }
39
+ return { type: 'unknown[]' };
40
+ }
41
+ /**
42
+ * 解析属性
43
+ */
44
+ function resolveProperties(params) {
45
+ const { schemaObject, getProps } = params;
46
+ return {
47
+ props: [getProps(schemaObject)],
48
+ };
49
+ }
50
+ /**
51
+ * 解析枚举对象
52
+ */
53
+ function resolveEnumObject(params) {
54
+ var _a;
55
+ const { schemaObject, config } = params;
56
+ const enumArray = schemaObject.enum;
57
+ let enumStr = '';
58
+ let enumLabelTypeStr = '';
59
+ if (config_2.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumber)(enumArray)) {
60
+ if (config.isSupportParseEnumDesc && schemaObject.description) {
61
+ const enumMap = (0, util_1.parseDescriptionEnum)(schemaObject.description);
62
+ enumStr = `{${(0, lodash_1.map)(enumArray, (value) => {
63
+ const enumLabel = enumMap.get(Number(value));
64
+ return `${enumLabel}=${Number(value)}`;
65
+ }).join(',')}}`;
66
+ }
67
+ else {
68
+ enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `"NUMBER_${value}"=${Number(value)}`).join(',')}}`;
69
+ }
70
+ }
71
+ else if ((0, util_1.isAllNumeric)(enumArray)) {
72
+ enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `"STRING_NUMBER_${value}"="${value}"`).join(',')}}`;
73
+ }
74
+ else {
75
+ enumStr = `{${(0, lodash_1.map)(enumArray, (value) => `"${value}"="${value}"`).join(',')}}`;
76
+ }
77
+ // 翻译枚举
78
+ if (schemaObject['x-enum-varnames'] && schemaObject['x-enum-comments']) {
79
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value, index) => {
80
+ const enumKey = schemaObject['x-enum-varnames'][index];
81
+ return `${value}:"${schemaObject['x-enum-comments'][enumKey]}"`;
82
+ }).join(',')}}`;
83
+ }
84
+ else if ((_a = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject['x-apifox']) === null || _a === void 0 ? void 0 : _a['enumDescriptions']) {
85
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => {
86
+ const enumLabel = schemaObject['x-apifox']['enumDescriptions'][value];
87
+ return `${value}:"${enumLabel}"`;
88
+ }).join(',')}}`;
89
+ }
90
+ else if (schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject['x-apifox-enum']) {
91
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => {
92
+ var _a;
93
+ const enumLabel = (_a = (0, lodash_1.find)(schemaObject['x-apifox-enum'], (item) => item.value === value)) === null || _a === void 0 ? void 0 : _a.description;
94
+ return `${value}:"${enumLabel}"`;
95
+ }).join(',')}}`;
96
+ }
97
+ else {
98
+ if (config_2.numberEnum.includes(schemaObject.type) || (0, util_1.isAllNumber)(enumArray)) {
99
+ if ((config.isSupportParseEnumDesc || config.supportParseEnumDescByReg) &&
100
+ schemaObject.description) {
101
+ const enumMap = config.isSupportParseEnumDesc
102
+ ? (0, util_1.parseDescriptionEnum)(schemaObject.description)
103
+ : (0, util_1.parseDescriptionEnumByReg)(schemaObject.description, config.supportParseEnumDescByReg);
104
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => {
105
+ const enumLabel = enumMap.get(Number(value));
106
+ return `${Number(value)}:"${enumLabel}"`;
107
+ }).join(',')}}`;
108
+ }
109
+ else {
110
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `${Number(value) >= 0 ? Number(value) : `"${value}"`}:"NUMBER_${value}"`).join(',')}}`;
111
+ }
112
+ }
113
+ else if ((0, util_1.isAllNumeric)(enumArray)) {
114
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `"${value}":"STRING_NUMBER_${value}"`).join(',')}}`;
115
+ }
116
+ else {
117
+ enumLabelTypeStr = `{${(0, lodash_1.map)(enumArray, (value) => `"${value}":"${value}"`).join(',')}}`;
118
+ }
119
+ }
120
+ return {
121
+ isEnum: true,
122
+ type: Array.isArray(enumArray) ? enumStr : 'string',
123
+ enumLabelType: enumLabelTypeStr,
124
+ description: schemaObject.description,
125
+ };
126
+ }
127
+ /**
128
+ * 解析 allOf 对象
129
+ */
130
+ function resolveAllOfObject(params) {
131
+ const { schemaObject, getType, getProps } = params;
132
+ const props = (0, lodash_1.map)(schemaObject.allOf, (item) => {
133
+ return (0, util_1.isReferenceObject)(item)
134
+ ? [Object.assign(Object.assign({}, item), { type: getType(item) })]
135
+ : getProps(item);
136
+ });
137
+ if (schemaObject.properties) {
138
+ const extProps = getProps(schemaObject);
139
+ return { props: [...props, extProps] };
140
+ }
141
+ return { props };
142
+ }
143
+ /**
144
+ * 获取 TS 类型的属性列表
145
+ */
146
+ function getProps(params) {
147
+ var _a;
148
+ const { schemaObject, getType } = params;
149
+ const requiredPropKeys = (_a = schemaObject === null || schemaObject === void 0 ? void 0 : schemaObject.required) !== null && _a !== void 0 ? _a : false;
150
+ const properties = schemaObject.properties;
151
+ return (0, lodash_1.keys)(properties).map((propKey) => {
152
+ const schema = ((properties === null || properties === void 0 ? void 0 : properties[propKey]) || config_2.DEFAULT_SCHEMA);
153
+ // 剔除属性键值中的特殊符号,因为函数入参变量存在特殊符号会导致解析文件失败
154
+ // eslint-disable-next-line no-useless-escape
155
+ propKey = propKey.replace(/[\[|\]]/g, '');
156
+ // 复用 schema 部分字段
157
+ return Object.assign(Object.assign({}, schema), { name: propKey, type: getType(schema), desc: [schema.title, schema.description]
158
+ .filter((item) => item)
159
+ .join(' ')
160
+ .replace(config_2.lineBreakReg, ''),
161
+ // 如果没有 required 信息,默认全部是非必填
162
+ required: requiredPropKeys
163
+ ? requiredPropKeys.some((key) => key === propKey)
164
+ : false });
165
+ });
166
+ }
167
+ /**
168
+ * 解析参数引用
169
+ */
170
+ function resolveParameterRef(params) {
171
+ var _a, _b;
172
+ const { param, openAPIData } = params;
173
+ if (!(0, util_1.isReferenceObject)(param)) {
174
+ return param;
175
+ }
176
+ // 解析 $ref 引用,从 components.parameters 中获取实际定义
177
+ const refName = (0, util_1.getLastRefName)(param.$ref);
178
+ const parameter = ((_b = (_a = openAPIData.components) === null || _a === void 0 ? void 0 : _a.parameters) === null || _b === void 0 ? void 0 : _b[refName]) || null;
179
+ return parameter;
180
+ }
181
+ /**
182
+ * 解析引用对象
183
+ */
184
+ function resolveRefObject(params) {
185
+ const { refObject, openAPIData, resolveRefObjectFunc } = params;
186
+ if (!(0, util_1.isReferenceObject)(refObject)) {
187
+ return refObject;
188
+ }
189
+ const refPaths = refObject.$ref.split('/');
190
+ if (refPaths[0] === '#') {
191
+ const schema = (0, util_1.resolveRefs)(openAPIData, refPaths.slice(1));
192
+ if (!schema) {
193
+ throw new Error(`[GenSDK] Data Error! Notfoud: ${refObject.$ref}`);
194
+ }
195
+ const resolvedSchema = resolveRefObjectFunc({
196
+ refObject: schema,
197
+ openAPIData,
198
+ resolveRefObjectFunc,
199
+ });
200
+ let resolvedType;
201
+ if ((0, util_1.isReferenceObject)(schema)) {
202
+ const refResolved = resolveRefObjectFunc({
203
+ refObject: schema,
204
+ openAPIData,
205
+ resolveRefObjectFunc,
206
+ });
207
+ resolvedType = refResolved === null || refResolved === void 0 ? void 0 : refResolved.type;
208
+ }
209
+ else {
210
+ const schemaObj = schema;
211
+ resolvedType = schemaObj.type;
212
+ }
213
+ const finalSchema = schema;
214
+ return Object.assign(Object.assign({}, (resolvedSchema || {})), { type: resolvedType || finalSchema.type });
215
+ }
216
+ return refObject;
217
+ }
218
+ /**
219
+ * 生成多状态码响应类型定义
220
+ */
221
+ function getResponsesType(params) {
222
+ const { responses, functionName, interfaceTPConfigs, components, getResponseTypeFromContentFunc, resolveRefObjectFunc, } = params;
223
+ if (Object.keys(responses || {}).length === 0 ||
224
+ interfaceTPConfigs.find((item) => item.typeName === (0, lodash_1.upperFirst)(`${functionName}Responses`))) {
225
+ return null;
226
+ }
227
+ // 生成主响应类型名称
228
+ const mainResponseTypeName = (0, lodash_1.upperFirst)(`${functionName}Response`);
229
+ const responseEntries = parseResponseEntries({
230
+ responses,
231
+ components,
232
+ getResponseTypeFromContentFunc,
233
+ resolveRefObjectFunc,
234
+ });
235
+ const responseTypes = responseEntries.map(({ statusCode, type, description = '' }) => {
236
+ var _a;
237
+ // 检查是否已存在对应的主响应类型,如果存在则复用,避免重复定义
238
+ const existType = interfaceTPConfigs.find((item) => item.typeName === mainResponseTypeName);
239
+ const lastType = existType ? mainResponseTypeName : type;
240
+ // 格式化描述文本,让描述支持换行
241
+ const formattedDescription = config_2.lineBreakReg.test(description)
242
+ ? (_a = description.split('\n')) === null || _a === void 0 ? void 0 : _a.join('\n * ')
243
+ : description;
244
+ // 生成带注释的类型定义
245
+ return formattedDescription
246
+ ? ` /**\n * ${formattedDescription}\n */\n ${statusCode}: ${lastType};`
247
+ : ` ${statusCode}: ${lastType};`;
248
+ });
249
+ // 返回完整的对象类型定义
250
+ return `{\n${responseTypes.join('\n')}\n}`;
251
+ }
252
+ /**
253
+ * 解析响应条目
254
+ */
255
+ function parseResponseEntries(params) {
256
+ const { responses, components, getResponseTypeFromContentFunc, resolveRefObjectFunc, } = params;
257
+ return (0, lodash_1.keys)(responses).map((statusCode) => {
258
+ const response = resolveRefObjectFunc(responses[statusCode]);
259
+ if (!response) {
260
+ return { statusCode, type: 'unknown', description: '' };
261
+ }
262
+ const responseType = getResponseTypeFromContentFunc({
263
+ response,
264
+ components,
265
+ });
266
+ const description = response.description || '';
267
+ return { statusCode, type: responseType, description };
268
+ });
269
+ }
270
+ /**
271
+ * 从响应内容中提取 TypeScript 类型
272
+ */
273
+ function getResponseTypeFromContent(params) {
274
+ var _a;
275
+ const { response, components, config, getType } = params;
276
+ if (!response.content) {
277
+ return 'unknown';
278
+ }
279
+ const resContent = response.content;
280
+ const resContentMediaTypes = (0, lodash_1.keys)(resContent);
281
+ // 检测二进制流媒体类型
282
+ const binaryMediaTypes = (0, util_1.getBinaryMediaTypes)(config.binaryMediaTypes);
283
+ const binaryMediaType = resContentMediaTypes.find((mediaType) => (0, util_1.isBinaryMediaType)(mediaType, binaryMediaTypes));
284
+ const mediaType = resContentMediaTypes.includes('application/json')
285
+ ? 'application/json'
286
+ : binaryMediaType || resContentMediaTypes[0];
287
+ if (!(0, lodash_1.isObject)(resContent) || !mediaType) {
288
+ return 'unknown';
289
+ }
290
+ // 如果是二进制媒体类型,直接返回二进制类型
291
+ if ((0, util_1.isBinaryMediaType)(mediaType, binaryMediaTypes)) {
292
+ return (0, util_1.getBinaryResponseType)();
293
+ }
294
+ let schema = (resContent[mediaType].schema || config_2.DEFAULT_SCHEMA);
295
+ if ((0, util_1.isReferenceObject)(schema)) {
296
+ const refName = (0, util_1.getLastRefName)(schema.$ref);
297
+ const childrenSchema = components.schemas[refName];
298
+ // 如果配置了 dataFields,尝试从指定字段提取类型
299
+ if ((0, util_1.isNonArraySchemaObject)(childrenSchema) && config.dataFields) {
300
+ schema = (((_a = config.dataFields
301
+ .map((field) => childrenSchema.properties[field])
302
+ .filter(Boolean)) === null || _a === void 0 ? void 0 : _a[0]) ||
303
+ resContent[mediaType].schema ||
304
+ config_2.DEFAULT_SCHEMA);
305
+ }
306
+ return getType(schema);
307
+ }
308
+ else if ((0, util_1.isSchemaObject)(schema)) {
309
+ // 设置属性的 required 状态
310
+ (0, lodash_1.keys)(schema.properties).map((fieldName) => {
311
+ var _a, _b;
312
+ schema.properties[fieldName]['required'] =
313
+ (_b = (_a = schema.required) === null || _a === void 0 ? void 0 : _a.includes(fieldName)) !== null && _b !== void 0 ? _b : false;
314
+ });
315
+ return getType(schema);
316
+ }
317
+ else {
318
+ return getType(schema);
319
+ }
320
+ }
321
+ /**
322
+ * 获取参数模板
323
+ */
324
+ function getParamsTP(params) {
325
+ const { parameters = [], path = null, namespace, openAPIData, getType, resolveParameterRefFunc, } = params;
326
+ const templateParams = {};
327
+ if (parameters === null || parameters === void 0 ? void 0 : parameters.length) {
328
+ (0, lodash_1.forEach)(config_2.parametersIn, (source) => {
329
+ const paramsList = parameters
330
+ .map((p) => resolveParameterRefFunc({ param: p, openAPIData }))
331
+ .filter((p) => p !== null && p.in === source)
332
+ .map((p) => {
333
+ var _a, _b, _c;
334
+ const isDirectObject = (((_a = p.schema) === null || _a === void 0 ? void 0 : _a.type) === 'object' ||
335
+ p.type) === 'object';
336
+ const refName = (0, util_1.getLastRefName)(((_b = p.schema) === null || _b === void 0 ? void 0 : _b.$ref) ||
337
+ p.$ref);
338
+ const schemas = ((_c = openAPIData.components) === null || _c === void 0 ? void 0 : _c.schemas) || {};
339
+ const deRefObjEntry = Object.entries(schemas).find(([k]) => k === refName);
340
+ const deRefSchema = deRefObjEntry === null || deRefObjEntry === void 0 ? void 0 : deRefObjEntry[1];
341
+ const isRefObject = deRefSchema !== undefined &&
342
+ (deRefSchema === null || deRefSchema === void 0 ? void 0 : deRefSchema.type) === 'object' &&
343
+ Object.keys((deRefSchema === null || deRefSchema === void 0 ? void 0 : deRefSchema.properties) || {}).length > 0;
344
+ return Object.assign(Object.assign({}, p), { isObject: isDirectObject || isRefObject, type: getType(p.schema || config_2.DEFAULT_SCHEMA, namespace) });
345
+ });
346
+ if (paramsList.length) {
347
+ templateParams[source] = paramsList;
348
+ }
349
+ });
350
+ }
351
+ if (path && path.length > 0) {
352
+ const regex = /\{(\w+)\}/g;
353
+ templateParams.path = templateParams.path || [];
354
+ let match = null;
355
+ while ((match = regex.exec(path))) {
356
+ if (!templateParams.path.some((p) => p.name === match[1])) {
357
+ templateParams.path.push(Object.assign(Object.assign({}, config_2.DEFAULT_PATH_PARAM), { name: match[1] }));
358
+ }
359
+ }
360
+ // 如果 path 没有内容,则将删除 path 参数,避免影响后续的 hasParams 判断
361
+ if (!templateParams.path.length)
362
+ delete templateParams.path;
363
+ }
364
+ return templateParams;
365
+ }
366
+ /**
367
+ * 分析类型定义中使用的类型名称
368
+ */
369
+ function analyzeTypeReferences(types) {
370
+ const references = new Set();
371
+ types.forEach((typeItem) => {
372
+ // 分析 type 字段
373
+ if (typeof typeItem.type === 'string') {
374
+ // 匹配类型引用,例如: Category, Tag[], _CInputDto, Category | null
375
+ // 支持以下划线开头的类型名
376
+ const typeMatches = typeItem.type.match(/\b_*[A-Z][a-zA-Z0-9_]*\b/g);
377
+ if (typeMatches) {
378
+ typeMatches.forEach((match) => references.add(match));
379
+ }
380
+ }
381
+ // 分析 props
382
+ if (typeItem.props && typeItem.props.length > 0) {
383
+ typeItem.props.forEach((propGroup) => {
384
+ propGroup.forEach((prop) => {
385
+ if (prop.type) {
386
+ // 匹配类型引用,支持以下划线开头的类型名
387
+ const propTypeMatches = prop.type.match(/\b_*[A-Z][a-zA-Z0-9_]*\b/g);
388
+ if (propTypeMatches) {
389
+ propTypeMatches.forEach((match) => references.add(match));
390
+ }
391
+ }
392
+ });
393
+ });
394
+ }
395
+ });
396
+ return references;
397
+ }
398
+ /**
399
+ * 获取模块需要导入的类型
400
+ */
401
+ function getModuleImports(params) {
402
+ const { moduleTypes, commonTypes, enumTypes } = params;
403
+ const references = analyzeTypeReferences(moduleTypes);
404
+ // 获取公共类型名称集合
405
+ const commonTypeNames = new Set(commonTypes.map((t) => t.typeName));
406
+ // 获取枚举类型名称集合(包括 IEnumName)
407
+ const enumTypeNames = new Set();
408
+ enumTypes.forEach((t) => {
409
+ enumTypeNames.add(t.typeName);
410
+ if (t.isEnum) {
411
+ enumTypeNames.add(`I${t.typeName}`);
412
+ }
413
+ });
414
+ // 筛选出实际需要导入的类型
415
+ const commonImports = Array.from(references).filter((ref) => commonTypeNames.has(ref));
416
+ const enumImports = Array.from(references).filter((ref) => enumTypeNames.has(ref));
417
+ return { commonImports, enumImports };
418
+ }
419
+ /**
420
+ * 将公共类型依赖的类型从模块类型移到公共类型
421
+ * 同时检测跨模块引用,将被多个模块使用的类型移到公共类型
422
+ * @param moduleTypes 模块类型
423
+ * @param commonTypes 公共类型
424
+ */
425
+ function moveCommonTypeDependenciesToCommon(params) {
426
+ const { moduleTypes, commonTypes } = params;
427
+ let moved = true;
428
+ while (moved) {
429
+ moved = false;
430
+ // 1. 处理公共类型的依赖
431
+ const commonTypeRefs = analyzeTypeReferences(commonTypes);
432
+ moduleTypes.forEach((types) => {
433
+ const toMove = [];
434
+ types.forEach((typeItem) => {
435
+ if (commonTypeRefs.has(typeItem.typeName)) {
436
+ toMove.push(typeItem);
437
+ moved = true;
438
+ }
439
+ });
440
+ toMove.forEach((typeItem) => {
441
+ const index = types.indexOf(typeItem);
442
+ if (index > -1) {
443
+ types.splice(index, 1);
444
+ commonTypes.push(typeItem);
445
+ }
446
+ });
447
+ });
448
+ // 2. 检测跨模块引用:如果一个类型被其他模块引用,移到 common
449
+ // 首先建立类型名称到定义模块的映射
450
+ const typeDefModule = new Map(); // typeName -> moduleName (where it's defined)
451
+ moduleTypes.forEach((types, moduleName) => {
452
+ types.forEach((typeItem) => {
453
+ typeDefModule.set(typeItem.typeName, moduleName);
454
+ });
455
+ });
456
+ // 收集每个模块引用了哪些定义在其他模块的类型
457
+ const crossModuleRefs = new Map(); // typeName -> Set<moduleName that references it>
458
+ moduleTypes.forEach((types, moduleName) => {
459
+ const referencedTypes = analyzeTypeReferences(types);
460
+ referencedTypes.forEach((typeName) => {
461
+ const defModule = typeDefModule.get(typeName);
462
+ // 如果这个类型定义在其他模块,记录跨模块引用
463
+ if (defModule && defModule !== moduleName) {
464
+ if (!crossModuleRefs.has(typeName)) {
465
+ crossModuleRefs.set(typeName, new Set());
466
+ }
467
+ crossModuleRefs.get(typeName).add(moduleName);
468
+ }
469
+ });
470
+ });
471
+ // 找出被其他模块引用的类型(包括被一个或多个其他模块引用)
472
+ const typesToMove = new Set();
473
+ crossModuleRefs.forEach((referencingModules, typeName) => {
474
+ if (referencingModules.size > 0) {
475
+ typesToMove.add(typeName);
476
+ }
477
+ });
478
+ // 将这些类型移到 common
479
+ if (typesToMove.size > 0) {
480
+ moduleTypes.forEach((types) => {
481
+ const toMove = [];
482
+ types.forEach((typeItem) => {
483
+ if (typesToMove.has(typeItem.typeName)) {
484
+ toMove.push(typeItem);
485
+ moved = true;
486
+ }
487
+ });
488
+ toMove.forEach((typeItem) => {
489
+ const index = types.indexOf(typeItem);
490
+ if (index > -1) {
491
+ types.splice(index, 1);
492
+ commonTypes.push(typeItem);
493
+ }
494
+ });
495
+ });
496
+ }
497
+ }
498
+ }
499
+ /**
500
+ * 解析文件模板
501
+ */
502
+ function resolveFileTP(params) {
503
+ var _a;
504
+ const { obj, resolveObjectFunc } = params;
505
+ let ret = [];
506
+ const resolved = resolveObjectFunc(obj);
507
+ const props = (((_a = resolved.props) === null || _a === void 0 ? void 0 : _a.length) > 0 &&
508
+ resolved.props[0].filter((p) => p.format === 'binary' ||
509
+ p.format === 'base64' ||
510
+ (0, util_1.isBinaryArraySchemaObject)(p))) ||
511
+ [];
512
+ if (props.length > 0) {
513
+ ret = props.map((p) => {
514
+ // 这里 p.type 是自定义type, 注意别混淆
515
+ return {
516
+ title: p.name,
517
+ multiple: p.type === `${config_1.SchemaObjectType.array}` ||
518
+ p.type === `${config_1.SchemaObjectType.stringArray}`,
519
+ };
520
+ });
521
+ }
522
+ if (resolved.type) {
523
+ ret = [
524
+ ...ret,
525
+ ...resolveFileTP({
526
+ obj: resolved.type,
527
+ resolveObjectFunc,
528
+ }),
529
+ ];
530
+ }
531
+ return ret;
532
+ }
@@ -27,6 +27,8 @@ export interface ITypeItem {
27
27
  displayLabelFuncName?: string;
28
28
  enumLabelType?: string;
29
29
  description?: string;
30
+ /** 类型所属的模块名称(用于拆分类型文件) */
31
+ belongsToModules?: Set<string>;
30
32
  }
31
33
  export type ICustomSchemaObject = SchemaObject & {
32
34
  isAllowed?: boolean;
package/dist/index.d.ts CHANGED
@@ -19,6 +19,12 @@ export type GenerateServiceProps = {
19
19
  * 方式二: import request from '@/request';
20
20
  */
21
21
  requestLibPath?: string;
22
+ /**
23
+ * 是否按模块拆分类型文件
24
+ * 默认: false
25
+ * 开启后会生成: {module}.type.ts, common.type.ts, types.ts(重新导出)
26
+ */
27
+ isSplitTypesByModule?: boolean;
22
28
  /**
23
29
  * 是否全量替换, 默认: true, 如果为false, 则进行增量替换
24
30
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "openapi-ts-request",
3
- "version": "1.11.0",
3
+ "version": "1.12.0",
4
4
  "description": "Swagger2/OpenAPI3/Apifox to TypeScript/JavaScript, request client(support any client), request mock service, enum and enum translation, react-query/vue-query, type field label, JSON Schemas",
5
5
  "engines": {
6
6
  "node": ">=18.0.0",
@@ -0,0 +1,14 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+
4
+ {%- if list.length > 0 %}
5
+ {% for type in list -%}
6
+ {%- if type.isEnum %}
7
+ export enum {{ type.typeName | safe }} {{ type.type }};
8
+
9
+ export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}
10
+ {%- endif %}
11
+ {% endfor %}
12
+ {%- else %}
13
+ export {}
14
+ {%- endif %}
@@ -0,0 +1,63 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+ {%- if enumImports and enumImports.length > 0 %}
4
+ import type { {{ enumImports.join(', ') }} } from './enum';
5
+ {%- endif %}
6
+ {%- if commonImports and commonImports.length > 0 %}
7
+ import type { {{ commonImports.join(', ') }} } from './common.type';
8
+ {%- endif %}
9
+
10
+ {%- if list.length > 0 %}
11
+ {% for type in list -%}
12
+ {%- if type.props.length %}
13
+ export type {{ type.typeName | safe }} =
14
+ {%- for prop in type.props %}
15
+ {%- if prop.length > 1 %}
16
+ {
17
+ {%- endif %}
18
+ {%- if prop.length == 1 %}
19
+ {%- if not prop[0].$ref or prop[0].name %}
20
+ {
21
+ {%- endif %}
22
+ {%- endif %}
23
+ {%- for p in prop %}
24
+ {%- if p.desc %}
25
+ /** {{ p.desc }} */
26
+ {%- endif %}
27
+ {%- if p["$ref"] and not p.name %}
28
+ // {{ p.$ref }}
29
+ {{ p.type | safe }}
30
+ {%- else %}
31
+ {%- if nullable %}
32
+ '{{ p.name }}': {{ p.type | safe }}{{'' if p.required else '| null'}};
33
+ {%- else %}
34
+ '{{ p.name }}'{{ '' if p.required else '?' }}: {{ p.type | safe }};
35
+ {%- endif %}
36
+ {%- endif %}
37
+ {%- endfor %}
38
+ {%- if prop.length > 1 %}
39
+ }
40
+ {%- endif %}
41
+ {%- if prop.length == 1 %}
42
+ {%- if not prop[0].$ref or prop[0].name %}
43
+ }
44
+ {%- endif %}
45
+ {%- endif %}
46
+ {%- if prop.length == 0 %}
47
+ {}
48
+ {%- endif %}
49
+ {{ '' if loop.last === true else ' & ' }}
50
+ {%- endfor %}
51
+ {%- else %}
52
+ {%- if type.isEnum %}
53
+ export enum {{ type.typeName | safe }} {{ type.type }};
54
+
55
+ export type I{{ type.typeName | safe }} = keyof typeof {{ type.typeName }}
56
+ {%- else %}
57
+ export type {{ type.typeName | safe }} = {{ type.type }};
58
+ {%- endif %}
59
+ {%- endif %}
60
+ {% endfor %}
61
+ {%- else %}
62
+ export {}
63
+ {%- endif %}
@@ -0,0 +1,17 @@
1
+ /* eslint-disable */
2
+ // @ts-ignore
3
+
4
+ // 导出枚举类型
5
+ {%- if hasEnumTypes %}
6
+ export * from './enum';
7
+ {%- endif %}
8
+
9
+ // 导出公共类型
10
+ {%- if hasCommonTypes %}
11
+ export * from './common.type';
12
+ {%- endif %}
13
+
14
+ // 导出各模块类型
15
+ {%- for module in moduleList %}
16
+ export * from './{{ module }}.type';
17
+ {%- endfor %}