-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambdaPut.js
50 lines (47 loc) · 1.53 KB
/
lambdaPut.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
const AWS = require('aws-sdk');
const ddb = new AWS.DynamoDB.DocumentClient();
exports.handler = (event, context, callback) => {
if (!event.requestContext.authorizer) {
errorResponse('Authorization not configured', context.awsRequestId, callback);
return;
}
const username = event.requestContext.authorizer.claims['cognito:username'];
const requestBody = JSON.parse(event.body);
insertItem(username, requestBody.Text).then(() => {
callback(null, {
statusCode: 201,
body: JSON.stringify({}),
headers: {
"Access-Control-Allow-Origin": "*",
'Access-Control-Allow-Methods': 'OPTIONS,POST,GET',
'Access-Control-Allow-Headers': 'Origin,Content-Type,Accept'
},
});
}).catch((err) => {
console.error(err);
errorResponse(err.message, context.awsRequestId, callback)
});
};
function insertItem(username, text) {
return ddb.put({
TableName: 'Items',
Item: {
// Id: Math.random().toString(36).substring(2) + Date.now().toString(36),
User: username,
Text: text,
Time: new Date().toISOString()
},
}).promise();
}
function errorResponse(errorMessage, awsRequestId, callback) {
callback(null, {
statusCode: 500,
body: JSON.stringify({
Error: errorMessage,
Reference: awsRequestId,
}),
headers: {
'Access-Control-Allow-Origin': '*',
},
});
}