-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.mjs
71 lines (65 loc) · 2.16 KB
/
create.mjs
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
import { v4 as uuid } from 'uuid';
import { createResponse } from '/opt/nodejs/sample-layer/utils.mjs';
const dynamodb = DynamoDBDocument.from(new DynamoDB({}));
const TABLE_NAME = process.env.TABLE_NAME;
const TTL_IN_SECONDS = 60;
function getExpirationTime() {
return Math.floor(Date.now() / 1000) + TTL_IN_SECONDS;
}
let corsHeaders = {
'Access-Control-Allow-Origin': process.env.CORS_ORIGIN,
'Access-Control-Allow-Credentials': true,
};
export const handler = async (event) => {
return new Promise(async (resolve) => {
let payload = null;
try {
payload = JSON.parse(event.body);
} catch (err) {
return resolve(createResponse({
"statusCode": 400,
"headers": corsHeaders,
"body": {
"success": false,
"reason": "unable to parse request body, expected valid JSON format"
}
}));
}
// add a new object to the table
let newDataOwner = uuid();
let newObjectId = uuid();
try {
await dynamodb.put({
TableName: TABLE_NAME,
Item: {
"dataOwner": newDataOwner,
"objectId": newObjectId,
"payload": payload,
"expiration": getExpirationTime()
}
});
resolve(createResponse({
"statusCode": 200,
"headers": corsHeaders,
"body": {
"success": true,
"dataOwner": newDataOwner,
"objectId": newObjectId
}
}));
} catch (err) {
console.error(err);
resolve(createResponse({
"statusCode": 500,
"headers": corsHeaders,
"body": {
"success": false,
"reason": "an unexpected error occurred",
"error": err
}
}));
}
});
}