-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (26 loc) · 856 Bytes
/
index.js
File metadata and controls
32 lines (26 loc) · 856 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
26
27
28
29
30
31
32
const { handleGetRequest, handleAddRequest, errorResponse } = require('./controllers.js');
exports.handler = async (eventInput, context) => {
const { action, thing, num } = eventInput;
// Global input validation -->
if (!['get','add'].includes(action)) {
return errorResponse('Invalid request: value of "action" must be: "add" or "get"');
}
if (!thing?.match(/^[a-zA-Z]/)) {
return errorResponse('Invalid request: "thing" param should be one or more words');
}
try {
// Dispatcher -->
if (action === 'get') {
return await handleGetRequest(thing);
}
if (action === 'add') {
return await handleAddRequest(num, thing);
}
}
catch(e) {
// Error handling -->
const errorMsg = `There was an error: ${e?.message}`;
console.error(errorMsg);
return errorResponse(errorMsg, 500);
}
};