Skip to content

Commit 2a7573d

Browse files
authored
feat: allowing cloudfront props to be passed in and merging with the defaults (#36)
1 parent 94be3c7 commit 2a7573d

File tree

4 files changed

+96
-6
lines changed

4 files changed

+96
-6
lines changed

packages/construct/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@
3838
"@aws-cdk/aws-route53-targets": "1.95.1",
3939
"@aws-cdk/aws-s3": "1.95.1",
4040
"@aws-cdk/aws-s3-deployment": "1.95.1",
41-
"@aws-cdk/core": "1.95.1"
41+
"@aws-cdk/core": "1.95.1",
42+
"deepmerge": "^4.2.2"
4243
},
4344
"files": [
4445
"tsconfig.json",

packages/construct/src/serverless-ui.construct.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ViewerProtocolPolicy,
1313
AllowedMethods,
1414
CachePolicy,
15+
DistributionProps,
1516
} from "@aws-cdk/aws-cloudfront";
1617
import { IFunction, Runtime } from "@aws-cdk/aws-lambda";
1718
import { NodejsFunction } from "@aws-cdk/aws-lambda-nodejs";
@@ -22,6 +23,7 @@ import * as path from "path";
2223
import { HttpOrigin, S3Origin } from "@aws-cdk/aws-cloudfront-origins";
2324
import { LambdaProxyIntegration } from "@aws-cdk/aws-apigatewayv2-integrations";
2425
import { HttpApi, IHttpApi } from "@aws-cdk/aws-apigatewayv2";
26+
import { overrideProps } from "./utils";
2527

2628
interface Domain {
2729
/**
@@ -68,6 +70,10 @@ interface ServerlessUIProps {
6870
readonly apiEnvironment?: {
6971
[key: string]: string;
7072
};
73+
/**
74+
* Optional user provided props to merge with the default props for CloudFront Distribution
75+
*/
76+
cloudFrontDistributionProps?: Partial<DistributionProps>;
7177
}
7278

7379
export class ServerlessUI extends Construct {
@@ -147,10 +153,7 @@ export class ServerlessUI extends Construct {
147153
},
148154
};
149155

150-
/**
151-
* Creating a Cloudfront distribution for the website bucket with an aggressive caching policy
152-
*/
153-
const distribution = new Distribution(this, "Distribution", {
156+
const defaultDistributionProps = {
154157
defaultBehavior: {
155158
origin: new S3Origin(websiteBucket),
156159
allowedMethods: AllowedMethods.ALLOW_ALL,
@@ -169,7 +172,21 @@ export class ServerlessUI extends Construct {
169172
]
170173
: undefined,
171174
enableLogging: true,
172-
});
175+
};
176+
177+
const mergedDistributionProps = overrideProps(
178+
defaultDistributionProps,
179+
props.cloudFrontDistributionProps ?? {}
180+
);
181+
182+
/**
183+
* Creating a Cloudfront distribution for the website bucket with an aggressive caching policy
184+
*/
185+
const distribution = new Distribution(
186+
this,
187+
"Distribution",
188+
mergedDistributionProps
189+
);
173190

174191
new BucketDeployment(this, "BucketDeployment", {
175192
sources: props.uiSources,

packages/construct/src/utils.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import deepmerge from "deepmerge";
2+
3+
function isObject(val: object) {
4+
return (
5+
val != null &&
6+
typeof val === "object" &&
7+
Object.prototype.toString.call(val) === "[object Object]"
8+
);
9+
}
10+
11+
function isPlainObject(o: object) {
12+
if (Array.isArray(o) === true) {
13+
return true;
14+
}
15+
16+
if (isObject(o) === false) {
17+
return false;
18+
}
19+
20+
// If has modified constructor
21+
const ctor = o.constructor;
22+
if (typeof ctor !== "function") {
23+
return false;
24+
}
25+
26+
// If has modified prototype
27+
const prot = ctor.prototype;
28+
if (isObject(prot) === false) {
29+
return false;
30+
}
31+
32+
// If constructor does not have an Object-specific method
33+
if (prot.hasOwnProperty("isPrototypeOf") === false) {
34+
return false;
35+
}
36+
37+
// Most likely a plain Object
38+
return true;
39+
}
40+
41+
function combineMerge(target: any[], source: any[]) {
42+
return target.concat(source);
43+
}
44+
45+
function overwriteMerge(target: any[], source: any[]) {
46+
target = source;
47+
return target;
48+
}
49+
50+
export function overrideProps(
51+
DefaultProps: object,
52+
userProps: object,
53+
concatArray: boolean = false
54+
): any {
55+
// Override the sensible defaults with user provided props
56+
if (concatArray) {
57+
return deepmerge(DefaultProps, userProps, {
58+
arrayMerge: combineMerge,
59+
isMergeableObject: isPlainObject,
60+
});
61+
} else {
62+
return deepmerge(DefaultProps, userProps, {
63+
arrayMerge: overwriteMerge,
64+
isMergeableObject: isPlainObject,
65+
});
66+
}
67+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,6 +3233,11 @@ deep-is@~0.1.3:
32333233
resolved "https://registry.yarnpkg.com/deep-is/-/deep-is-0.1.3.tgz"
32343234
integrity sha1-s2nW+128E+7PUk+RsHD+7cNXzzQ=
32353235

3236+
deepmerge@^4.2.2:
3237+
version "4.2.2"
3238+
resolved "https://registry.yarnpkg.com/deepmerge/-/deepmerge-4.2.2.tgz#44d2ea3679b8f4d4ffba33f03d865fc1e7bf4955"
3239+
integrity sha512-FJ3UgI4gIl+PHZm53knsuSFpE+nESMr7M4v9QcgB7S63Kj/6WqMiFQJpBBYz1Pt+66bZpP3Q7Lye0Oo9MPKEdg==
3240+
32363241
defaults@^1.0.3:
32373242
version "1.0.3"
32383243
resolved "https://registry.yarnpkg.com/defaults/-/defaults-1.0.3.tgz"

0 commit comments

Comments
 (0)