1+ const  {  Capi }  =  require ( '@tencent-sdk/capi' ) ; 
2+ 
3+ const  ApiRequest  =  function  ( auth ,  func ,  Region ,  debugOptions )  { 
4+  const  {  SecretId,  SecretKey }  =  auth 
5+  const  Token  =  auth . Token  ||  auth . token 
6+  const  body  =  { 
7+  FunctionName : func . functionName  ||  func . FunctionName , 
8+  Namespace : func . nameSpace  ||  func . Namespace  ||  'default' , 
9+  Qualifier : func . Qualifier  ||  func . qualifier  ||  '$LATEST' 
10+  } 
11+  if  ( ! SecretId  ||  ! SecretKey )  { 
12+  throw  Error ( 'The SecretId or SecretKey does not exist.' ) ; 
13+  } 
14+  if  ( ! body . FunctionName )  { 
15+  throw  Error ( 'The FunctionName does not exist.' ) ; 
16+  } 
17+ 
18+  this . client  =  new  Capi ( { 
19+  Region, 
20+  SecretId, 
21+  SecretKey, 
22+  Token, 
23+  ServiceType : 'scf' , 
24+  baseHost : 'tencentcloudapi.com' 
25+  } ) ; 
26+  this . commonParams  =  { 
27+  Version : '2018-04-16' , 
28+  ...body 
29+  } 
30+  this . debugOptions  =  debugOptions 
31+ } 
32+ 
33+ ApiRequest . prototype . request  =  async  function  ( action ,  params )  { 
34+  return  this . client . request ( 
35+  { 
36+  Action : action , 
37+  ...this . commonParams , 
38+  ...params , 
39+  } , 
40+  this . debugOptions , 
41+  true 
42+  ) ; 
43+ } 
44+ 
45+ ApiRequest . prototype . startDebugging  =  async  function  ( params )  { 
46+  return  this . request ( 'StartDebugging' ,  params ) ; 
47+ } 
48+ 
49+ ApiRequest . prototype . stopDebugging  =  async  function  ( params )  { 
50+  return  this . request ( 'StopDebugging' ,  params ) ; 
51+ } 
52+ 
53+ ApiRequest . prototype . getDebuggingInfo  =  async  function  ( params )  { 
54+  const  getDebuggingInfoPm  =  ( )  =>  { 
55+  return  new  Promise ( ( resolve ,  reject )  =>  { 
56+  let  debuggingInfo 
57+  let  count  =  0 
58+  let  timer  =  setInterval ( async  ( )  =>  { 
59+  try  { 
60+  count ++ 
61+  if  ( count  >  20 )  { 
62+  clearInterval ( timer ) 
63+  reject ( { 
64+  "Response" :
65+  { 
66+  "Error" :
67+  { 
68+  "Code" : "Timeout" , 
69+  "Message" : "Get debugging info timeout." 
70+  } 
71+  } 
72+  } ) 
73+  } 
74+  debuggingInfo  =  await  this . request ( 'GetDebuggingInfo' ,  params )  ||  { } 
75+  const  {  Response =  { }  }  =  debuggingInfo 
76+  if  ( Response . Status  ===  'Active' )  { 
77+  resolve ( Response . DebuggingInfo ) 
78+  clearInterval ( timer ) 
79+  } 
80+  }  catch  ( e )  { 
81+  clearInterval ( timer ) 
82+  reject ( e ) 
83+  } 
84+  } ,  2000 ) ; 
85+  } ) 
86+  } 
87+  return  getDebuggingInfoPm ( ) 
88+ } 
89+ 
90+ module . exports  =  ApiRequest 
0 commit comments