@@ -14,78 +14,73 @@ const resolveCode = (location: string): string => {
1414 return fileContent . toString ( 'base64' ) ;
1515} ;
1616
17- const replaceVars = (
18- value : { [ key : string ] : unknown } | string | number | unknown ,
19- ) : { [ key : string ] : unknown } | string | number | unknown => {
17+ const replaceVars = < T > ( value : T ) : T => {
2018 if ( typeof value === 'string' ) {
2119 const matchVar = value . match ( / ^ \$ \{ v a r s \. ( \w + ) } $ / ) ;
2220 const containsVar = value . match ( / \$ \{ v a r s \. ( \w + ) } / ) ;
2321 if ( matchVar ?. length ) {
24- return ros . Fn . ref ( matchVar [ 1 ] ) ;
22+ return ros . Fn . ref ( matchVar [ 1 ] ) as T ;
2523 }
2624 if ( containsVar ?. length ) {
27- return ros . Fn . sub ( value . replace ( / \$ \{ v a r s \. ( \w + ) } / g, '${$1}' ) ) ;
25+ return ros . Fn . sub ( value . replace ( / \$ \{ v a r s \. ( \w + ) } / g, '${$1}' ) ) as T ;
2826 }
2927 return value ;
3028 }
3129
3230 if ( Array . isArray ( value ) ) {
33- return value . map ( replaceVars ) ;
31+ return value . map ( replaceVars ) as T ;
3432 }
3533
3634 if ( typeof value === 'object' && value !== null ) {
37- return Object . fromEntries ( Object . entries ( value ) . map ( ( [ key , val ] ) => [ key , replaceVars ( val ) ] ) ) ;
35+ return Object . fromEntries (
36+ Object . entries ( value ) . map ( ( [ key , val ] ) => [ key , replaceVars ( val ) ] ) ,
37+ ) as T ;
3838 }
3939
4040 return value ;
4141} ;
4242
4343export class IacStack extends ros . Stack {
44- private readonly iac : ServerlessIac ;
45-
4644 constructor ( scope : ros . Construct , iac : ServerlessIac , context : ActionContext ) {
4745 super ( scope , iac . service , {
4846 tags : iac . tags . reduce ( ( acc : { [ key : string ] : string } , tag ) => {
49- acc [ tag . key ] = tag . value ;
47+ acc [ tag . key ] = replaceVars ( tag . value ) ;
5048 return acc ;
5149 } , { } ) ,
5250 } ) ;
5351
54- this . iac = replaceVars ( iac ) as ServerlessIac ;
55- console . log ( 'IAC:' , JSON . stringify ( this . iac ) ) ;
56-
57- Object . entries ( this . iac . vars ) . map (
52+ Object . entries ( iac . vars ) . map (
5853 ( [ key , value ] ) =>
5954 new ros . RosParameter ( this , key , {
6055 type : RosParameterType . STRING ,
6156 defaultValue : value ,
6257 } ) ,
6358 ) ;
6459
65- new ros . RosInfo ( this , ros . RosInfo . description , `${ this . iac . service } stack` ) ;
60+ new ros . RosInfo ( this , ros . RosInfo . description , replaceVars ( `${ iac . service } stack` ) ) ;
6661
6762 const service = new fc . RosService (
6863 this ,
69- `${ this . iac . service } -service` ,
64+ replaceVars ( `${ iac . service } -service` ) ,
7065 {
71- serviceName : `${ this . iac . service } -service` ,
72- tags : this . iac . tags ,
66+ serviceName : replaceVars ( `${ iac . service } -service` ) ,
67+ tags : replaceVars ( iac . tags ) ,
7368 } ,
7469 true ,
7570 ) ;
7671
77- this . iac . functions . forEach ( ( fnc ) => {
72+ iac . functions . forEach ( ( fnc ) => {
7873 const func = new fc . RosFunction (
7974 this ,
8075 fnc . key ,
8176 {
82- functionName : fnc . name ,
83- serviceName : service . serviceName ,
84- handler : fnc . handler ,
85- runtime : fnc . runtime ,
86- memorySize : fnc . memory ,
87- timeout : fnc . timeout ,
88- environmentVariables : fnc . environment ,
77+ functionName : replaceVars ( fnc . name ) ,
78+ serviceName : replaceVars ( service . serviceName ) ,
79+ handler : replaceVars ( fnc . handler ) ,
80+ runtime : replaceVars ( fnc . runtime ) ,
81+ memorySize : replaceVars ( fnc . memory ) ,
82+ timeout : replaceVars ( fnc . timeout ) ,
83+ environmentVariables : replaceVars ( fnc . environment ) ,
8984 code : {
9085 zipFile : resolveCode ( fnc . code ) ,
9186 } ,
@@ -95,14 +90,14 @@ export class IacStack extends ros.Stack {
9590 func . addDependsOn ( service ) ;
9691 } ) ;
9792
98- const apiGateway = this . iac . events . find ( ( event ) => event . type === EventTypes . API_GATEWAY ) ;
93+ const apiGateway = iac . events . find ( ( event ) => event . type === EventTypes . API_GATEWAY ) ;
9994 if ( apiGateway ) {
10095 const gatewayAccessRole = new ram . RosRole (
10196 this ,
102- `${ this . iac . service } _role` ,
97+ replaceVars ( `${ iac . service } _role` ) ,
10398 {
104- roleName : `${ this . iac . service } -gateway-access-role` ,
105- description : `${ this . iac . service } role` ,
99+ roleName : replaceVars ( `${ iac . service } -gateway-access-role` ) ,
100+ description : replaceVars ( `${ iac . service } role` ) ,
106101 assumeRolePolicyDocument : {
107102 version : '1' ,
108103 statement : [
@@ -117,7 +112,7 @@ export class IacStack extends ros.Stack {
117112 } ,
118113 policies : [
119114 {
120- policyName : `${ iac . service } -policy` ,
115+ policyName : replaceVars ( `${ iac . service } -policy` ) ,
121116 policyDocument : {
122117 version : '1' ,
123118 statement : [
@@ -137,30 +132,31 @@ export class IacStack extends ros.Stack {
137132
138133 const apiGatewayGroup = new agw . RosGroup (
139134 this ,
140- `${ this . iac . service } _apigroup` ,
135+ replaceVars ( `${ iac . service } _apigroup` ) ,
141136 {
142- groupName : `${ this . iac . service } _apigroup` ,
143- tags : this . iac . tags ,
137+ groupName : replaceVars ( `${ iac . service } _apigroup` ) ,
138+ tags : replaceVars ( iac . tags ) ,
144139 } ,
145140 true ,
146141 ) ;
147142
148- this . iac . events
143+ iac . events
149144 . filter ( ( event ) => event . type === EventTypes . API_GATEWAY )
150145 . forEach ( ( event ) => {
151146 event . triggers . forEach ( ( trigger ) => {
152147 const key = `${ trigger . method } _${ trigger . path } ` . toLowerCase ( ) . replace ( / \/ / g, '_' ) ;
148+
153149 const api = new agw . RosApi (
154150 this ,
155- `${ event . key } _api_${ key } ` ,
151+ replaceVars ( `${ event . key } _api_${ key } ` ) ,
156152 {
157- apiName : `${ event . name } _api_${ key } ` ,
153+ apiName : replaceVars ( `${ event . name } _api_${ key } ` ) ,
158154 groupId : apiGatewayGroup . attrGroupId ,
159155 visibility : 'PRIVATE' ,
160156 requestConfig : {
161157 requestProtocol : 'HTTP' ,
162- requestHttpMethod : trigger . method ,
163- requestPath : trigger . path ,
158+ requestHttpMethod : replaceVars ( trigger . method ) ,
159+ requestPath : replaceVars ( trigger . path ) ,
164160 requestMode : 'PASSTHROUGH' ,
165161 } ,
166162 serviceConfig : {
@@ -174,7 +170,7 @@ export class IacStack extends ros.Stack {
174170 } ,
175171 resultSample : 'ServerlessInsight resultSample' ,
176172 resultType : 'JSON' ,
177- tags : iac . tags ,
173+ tags : replaceVars ( iac . tags ) ,
178174 } ,
179175 true ,
180176 ) ;
0 commit comments