11const ensureIterable = require ( 'type/iterable/ensure' )
2- const ensurePlainObject = require ( 'type/plain-object/ensure' )
3- const ensureString = require ( 'type/string/ensure' )
4- const random = require ( 'ext/string/random' )
52const path = require ( 'path' )
63const { Component } = require ( '@serverless/core' )
74
@@ -12,113 +9,40 @@ const DEFAULTS = {
129}
1310
1411class TencentBottle extends Component {
15- getDefaultProtocol ( protocols ) {
16- if ( protocols . map ( ( i ) => i . toLowerCase ( ) ) . includes ( 'https' ) ) {
17- return 'https'
18- }
19- return 'http'
20- }
21-
22- async prepareInputs ( inputs = { } ) {
23- inputs . name =
24- ensureString ( inputs . functionName , { isOptional : true } ) ||
25- this . state . functionName ||
26- `BottleComponent_${ random ( { length : 6 } ) } `
27- inputs . codeUri = ensureString ( inputs . code , { isOptional : true } ) || process . cwd ( )
28- inputs . region = ensureString ( inputs . region , { default : 'ap-guangzhou' } )
29- inputs . namespace = ensureString ( inputs . namespace , { default : 'default' } )
30- inputs . include = ensureIterable ( inputs . include , { default : [ ] , ensureItem : ensureString } )
31- inputs . exclude = ensureIterable ( inputs . exclude , { default : [ ] , ensureItem : ensureString } )
32- inputs . apigatewayConf = ensurePlainObject ( inputs . apigatewayConf , { default : { } } )
33-
34- inputs . include = [ path . join ( __dirname , 'component' ) ]
35- inputs . exclude . push ( '.git/**' , '.gitignore' , '.serverless' , '.DS_Store' )
36-
37- inputs . handler = ensureString ( inputs . handler , { default : DEFAULTS . handler } )
38- inputs . runtime = ensureString ( inputs . runtime , { default : DEFAULTS . runtime } )
39- inputs . apigatewayConf = ensurePlainObject ( inputs . apigatewayConf , { default : { } } )
40-
41- if ( inputs . functionConf ) {
42- inputs . timeout = inputs . functionConf . timeout ? inputs . functionConf . timeout : 3
43- inputs . memorySize = inputs . functionConf . memorySize ? inputs . functionConf . memorySize : 128
44- if ( inputs . functionConf . environment ) {
45- inputs . environment = inputs . functionConf . environment
46- }
47- if ( inputs . functionConf . vpcConfig ) {
48- inputs . vpcConfig = inputs . functionConf . vpcConfig
49- }
50- }
51-
52- return inputs
53- }
54-
5512 async default ( inputs = { } ) {
56- inputs = await this . prepareInputs ( inputs )
57-
58- const tencentCloudFunction = await this . load ( '@serverless/tencent-scf' )
59- const tencentApiGateway = await this . load ( '@serverless/tencent-apigateway' )
13+ inputs . include = [ ]
6014
61- inputs . fromClientRemark = inputs . fromClientRemark || 'tencent-pyramid'
62- const tencentCloudFunctionOutputs = await tencentCloudFunction ( inputs )
63- const apigwParam = {
64- serviceName : inputs . serviceName ,
65- description : 'Serverless Framework Tencent-Bottle Component' ,
66- serviceId : inputs . serviceId ,
67- region : inputs . region ,
68- protocols : inputs . apigatewayConf . protocols || [ 'http' ] ,
69- environment :
70- inputs . apigatewayConf && inputs . apigatewayConf . environment
71- ? inputs . apigatewayConf . environment
72- : 'release' ,
73- endpoints : [
74- {
75- path : '/' ,
76- method : 'ANY' ,
77- function : {
78- isIntegratedResponse : true ,
79- functionName : tencentCloudFunctionOutputs . Name ,
80- functionNamespace : inputs . namespace
81- }
82- }
83- ] ,
84- customDomain : inputs . apigatewayConf . customDomain
85- }
15+ const cachePath = path . join ( __dirname , 'component' )
16+ inputs . include = ensureIterable ( inputs . include , { default : [ ] } )
17+ inputs . include . push ( cachePath )
8618
87- if ( inputs . apigatewayConf && inputs . apigatewayConf . auth ) {
88- apigwParam . endpoints [ 0 ] . usagePlan = inputs . apigatewayConf . usagePlan
89- }
90- if ( inputs . apigatewayConf && inputs . apigatewayConf . auth ) {
91- apigwParam . endpoints [ 0 ] . auth = inputs . apigatewayConf . auth
92- }
19+ inputs . handelr = DEFAULTS . handler
20+ inputs . runtime = DEFAULTS . runtime
9321
94- apigwParam . fromClientRemark = inputs . fromClientRemark || 'tencent-bottle'
95- const tencentApiGatewayOutputs = await tencentApiGateway ( apigwParam )
96- const outputs = {
97- region : inputs . region ,
98- functionName : inputs . name ,
99- apiGatewayServiceId : tencentApiGatewayOutputs . serviceId ,
100- url : `${ this . getDefaultProtocol ( tencentApiGatewayOutputs . protocols ) } ://${
101- tencentApiGatewayOutputs . subDomain
102- } /${ tencentApiGatewayOutputs . environment } /`
103- }
22+ const Framework = await this . load ( '@serverless/tencent-framework' )
10423
105- this . state = outputs
24+ const framworkOutpus = await Framework ( {
25+ ...inputs ,
26+ ...{
27+ framework : 'bottle'
28+ }
29+ } )
10630
31+ this . state = framworkOutpus
10732 await this . save ( )
108-
109- return outputs
33+ return framworkOutpus
11034 }
11135
11236 async remove ( inputs = { } ) {
113- const removeInput = {
114- fromClientRemark : inputs . fromClientRemark || 'tencent-bottle'
115- }
116- const tencentCloudFunction = await this . load ( '@serverless/tencent-scf' )
117- const tencentApiGateway = await this . load ( '@serverless/tencent-apigateway' )
118-
119- await tencentCloudFunction . remove ( removeInput )
120- await tencentApiGateway . remove ( removeInput )
121-
37+ const Framework = await this . load ( '@serverless/tencent-framework' )
38+ await Framework . remove ( {
39+ ... inputs ,
40+ ... {
41+ framework : 'bottle'
42+ }
43+ } )
44+ this . state = { }
45+ await this . save ( )
12246 return { }
12347 }
12448}
0 commit comments