1
1
import { Capi } from '@tencent-sdk/capi' ;
2
2
import {
3
+ UpdateApiInputs ,
3
4
ApiDeployInputs ,
4
5
ApiDeployOutputs ,
5
6
CreateOrUpdateApiInputs ,
6
7
ApiRemoveInputs ,
7
8
ApiBulkRemoveInputs ,
8
9
ApiBulkDeployInputs ,
10
+ ApiDetail ,
9
11
} from '../interface' ;
10
12
import { pascalCaseProps } from '../../../utils' ;
11
13
import { ApiTypeError } from '../../../utils/error' ;
@@ -39,7 +41,7 @@ export default class ApiEntity {
39
41
return true ;
40
42
}
41
43
42
- async create ( { serviceId, endpoint, environment, created } : CreateOrUpdateApiInputs ) {
44
+ async create ( { serviceId, endpoint, environment } : CreateOrUpdateApiInputs ) {
43
45
// compatibility for secret auth config depends on auth & usagePlan
44
46
const authType = endpoint ?. auth ? 'SECRET' : endpoint ?. authType ?? 'NONE' ;
45
47
const businessType = endpoint ?. businessType ?? 'NORMAL' ;
@@ -90,70 +92,31 @@ export default class ApiEntity {
90
92
91
93
this . formatInput ( endpoint , apiInputs ) ;
92
94
93
- let apiDetail : {
94
- ApiId ?: string ;
95
- InternalDomain ?: string ;
96
- } ;
95
+ const res = await this . request ( {
96
+ Action : 'CreateApi' ,
97
+ ...apiInputs ,
98
+ } ) ;
99
+ const { ApiId } = res ;
100
+ output . apiId = ApiId ;
97
101
98
- if ( endpoint ?. apiId ) {
99
- apiDetail = await this . getById ( { serviceId : serviceId ! , apiId : endpoint . apiId } ) ;
100
- }
102
+ console . log ( `API ${ ApiId } created` ) ;
103
+ const apiDetail : ApiDetail = await this . request ( {
104
+ Action : 'DescribeApi' ,
105
+ serviceId : serviceId ,
106
+ apiId : output . apiId ,
107
+ } ) ;
108
+ output . internalDomain = apiDetail . InternalDomain || '' ;
101
109
102
- if ( ! apiDetail ! ) {
103
- apiDetail = await this . getByPathAndMethod ( {
104
- serviceId : serviceId ! ,
105
- path : endpoint ?. path ! ,
106
- method : endpoint ?. method ! ,
107
- } ) ;
110
+ if ( endpoint ?. isBase64Encoded && endpoint . isBase64Trigger ) {
111
+ apiInputs . isBase64Trigger = endpoint . isBase64Trigger ;
112
+ apiInputs . base64EncodedTriggerRules = endpoint . base64EncodedTriggerRules ;
108
113
}
109
114
110
- if ( apiDetail && endpoint ) {
111
- console . log ( `Api method ${ endpoint ?. method } , path ${ endpoint ?. path } already exist` ) ;
112
- endpoint . apiId = apiDetail . ApiId ;
113
-
114
- if ( endpoint . isBase64Encoded && endpoint . isBase64Trigger ) {
115
- apiInputs . isBase64Trigger = endpoint . isBase64Trigger ;
116
- apiInputs . base64EncodedTriggerRules = endpoint . base64EncodedTriggerRules ;
117
- }
118
-
119
- await this . request ( {
120
- Action : 'ModifyApi' ,
121
- apiId : endpoint . apiId ,
122
- ...apiInputs ,
123
- } ) ;
124
-
125
- output . apiId = endpoint . apiId ;
126
- output . created = ! ! created ;
127
- output . internalDomain = apiDetail . InternalDomain || '' ;
128
- console . log ( `Api ${ output . apiId } updated` ) ;
129
- } else {
130
- const res = await this . request ( {
131
- Action : 'CreateApi' ,
132
- ...apiInputs ,
133
- } ) ;
134
- const { ApiId } = res ;
135
- output . apiId = ApiId ;
136
- output . created = true ;
137
-
138
- console . log ( `API ${ ApiId } created` ) ;
139
- apiDetail = await this . request ( {
140
- Action : 'DescribeApi' ,
141
- serviceId : serviceId ,
142
- apiId : output . apiId ,
143
- } ) ;
144
- output . internalDomain = apiDetail . InternalDomain || '' ;
145
-
146
- if ( endpoint ?. isBase64Encoded && endpoint . isBase64Trigger ) {
147
- apiInputs . isBase64Trigger = endpoint . isBase64Trigger ;
148
- apiInputs . base64EncodedTriggerRules = endpoint . base64EncodedTriggerRules ;
149
- }
150
-
151
- await this . request ( {
152
- Action : 'ModifyApi' ,
153
- apiId : ApiId ,
154
- ...apiInputs ,
155
- } ) ;
156
- }
115
+ await this . request ( {
116
+ Action : 'ModifyApi' ,
117
+ apiId : ApiId ,
118
+ ...apiInputs ,
119
+ } ) ;
157
120
158
121
output . apiName = apiInputs . apiName ;
159
122
@@ -172,15 +135,18 @@ export default class ApiEntity {
172
135
return output ;
173
136
}
174
137
175
- async update ( { serviceId, endpoint, environment, created } : CreateOrUpdateApiInputs ) {
138
+ async update (
139
+ { serviceId, endpoint, environment, created } : UpdateApiInputs ,
140
+ apiDetail : ApiDetail ,
141
+ ) {
176
142
// compatibility for secret auth config depends on auth & usagePlan
177
143
const authType = endpoint ?. auth ? 'SECRET' : endpoint ?. authType ?? 'NONE' ;
178
144
const businessType = endpoint ?. businessType ?? 'NORMAL' ;
179
145
const output : ApiDeployOutputs = {
180
146
path : endpoint ?. path ,
181
147
method : endpoint ?. method ,
182
148
apiName : endpoint ?. apiName || 'index' ,
183
- created : true ,
149
+ created : false ,
184
150
authType : authType ,
185
151
businessType : businessType ,
186
152
isBase64Encoded : endpoint ?. isBase64Encoded === true ,
@@ -223,70 +189,24 @@ export default class ApiEntity {
223
189
224
190
this . formatInput ( endpoint , apiInputs ) ;
225
191
226
- let apiDetail : {
227
- ApiId ?: string ;
228
- InternalDomain ?: string ;
229
- } ;
230
-
231
- if ( endpoint ?. apiId ) {
232
- apiDetail = await this . getById ( { serviceId : serviceId ! , apiId : endpoint . apiId } ) ;
233
- }
192
+ console . log ( `Api method ${ endpoint ?. method } , path ${ endpoint ?. path } already exist` ) ;
193
+ endpoint . apiId = apiDetail . ApiId ;
234
194
235
- if ( ! apiDetail ! ) {
236
- apiDetail = await this . getByPathAndMethod ( {
237
- serviceId : serviceId ! ,
238
- path : endpoint ?. path ! ,
239
- method : endpoint ?. method ! ,
240
- } ) ;
195
+ if ( endpoint . isBase64Encoded && endpoint . isBase64Trigger ) {
196
+ apiInputs . isBase64Trigger = endpoint . isBase64Trigger ;
197
+ apiInputs . base64EncodedTriggerRules = endpoint . base64EncodedTriggerRules ;
241
198
}
242
199
243
- if ( apiDetail && endpoint ) {
244
- console . log ( `Api method ${ endpoint ?. method } , path ${ endpoint ?. path } already exist` ) ;
245
- endpoint . apiId = apiDetail . ApiId ;
246
-
247
- if ( endpoint . isBase64Encoded && endpoint . isBase64Trigger ) {
248
- apiInputs . isBase64Trigger = endpoint . isBase64Trigger ;
249
- apiInputs . base64EncodedTriggerRules = endpoint . base64EncodedTriggerRules ;
250
- }
251
-
252
- await this . request ( {
253
- Action : 'ModifyApi' ,
254
- apiId : endpoint . apiId ,
255
- ...apiInputs ,
256
- } ) ;
257
-
258
- output . apiId = endpoint . apiId ;
259
- output . created = ! ! created ;
260
- output . internalDomain = apiDetail . InternalDomain || '' ;
261
- console . log ( `Api ${ output . apiId } updated` ) ;
262
- } else {
263
- const res = await this . request ( {
264
- Action : 'CreateApi' ,
265
- ...apiInputs ,
266
- } ) ;
267
- const { ApiId } = res ;
268
- output . apiId = ApiId ;
269
- output . created = true ;
270
-
271
- console . log ( `API ${ ApiId } created` ) ;
272
- apiDetail = await this . request ( {
273
- Action : 'DescribeApi' ,
274
- serviceId : serviceId ,
275
- apiId : output . apiId ,
276
- } ) ;
277
- output . internalDomain = apiDetail . InternalDomain || '' ;
278
-
279
- if ( endpoint ?. isBase64Encoded && endpoint . isBase64Trigger ) {
280
- apiInputs . isBase64Trigger = endpoint . isBase64Trigger ;
281
- apiInputs . base64EncodedTriggerRules = endpoint . base64EncodedTriggerRules ;
282
- }
200
+ await this . request ( {
201
+ Action : 'ModifyApi' ,
202
+ apiId : endpoint . apiId ,
203
+ ...apiInputs ,
204
+ } ) ;
283
205
284
- await this . request ( {
285
- Action : 'ModifyApi' ,
286
- apiId : ApiId ,
287
- ...apiInputs ,
288
- } ) ;
289
- }
206
+ output . apiId = endpoint . apiId ;
207
+ output . created = ! ! created ;
208
+ output . internalDomain = apiDetail . InternalDomain || '' ;
209
+ console . log ( `Api ${ output . apiId } updated` ) ;
290
210
291
211
output . apiName = apiInputs . apiName ;
292
212
@@ -387,13 +307,35 @@ export default class ApiEntity {
387
307
}
388
308
389
309
let curApi ;
310
+ let apiDetail : ApiDetail | null = null ;
311
+ let apiExist = false ;
390
312
if ( apiConfig . apiId ) {
391
- curApi = await this . update ( {
392
- serviceId,
393
- environment,
394
- endpoint : apiConfig ,
395
- created : exist && exist . created ,
313
+ apiDetail = await this . getById ( { serviceId : serviceId ! , apiId : apiConfig . apiId } ) ;
314
+ }
315
+
316
+ if ( ! apiDetail ) {
317
+ apiDetail = await this . getByPathAndMethod ( {
318
+ serviceId : serviceId ! ,
319
+ path : apiConfig ?. path ! ,
320
+ method : apiConfig ?. method ! ,
396
321
} ) ;
322
+ }
323
+
324
+ if ( apiDetail ) {
325
+ apiExist = true ;
326
+ }
327
+
328
+ // api 存在就更新,不存在就创建
329
+ if ( apiExist ) {
330
+ curApi = await this . update (
331
+ {
332
+ serviceId,
333
+ environment,
334
+ endpoint : apiConfig ,
335
+ created : exist && exist . created ,
336
+ } ,
337
+ apiDetail ,
338
+ ) ;
397
339
} else {
398
340
curApi = await this . create ( {
399
341
serviceId,
0 commit comments