Nuanced Pterodactyl Monk
Share your code. npm Orgs help your team discover, share, and reuse code. Create a free org »

vingle-corgi

1.18.0 • Public • Published

Travis Build Status npm version

Corgi

HTTP Web Application Framework for AWS Lambda - AWS API Gateway Proxy Integration

Features

  1. Cascade Routing
  2. Route parameter
    • such as "users/:userId/followings"
  3. Parameter Validation
  4. Exception Handling
  5. Swagger Document Generation
    • Swagger is API Documentation spec. Corgi support automatic swagger document generation.
    • refer example
  6. View
    • Named "Presenter". basically, you return "model" from Route, and "presenter" defines how you convert this model into HTTP resource such as JSON
      The whole thing supports async/await!, written in typescript from scratch also

TODO

  1. HTTP Body Parser
    • Base64Encoding support

Why do I need an extra Framework for Lambda?

So simple lambda handler looks like this

exports.myHandler = function(event, context, callback) {
   console.log("value1 = " + event.key1);
   console.log("value2 = " + event.key2);
   callback(null, "some success message");
}

let's say you connected API Gateway, (using serverless maybe), as Lambda Proxy. and built some Restful API with that.

exports.myHandler = function(event, context, callback) {
  if (
    event.path === '/api/someapi'
    && event.method == 'GET'
  ) {
    callback(
      null, {
        statusCode: 200,
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          data: {
            response: "XXX"
          }
        })
      }
    )
  } else {
    callback(
      null, {
        statusCode: 404,
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          error: 'Not Found',
        })
      }
    )
  }
}

Ok, fairly good, since it's on lambda and APIGateway so everything is managed and scaled....etc. but also you can clearly see that this is at the tipping point of going unmanageable.

there are several frameworks that built for this, (such as running express itself on lambda, even though which is what exactly AWS APIGateway is for) lambda-req aws-serverless-express serverless-express

At Vingle, we did seriously consider about using these kinds of express wrapping. But clearly those are really inefficient and not really reliable for production usage and, most of all, we thought we can do better. so just decided to make one Inspired by Grape a lot, since we really liked it

Corgi Example

export const routes: Routes = [
  new Namespace('/api/:userId', {
    params: {
      userId: Joi.number(),
    },
    before: async function() {
      this.params.user = await User.findByUserId(this.params.userId)
    },
    exceptionHandler: async function(error) {
      if (error.name == 'ValidationError') {
        const validationError = error as Joi.ValidationError;
        return this.json(
          {
            errors: validationError.details.map(e => e.message),
          },
          422
        );
      }
    },
    children: [
      Route.GET('/followers', {}, 'List of users that following me', async function() {
        return this.json({
          data: {}
        })
      }),
      new Namespace('/followings', {
        children: [
          Route.POST('/', '', {}, async function() {
            return this.json({ userId: this.params.user.id });
          }),
          Route.DELETE('/', '', {}, async function() {
            const userId = Number(this.params.userId);
            return this.json({ userId: this.params.user.id });
          }),
        ]
      })
    ]
  })
];
 
const router = new Router(routes);
exports.myHandler = router.handler();

Or refer src/test/e2e/complex_api.ts

Keywords

none

install

npm i vingle-corgi

Downloadsweekly downloads

188

version

1.18.0

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
  • avatar
Report a vulnerability