-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhandler.js
More file actions
25 lines (22 loc) · 829 Bytes
/
handler.js
File metadata and controls
25 lines (22 loc) · 829 Bytes
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
const response = (statusCode, body, additionalHeaders) => ({
statusCode,
body: JSON.stringify(body),
headers: { 'Content-Type': 'application/json', ...additionalHeaders },
});
// Factory function creating and returning the handler function
module.exports = deps => async (event) => {
try {
switch (event.httpMethod) {
case 'GET': return response('200', await deps.dynamo.scan(
{ TableName: event.queryStringParameters.TableName }).promise());
case 'POST': return response('204', await deps.dynamo.putItem(
JSON.parse(event.body)).promise());
default: return response('405',
{ message: `Unsupported method: ${event.httpMethod}` },
{ Allow: 'GET, POST' });
}
} catch (err) {
console.error(err);
return response('400', { message: err.message });
}
};