-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathindex.js
49 lines (46 loc) · 1.72 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
exports.handler = async (event) => {
console.log('Received event:', JSON.stringify(event, null, 2));
let body;
try {
switch (event.httpMethod) {
case "GET":
if(event.queryStringParameters != null) {
body = `Processing Get Product Id with "${event.pathParameters.id}" and Category with "${event.queryStringParameters.category}" `; // GET product/1234?category=Phone
}
else if (event.pathParameters != null) {
body = `Processing Get Product Id with "${event.pathParameters.id}"`; // GET product/1234
} else {
body = `Processing Get All Products`; // GET product
}
break;
case "POST":
let payload = JSON.parse(event.body);
body = `Processing Post Product with "${payload}"`; // POST /product
break;
case "DELETE":
if(event.pathParameters != null) {
body = `Processing Delete Product Id with "${event.pathParameters.id}"`; // DELETE product/1234
}
break;
default:
throw new Error(`Unsupported route: "${event.httpMethod}"`);
}
console.log(body);
return {
statusCode: 200,
body: JSON.stringify({
message: `Successfully finished operation: "${event.httpMethod}"`,
body: body
})
};
} catch (e) {
console.error(e);
return {
statusCode: 400,
body: JSON.stringify({
message: "Failed to perform operation.",
errorMsg: e.message
})
};
}
}