Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit ee55821

Browse files
author
charles.anim
committed
chore(todo-src): code cleanup and prettier formatting
1 parent 2e79a38 commit ee55821

File tree

6 files changed

+375
-368
lines changed

6 files changed

+375
-368
lines changed

‎todo-src/addTodo/app.js

Lines changed: 75 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,95 @@
22
// SPDX-License-Identifier: MIT-0
33

44
// 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" })
9-
const { v1: uuidv1 } = require('uuid');
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+
const { v1: uuidv1 } = require("uuid");
1010

1111
// environment variables
12-
const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env
13-
const options = { region: REGION }
14-
AWS.config.update({ region: REGION })
12+
const { TABLE_NAME, ENDPOINT_OVERRIDE, REGION } = process.env;
13+
const options = { region: REGION };
14+
AWS.config.update({ region: REGION });
1515

1616
if (ENDPOINT_OVERRIDE !== "") {
17-
options.endpoint = ENDPOINT_OVERRIDE
17+
options.endpoint = ENDPOINT_OVERRIDE;
1818
}
1919

20-
const docClient = new AWS.DynamoDB.DocumentClient(options)
20+
const docClient = new AWS.DynamoDB.DocumentClient(options);
2121
// response helper
2222
const response = (statusCode, body, additionalHeaders) => ({
23-
statusCode,
24-
body: JSON.stringify(body),
25-
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*', ...additionalHeaders },
26-
})
27-
28-
function isValidRequest(context, event) {
29-
return (event.body !== null)
23+
statusCode,
24+
body: JSON.stringify(body),
25+
headers: {
26+
"Content-Type": "application/json",
27+
"Access-Control-Allow-Origin": "*",
28+
...additionalHeaders,
29+
},
30+
});
31+
32+
function isValidRequest(event) {
33+
return event.body !== null;
3034
}
3135

32-
function getCognitoUsername(event){
33-
let authHeader = event.requestContext.authorizer;
34-
if (authHeader !== null)
35-
{
36-
return authHeader.claims["cognito:username"];
37-
}
38-
return null;
39-
36+
function getCognitoUsername(event) {
37+
let authHeader = event.requestContext.authorizer;
38+
if (authHeader !== null) {
39+
return authHeader.claims["cognito:username"];
40+
}
41+
return null;
4042
}
4143

4244
function addRecord(event) {
43-
44-
let usernameField = {
45-
"cognito-username": getCognitoUsername(event)
46-
}
47-
48-
// auto generated date fields
49-
let d = new Date()
50-
let dISO = d.toISOString()
51-
let auto_fields = {
52-
"id": uuidv1(),
53-
"creation_date": dISO,
54-
"lastupdate_date": dISO
55-
}
56-
57-
//merge the json objects
58-
let item_body = {...usernameField, ...auto_fields, ...JSON.parse(event.body) }
59-
60-
console.log(item_body);
61-
62-
//final params to DynamoDB
63-
const params = {
64-
TableName: TABLE_NAME,
65-
Item: item_body
66-
}
67-
68-
return docClient.put(params)
45+
let usernameField = {
46+
"cognito-username": getCognitoUsername(event),
47+
};
48+
49+
// auto generated date fields
50+
let dISO = new Date().toISOString();
51+
let auto_fields = {
52+
id: uuidv1(),
53+
creation_date: dISO,
54+
lastupdate_date: dISO,
55+
};
56+
57+
//merge the json objects
58+
let item_body = {
59+
...usernameField,
60+
...auto_fields,
61+
...JSON.parse(event.body),
62+
};
63+
64+
console.log(item_body);
65+
66+
//final params to DynamoDB
67+
const params = {
68+
TableName: TABLE_NAME,
69+
Item: item_body,
70+
};
71+
72+
return docClient.put(params);
6973
}
7074

7175
// Lambda Handler
72-
exports.addToDoItem =
73-
metricScope(metrics =>
74-
async (event, context, callback) => {
75-
metrics.setNamespace('TodoApp')
76-
metrics.putDimensions({ Service: "addTodo" })
77-
metrics.setProperty("RequestId", context.requestId)
78-
79-
if (!isValidRequest(context, event)) {
80-
metrics.putMetric("Error", 1, Unit.Count)
81-
return response(400, { message: "Error: Invalid request" })
82-
}
76+
exports.addToDoItem = metricScope(
77+
(metrics) => async (event, context, callback) => {
78+
metrics.setNamespace("TodoApp");
79+
metrics.putDimensions({ Service: "addTodo" });
80+
metrics.setProperty("RequestId", context.requestId);
81+
82+
if (!isValidRequest(context, event)) {
83+
metrics.putMetric("Error", 1, Unit.Count);
84+
return response(400, { message: "Error: Invalid request" });
85+
}
8386

84-
try {
85-
let data = await addRecord(event).promise()
86-
metrics.putMetric("Success", 1, Unit.Count)
87-
return response(200, data)
88-
} catch (err) {
89-
metrics.putMetric("Error", 1, Unit.Count)
90-
return response(400, { message: err.message })
91-
}
92-
}
93-
)
87+
try {
88+
let data = await addRecord(event).promise();
89+
metrics.putMetric("Success", 1, Unit.Count);
90+
return response(200, data);
91+
} catch (err) {
92+
metrics.putMetric("Error", 1, Unit.Count);
93+
return response(400, { message: err.message });
94+
}
95+
}
96+
);

‎todo-src/completeTodo/app.js

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,81 @@
22
// SPDX-License-Identifier: MIT-0
33

44
// 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" });
99

1010
// 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 });
1414

1515
if (ENDPOINT_OVERRIDE !== "") {
16-
options.endpoint = ENDPOINT_OVERRIDE
16+
options.endpoint = ENDPOINT_OVERRIDE;
1717
}
1818

19-
const docClient = new AWS.DynamoDB.DocumentClient(options)
19+
const docClient = new AWS.DynamoDB.DocumentClient(options);
2020
// response helper
2121
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+
});
2630

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+
);
3238
}
3339

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;
4246
}
4347

4448
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);
5660
}
5761

5862
// 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);
7067

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+
}
8272

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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /