Skip to content

Commit 7ab7922

Browse files
committed
feat: set evet type difinition
1 parent 0b5e969 commit 7ab7922

File tree

5 files changed

+85
-73
lines changed

5 files changed

+85
-73
lines changed

__tests__/index.test.ts renamed to __tests__/controller.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import Controller from '../libs/index'
1+
import Controller from '../libs/controller'
22

3-
describe('libs/index.ts', () => {
3+
describe('libs/contorller.ts', () => {
44
describe('constructor', () => {
55
it('should not be throw error', () => {
66
expect(() => {

libs/configUpdator.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { CloudFront } from 'aws-sdk'
2+
import { EventType } from './models'
23
import Types = CloudFront.Types
34

4-
export class LambdaEdgeController {
5+
export class ConfigUpdator {
56
// Lambda function ARN
67
private lambdaArn: string
78

89
// Lambda@edge function event type
9-
private eventType: Types.EventType
10+
private eventType: EventType
1011

1112
private defaultLambdaFunctionAssociations: Types.LambdaFunctionAssociations
1213

@@ -16,7 +17,7 @@ export class LambdaEdgeController {
1617
* @param {string} lambdaArn - Lambda arn
1718
* @param {string} [stage='development'] - stage
1819
**/
19-
constructor (lambdaArn: string, eventType: Types.EventType = 'viewer-request', defaultLambdaFunctionAssociations: Types.LambdaFunctionAssociations = {
20+
constructor (lambdaArn: string, eventType: EventType = 'viewer-request', defaultLambdaFunctionAssociations: Types.LambdaFunctionAssociations = {
2021
Quantity: 0,
2122
Items: []
2223
}) {
@@ -36,18 +37,18 @@ export class LambdaEdgeController {
3637

3738
/**
3839
* Update Lambda@edge function event type
39-
* @param {CloudFront.Types.EventType} type
40+
* @param {CloudFront.EventType} type
4041
*/
41-
public updateEventType (type: Types.EventType): this {
42+
public updateEventType (type: EventType): this {
4243
this.eventType = type
4344
return this
4445
}
4546

4647
/**
4748
* Get target event type
48-
* @return {CloudFront.Types.EventType}
49+
* @return {CloudFront.EventType}
4950
*/
50-
public getTargetEventType (): Types.EventType {
51+
public getTargetEventType (): EventType {
5152
return this.eventType
5253
}
5354

@@ -150,4 +151,4 @@ export class LambdaEdgeController {
150151
}
151152
}
152153

153-
export default LambdaEdgeController
154+
export default ConfigUpdator

libs/controller.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import 'tslib'
2+
import { CloudFront } from 'aws-sdk'
3+
import ConfigUpdator from './configUpdator'
4+
import { ControllerClient, EventType } from './models'
5+
6+
export class LambdaEdgeController {
7+
// CloudFront client from AWS SDK
8+
private cloudfront: CloudFront
9+
10+
// CloudFront config updator
11+
private updator: ConfigUpdator
12+
13+
/**
14+
* constructor
15+
* @param {string} lambdaArn - Lambda arn
16+
* @param {string} [stage='development'] - stage
17+
* @param {ControllerClient} clients - Clients object
18+
**/
19+
constructor (lambdaArn: string, eventType: EventType = 'viewer-request', client?: ControllerClient) {
20+
this.cloudfront = client && client.cloudfront ? client.cloudfront : new CloudFront()
21+
this.updator = client && client.configUpdator ? client.configUpdator : new ConfigUpdator(lambdaArn, eventType)
22+
}
23+
24+
/**
25+
* Reove lambda edge function from specific CloudFront Distribution
26+
*
27+
* @param {string} distributionId - CloudFront Distribution ID
28+
* @return {Promise} results of the workflow
29+
**/
30+
public async detachEdgeFunction (distributionId: string): Promise<CloudFront.UpdateDistributionResult> {
31+
const data = await this.cloudfront.getDistribution({ Id: distributionId }).promise()
32+
if (!data.Distribution) throw new Error('No such distribution')
33+
const config = await this.updator.createUpdateDistributionConfig(
34+
data.Distribution.DistributionConfig,
35+
'detachEdge'
36+
)
37+
const params = this.updator.createUpdateDistributionParam(data, config)
38+
return this.cloudfront.updateDistribution(params).promise()
39+
}
40+
41+
/**
42+
* Attach lambda edge function to specific CloudFront Distribution
43+
*
44+
* @param {string} distributionId - CloudFront Distribution ID
45+
* @return {Promise} results of the workflow
46+
**/
47+
public async attachEdgeFunction (distributionId: string): Promise<CloudFront.UpdateDistributionResult> {
48+
const data = await this.cloudfront.getDistribution({ Id: distributionId }).promise()
49+
if (!data || !data.Distribution) throw new Error('No such distribution')
50+
const config = await this.updator.createUpdateDistributionConfig(
51+
data.Distribution.DistributionConfig,
52+
'attachEdge'
53+
)
54+
const params = this.updator.createUpdateDistributionParam(data, config)
55+
return this.cloudfront.updateDistribution(params).promise()
56+
}
57+
}
58+
59+
export default LambdaEdgeController

libs/index.ts

Lines changed: 3 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,3 @@
1-
import 'tslib'
2-
import { CloudFront } from 'aws-sdk'
3-
import ConfigUpdator from './configUpdator'
4-
5-
interface ControllerClient {
6-
cloudfront?: CloudFront;
7-
configUpdator?: ConfigUpdator;
8-
}
9-
10-
export class LambdaEdgeController {
11-
// CloudFront client from AWS SDK
12-
private cloudfront: CloudFront
13-
14-
// CloudFront config updator
15-
private updator: ConfigUpdator
16-
17-
/**
18-
* constructor
19-
* @param {string} lambdaArn - Lambda arn
20-
* @param {string} [stage='development'] - stage
21-
* @param {ControllerClient} clients - Clients object
22-
**/
23-
constructor (lambdaArn: string, eventType: CloudFront.Types.EventType = 'viewer-request', client?: ControllerClient) {
24-
this.cloudfront = client && client.cloudfront ? client.cloudfront : new CloudFront()
25-
this.updator = client && client.configUpdator ? client.configUpdator : new ConfigUpdator(lambdaArn, eventType)
26-
}
27-
28-
/**
29-
* Reove lambda edge function from specific CloudFront Distribution
30-
*
31-
* @param {string} distributionId - CloudFront Distribution ID
32-
* @return {Promise} results of the workflow
33-
**/
34-
public async detachEdgeFunction (distributionId: string): Promise<CloudFront.UpdateDistributionResult> {
35-
const data = await this.cloudfront.getDistribution({ Id: distributionId }).promise()
36-
if (!data.Distribution) throw new Error('No such distribution')
37-
const config = await this.updator.createUpdateDistributionConfig(
38-
data.Distribution.DistributionConfig,
39-
'detachEdge'
40-
)
41-
const params = this.updator.createUpdateDistributionParam(data, config)
42-
return this.cloudfront.updateDistribution(params).promise()
43-
}
44-
45-
/**
46-
* Attach lambda edge function to specific CloudFront Distribution
47-
*
48-
* @param {string} distributionId - CloudFront Distribution ID
49-
* @return {Promise} results of the workflow
50-
**/
51-
public async attachEdgeFunction (distributionId: string): Promise<CloudFront.UpdateDistributionResult> {
52-
const data = await this.cloudfront.getDistribution({ Id: distributionId }).promise()
53-
if (!data || !data.Distribution) throw new Error('No such distribution')
54-
const config = await this.updator.createUpdateDistributionConfig(
55-
data.Distribution.DistributionConfig,
56-
'attachEdge'
57-
)
58-
const params = this.updator.createUpdateDistributionParam(data, config)
59-
return this.cloudfront.updateDistribution(params).promise()
60-
}
61-
}
62-
63-
export default LambdaEdgeController
1+
export * from './configUpdator'
2+
export * from './controller'
3+
export * from './models'

libs/models.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import 'tslib'
2+
import { CloudFront } from 'aws-sdk'
3+
import ConfigUpdator from './configUpdator'
4+
5+
// Service Client configs
6+
export interface ControllerClient {
7+
cloudfront?: CloudFront;
8+
configUpdator?: ConfigUpdator;
9+
}
10+
11+
// Allowed CloudFront request type
12+
export type EventType = 'origin-request' | 'origin-response' | 'viewer-request' | 'viewer-response'

0 commit comments

Comments
 (0)