Skip to content

Commit bab344f

Browse files
committed
Replaced custom CorsConfig interface with in-built Partial<ResponseHeadersCorsBehavior> interface
1 parent d4e2763 commit bab344f

File tree

3 files changed

+48
-66
lines changed

3 files changed

+48
-66
lines changed

packages/static-hosting/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
StaticHostingProps,
44
remapPath,
55
ResponseHeaderMappings,
6-
CorsConfig,
76
} from "./lib/static-hosting";
87
import { CSP } from "./types/csp";
98

@@ -13,5 +12,4 @@ export {
1312
CSP,
1413
remapPath,
1514
ResponseHeaderMappings,
16-
CorsConfig,
1715
};

packages/static-hosting/lib/static-hosting.test.ts

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,10 @@ describe("StaticHosting", () => {
536536
const hosting = new StaticHosting(stack, "TestConstruct", {
537537
...defaultProps,
538538
corsConfig: {
539-
allowOrigins: ["https://example.com", "https://app.example.com"],
539+
accessControlAllowOrigins: [
540+
"https://example.com",
541+
"https://app.example.com",
542+
],
540543
},
541544
});
542545

@@ -567,10 +570,10 @@ describe("StaticHosting", () => {
567570
new StaticHosting(stack, "TestConstruct", {
568571
...defaultProps,
569572
corsConfig: {
570-
allowOrigins: ["https://example.com"],
571-
allowCredentials: true,
572-
allowHeaders: ["Content-Type", "Authorization"],
573-
allowMethods: ["GET", "HEAD", "OPTIONS", "POST"],
573+
accessControlAllowOrigins: ["https://example.com"],
574+
accessControlAllowCredentials: true,
575+
accessControlAllowHeaders: ["Content-Type", "Authorization"],
576+
accessControlAllowMethods: ["GET", "HEAD", "OPTIONS", "POST"],
574577
originOverride: false,
575578
},
576579
});
@@ -599,7 +602,7 @@ describe("StaticHosting", () => {
599602
const { stack } = createTestStack();
600603
new StaticHosting(stack, "TestConstruct", {
601604
...defaultProps,
602-
corsConfig: { allowOrigins: ["https://example.com"] },
605+
corsConfig: { accessControlAllowOrigins: ["https://example.com"] },
603606
});
604607

605608
const template = Template.fromStack(stack);
@@ -623,11 +626,11 @@ describe("StaticHosting", () => {
623626
expect(cssBehavior.ResponseHeadersPolicyId).toBeDefined();
624627
});
625628

626-
it("should not apply CORS policy to static files when allowOrigins is empty array", () => {
629+
it("should not apply CORS policy to static files when accessControlAllowOrigins is empty array", () => {
627630
const { stack } = createTestStack();
628631
const hosting = new StaticHosting(stack, "TestConstruct", {
629632
...defaultProps,
630-
corsConfig: { allowOrigins: [] },
633+
corsConfig: { accessControlAllowOrigins: [] },
631634
});
632635

633636
expect(hosting.corsResponseHeadersPolicy).toBeUndefined();
@@ -651,7 +654,7 @@ describe("StaticHosting", () => {
651654
const { stack } = createTestStack();
652655
const hosting = new StaticHosting(stack, "TestConstruct", {
653656
...defaultProps,
654-
corsConfig: { allowOrigins: ["https://example.com"] },
657+
corsConfig: { accessControlAllowOrigins: ["https://example.com"] },
655658
});
656659

657660
// The policy should be accessible for downstream projects to use
@@ -664,7 +667,7 @@ describe("StaticHosting", () => {
664667
const { stack } = createTestStack();
665668
new StaticHosting(stack, "TestConstruct", {
666669
...defaultProps,
667-
corsConfig: { allowOrigins: ["https://example.com"] },
670+
corsConfig: { accessControlAllowOrigins: ["https://example.com"] },
668671
remapPaths: [{ from: "/test-path", to: "/remapped-path" }],
669672
});
670673

@@ -686,7 +689,7 @@ describe("StaticHosting", () => {
686689
const { stack } = createTestStack();
687690
new StaticHosting(stack, "TestConstruct", {
688691
...defaultProps,
689-
corsConfig: { allowOrigins: ["https://example.com"] },
692+
corsConfig: { accessControlAllowOrigins: ["https://example.com"] },
690693
backendHost: "backend.example.com",
691694
remapBackendPaths: [{ from: "/api/*", to: "/api/*" }],
692695
});
@@ -709,7 +712,7 @@ describe("StaticHosting", () => {
709712
const { stack } = createTestStack();
710713
new StaticHosting(stack, "TestConstruct", {
711714
...defaultProps,
712-
corsConfig: { allowOrigins: ["https://example.com"] },
715+
corsConfig: { accessControlAllowOrigins: ["https://example.com"] },
713716
indexable: true,
714717
});
715718

@@ -787,7 +790,7 @@ describe("StaticHosting", () => {
787790
new StaticHosting(stack, "TestConstruct", {
788791
...defaultProps,
789792
indexable: false,
790-
corsConfig: { allowOrigins: ["https://example.com"] },
793+
corsConfig: { accessControlAllowOrigins: ["https://example.com"] },
791794
});
792795

793796
const template = Template.fromStack(stack);

packages/static-hosting/lib/static-hosting.ts

Lines changed: 32 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -46,41 +46,6 @@ import { CSP } from "../types/csp";
4646
import { PathRemapFunction } from "./path-remap";
4747
import { RequestFunction, ResponseFunction } from "./csp";
4848

49-
/**
50-
* Configuration for CORS (Cross-Origin Resource Sharing) headers.
51-
* Only `allowOrigins` is required; other fields have sensible defaults.
52-
*/
53-
export interface CorsConfig {
54-
/**
55-
* The origins to allow in the Access-Control-Allow-Origin header.
56-
* @example ['https://example.com', 'https://app.example.com']
57-
*/
58-
allowOrigins: string[];
59-
60-
/**
61-
* Whether to include credentials in CORS requests.
62-
* @default false
63-
*/
64-
allowCredentials?: boolean;
65-
66-
/**
67-
* The headers to allow in CORS requests.
68-
* @default ['*']
69-
*/
70-
allowHeaders?: string[];
71-
72-
/**
73-
* The HTTP methods to allow in CORS requests.
74-
* @default ['GET', 'HEAD', 'OPTIONS']
75-
*/
76-
allowMethods?: string[];
77-
78-
/**
79-
* Whether CloudFront should override the response from the origin.
80-
* @default true
81-
*/
82-
originOverride?: boolean;
83-
}
8449

8550
export interface StaticHostingProps {
8651
/**
@@ -103,7 +68,8 @@ export interface StaticHostingProps {
10368
* automatically applied to all static file behaviors (*.js, *.css, etc.),
10469
* remapPaths, remapBackendPaths, and the default behavior.
10570
*
106-
* Only `allowOrigins` is required. Other settings have sensible defaults:
71+
* Uses the CDK ResponseHeadersCorsBehavior type. Only `accessControlAllowOrigins`
72+
* is required. Other settings have sensible defaults:
10773
* - accessControlAllowCredentials: false
10874
* - accessControlAllowHeaders: ['*']
10975
* - accessControlAllowMethods: ['GET', 'HEAD', 'OPTIONS']
@@ -112,22 +78,24 @@ export interface StaticHostingProps {
11278
* @example
11379
* // Simple usage - just origins
11480
* corsConfig: {
115-
* allowOrigins: ['https://example.com', 'https://app.example.com']
81+
* accessControlAllowOrigins: ['https://example.com', 'https://app.example.com']
11682
* }
11783
*
11884
* @example
11985
* // Full customisation
12086
* corsConfig: {
121-
* allowOrigins: ['https://example.com'],
122-
* allowCredentials: true,
123-
* allowHeaders: ['Content-Type', 'Authorization'],
124-
* allowMethods: ['GET', 'HEAD', 'OPTIONS', 'POST'],
87+
* accessControlAllowOrigins: ['https://example.com'],
88+
* accessControlAllowCredentials: true,
89+
* accessControlAllowHeaders: ['Content-Type', 'Authorization'],
90+
* accessControlAllowMethods: ['GET', 'HEAD', 'OPTIONS', 'POST'],
91+
* accessControlExposeHeaders: ['X-Custom-Header'],
92+
* accessControlMaxAge: Duration.seconds(600),
12593
* originOverride: false
12694
* }
12795
*
12896
* @default undefined - no CORS policy will be applied
12997
*/
130-
corsConfig?: CorsConfig;
98+
corsConfig?: Partial<ResponseHeadersCorsBehavior>;
13199

132100
/**
133101
* Whether the site should be indexable by search engines.
@@ -659,18 +627,31 @@ export class StaticHosting extends Construct {
659627

660628
// Create CORS behavior config if corsConfig is specified
661629
const corsBehavior: ResponseHeadersCorsBehavior | undefined =
662-
props.corsConfig && props.corsConfig.allowOrigins.length > 0
630+
props.corsConfig &&
631+
props.corsConfig.accessControlAllowOrigins &&
632+
props.corsConfig.accessControlAllowOrigins.length > 0
663633
? {
664634
accessControlAllowCredentials:
665-
props.corsConfig.allowCredentials ?? false,
666-
accessControlAllowHeaders: props.corsConfig.allowHeaders ?? ["*"],
667-
accessControlAllowMethods: props.corsConfig.allowMethods ?? [
668-
"GET",
669-
"HEAD",
670-
"OPTIONS",
671-
],
672-
accessControlAllowOrigins: props.corsConfig.allowOrigins,
635+
props.corsConfig.accessControlAllowCredentials ?? false,
636+
accessControlAllowHeaders:
637+
props.corsConfig.accessControlAllowHeaders ?? ["*"],
638+
accessControlAllowMethods:
639+
props.corsConfig.accessControlAllowMethods ?? [
640+
"GET",
641+
"HEAD",
642+
"OPTIONS",
643+
],
644+
accessControlAllowOrigins:
645+
props.corsConfig.accessControlAllowOrigins,
673646
originOverride: props.corsConfig.originOverride ?? true,
647+
// Pass through optional fields if provided
648+
...(props.corsConfig.accessControlExposeHeaders && {
649+
accessControlExposeHeaders:
650+
props.corsConfig.accessControlExposeHeaders,
651+
}),
652+
...(props.corsConfig.accessControlMaxAge && {
653+
accessControlMaxAge: props.corsConfig.accessControlMaxAge,
654+
}),
674655
}
675656
: undefined;
676657

0 commit comments

Comments
 (0)