@@ -2,7 +2,6 @@ import { existsSync, readFileSync } from 'fs';
22import { globSync } from 'glob' ;
33import type { Dictionary } from 'lodash' ;
44import {
5- // camelCase,
65 entries ,
76 filter ,
87 find ,
@@ -81,7 +80,10 @@ import { type MergeOption } from './type';
8180import {
8281 capitalizeFirstLetter ,
8382 genDefaultFunctionName ,
83+ getAxiosResponseType ,
8484 getBasePrefix ,
85+ getBinaryMediaTypes ,
86+ getBinaryResponseType ,
8587 getDefaultFileTag ,
8688 getDefaultType ,
8789 getFinalFileName ,
@@ -92,13 +94,13 @@ import {
9294 isAllNumeric ,
9395 isArraySchemaObject ,
9496 isBinaryArraySchemaObject ,
97+ isBinaryMediaType ,
9598 isNonArraySchemaObject ,
9699 isReferenceObject ,
97100 isSchemaObject ,
98101 markAllowedSchema ,
99102 parseDescriptionEnum ,
100103 replaceDot ,
101- // resolveFunctionName,
102104 resolveRefs ,
103105 resolveTypeName ,
104106} from './util' ;
@@ -1152,6 +1154,7 @@ export default class ServiceGenerator {
11521154 mediaType : '*/*' ,
11531155 type : 'unknown' ,
11541156 isAnonymous : false ,
1157+ responseType : undefined as string | undefined ,
11551158 } ;
11561159
11571160 if ( ! response ) {
@@ -1160,9 +1163,16 @@ export default class ServiceGenerator {
11601163
11611164 const resContent : ContentObject | undefined = response . content ;
11621165 const resContentMediaTypes = keys ( resContent ) ;
1166+
1167+ // 检测二进制流媒体类型
1168+ const binaryMediaTypes = getBinaryMediaTypes ( this . config . binaryMediaTypes ) ;
1169+ const binaryMediaType = resContentMediaTypes . find ( ( mediaType ) =>
1170+ isBinaryMediaType ( mediaType , binaryMediaTypes )
1171+ ) ;
1172+
11631173 const mediaType = resContentMediaTypes . includes ( 'application/json' )
11641174 ? 'application/json'
1165- : resContentMediaTypes [ 0 ] ; // 优先使用 application/json
1175+ : binaryMediaType || resContentMediaTypes [ 0 ] ; // 优先使用 application/json,然后是二进制类型
11661176
11671177 if ( ! isObject ( resContent ) || ! mediaType ) {
11681178 return defaultResponse ;
@@ -1174,8 +1184,20 @@ export default class ServiceGenerator {
11741184 mediaType,
11751185 type : 'unknown' ,
11761186 isAnonymous : false ,
1187+ responseType : undefined as string | undefined ,
11771188 } ;
11781189
1190+ // 如果是二进制媒体类型,直接返回二进制类型
1191+ if ( isBinaryMediaType ( mediaType , binaryMediaTypes ) ) {
1192+ const binaryType = getBinaryResponseType ( ) ;
1193+ responseSchema . type = binaryType ;
1194+
1195+ // 自动为二进制响应添加 responseType 配置
1196+ responseSchema . responseType = getAxiosResponseType ( binaryType ) ;
1197+
1198+ return responseSchema ;
1199+ }
1200+
11791201 if ( isReferenceObject ( schema ) ) {
11801202 const refName = getLastRefName ( schema . $ref ) ;
11811203 const childrenSchema = components . schemas [ refName ] ;
@@ -1306,14 +1328,26 @@ export default class ServiceGenerator {
13061328
13071329 const resContent : ContentObject = response . content ;
13081330 const resContentMediaTypes = keys ( resContent ) ;
1331+
1332+ // 检测二进制流媒体类型
1333+ const binaryMediaTypes = getBinaryMediaTypes ( this . config . binaryMediaTypes ) ;
1334+ const binaryMediaType = resContentMediaTypes . find ( ( mediaType ) =>
1335+ isBinaryMediaType ( mediaType , binaryMediaTypes )
1336+ ) ;
1337+
13091338 const mediaType = resContentMediaTypes . includes ( 'application/json' )
13101339 ? 'application/json'
1311- : resContentMediaTypes [ 0 ] ;
1340+ : binaryMediaType || resContentMediaTypes [ 0 ] ;
13121341
13131342 if ( ! isObject ( resContent ) || ! mediaType ) {
13141343 return 'unknown' ;
13151344 }
13161345
1346+ // 如果是二进制媒体类型,直接返回二进制类型
1347+ if ( isBinaryMediaType ( mediaType , binaryMediaTypes ) ) {
1348+ return getBinaryResponseType ( ) ;
1349+ }
1350+
13171351 let schema = ( resContent [ mediaType ] . schema ||
13181352 DEFAULT_SCHEMA ) as SchemaObject ;
13191353
0 commit comments