Skip to content

Instantly share code, notes, and snippets.

@joandvgv
Last active April 25, 2020 17:00
Show Gist options
  • Save joandvgv/bfc6734edbbff3b61883e90fc3cd87ee to your computer and use it in GitHub Desktop.
Save joandvgv/bfc6734edbbff3b61883e90fc3cd87ee to your computer and use it in GitHub Desktop.
Map API Gateway to an express server
import express from "express";
import * as handlers from "./express-handler";
import { connector } from "swagger-routes-express";
import { apis, apiKeys } from "./constants";
import { getExport } from "./api-gateway";
import { setOperationIds } from "./utils";
import { ApiDefinition } from "./types";
const makeApp = (): express.Express => {
const app = express();
app.use(express.json()); // allow json body
apis.forEach(async name => {
const key = apiKeys[name];
const { body } = await getExport(key); // get api definition Buffer
const rawDefinition: ApiDefinition = JSON.parse(body.toString());
const apiDefinition = setOperationIds(rawDefinition); // add operation ids according to each lambda
const connect = connector(handlers, apiDefinition, { onCreateRoute });
connect(app); // attach the routes
});
return app;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment