1- ' use strict' ;
2- const $Credential = require ( ' @alicloud/credentials' ) ;
3- const { default : Credential } = require ( ' @alicloud/credentials' ) ;
4- const FILTER_METHOD = [ ' HEAD' , ' TRACE' , ' OPTIONS' , ' PATCH' , ' CONNECT' ] ;
5- import { OpenAPIClient } from ' ../lib/client'
1+ " use strict" ;
2+ const $Credential = require ( " @alicloud/credentials" ) ;
3+ const { default : Credential } = require ( " @alicloud/credentials" ) ;
4+ const FILTER_METHOD = [ " HEAD" , " TRACE" , " OPTIONS" , " PATCH" , " CONNECT" ] ;
5+ import { OpenAPIClient } from " ../lib/client" ;
66
77function _bodyType ( types ) {
88 if ( ! types || types . length < 1 ) {
9- return ' none' ;
9+ return " none" ;
1010 }
1111
1212 const type = types [ 0 ] ;
1313
14- if ( type === ' application/json' ) {
15- return ' json' ;
14+ if ( type === " application/json" ) {
15+ return " json" ;
1616 }
1717
18- if ( type === ' application/xml' ) {
19- return ' xml' ;
18+ if ( type === " application/xml" ) {
19+ return " xml" ;
2020 }
2121
22- if ( type === ' application/x-www-form-urlencoded' ) {
23- return ' form' ;
22+ if ( type === " application/x-www-form-urlencoded" ) {
23+ return " form" ;
2424 }
2525
26- if ( type === ' application/octet-stream' ) {
27- return ' binary' ;
26+ if ( type === " application/octet-stream" ) {
27+ return " binary" ;
2828 }
2929
30- return ' none' ;
30+ return " none" ;
3131}
3232
33- function getMethod ( method ) {
33+ function getMethodByString ( method ) {
3434 let methodArray ;
35- if ( method . includes ( '|' ) ) {
36- methodArray = method . split ( '|' ) ;
37- } else if ( method . includes ( ',' ) ) {
38- methodArray = method . split ( ',' ) ;
35+ if ( method . includes ( "|" ) ) {
36+ methodArray = method . split ( "|" ) ;
37+ } else if ( method . includes ( "," ) ) {
38+ methodArray = method . split ( "," ) ;
3939 } else {
4040 return method ;
4141 }
42- if ( methodArray . includes ( ' POST' ) ) {
43- return ' POST' ;
42+ if ( methodArray . includes ( " POST" ) ) {
43+ return " POST" ;
4444 }
45- const vaildMethod = methodArray . filter ( item => FILTER_METHOD . indexOf ( item ) <= - 1 ) ;
45+ const vaildMethod = methodArray . filter ( ( item ) => FILTER_METHOD . indexOf ( item ) <= - 1 ) ;
4646 if ( vaildMethod . length > 0 ) {
4747 return vaildMethod [ 0 ] ;
48- }
48+ }
4949 return methodArray [ 0 ] ;
50-
50+ }
51+
52+ function getMethod ( methods : string [ ] ) {
53+ const vaildMethod = methods ?. filter ( ( item ) => FILTER_METHOD . indexOf ( item ?. toUpperCase ( ) ) <= - 1 ) ;
54+ if ( methods ?. includes ( "post" ) ) {
55+ return "POST" ;
56+ } else if ( vaildMethod . length > 0 ) {
57+ return vaildMethod [ 0 ] ?. toUpperCase ( ) ;
58+ }
59+ return methods [ 0 ] ?. toUpperCase ( ) ;
5160}
5261
5362export class OpenAPIOptions {
@@ -86,47 +95,40 @@ export class OpenAPIOptions {
8695}
8796
8897export const request = async function ( options : OpenAPIOptions ) {
89- let {
90- endpoint,
91- action,
92- apiVersion,
93- params,
94- accessKeyId,
95- accessKeySecret,
96- productName,
97- meta,
98- bodyStyle,
99- credential
100- } = options ;
101- let method = meta ?. method ?. toUpperCase ( ) ;
102- let protocol = 'https' ;
103- endpoint = endpoint ? endpoint . replace ( 'http://' , '' ) . replace ( 'https://' , '' ) : `${ productName . toLowerCase ( ) } .cn-hangzhou.aliyuncs.com` ;
104- let pathname = '/' ;
105- const schema = meta ?. responses [ '200' ] && meta ?. responses [ '200' ] . schema ;
98+ let { endpoint, action, apiVersion, params, accessKeyId, accessKeySecret, productName, meta, bodyStyle, credential } =
99+ options ;
100+ // let method = meta?.method?.toUpperCase();
101+ let method = getMethod ( meta ?. methods ) ;
102+ let protocol = "https" ;
103+ endpoint = endpoint
104+ ? endpoint . replace ( "http://" , "" ) . replace ( "https://" , "" )
105+ : `${ productName . toLowerCase ( ) } .cn-hangzhou.aliyuncs.com` ;
106+ let pathname = meta ?. path ?. length ? meta . path : "/" ;
107+ const schema = meta ?. responses [ "200" ] && meta ?. responses [ "200" ] . schema ;
106108 let requestType ;
107- if ( meta ?. consumes ) {
108- requestType = _bodyType ( meta ?. consumes )
109- } else {
110- requestType = bodyStyle === ' json' ? ' json' : ' formData' ;
109+ if ( meta ?. consumes ) {
110+ requestType = _bodyType ( meta ?. consumes ) ;
111+ } else {
112+ requestType = bodyStyle === " json" ? " json" : " formData" ;
111113 }
112114 let responseType ;
113-
115+
114116 if ( ! schema ) {
115117 responseType = _bodyType ( meta . apis [ action ] && meta . apis [ action ] . produces ) ;
116118 } else if ( schema . xml ) {
117- responseType = ' xml' ;
118- } else if ( schema . type && schema . type !== ' object' ) {
119+ responseType = " xml" ;
120+ } else if ( schema . type && schema . type !== " object" ) {
119121 responseType = schema . format || schema . type ;
120- } else if ( meta ?. ext ?. produces ) {
122+ } else if ( meta ?. ext ?. produces ) {
121123 responseType = _bodyType ( meta . ext . produces ) ;
122- } else {
123- responseType = ' json' ;
124+ } else {
125+ responseType = " json" ;
124126 }
125127
126128 let request = { } as any ;
127129 request . headers = { } ;
128- if ( productName === ' ROS' && params . RegionId ) {
129- request . headers [ ' x-acs-region-id' ] = params . RegionId ;
130+ if ( productName === " ROS" && params . RegionId ) {
131+ request . headers [ " x-acs-region-id" ] = params . RegionId ;
130132 }
131133
132134 // const newParams = {};
@@ -136,90 +138,87 @@ export const request = async function (options: OpenAPIOptions) {
136138 // });
137139 // paramObject.params = newParams;
138140 const parameters = { } ;
139- meta ?. parameters ?. map ( param => {
141+ meta ?. parameters ?. map ( ( param ) => {
140142 parameters [ param . name ] = param ;
141- } )
143+ } ) ;
142144 // paramObject.params = params;
143145
144146 // eslint-disable-next-line guard-for-in
145147 for ( let name in params ) {
146148 let paramInfo = parameters [ name ] ;
147149 let value = params [ name ] ;
148150 if ( paramInfo ) {
149- if ( paramInfo . style
150- && paramInfo . style === 'json'
151- && typeof value !== 'string' ) {
151+ if ( paramInfo . style && paramInfo . style === "json" && typeof value !== "string" ) {
152152 value = JSON . stringify ( value ) ;
153153 }
154- if ( paramInfo . style
155- && Array . isArray ( value ) ) {
154+ if ( paramInfo . style && Array . isArray ( value ) ) {
156155 switch ( paramInfo . style ) {
157- case ' simple' :
158- value = value . join ( ',' ) ;
156+ case " simple" :
157+ value = value . join ( "," ) ;
159158 break ;
160- case ' spaceDelimited' :
161- value = value . join ( ' ' ) ;
159+ case " spaceDelimited" :
160+ value = value . join ( " " ) ;
162161 break ;
163- case ' pipeDelimited' :
164- value = value . join ( '|' ) ;
162+ case " pipeDelimited" :
163+ value = value . join ( "|" ) ;
165164 break ;
166165 }
167166 }
168167
169168 switch ( paramInfo . in ) {
170- case ' path' :
169+ case " path" :
171170 // path:"/repos/{RepoNamespace}/{RepoName}/build/[BuildId]/cancel"
172- if ( pathname . indexOf ( '*' ) !== - 1 && name === ' requestPath' ) {
171+ if ( pathname . indexOf ( "*" ) !== - 1 && name === " requestPath" ) {
173172 pathname = value ;
174173 } else if ( pathname . includes ( name ) ) {
175174 pathname = pathname . replace ( `{${ name } }` , value ) ;
176175 }
177176 break ;
178- case ' host' :
177+ case " host" :
179178 // endpoint 已经在前端处理过了
180179 // host 一般是 regionId
181180 break ;
182- case ' query' :
181+ case " query" :
183182 if ( ! request . query ) {
184183 request . query = { } ;
185184 }
186185 request . query [ name ] = value ;
187186 break ;
188- case ' body' :
189- case ' formData' :
187+ case " body" :
188+ case " formData" :
190189 if ( ! request . body ) {
191190 request . body = { } ;
192191 }
193- if ( bodyStyle === ' json' ) {
192+ if ( bodyStyle === " json" ) {
194193 request . body = params [ name ] ;
195- } else if ( name === ' RequestBody' && paramInfo . type === ' RequestBody' ) {
194+ } else if ( name === " RequestBody" && paramInfo . type === " RequestBody" ) {
196195 request . body = params [ name ] ;
197196 } else {
198197 request . body [ name ] = value ;
199198 }
200- if ( paramInfo . type === ' Binary' ) {
199+ if ( paramInfo . type === " Binary" ) {
201200 // TODO:上传文件
202201 // request.stream = await ossUtil.getStream(`tmpFile/${params[name]}`);
203202 }
204203 break ;
205- case ' header' :
204+ case " header" :
206205 request . headers [ name ] = value ;
207206 break ;
208207 }
209- } else if ( bodyStyle === ' json' && name === ' body' && params . body ) {
208+ } else if ( bodyStyle === " json" && name === " body" && params . body ) {
210209 // 兼容 bodyStyle 为 json,且不传 body 层的情况
211210 // 兼容 bodyStyle 为 json,且不传 body 层的情况
212- if ( typeof params . body === ' string' ) {
211+ if ( typeof params . body === " string" ) {
213212 request . body = JSON . parse ( params . body ) ;
214213 } else {
215214 request . body = params . body ;
216215 }
217216 }
218217 }
219218 let _credential ;
220- if ( credential && credential . type === ' bearer' ) {
219+ if ( credential && credential . type === " bearer" ) {
221220 let credentialConfig = new $Credential . Config ( {
222- type : ' bearer' ,
221+ type : " bearer" ,
223222 bearerToken : credential . token ,
224223 } ) ;
225224 _credential = new Credential ( credentialConfig ) ;
@@ -231,7 +230,7 @@ export const request = async function (options: OpenAPIOptions) {
231230 credential : _credential ,
232231 protocol : protocol ,
233232 readTimeout : 50000 ,
234- connectTimeout : 50000
233+ connectTimeout : 50000 ,
235234 } ) ;
236235 const data = {
237236 version : apiVersion ,
@@ -240,7 +239,7 @@ export const request = async function (options: OpenAPIOptions) {
240239 action,
241240 reqBodyType : requestType ,
242241 bodyType : responseType ,
243- authType : 'AK' ,
242+ authType : "AK" ,
244243 } ;
245244 return await client . doRequest ( data , request , { } ) ;
246- } ;
245+ } ;
0 commit comments