-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
51 lines (45 loc) · 1.29 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import * as cors from "@koa/cors";
import {
APIGatewayEvent,
APIGatewayEventRequestContext,
APIGatewayProxyHandler
} from "aws-lambda";
import * as Koa from "koa";
import * as bodyParser from "koa-bodyparser";
import "source-map-support/register";
import { HttpWarmup } from "./lib/http-warmup";
import serverlessHttp = require("serverless-http");
/**
* This type definition allows us to update the Koa.ParameterizedContext with
* the custom request mapping defined below.
*/
interface CustomRequestContext {
req: {
event: APIGatewayEvent;
context: APIGatewayEventRequestContext;
};
}
const app = new Koa<any, CustomRequestContext>();
app.use(bodyParser());
app.use(cors({ keepHeadersOnError: true }));
app.use(HttpWarmup);
app.use(async ctx => {
const {} = ctx.request.body;
// const key = process.env["API_KEY"];
try {
ctx.response.body = { data: "hello world" };
} catch (error) {
console.error("error", error);
ctx.throw(400, error.description);
}
});
export const post = (serverlessHttp(app, {
request: (
request,
event: APIGatewayEvent,
context: APIGatewayEventRequestContext
) => {
request.event = event;
request.context = context;
}
}) as unknown) as APIGatewayProxyHandler; // Until serverless-http releases a new package with updated types