@softchef/cdk-restapi
TypeScript icon, indicating that this package has built-in type declarations

2.0.183 • Public • Published

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

/@softchef/cdk-restapi/

    Package Sidebar

    Install

    npm i @softchef/cdk-restapi

    Weekly Downloads

    388

    Version

    2.0.183

    License

    Apache-2.0

    Unpacked Size

    202 kB

    Total Files

    40

    Last publish

    Collaborators

    • softchef-iot-lab