@@ -15,6 +15,7 @@ import {
15
15
aws_s3_deployment as s3deploy ,
16
16
aws_route53_targets as targets ,
17
17
aws_wafv2 as wafv2 ,
18
+ Fn ,
18
19
} from 'aws-cdk-lib'
19
20
import { hasFiles } from '@stacksjs/storage'
20
21
import { path as p } from '@stacksjs/path'
@@ -24,7 +25,7 @@ export class StacksCloud extends Stack {
24
25
domain : string
25
26
apiPath : string
26
27
docsDomain : string
27
- apiVanityUrl ? : string
28
+ apiVanityUrl : string
28
29
vanityUrl : string
29
30
docsSource : string
30
31
websiteSource : string
@@ -41,6 +42,7 @@ export class StacksCloud extends Stack {
41
42
firewall : wafv2 . CfnWebACL
42
43
originAccessIdentity : cloudfront . OriginAccessIdentity
43
44
cdnCachePolicy : cloudfront . CachePolicy
45
+ apiCachePolicy : cloudfront . CachePolicy | undefined
44
46
45
47
constructor ( scope : Construct , id : string , props ?: StackProps ) {
46
48
super ( scope , id , props )
@@ -54,6 +56,7 @@ export class StacksCloud extends Stack {
54
56
this . docsSource = '../../../storage/framework/docs'
55
57
this . websiteSource = app . docMode ? this . docsSource : '../../../storage/public'
56
58
this . privateSource = '../../../storage/private'
59
+ this . apiVanityUrl = ''
57
60
58
61
this . zone = this . manageZone ( )
59
62
this . certificate = this . manageCertificate ( )
@@ -80,8 +83,11 @@ export class StacksCloud extends Stack {
80
83
return cloud . deploy ?. api
81
84
}
82
85
83
- apiCachePolicy ( ) {
84
- return new cloudfront . CachePolicy ( this , 'StacksApiCachePolicy' , {
86
+ setApiCachePolicy ( ) {
87
+ if ( this . apiCachePolicy )
88
+ return this . apiCachePolicy
89
+
90
+ this . apiCachePolicy = new cloudfront . CachePolicy ( this , 'StacksApiCachePolicy' , {
85
91
comment : 'Stacks API Cache Policy' ,
86
92
cachePolicyName : 'StacksApiCachePolicy' ,
87
93
// minTtl: cloud.cdn?.minTtl ? Duration.seconds(cloud.cdn.minTtl) : undefined,
@@ -90,9 +96,11 @@ export class StacksCloud extends Stack {
90
96
headerBehavior : cloudfront . CacheHeaderBehavior . allowList ( 'Accept' , 'x-api-key' , 'Authorization' ) ,
91
97
queryStringBehavior : cloudfront . CacheQueryStringBehavior . none ( ) ,
92
98
} )
99
+
100
+ return this . apiCachePolicy
93
101
}
94
102
95
- deployApi ( ) : lambda . FunctionUrl {
103
+ deployApi ( ) {
96
104
const layer = new lambda . LayerVersion ( this , 'StacksLambdaLayer' , {
97
105
code : lambda . Code . fromAsset ( p . projectStoragePath ( 'framework/cloud/bun-lambda-layer.zip' ) ) ,
98
106
compatibleRuntimes : [ lambda . Runtime . PROVIDED_AL2 ] ,
@@ -124,8 +132,6 @@ export class StacksCloud extends Stack {
124
132
} )
125
133
126
134
this . apiVanityUrl = api . url
127
-
128
- return api
129
135
}
130
136
131
137
allowedMethodsFromString ( methods ?: 'ALL' | 'GET_HEAD' | 'GET_HEAD_OPTIONS' ) : cloudfront . AllowedMethods {
@@ -321,15 +327,25 @@ export class StacksCloud extends Stack {
321
327
this . deployApi ( )
322
328
323
329
behaviorOptions = {
330
+ '/api' : {
331
+ origin : new origins . HttpOrigin ( Fn . select ( 2 , Fn . split ( '/' , this . apiVanityUrl ) ) , { // removes the https://
332
+ originPath : '/api' ,
333
+ protocolPolicy : cloudfront . OriginProtocolPolicy . HTTPS_ONLY ,
334
+ } ) ,
335
+ compress : true ,
336
+ allowedMethods : cloudfront . AllowedMethods . ALLOW_ALL ,
337
+ cachedMethods : cloudfront . CachedMethods . CACHE_GET_HEAD_OPTIONS ,
338
+ cachePolicy : this . setApiCachePolicy ( ) ,
339
+ } ,
324
340
'/api/*' : {
325
- origin : new origins . HttpOrigin ( this . domain , {
341
+ origin : new origins . HttpOrigin ( Fn . select ( 2 , Fn . split ( '/' , this . apiVanityUrl ) ) , { // removes the https://
326
342
originPath : '/api' ,
327
343
protocolPolicy : cloudfront . OriginProtocolPolicy . HTTPS_ONLY ,
328
344
} ) ,
329
345
compress : true ,
330
346
allowedMethods : cloudfront . AllowedMethods . ALLOW_ALL ,
331
347
cachedMethods : cloudfront . CachedMethods . CACHE_GET_HEAD_OPTIONS ,
332
- cachePolicy : this . apiCachePolicy ( ) ,
348
+ cachePolicy : this . apiCachePolicy ,
333
349
} ,
334
350
}
335
351
}
0 commit comments