forked from open-constructs/cdk-serverless
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase-api.ts
117 lines (99 loc) · 3.06 KB
/
base-api.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import * as cdk from '@aws-cdk/core';
import { AssetCdn, AssetCdnProps } from './asset-cdn';
import { Authentication, AuthenticationProps, IAuthentication } from './auth';
import { LambdaOptions, LambdaTracingOptions } from './func';
import { Monitoring } from './monitoring';
import { SingleTableDatastore, SingleTableDatastoreProps } from './table';
export interface BaseApiProps {
/**
* Name of the API
*/
apiName: string;
/**
* Deployment stage (e.g. dev)
*/
stageName: string;
/**
* Configure CloudWatch Dashboard for the API and the Lambda functions
*
* @default true
*/
monitoring?: boolean;
/**
* Create a DynamoDB Table to store data using the single table design
*
* @default none
*/
singleTableDatastore?: SingleTableDatastoreProps;
/**
* Configure a Cognito user pool and use it for authorization
*
* @default none
*/
authentication?: AuthenticationProps;
/**
* Use an existing Cognito user pool and use it for authorization
*
* @default none
*/
existingAuthentication?: IAuthentication;
/**
* Configure a content delivery network for static assets
*
* @default none
*/
assetCdn?: AssetCdnProps;
/**
* Additional environment variables of all Lambda functions
*/
additionalEnv?: {
[key: string]: string;
};
/**
* additional options for the underlying Lambda function construct
*/
lambdaOptions?: LambdaOptions;
/**
* Tracing config for the generated Lambda functions
*/
lambdaTracing?: LambdaTracingOptions;
}
export abstract class BaseApi extends cdk.Construct {
public readonly singleTableDatastore?: SingleTableDatastore;
public readonly authentication?: IAuthentication;
public readonly assetCdn?: AssetCdn;
public readonly monitoring?: Monitoring;
constructor(scope: cdk.Construct, id: string, props: BaseApiProps) {
super(scope, id);
if (props.singleTableDatastore) {
this.singleTableDatastore = new SingleTableDatastore(this, 'SingleTableDS', props.singleTableDatastore);
}
if (props.existingAuthentication && props.authentication) {
throw new Error('Cannot specify new and existing authentication at the same time');
}
if (props.existingAuthentication) {
this.authentication = props.existingAuthentication;
}
if (props.authentication) {
const newAuth = new Authentication(this, 'Authentication', props.authentication);
if (this.singleTableDatastore) {
if (newAuth.customMessageFunction) {
newAuth.customMessageFunction.setTable(this.singleTableDatastore.table, false);
}
if (newAuth.preTokenGenerationFunction) {
newAuth.preTokenGenerationFunction.setTable(this.singleTableDatastore.table, false);
}
}
this.authentication = newAuth;
}
if (props.assetCdn) {
this.assetCdn = new AssetCdn(this, 'AssetCdn', props.assetCdn);
}
if (props.monitoring ?? true) {
this.monitoring = new Monitoring(this, 'Monitoring', {
apiName: props.apiName,
stageName: props.stageName,
});
}
}
}