|
2 | 2 | // SPDX-License-Identifier: MIT-0
|
3 | 3 |
|
4 | 4 | // default imports
|
5 |
| -const AWSXRay = require('aws-xray-sdk-core') |
6 |
| -const AWS = AWSXRay.captureAWS(require('aws-sdk')) |
7 |
| -const { metricScope, Unit } = require("aws-embedded-metrics") |
8 |
| -const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" }) |
| 5 | +const AWSXRay = require("aws-xray-sdk-core"); |
| 6 | +const AWS = AWSXRay.captureAWS(require("aws-sdk")); |
| 7 | +const { metricScope, Unit } = require("aws-embedded-metrics"); |
| 8 | +const DDB = new AWS.DynamoDB({ apiVersion: "2012-10-08" }); |
9 | 9 |
|
10 | 10 | // environment variables
|
11 |
| -const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env |
12 |
| -const options = { region: REGION } |
13 |
| -AWS.config.update({ region: REGION }) |
| 11 | +const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env; |
| 12 | +const options = { region: REGION }; |
| 13 | +AWS.config.update({ region: REGION }); |
14 | 14 |
|
15 | 15 | if (ENDPOINT_OVERRIDE !== "") {
|
16 |
| - options.endpoint = ENDPOINT_OVERRIDE |
| 16 | + options.endpoint = ENDPOINT_OVERRIDE; |
17 | 17 | }
|
18 | 18 |
|
19 |
| -const docClient = new AWS.DynamoDB.DocumentClient(options) |
| 19 | +const docClient = new AWS.DynamoDB.DocumentClient(options); |
20 | 20 | // response helper
|
21 | 21 | const response = (statusCode, body, additionalHeaders) => ({
|
22 |
| - statusCode, |
23 |
| - body: JSON.stringify(body), |
24 |
| - headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', ...additionalHeaders }, |
25 |
| -}) |
| 22 | + statusCode, |
| 23 | + body: JSON.stringify(body), |
| 24 | + headers: { |
| 25 | + "Content-Type": "application/json", |
| 26 | + "Access-Control-Allow-Origin": "*", |
| 27 | + ...additionalHeaders, |
| 28 | + }, |
| 29 | +}); |
26 | 30 |
|
27 |
| -function isValidRequest(context, event) { |
28 |
| - return (event !== null) && |
29 |
| - (event.pathParameters !== null) && |
30 |
| - (event.pathParameters.id !== null) && |
31 |
| - (/^[\w-]+$/.test(event.pathParameters.id)) |
| 31 | +function isValidRequest(event) { |
| 32 | + return ( |
| 33 | + event !== null && |
| 34 | + event.pathParameters !== null && |
| 35 | + event.pathParameters.id !== null && |
| 36 | + /^[\w-]+$/.test(event.pathParameters.id) |
| 37 | + ); |
32 | 38 | }
|
33 | 39 |
|
34 |
| -function getCognitoUsername(event){ |
35 |
| - let authHeader = event.requestContext.authorizer; |
36 |
| - if (authHeader !== null) |
37 |
| - { |
38 |
| - return authHeader.claims["cognito:username"]; |
39 |
| - } |
40 |
| - return null; |
41 |
| - |
| 40 | +function getCognitoUsername(event) { |
| 41 | + let authHeader = event.requestContext.authorizer; |
| 42 | + if (authHeader !== null) { |
| 43 | + return authHeader.claims["cognito:username"]; |
| 44 | + } |
| 45 | + return null; |
42 | 46 | }
|
43 | 47 |
|
44 | 48 | function updateRecord(username, recordId) {
|
45 |
| - let params = { |
46 |
| - TableName: TABLE_NAME, |
47 |
| - Key: { |
48 |
| - "cognito-username": username, |
49 |
| - "id": recordId |
50 |
| - }, |
51 |
| - UpdateExpression: "set #field = :value", |
52 |
| - ExpressionAttributeNames: { '#field': 'completed' }, |
53 |
| - ExpressionAttributeValues: { ':value': true } |
54 |
| - } |
55 |
| - return docClient.update(params) |
| 49 | + let params = { |
| 50 | + TableName: TABLE_NAME, |
| 51 | + Key: { |
| 52 | + "cognito-username": username, |
| 53 | + id: recordId, |
| 54 | + }, |
| 55 | + UpdateExpression: "set #field = :value", |
| 56 | + ExpressionAttributeNames: { "#field": "completed" }, |
| 57 | + ExpressionAttributeValues: { ":value": true }, |
| 58 | + }; |
| 59 | + return docClient.update(params); |
56 | 60 | }
|
57 | 61 |
|
58 | 62 | // Lambda Handler
|
59 |
| -exports.completeToDoItem = |
60 |
| - metricScope(metrics => |
61 |
| - async (event, context, callback) => { |
62 |
| - metrics.setNamespace('TodoApp') |
63 |
| - metrics.putDimensions({ Service: "completeTodo" }) |
64 |
| - metrics.setProperty("RequestId", context.requestId) |
65 |
| - |
66 |
| - if (!isValidRequest(context, event)) { |
67 |
| - metrics.putMetric("Error", 1, Unit.Count) |
68 |
| - return response(400, { message: "Error: Invalid request" }) |
69 |
| - } |
| 63 | +exports.completeToDoItem = metricScope((metrics) => async (event, context) => { |
| 64 | + metrics.setNamespace("TodoApp"); |
| 65 | + metrics.putDimensions({ Service: "completeTodo" }); |
| 66 | + metrics.setProperty("RequestId", context.requestId); |
70 | 67 |
|
71 |
| - try { |
72 |
| - let username = getCognitoUsername(event); |
73 |
| - let data = await updateRecord(username, event.pathParameters.id).promise() |
74 |
| - metrics.putMetric("Success", 1, Unit.Count) |
75 |
| - return response(200, data) |
76 |
| - } catch (err) { |
77 |
| - metrics.putMetric("Error", 1, Unit.Count) |
78 |
| - return response(400, { message: err.message }) |
79 |
| - } |
80 |
| - } |
81 |
| - ) |
| 68 | + if (!isValidRequest(context, event)) { |
| 69 | + metrics.putMetric("Error", 1, Unit.Count); |
| 70 | + return response(400, { message: "Error: Invalid request" }); |
| 71 | + } |
82 | 72 |
|
| 73 | + try { |
| 74 | + let username = getCognitoUsername(event); |
| 75 | + let data = await updateRecord(username, event.pathParameters.id).promise(); |
| 76 | + metrics.putMetric("Success", 1, Unit.Count); |
| 77 | + return response(200, data); |
| 78 | + } catch (err) { |
| 79 | + metrics.putMetric("Error", 1, Unit.Count); |
| 80 | + return response(400, { message: err.message }); |
| 81 | + } |
| 82 | +}); |
0 commit comments