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" ;
6
6
7
7
function _bodyType ( types ) {
8
8
if ( ! types || types . length < 1 ) {
9
- return ' none' ;
9
+ return " none" ;
10
10
}
11
11
12
12
const type = types [ 0 ] ;
13
13
14
- if ( type === ' application/json' ) {
15
- return ' json' ;
14
+ if ( type === " application/json" ) {
15
+ return " json" ;
16
16
}
17
17
18
- if ( type === ' application/xml' ) {
19
- return ' xml' ;
18
+ if ( type === " application/xml" ) {
19
+ return " xml" ;
20
20
}
21
21
22
- if ( type === ' application/x-www-form-urlencoded' ) {
23
- return ' form' ;
22
+ if ( type === " application/x-www-form-urlencoded" ) {
23
+ return " form" ;
24
24
}
25
25
26
- if ( type === ' application/octet-stream' ) {
27
- return ' binary' ;
26
+ if ( type === " application/octet-stream" ) {
27
+ return " binary" ;
28
28
}
29
29
30
- return ' none' ;
30
+ return " none" ;
31
31
}
32
32
33
- function getMethod ( method ) {
33
+ function getMethodByString ( method ) {
34
34
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 ( "," ) ;
39
39
} else {
40
40
return method ;
41
41
}
42
- if ( methodArray . includes ( ' POST' ) ) {
43
- return ' POST' ;
42
+ if ( methodArray . includes ( " POST" ) ) {
43
+ return " POST" ;
44
44
}
45
- const vaildMethod = methodArray . filter ( item => FILTER_METHOD . indexOf ( item ) <= - 1 ) ;
45
+ const vaildMethod = methodArray . filter ( ( item ) => FILTER_METHOD . indexOf ( item ) <= - 1 ) ;
46
46
if ( vaildMethod . length > 0 ) {
47
47
return vaildMethod [ 0 ] ;
48
- }
48
+ }
49
49
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 ( ) ;
51
60
}
52
61
53
62
export class OpenAPIOptions {
@@ -86,47 +95,40 @@ export class OpenAPIOptions {
86
95
}
87
96
88
97
export 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 ;
106
108
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" ;
111
113
}
112
114
let responseType ;
113
-
115
+
114
116
if ( ! schema ) {
115
117
responseType = _bodyType ( meta . apis [ action ] && meta . apis [ action ] . produces ) ;
116
118
} 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" ) {
119
121
responseType = schema . format || schema . type ;
120
- } else if ( meta ?. ext ?. produces ) {
122
+ } else if ( meta ?. ext ?. produces ) {
121
123
responseType = _bodyType ( meta . ext . produces ) ;
122
- } else {
123
- responseType = ' json' ;
124
+ } else {
125
+ responseType = " json" ;
124
126
}
125
127
126
128
let request = { } as any ;
127
129
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 ;
130
132
}
131
133
132
134
// const newParams = {};
@@ -136,90 +138,87 @@ export const request = async function (options: OpenAPIOptions) {
136
138
// });
137
139
// paramObject.params = newParams;
138
140
const parameters = { } ;
139
- meta ?. parameters ?. map ( param => {
141
+ meta ?. parameters ?. map ( ( param ) => {
140
142
parameters [ param . name ] = param ;
141
- } )
143
+ } ) ;
142
144
// paramObject.params = params;
143
145
144
146
// eslint-disable-next-line guard-for-in
145
147
for ( let name in params ) {
146
148
let paramInfo = parameters [ name ] ;
147
149
let value = params [ name ] ;
148
150
if ( paramInfo ) {
149
- if ( paramInfo . style
150
- && paramInfo . style === 'json'
151
- && typeof value !== 'string' ) {
151
+ if ( paramInfo . style && paramInfo . style === "json" && typeof value !== "string" ) {
152
152
value = JSON . stringify ( value ) ;
153
153
}
154
- if ( paramInfo . style
155
- && Array . isArray ( value ) ) {
154
+ if ( paramInfo . style && Array . isArray ( value ) ) {
156
155
switch ( paramInfo . style ) {
157
- case ' simple' :
158
- value = value . join ( ',' ) ;
156
+ case " simple" :
157
+ value = value . join ( "," ) ;
159
158
break ;
160
- case ' spaceDelimited' :
161
- value = value . join ( ' ' ) ;
159
+ case " spaceDelimited" :
160
+ value = value . join ( " " ) ;
162
161
break ;
163
- case ' pipeDelimited' :
164
- value = value . join ( '|' ) ;
162
+ case " pipeDelimited" :
163
+ value = value . join ( "|" ) ;
165
164
break ;
166
165
}
167
166
}
168
167
169
168
switch ( paramInfo . in ) {
170
- case ' path' :
169
+ case " path" :
171
170
// path:"/repos/{RepoNamespace}/{RepoName}/build/[BuildId]/cancel"
172
- if ( pathname . indexOf ( '*' ) !== - 1 && name === ' requestPath' ) {
171
+ if ( pathname . indexOf ( "*" ) !== - 1 && name === " requestPath" ) {
173
172
pathname = value ;
174
173
} else if ( pathname . includes ( name ) ) {
175
174
pathname = pathname . replace ( `{${ name } }` , value ) ;
176
175
}
177
176
break ;
178
- case ' host' :
177
+ case " host" :
179
178
// endpoint 已经在前端处理过了
180
179
// host 一般是 regionId
181
180
break ;
182
- case ' query' :
181
+ case " query" :
183
182
if ( ! request . query ) {
184
183
request . query = { } ;
185
184
}
186
185
request . query [ name ] = value ;
187
186
break ;
188
- case ' body' :
189
- case ' formData' :
187
+ case " body" :
188
+ case " formData" :
190
189
if ( ! request . body ) {
191
190
request . body = { } ;
192
191
}
193
- if ( bodyStyle === ' json' ) {
192
+ if ( bodyStyle === " json" ) {
194
193
request . body = params [ name ] ;
195
- } else if ( name === ' RequestBody' && paramInfo . type === ' RequestBody' ) {
194
+ } else if ( name === " RequestBody" && paramInfo . type === " RequestBody" ) {
196
195
request . body = params [ name ] ;
197
196
} else {
198
197
request . body [ name ] = value ;
199
198
}
200
- if ( paramInfo . type === ' Binary' ) {
199
+ if ( paramInfo . type === " Binary" ) {
201
200
// TODO:上传文件
202
201
// request.stream = await ossUtil.getStream(`tmpFile/${params[name]}`);
203
202
}
204
203
break ;
205
- case ' header' :
204
+ case " header" :
206
205
request . headers [ name ] = value ;
207
206
break ;
208
207
}
209
- } else if ( bodyStyle === ' json' && name === ' body' && params . body ) {
208
+ } else if ( bodyStyle === " json" && name === " body" && params . body ) {
210
209
// 兼容 bodyStyle 为 json,且不传 body 层的情况
211
210
// 兼容 bodyStyle 为 json,且不传 body 层的情况
212
- if ( typeof params . body === ' string' ) {
211
+ if ( typeof params . body === " string" ) {
213
212
request . body = JSON . parse ( params . body ) ;
214
213
} else {
215
214
request . body = params . body ;
216
215
}
217
216
}
218
217
}
219
218
let _credential ;
220
- if ( credential && credential . type === ' bearer' ) {
219
+ if ( credential && credential . type === " bearer" ) {
221
220
let credentialConfig = new $Credential . Config ( {
222
- type : ' bearer' ,
221
+ type : " bearer" ,
223
222
bearerToken : credential . token ,
224
223
} ) ;
225
224
_credential = new Credential ( credentialConfig ) ;
@@ -231,7 +230,7 @@ export const request = async function (options: OpenAPIOptions) {
231
230
credential : _credential ,
232
231
protocol : protocol ,
233
232
readTimeout : 50000 ,
234
- connectTimeout : 50000
233
+ connectTimeout : 50000 ,
235
234
} ) ;
236
235
const data = {
237
236
version : apiVersion ,
@@ -240,7 +239,7 @@ export const request = async function (options: OpenAPIOptions) {
240
239
action,
241
240
reqBodyType : requestType ,
242
241
bodyType : responseType ,
243
- authType : 'AK' ,
242
+ authType : "AK" ,
244
243
} ;
245
244
return await client . doRequest ( data , request , { } ) ;
246
- } ;
245
+ } ;
0 commit comments