OpenAPI Router
Warning: This is in early development and not recommended for production use yet.
An opinionated framework for routing HTTP requests to controller logic via an OpenAPI spec.
This library is intended for use in place of Express when running in an AWS Lambda-based Serverless environment, where a single Lambda responds to multiple paths/endpoints.
The rationale is that using Express in Lambda converts an object request into a socket request and then reads the response off the socket while serializing and deserializing additonal context in headers. It's much easier to just handle the request itself.
Features
- Document-driven functionality.
- Middleware via SecurityDefinitions.
- JSON-schema validation with AJV.
- Response compression.
- Convenience methods for HTTP responses.
Examples
API Gateway
import { Request, Response } from 'open-api-router';
class DocumentsController {
getById(req: Request, resp: Response) {
const doc = await db.documents.findOne(req.params.id);
if (!doc) {
return resp.notFound();
}
resp.ok({data: doc});
}
}
export = new DocumentsController();
import { ApiGatewayOpenApiRouter } from 'open-api-router';
import documents from './controllers/documents`;
// Create the Swagger Router with API Gateway type bindings
const router = new ApiGatewayOpenApiRouter({
location: '../swagger.yml',
controllers: { documents },
});
export handler = async (ev) => router.handle(ev);