-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-lambda.ts
48 lines (37 loc) · 1.33 KB
/
run-lambda.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
import { ApiServer, HttpMethod } from '@arinoto/core';
import './aws-architecture';
import { buildRouteKey } from './aws/tools';
const runtimeToRun = process.env.ANTHILL_RUNTIME;
if (!runtimeToRun) {
throw new Error('process.env.ANTHILL_RUNTIME is empty - cannot run.');
}
const server = ApiServer.registry.get(runtimeToRun);
if (!server) {
throw new Error(`Api server registry has no entry for ${runtimeToRun} from ${process.env.ANTHILL_RUNTIME ?? ''} ${JSON.stringify(ApiServer.registry)}`);
}
console.log('found Api server', server, 'for', runtimeToRun);
export type LambdaInputPayload = {
routeKey: string;
rawPath: string;
cookies: string[];
headers: Record<string, string>;
queryStringParameters: Record<string, string>;
pathParameters: Record<string, string>;
stageVariables: Record<string, string>;
body: unknown;
requestContext: {
http: {
method: HttpMethod;
path: string;
};
};
};
export const handler = async (input: LambdaInputPayload) => {
console.error(input);
const { routeKey } = input;
const apiDef = server.listener.apis.find(({ spec }) => routeKey === buildRouteKey(spec));
if (!apiDef) {
throw new Error(`No api found for ${routeKey} in ${JSON.stringify(server)}`);
}
return apiDef?.api.localExec(input.body);
};