@@ -8,11 +8,37 @@ import {
8
8
Duration ,
9
9
aws_logs ,
10
10
} from "aws-cdk-lib" ;
11
- import { IDomainName , HttpApi , ParameterMapping , MappingValue } from "@aws-cdk/aws-apigatewayv2-alpha" ;
11
+ import {
12
+ IDomainName ,
13
+ HttpApi ,
14
+ ParameterMapping ,
15
+ MappingValue ,
16
+ } from "@aws-cdk/aws-apigatewayv2-alpha" ;
12
17
import { HttpLambdaIntegration } from "@aws-cdk/aws-apigatewayv2-integrations-alpha" ;
13
18
import { Construct } from "constructs" ;
14
19
import { CustomLambdaFunctionProps } from "../utils" ;
15
- import * as path from 'path' ;
20
+ import * as path from "path" ;
21
+
22
+ export const EXTENSIONS = {
23
+ QUERY : "query" ,
24
+ SORT : "sort" ,
25
+ FIELDS : "fields" ,
26
+ FILTER : "filter" ,
27
+ FREE_TEXT : "free_text" ,
28
+ PAGINATION : "pagination" ,
29
+ COLLECTION_SEARCH : "collection_search" ,
30
+ TRANSACTION : "transaction" ,
31
+ BULK_TRANSACTION : "bulk_transactions" ,
32
+ } as const ;
33
+
34
+ type ExtensionType = ( typeof EXTENSIONS ) [ keyof typeof EXTENSIONS ] ;
35
+
36
+ /**
37
+ * Validates if a given string is a valid STAC extension
38
+ */
39
+ function isValidExtension ( value : string ) : value is ExtensionType {
40
+ return Object . values ( EXTENSIONS ) . includes ( value as any ) ;
41
+ }
16
42
17
43
export class PgStacApiLambda extends Construct {
18
44
readonly url : string ;
@@ -21,16 +47,40 @@ export class PgStacApiLambda extends Construct {
21
47
constructor ( scope : Construct , id : string , props : PgStacApiLambdaProps ) {
22
48
super ( scope , id ) ;
23
49
50
+ const defaultExtensions : ExtensionType [ ] = [
51
+ EXTENSIONS . QUERY ,
52
+ EXTENSIONS . SORT ,
53
+ EXTENSIONS . FIELDS ,
54
+ EXTENSIONS . FILTER ,
55
+ EXTENSIONS . FREE_TEXT ,
56
+ EXTENSIONS . PAGINATION ,
57
+ EXTENSIONS . COLLECTION_SEARCH ,
58
+ ] ;
59
+
60
+ if ( props . enabledExtensions ) {
61
+ for ( const ext of props . enabledExtensions ) {
62
+ if ( ! isValidExtension ( ext ) ) {
63
+ throw new Error (
64
+ `Invalid extension: "${ ext } ". Must be one of: ${ Object . values (
65
+ EXTENSIONS
66
+ ) . join ( ", " ) } `
67
+ ) ;
68
+ }
69
+ }
70
+ }
71
+
72
+ const enabledExtensions = props . enabledExtensions || defaultExtensions ;
73
+
24
74
this . stacApiLambdaFunction = new lambda . Function ( this , "lambda" , {
25
75
// defaults
26
76
runtime : lambda . Runtime . PYTHON_3_11 ,
27
77
handler : "handler.handler" ,
28
78
memorySize : 8192 ,
29
79
logRetention : aws_logs . RetentionDays . ONE_WEEK ,
30
80
timeout : Duration . seconds ( 30 ) ,
31
- code : lambda . Code . fromDockerBuild ( path . join ( __dirname , '..' ) , {
81
+ code : lambda . Code . fromDockerBuild ( path . join ( __dirname , ".." ) , {
32
82
file : "stac-api/runtime/Dockerfile" ,
33
- buildArgs : { PYTHON_VERSION : ' 3.11' } ,
83
+ buildArgs : { PYTHON_VERSION : " 3.11" } ,
34
84
} ) ,
35
85
vpc : props . vpc ,
36
86
vpcSubnets : props . subnetSelection ,
@@ -39,28 +89,40 @@ export class PgStacApiLambda extends Construct {
39
89
PGSTAC_SECRET_ARN : props . dbSecret . secretArn ,
40
90
DB_MIN_CONN_SIZE : "0" ,
41
91
DB_MAX_CONN_SIZE : "1" ,
92
+ ENABLED_EXTENSIONS : enabledExtensions . join ( "," ) ,
42
93
...props . apiEnv ,
43
94
} ,
44
95
// overwrites defaults with user-provided configurable properties
45
- ...props . lambdaFunctionOptions
96
+ ...props . lambdaFunctionOptions ,
46
97
} ) ;
47
98
48
99
props . dbSecret . grantRead ( this . stacApiLambdaFunction ) ;
49
100
50
- if ( props . vpc ) {
51
- this . stacApiLambdaFunction . connections . allowTo ( props . db , ec2 . Port . tcp ( 5432 ) , "allow connections from stac-fastapi-pgstac" ) ;
101
+ if ( props . vpc ) {
102
+ this . stacApiLambdaFunction . connections . allowTo (
103
+ props . db ,
104
+ ec2 . Port . tcp ( 5432 ) ,
105
+ "allow connections from stac-fastapi-pgstac"
106
+ ) ;
52
107
}
53
108
54
109
const stacApi = new HttpApi ( this , `${ Stack . of ( this ) . stackName } -stac-api` , {
55
- defaultDomainMapping : props . stacApiDomainName ? {
56
- domainName : props . stacApiDomainName
57
- } : undefined ,
110
+ defaultDomainMapping : props . stacApiDomainName
111
+ ? {
112
+ domainName : props . stacApiDomainName ,
113
+ }
114
+ : undefined ,
58
115
defaultIntegration : new HttpLambdaIntegration (
59
116
"integration" ,
60
117
this . stacApiLambdaFunction ,
61
- props . stacApiDomainName ? {
62
- parameterMapping : new ParameterMapping ( ) . overwriteHeader ( 'host' , MappingValue . custom ( props . stacApiDomainName . name ) )
63
- } : undefined
118
+ props . stacApiDomainName
119
+ ? {
120
+ parameterMapping : new ParameterMapping ( ) . overwriteHeader (
121
+ "host" ,
122
+ MappingValue . custom ( props . stacApiDomainName . name )
123
+ ) ,
124
+ }
125
+ : undefined
64
126
) ,
65
127
} ) ;
66
128
@@ -102,12 +164,19 @@ export interface PgStacApiLambdaProps {
102
164
/**
103
165
* Custom Domain Name Options for STAC API,
104
166
*/
105
- readonly stacApiDomainName ?: IDomainName ;
167
+ readonly stacApiDomainName ?: IDomainName ;
168
+
169
+ /**
170
+ * List of STAC API extensions to enable.
171
+ *
172
+ * @default - query, sort, fields, filter, free_text, pagniation, collection_search
173
+ */
174
+ readonly enabledExtensions ?: ExtensionType [ ] ;
106
175
107
176
/**
108
- * Can be used to override the default lambda function properties.
109
- *
110
- * @default - defined in the construct.
111
- */
177
+ * Can be used to override the default lambda function properties.
178
+ *
179
+ * @default - defined in the construct.
180
+ */
112
181
readonly lambdaFunctionOptions ?: CustomLambdaFunctionProps ;
113
182
}
0 commit comments