项目作者: jetbridge

项目描述 :
Cloud-native TypeScript API development kit for AWS CDK.
高级语言: TypeScript
项目地址: git://github.com/jetbridge/jetkit-cdk.git
创建时间: 2021-04-24T10:47:10Z
项目社区:https://github.com/jetbridge/jetkit-cdk

开源协议:MIT License

下载


JetKit/CDK

Tests
npm version
Open in Visual Studio Code

An anti-framework for building cloud-native serverless applications.

This module provides convenient tools for writing Lambda functions, RESTful API views,
and generating cloud infrastructure with AWS CDK.

Motivation

Frameworkless web applications.

We want to build maintainable and scalable cloud-first applications, with cloud resources generated from application code.

Using AWS CDK we can automate generating API Gateway routes and Lambda functions from class and function metadata.

Each class or function view is a self-contained Lambda function that only pulls in the dependencies needed for its
functioning, keeping startup times low and applications modular.

Documentation

Guides and API reference can be found at https://jetkit.dev/docs/.

Super Quickstart

Use this monorepo project template: typescript-cdk-template.

Installation

  1. npm install @jetkit/cdk

Synopsis

API View

Combine related functionality and routes into a single Lambda function bundle.

  1. import { HttpMethod } from "@aws-cdk/aws-apigatewayv2"
  2. import { badRequest, methodNotAllowed } from "@jdpnielsen/http-error"
  3. import { ApiView, SubRoute, ApiEvent, ApiResponse, ApiViewBase, apiViewHandler } from "@jetkit/cdk-runtime"
  4. @ApiView({
  5. path: "/album",
  6. })
  7. export class AlbumApi extends ApiViewBase {
  8. // define POST /album handler
  9. @SubRoute({ methods: [HttpMethod.POST] })
  10. async post() {
  11. return "Created new album"
  12. }
  13. // custom endpoint in the view
  14. // routes to the ApiView function
  15. @SubRoute({
  16. path: "/{albumId}/like", // will be /album/123/like
  17. methods: [HttpMethod.POST, HttpMethod.DELETE],
  18. })
  19. async like(event: ApiEvent): ApiResponse {
  20. const albumId = event.pathParameters?.albumId
  21. if (!albumId) throw badRequest("albumId is required in path")
  22. const method = event.requestContext.http.method
  23. // POST - mark album as liked
  24. if (method == HttpMethod.POST) return `Liked album ${albumId}`
  25. // DELETE - unmark album as liked
  26. else if (method == HttpMethod.DELETE) return `Unliked album ${albumId}`
  27. // should never be reached
  28. else return methodNotAllowed()
  29. }
  30. }
  31. // Not required but lets you omit specifying the `entry` path to this file for convenience.
  32. export const handler = apiViewHandler(__filename, AlbumApi)
  33. // If using ES modules
  34. // export const handler = apiViewHandlerEs(import.meta, AlbumApi)

Handler Function With Route

  1. import { HttpMethod } from "@aws-cdk/aws-apigatewayv2"
  2. import { Lambda, ApiEvent } from "@jetkit/cdk-runtime"
  3. // a simple standalone function with a route attached
  4. export async function topSongsHandler(event: ApiEvent) {
  5. return JSON.stringify(event.headers)
  6. }
  7. // define route and lambda properties
  8. Lambda({
  9. path: "/top-songs",
  10. methods: [HttpMethod.GET],
  11. })(topSongsHandler)
  12. // If using ES modules:
  13. // LambdaEs(import.meta, { path: "/top-songs" })(topSongsHandler)
  14. // alternate, uglier way of writing the same thing
  15. const topSongsFuncInner = Lambda({
  16. path: "/top-songs-inner",
  17. methods: [HttpMethod.GET],
  18. // this function name should match the exported name
  19. // or you must specify the exported function name in `handler`
  20. })(async function topSongsFuncInner(event: ApiEvent) {
  21. return JSON.stringify(event.headers)
  22. })
  23. export { topSongsFuncInner }

CDK Stack

To start from scratch:

  1. npm install -g aws-cdk
  2. cdk init app --language typescript
  3. npm install @jetkit/cdk @aws-cdk/core @aws-cdk/aws-apigatewayv2

See the guide for more details.

To deploy your stack:

  1. cdk deploy

To generate API Gateway routes and Lambda function handlers from your application code:

  1. import { CorsHttpMethod, HttpApi } from "@aws-cdk/aws-apigatewayv2"
  2. import { Duration, Stack, StackProps, App } from "@aws-cdk/core"
  3. import { ResourceGeneratorConstruct, ApiViewCdk } from "@jetkit/cdk"
  4. import { topSongsHandler, AlbumApi } from "./test/sampleApp"
  5. export class InfraStack extends Stack {
  6. constructor(scope: App, id: string, props?: StackProps) {
  7. super(scope, id, props)
  8. // create API Gateway
  9. const httpApi = new HttpApi(this, "Api", {
  10. corsPreflight: {
  11. allowHeaders: ["Authorization"],
  12. allowMethods: [CorsHttpMethod.ANY],
  13. allowOrigins: ["*"],
  14. maxAge: Duration.days(10),
  15. },
  16. })
  17. // transmute your app code into infrastructure
  18. new ResourceGeneratorConstruct(this, "Generator", {
  19. resources: [
  20. // supply your functions and view classes here
  21. topSongsHandler,
  22. // can add additional lambda attributes here if desired
  23. ApiViewCdk({ memorySize: 1024, bundling: { minify: true }})(AlbumApi)
  24. ],
  25. httpApi,
  26. })
  27. }
  28. }

How It Works

This library provides decorators that can be attached to view classes, methods, and functions. The decorator attaches metadata in the form of options for constructing the Lambda function and optionally API Gateway routes.

It also includes some convenient CDK L3 constructs to generate the Lambda functions and API Gateway routes from your decorated application code.

Local Development

AWS SAM supports running CDK applications locally (in beta).