MCPcopy Index your code
hub / github.com/balmbees/corgi

github.com/balmbees/corgi @v3.2.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v3.2.0 ↗ · + Follow
149 symbols 268 edges 29 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Travis Build Status npm version

Corgi

Grape like lightweight HTTP API Framework for AWS Lambda

Example

const router = new Router([
  new Namespace('/api/:userId', {
    params: {
      userId: Joi.number(),
    },
    async before() {
      this.params.user = await User.findByUserId(this.params.userId);
      if (!this.params.user) {
        this.json({
          error: "User not exists!",
        }, 404);
        // You can also just throw error - which goes to exceptionHandler
      }
    },
    async exceptionHandler(error) {
      // Global Exception Handling.
      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() {
            const user = this.params.user as User;
            return this.json({ userId: user.id });
          }),

          Route.DELETE('/', '', {}, async function() {
            const user = this.params.user as User;
            return this.json({ userId: user.id });
          }),
        ]
      })
    ]
  })
]);

// this goes directly into lambda.
export const handler = router.handler();

Or refer src/test/e2e/complex_api.ts

How to start

  1. npm install vingle-corgi
  2. exports.handler = new Router([routes]).handler();
  3. deploy lambda

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 consider about using these kinds of express wrapping.
But those are really inefficient and not reliable for production usage,
and, most of all, We really thought we can do better.
Inspired by Grape a lot, since we really liked it

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

Requirements

From v2.0, it only supports lambda nodejs8.10. if you need 6.10 support, either use v1.x or wrap router.handler

Extension points exported contracts — how you extend this code

ParameterDefinition (Interface)
(no doc)
src/parameter.ts
RouteSimplifiedOptions (Interface)
(no doc)
src/route.ts
MiddlewareBeforeOptions (Interface)
(no doc)
src/middleware.ts
StandardErrorResponseBody (Interface)
(no doc)
src/error_response.ts
Event (Interface)
(no doc)
src/lambda-proxy.ts
NamespaceOptions (Interface)
(no doc)
src/namespace.ts
AWSXRaySegment (Interface)
(no doc)
src/middlewares/xray-middleware/index.ts
Presenter (Interface)
(no doc)
src/route_factories/presenter_route/presenter.ts

Core symbols most depended-on inside this repo

json
called by 27
src/routing-context.ts
Query
called by 16
src/parameter.ts
GET
called by 15
src/route_factories/presenter_route/presenter_route.ts
resolve
called by 13
src/router.ts
create
called by 8
src/route_factories/presenter_route/presenter_route.ts
Path
called by 7
src/parameter.ts
_factory
called by 7
src/route.ts
Body
called by 6
src/parameter.ts

Shape

Method 68
Class 42
Interface 24
Function 15

Languages

TypeScript100%

Modules by API surface

src/route.ts15 symbols
src/open_api/index.ts12 symbols
src/lambda-proxy.ts12 symbols
src/__test__/router_spec.ts12 symbols
src/routing-context.ts11 symbols
src/router.ts11 symbols
src/route_factories/presenter_route/presenter_route.ts11 symbols
src/middlewares/xray-middleware/index.ts10 symbols
src/error_response.ts10 symbols
src/namespace.ts8 symbols
src/parameter.ts7 symbols
src/middleware.ts7 symbols

For agents

$ claude mcp add corgi \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact