Code Monkey home page Code Monkey logo

softchef / cdk-restapi Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 0.0 3.18 MB

This construct is base on @aws/aws-apigateway.RestApi. The RestApi has addResource & addMethod function to create REST API routes. But the source code are low readability about what kind of resources and method in the API. So this construct are redefine the resources to improve readability.

JavaScript 3.88% TypeScript 96.12%
restapi api-gateway cdk elb fargate lambda

cdk-restapi's Introduction

AWS CDK with RestApi

npm version Release npm

This construct is base on @aws/aws-apigateway.RestApi.

The RestApi has addResource & addMethod function to create REST API routes. But the source code are low readability about what kind of resources and method in the API. So this construct are redefine the resources to improve readability.

Installation

  npm install @softchef/cdk-restapi
  // or
  yarn add @softchef/cdk-restapi

Example

const demoRestApi = new RestApi(stack, 'RestApiDemo', {
  resources: [
    {
      path: '/articles',
      httpMethod: HttpMethod.GET,
      lambdaFunction: new lambda.NodejsFunction(stack, 'GetArticles', {
        entry: `./lambda-assets/articles/get-articles/app.ts`,
      }),
    },
    {
      path: '/articles',
      httpMethod: HttpMethod.POST,
      lambdaFunction: new lambda.NodejsFunction(stack, 'CreateArticle', {
        entry: `./lambda-assets/articles/create-article/app.ts`,
      }),
    },
    {
      path: '/articles/{articleId}',
      httpMethod: HttpMethod.GET,
      lambdaFunction: new lambda.NodejsFunction(stack, 'GetArticle', {
        entry: `./lambda-assets/articles/get-article/app.ts`,
      }),
    },
    {
      path: '/articles/{articleId}',
      httpMethod: HttpMethod.PUT,
      lambdaFunction: new lambda.NodejsFunction(stack, 'UpdateArticle', {
        entry: `./lambda-assets/articles/update-article/app.ts`,
      }),
    },
    {
      path: '/articles/{articleId}/comments',
      httpMethod: HttpMethod.GET,
      lambdaFunction: new lambda.NodejsFunction(stack, 'GetComments', {
        entry: `./lambda-assets/articles/get-comments/app.ts`,
      }),
    },
    {
      path: '/articles/{articleId}/comments',
      httpMethod: HttpMethod.POST,
      lambdaFunction: new lambda.NodejsFunction(stack, 'CreateComment', {
        entry: `./lambda-assets/articles/create-comment/app.ts`,
      }),
    },
  ],
  enableCors: true,
});

// Add multi-resources
demoRestApi.addResources([
  {
    path: '/authors',
    httpMethod: HttpMethod.GET,
    lambdaFunction: new lambda.NodejsFunction(stack, 'GetAuthors', {
      entry: `./lambda-assets/authors/get-authors/app.ts`,
    }),
  },
  {
    path: '/authors/{authorId}',
    httpMethod: HttpMethod.GET,
    lambdaFunction: new lambda.NodejsFunction(stack, 'GetAuthor', {
      entry: `./lambda-assets/authors/get-author/app.ts`,
    }),
  }
]);

// Add single resource
demoRestApi.addResource({
  path: '/authors',
  httpMethod: HttpMethod.POST,
  lambdaFunction: new lambda.NodejsFunction(stack, 'CreateAuthors', {
    entry: `./lambda-assets/authors/create-authors/app.ts`,
  }),
})

Demo

  1. Clone this repository
  2. Run commands
npx projen
cdk deploy --app lib/demo/demo.js
  1. Checkout the CloudFormation stacks and API Gateway

cdk-restapi's People

Contributors

dependabot[bot] avatar kerwin-jhong avatar minche-tsai avatar minchetsai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.