UNPKG

the-ldk

Version:

Welcome to the LDK! A collection of custom AWS CDK constructs to help you build serverless applications faster.

96 lines 4.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.APIGateway = void 0; const constructs_1 = require("constructs"); const aws_apigateway_1 = require("aws-cdk-lib/aws-apigateway"); class APIGateway extends constructs_1.Construct { constructor(scope, id, props) { super(scope, id); const { serviceName, rateLimit = 100000, burstLimit = 1000, restApiProps, apiKeyRequired = true, version, customDomainName, domainNameAliasTarget, domainNameAliasHostedZoneId, corsOptions, customDomainCertificate, enableTracing = false, loggingLevel = 'INFO', dataTraceEnabled = false, authorizer, env, } = props; const defaultDeployOptions = { stageName: env, tracingEnabled: enableTracing, loggingLevel: loggingLevel.toUpperCase(), dataTraceEnabled: dataTraceEnabled, }; const defaultMethodOptions = { apiKeyRequired, authorizer: authorizer, authorizationType: authorizer ? aws_apigateway_1.AuthorizationType.CUSTOM : undefined, }; this.restAPI = new aws_apigateway_1.RestApi(this, `${serviceName}-restAPI-${env}`, { ...restApiProps, deployOptions: { ...defaultDeployOptions, ...(restApiProps && restApiProps.deployOptions), }, defaultMethodOptions: { ...defaultMethodOptions, ...(restApiProps && restApiProps.defaultMethodOptions), }, }); this.apiKey = new aws_apigateway_1.ApiKey(this, `${serviceName}-apiKey-${env}`, { apiKeyName: `${serviceName}-apiKey-${env}`, generateDistinctId: true, stages: [this.restAPI.deploymentStage], }); this.usagePlan = new aws_apigateway_1.UsagePlan(this, `${serviceName}-usagePlan-${env}`, { name: `${serviceName}-${env}`, throttle: { rateLimit, burstLimit, }, apiStages: [ { stage: this.restAPI.deploymentStage, }, ], }); this.usagePlan.addApiKey(this.apiKey); if (customDomainName) { if (!domainNameAliasTarget || !domainNameAliasHostedZoneId) { throw new Error('domainNameAliasTarget and domainNameAliasHostedZoneId must be provided when customDomainName is provided'); } const domainProps = { domainName: customDomainName, domainNameAliasTarget, domainNameAliasHostedZoneId, }; if (customDomainCertificate) { domainProps.certificate = customDomainCertificate; } const customDomain = aws_apigateway_1.DomainName.fromDomainNameAttributes(this, `${serviceName}-customDomain-${env}`, domainProps); new aws_apigateway_1.BasePathMapping(this, `${serviceName}-basePathMapping-${env}`, { domainName: customDomain, restApi: this.restAPI, stage: this.restAPI.deploymentStage, }); } if (corsOptions) { this.restAPI.root.addCorsPreflight({ allowOrigins: corsOptions.allowOrigins, allowMethods: corsOptions.allowMethods ?? [ 'GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', ], allowHeaders: corsOptions.allowHeaders ?? [ 'Content-Type', 'X-Amz-Date', 'Authorization', 'X-Api-Key', ], }); } if (version) { this.versionPath = new aws_apigateway_1.Resource(this.restAPI, `${version}Resource`, { parent: this.restAPI.root, pathPart: version, }); } } } exports.APIGateway = APIGateway; //# sourceMappingURL=APIGateway.js.map