1- import { CloudFront } from 'aws-sdk'
1+ import { GetDistributionResult , UpdateDistributionRequest , DistributionConfig , LambdaFunctionAssociations , LambdaFunctionAssociation } from '@ aws-sdk/client-cloudfront '
22import { EventType } from './models'
3- import Types = CloudFront . Types
43
54export class ConfigUpdator {
65 // Lambda function ARN
@@ -9,15 +8,15 @@ export class ConfigUpdator {
98 // Lambda@edge function event type
109 private eventType : EventType
1110
12- private defaultLambdaFunctionAssociations : Types . LambdaFunctionAssociations
11+ private defaultLambdaFunctionAssociations : LambdaFunctionAssociations
1312
1413 /**
1514 * constructor
1615 *
1716 * @param {string } lambdaArn - Lambda arn
1817 * @param {string } [stage='development'] - stage
1918 **/
20- constructor ( lambdaArn : string , eventType : EventType = 'viewer-request' , defaultLambdaFunctionAssociations : Types . LambdaFunctionAssociations = {
19+ constructor ( lambdaArn : string , eventType : EventType = 'viewer-request' , defaultLambdaFunctionAssociations : LambdaFunctionAssociations = {
2120 Quantity : 0 ,
2221 Items : [ ]
2322 } ) {
@@ -59,7 +58,7 @@ export class ConfigUpdator {
5958 * @param {CloudFront.Types.DistributionConfig } config - updated distribution config
6059 * @return {CloudFront.UpdateDistributionRequest } update distribution param
6160 **/
62- public createUpdateDistributionParam ( data : CloudFront . GetDistributionResult , config : Types . DistributionConfig ) : CloudFront . UpdateDistributionRequest {
61+ public createUpdateDistributionParam ( data : GetDistributionResult , config : DistributionConfig ) : UpdateDistributionRequest {
6362 if ( ! data || ! data . Distribution ) throw new Error ( 'No such distribution' )
6463 const distribution = data . Distribution
6564 const params = {
@@ -77,7 +76,7 @@ export class ConfigUpdator {
7776 * @param {'detachEdge' | 'attachEdge' } action - update action type
7877 * @return {CloudFront.Types.DistributionConfig } config
7978 **/
80- public createUpdateDistributionConfig ( config : Types . DistributionConfig , action : string ) : Types . DistributionConfig {
79+ public createUpdateDistributionConfig ( config : DistributionConfig , action : string ) : DistributionConfig {
8180 switch ( action ) {
8281 case 'detachEdge' :
8382 return this . detatchEdgeFunction ( config )
@@ -105,15 +104,17 @@ export class ConfigUpdator {
105104 * @param {CloudFront.Types.DistributionConfig } config - CloudFront distribution config
106105 * @return {CloudFront.Types.DistributionConfig } updated distribution config
107106 **/
108- public detatchEdgeFunction ( config : Types . DistributionConfig ) : Types . DistributionConfig {
107+ public detatchEdgeFunction ( config : DistributionConfig ) : DistributionConfig {
109108 const defaultCacheBehavior = config . DefaultCacheBehavior
110- const lambdas : Types . LambdaFunctionAssociations = defaultCacheBehavior . LambdaFunctionAssociations || this . defaultLambdaFunctionAssociations
111- if ( lambdas . Quantity < 1 || ! lambdas . Items ) return config
112- const newLambdaItems : Types . LambdaFunctionAssociationList = [ ]
113- lambdas . Items . forEach ( item => {
109+ if ( ! defaultCacheBehavior ) return config
110+ const lambdas : LambdaFunctionAssociations = defaultCacheBehavior . LambdaFunctionAssociations || this . defaultLambdaFunctionAssociations
111+ if ( ! lambdas . Quantity || lambdas . Quantity < 1 || ! lambdas . Items ) return config
112+ const newLambdaItems : LambdaFunctionAssociation [ ] = [ ]
113+ lambdas . Items . forEach ( ( item : LambdaFunctionAssociation ) => {
114114 if ( ! item . EventType ) return
115115 if (
116116 item . EventType === this . eventType &&
117+ item . LambdaFunctionARN &&
117118 this . isTargetLambdaArn ( item . LambdaFunctionARN )
118119 ) {
119120 return
@@ -122,7 +123,7 @@ export class ConfigUpdator {
122123 } )
123124 lambdas . Quantity = newLambdaItems . length
124125 lambdas . Items = newLambdaItems
125- config . DefaultCacheBehavior . LambdaFunctionAssociations = lambdas
126+ defaultCacheBehavior . LambdaFunctionAssociations = lambdas
126127 return config
127128 }
128129
@@ -132,11 +133,12 @@ export class ConfigUpdator {
132133 * @param {CloudFront.Types.DistributionConfig } config - CloudFront distribution config
133134 * @return {CloudFront.Types.DistributionConfig } updated distribution config
134135 **/
135- public attatchEdgeFunction ( config : Types . DistributionConfig ) : Types . DistributionConfig {
136+ public attatchEdgeFunction ( config : DistributionConfig ) : DistributionConfig {
136137 const param = this . detatchEdgeFunction ( config )
137138 const defaultCacheBehavior = param . DefaultCacheBehavior
138- const lambdas : Types . LambdaFunctionAssociations = defaultCacheBehavior . LambdaFunctionAssociations || this . defaultLambdaFunctionAssociations
139- const newItem = {
139+ if ( ! defaultCacheBehavior ) return param
140+ const lambdas : LambdaFunctionAssociations = defaultCacheBehavior . LambdaFunctionAssociations || this . defaultLambdaFunctionAssociations
141+ const newItem : LambdaFunctionAssociation = {
140142 LambdaFunctionARN : this . getLambdaArn ( ) ,
141143 EventType : this . eventType
142144 }
@@ -146,7 +148,7 @@ export class ConfigUpdator {
146148 lambdas . Items . push ( newItem )
147149 }
148150 lambdas . Quantity = lambdas . Items . length
149- param . DefaultCacheBehavior . LambdaFunctionAssociations = lambdas
151+ defaultCacheBehavior . LambdaFunctionAssociations = lambdas
150152 return param
151153 }
152154}
0 commit comments