diff --git a/packages/cdk-api/LICENSE.md b/packages/cdk-api/LICENSE.md new file mode 100644 index 00000000..811b1d9f --- /dev/null +++ b/packages/cdk-api/LICENSE.md @@ -0,0 +1,9 @@ +The MIT License (MIT) + +Copyright (c) 2022 Code d'Azur Interactive B.V. + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/packages/cdk-api/README.md b/packages/cdk-api/README.md new file mode 100644 index 00000000..dc4f52da --- /dev/null +++ b/packages/cdk-api/README.md @@ -0,0 +1 @@ +# @codedazur/cdk-api diff --git a/packages/cdk-api/constructs/RestApi.ts b/packages/cdk-api/constructs/RestApi.ts new file mode 100644 index 00000000..e9b763a9 --- /dev/null +++ b/packages/cdk-api/constructs/RestApi.ts @@ -0,0 +1,117 @@ +import { + RestApi, + RestApiProps, + EndpointType, + IResource, + LambdaIntegration, + SecurityPolicy, +} from "aws-cdk-lib/aws-apigateway"; +import { DnsValidatedCertificate } from "aws-cdk-lib/aws-certificatemanager"; +import { HttpMethod, IFunction } from "aws-cdk-lib/aws-lambda"; +import { + ARecord, + HostedZone, + IHostedZone, + RecordTarget, +} from "aws-cdk-lib/aws-route53"; +import { ApiGateway } from "aws-cdk-lib/aws-route53-targets"; +import { Construct } from "constructs"; + +export interface ApiProps extends RestApiProps { + endpoints: Record; + domain?: DomainProps; +} + +interface DomainProps { + name: string; + subdomain?: string; +} + +interface ResourceProps { + method?: HttpMethod; + handler?: IFunction; + endpoints?: Record; +} + +export class Api extends RestApi { + constructor(scope: Construct, id: string, { endpoints, ...props }: ApiProps) { + super(scope, id, props); + + this.createEndpoints(this.root, endpoints); + this.createDomain(props.domain); + } + + protected createEndpoints( + parent: IResource, + children: Record, + ) { + Object.entries(children).forEach(([slug, child]) => + this.createEndpoint(parent, slug, child), + ); + } + + protected createEndpoint( + parent: IResource, + slug: string, + { method, handler, endpoints }: ResourceProps, + ) { + const resource = parent.addResource(slug, { + defaultIntegration: + handler && !method ? new LambdaIntegration(handler) : undefined, + }); + + if (handler && method) { + resource.addMethod(method, new LambdaIntegration(handler)); + } + + if (endpoints) { + this.createEndpoints(resource, endpoints); + } + } + + protected createDomain(domain?: DomainProps) { + if (!domain) { + return; + } + + const zone = this.findHostedZone(domain); + const certificate = this.createCertificate(domain, zone); + + this.addDomainName("Domain", { + domainName: this.fqdn(domain), + endpointType: EndpointType.EDGE, + securityPolicy: SecurityPolicy.TLS_1_2, + certificate, + }); + + this.createAlias(domain, zone); + } + + protected findHostedZone(domain: DomainProps) { + return HostedZone.fromLookup(this, "HostedZone", { + domainName: domain.name, + }); + } + + protected createCertificate(domain: DomainProps, zone: IHostedZone) { + return new DnsValidatedCertificate(this, "Certificate", { + domainName: this.fqdn(domain), + hostedZone: zone, + region: "us-east-1", + }); + } + + protected createAlias(domain: DomainProps, zone: IHostedZone) { + return domain && zone + ? new ARecord(this, "DomainAlias", { + recordName: this.fqdn(domain), + target: RecordTarget.fromAlias(new ApiGateway(this)), + zone: zone, + }) + : undefined; + } + + protected fqdn(domain: DomainProps) { + return [domain.subdomain, domain.name].filter(Boolean).join("."); + } +} diff --git a/packages/cdk-api/index.ts b/packages/cdk-api/index.ts new file mode 100644 index 00000000..c78af47c --- /dev/null +++ b/packages/cdk-api/index.ts @@ -0,0 +1 @@ +export * from "./constructs/RestApi"; diff --git a/packages/cdk-api/package.json b/packages/cdk-api/package.json new file mode 100644 index 00000000..f0445e21 --- /dev/null +++ b/packages/cdk-api/package.json @@ -0,0 +1,27 @@ +{ + "name": "@codedazur/cdk-rest-api", + "version": "0.0.0", + "main": "./index.ts", + "types": "./index.ts", + "publishConfig": { + "access": "public" + }, + "license": "MIT", + "scripts": { + "lint": "TIMING=1 eslint \"**/*.ts*\"", + "test": "echo \"No tests configured.\"" + }, + "peerDependencies": { + "aws-cdk-lib": ">=2", + "constructs": ">=10" + }, + "devDependencies": { + "@types/node": "^20.4.5", + "aws-cdk-lib": "^2.89.0", + "constructs": "^10.2.69", + "eslint": "^8.46.0", + "@codedazur/eslint-config": "*", + "@codedazur/tsconfig": "*", + "typescript": "^5.1.6" + } +} diff --git a/packages/cdk-api/tsconfig.json b/packages/cdk-api/tsconfig.json new file mode 100644 index 00000000..3bed3f50 --- /dev/null +++ b/packages/cdk-api/tsconfig.json @@ -0,0 +1,5 @@ +{ + "extends": "@codedazur/tsconfig/cdk.json", + "include": ["."], + "exclude": ["dist", "build", "node_modules"] +} diff --git a/packages/cdk-cache-invalidator/README.md b/packages/cdk-cache-invalidator/README.md index d3d7af48..d5c4c573 100644 --- a/packages/cdk-cache-invalidator/README.md +++ b/packages/cdk-cache-invalidator/README.md @@ -1 +1 @@ -# @codedazur/cdk-static-site +# @codedazur/cdk-cache-invalidator