-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathindex.js
52 lines (46 loc) · 1.47 KB
/
index.js
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
52
exports.handler = async (event, context, callback) => {
const moment = require('moment');
const METHODS = require('./methods');
const { parseParams, parseError, finalizeResponse } = require('./utils/io');
const { log } = require('./utils/logger');
const { version } = require('./package.json');
// parse event to req
const req = {
url: (event.routeKey || '').replace('ANY ', ''),
method: event.requestContext?.http?.method,
headers: event.headers,
params: { ...event.pathParameters },
query: { ...event.queryStringParameters },
body: { ...(event.body && JSON.parse(event.body)) },
};
// create params from req
const params = parseParams(req, 'api');
const { method } = { ...params };
// when not triggered by API
if (!method && !event.requestContext) {
try {
await METHODS.archive();
await METHODS.alerts();
} catch (error) {}
}
if (!method) return { version, log_level: process.env.LOG_LEVEL };
// for calculate timeSpent
const startTime = moment();
let response;
switch (method) {
default:
if (method in METHODS) {
try {
response = await METHODS[method](params);
} catch (error) {
response = parseError(error);
}
break;
}
response = { error: true, code: 400, message: 'method not supported' };
break;
}
response = finalizeResponse(response, params, startTime);
log('debug', 'api', 'send response', response);
return response;
};