11import { Capi } from '@tencent-sdk/capi' ;
22import {
3+ UpdateApiInputs ,
34 ApiDeployInputs ,
45 ApiDeployOutputs ,
56 CreateOrUpdateApiInputs ,
67 ApiRemoveInputs ,
78 ApiBulkRemoveInputs ,
89 ApiBulkDeployInputs ,
10+ ApiDetail ,
911} from '../interface' ;
1012import { pascalCaseProps } from '../../../utils' ;
1113import { ApiTypeError } from '../../../utils/error' ;
@@ -39,7 +41,7 @@ export default class ApiEntity {
3941 return true ;
4042 }
4143
42- async create ( { serviceId, endpoint, environment, created } : CreateOrUpdateApiInputs ) {
44+ async create ( { serviceId, endpoint, environment } : CreateOrUpdateApiInputs ) {
4345 // compatibility for secret auth config depends on auth & usagePlan
4446 const authType = endpoint ?. auth ? 'SECRET' : endpoint ?. authType ?? 'NONE' ;
4547 const businessType = endpoint ?. businessType ?? 'NORMAL' ;
@@ -90,70 +92,31 @@ export default class ApiEntity {
9092
9193 this . formatInput ( endpoint , apiInputs ) ;
9294
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 ;
97101
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 || '' ;
101109
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 ;
108113 }
109114
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+ } ) ;
157120
158121 output . apiName = apiInputs . apiName ;
159122
@@ -172,15 +135,18 @@ export default class ApiEntity {
172135 return output ;
173136 }
174137
175- async update ( { serviceId, endpoint, environment, created } : CreateOrUpdateApiInputs ) {
138+ async update (
139+ { serviceId, endpoint, environment, created } : UpdateApiInputs ,
140+ apiDetail : ApiDetail ,
141+ ) {
176142 // compatibility for secret auth config depends on auth & usagePlan
177143 const authType = endpoint ?. auth ? 'SECRET' : endpoint ?. authType ?? 'NONE' ;
178144 const businessType = endpoint ?. businessType ?? 'NORMAL' ;
179145 const output : ApiDeployOutputs = {
180146 path : endpoint ?. path ,
181147 method : endpoint ?. method ,
182148 apiName : endpoint ?. apiName || 'index' ,
183- created : true ,
149+ created : false ,
184150 authType : authType ,
185151 businessType : businessType ,
186152 isBase64Encoded : endpoint ?. isBase64Encoded === true ,
@@ -223,70 +189,24 @@ export default class ApiEntity {
223189
224190 this . formatInput ( endpoint , apiInputs ) ;
225191
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 ;
234194
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 ;
241198 }
242199
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+ } ) ;
283205
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` ) ;
290210
291211 output . apiName = apiInputs . apiName ;
292212
@@ -387,13 +307,35 @@ export default class ApiEntity {
387307 }
388308
389309 let curApi ;
310+ let apiDetail : ApiDetail | null = null ;
311+ let apiExist = false ;
390312 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 ! ,
396321 } ) ;
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+ ) ;
397339 } else {
398340 curApi = await this . create ( {
399341 serviceId,
0 commit comments