Skip to content

Commit a0fd61d

Browse files
committed
Merge branch 'new-master'
2 parents d7df742 + 6b0e70e commit a0fd61d

15 files changed

Lines changed: 10990 additions & 3875 deletions

billing.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
import stripePackage from "stripe";
2+
import handler from "./libs/handler-lib";
23
import { calculateCost } from "./libs/billing-lib";
3-
import { success, failure } from "./libs/response-lib";
44

5-
export async function main(event, context) {
5+
export const main = handler(async (event, context) => {
66
const { storage, source } = JSON.parse(event.body);
77
const amount = calculateCost(storage);
88
const description = "Scratch charge";
99

1010
// Load our secret key from the environment variables
1111
const stripe = stripePackage(process.env.stripeSecretKey);
1212

13-
try {
14-
await stripe.charges.create({
15-
source,
16-
amount,
17-
description,
18-
currency: "usd"
19-
});
20-
return success({ status: true });
21-
} catch (e) {
22-
return failure({ message: e.message });
23-
}
24-
}
13+
await stripe.charges.create({
14+
source,
15+
amount,
16+
description,
17+
currency: "usd"
18+
});
19+
return { status: true };
20+
});

create.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import uuid from "uuid";
2-
import * as dynamoDbLib from "./libs/dynamodb-lib";
3-
import { success, failure } from "./libs/response-lib";
1+
import * as uuid from "uuid";
2+
import handler from "./libs/handler-lib";
3+
import dynamoDb from "./libs/dynamodb-lib";
44

5-
export async function main(event, context) {
5+
export const main = handler(async (event, context) => {
66
const data = JSON.parse(event.body);
77
const params = {
88
TableName: process.env.tableName,
9+
// 'Item' contains the attributes of the item to be created
10+
// - 'userId': user identities are federated through the
11+
// Cognito Identity Pool, we will use the identity id
12+
// as the user id of the authenticated user
13+
// - 'noteId': a unique uuid
14+
// - 'content': parsed from request body
15+
// - 'attachment': parsed from request body
16+
// - 'createdAt': current Unix timestamp
917
Item: {
1018
userId: event.requestContext.identity.cognitoIdentityId,
1119
noteId: uuid.v1(),
@@ -15,10 +23,7 @@ export async function main(event, context) {
1523
}
1624
};
1725

18-
try {
19-
await dynamoDbLib.call("put", params);
20-
return success(params.Item);
21-
} catch (e) {
22-
return failure({ status: false });
23-
}
24-
}
26+
await dynamoDb.put(params);
27+
28+
return params.Item;
29+
});

delete.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as dynamoDbLib from "./libs/dynamodb-lib";
2-
import { success, failure } from "./libs/response-lib";
1+
import handler from "./libs/handler-lib";
2+
import dynamoDb from "./libs/dynamodb-lib";
33

4-
export async function main(event, context) {
4+
export const main = handler(async (event, context) => {
55
const params = {
66
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be removed
@@ -13,10 +13,7 @@ export async function main(event, context) {
1313
}
1414
};
1515

16-
try {
17-
await dynamoDbLib.call("delete", params);
18-
return success({ status: true });
19-
} catch (e) {
20-
return failure({ status: false });
21-
}
22-
}
16+
await dynamoDb.delete(params);
17+
18+
return { status: true };
19+
});

get.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as dynamoDbLib from "./libs/dynamodb-lib";
2-
import { success, failure } from "./libs/response-lib";
1+
import handler from "./libs/handler-lib";
2+
import dynamoDb from "./libs/dynamodb-lib";
33

4-
export async function main(event, context) {
4+
export const main = handler(async (event, context) => {
55
const params = {
66
TableName: process.env.tableName,
77
// 'Key' defines the partition key and sort key of the item to be retrieved
@@ -13,15 +13,11 @@ export async function main(event, context) {
1313
}
1414
};
1515

16-
try {
17-
const result = await dynamoDbLib.call("get", params);
18-
if (result.Item) {
19-
// Return the retrieved item
20-
return success(result.Item);
21-
} else {
22-
return failure({ status: false, error: "Item not found." });
23-
}
24-
} catch (e) {
25-
return failure({ status: false });
16+
const result = await dynamoDb.get(params);
17+
if ( ! result.Item) {
18+
throw new Error("Item not found.");
2619
}
27-
}
20+
21+
// Return the retrieved item
22+
return result.Item;
23+
});

libs/debug-lib.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import AWS from "aws-sdk";
2+
import util from "util";
3+
4+
// Log AWS SDK calls
5+
AWS.config.logger = { log: debug };
6+
7+
let logs;
8+
let timeoutTimer;
9+
10+
export function init(event, context) {
11+
logs = [];
12+
13+
// Log API event
14+
debug("API event", {
15+
body: event.body,
16+
pathParameters: event.pathParameters,
17+
queryStringParameters: event.queryStringParameters,
18+
});
19+
20+
// Start timeout timer
21+
timeoutTimer = setTimeout(() => {
22+
timeoutTimer && flush(new Error("Lambda will timeout in 100 ms"));
23+
}, context.getRemainingTimeInMillis() - 100);
24+
}
25+
26+
export function end() {
27+
// Clear timeout timer
28+
clearTimeout(timeoutTimer);
29+
timeoutTimer = null;
30+
}
31+
32+
export function flush(e) {
33+
logs.forEach(({ date, string }) => console.debug(date, string));
34+
console.error(e);
35+
}
36+
37+
export default function debug() {
38+
logs.push({
39+
date: new Date(),
40+
string: util.format.apply(null, arguments),
41+
});
42+
}

libs/dynamodb-lib.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import AWS from "aws-sdk";
22

3-
export function call(action, params) {
4-
const dynamoDb = new AWS.DynamoDB.DocumentClient();
3+
const client = new AWS.DynamoDB.DocumentClient();
54

6-
return dynamoDb[action](params).promise();
7-
}
5+
export default {
6+
get : (params) => client.get(params).promise(),
7+
put : (params) => client.put(params).promise(),
8+
query : (params) => client.query(params).promise(),
9+
update: (params) => client.update(params).promise(),
10+
delete: (params) => client.delete(params).promise(),
11+
};

libs/handler-lib.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import * as debug from "./debug-lib";
2+
3+
export default function handler(lambda) {
4+
return function (event, context) {
5+
return Promise.resolve()
6+
// Start debugger
7+
.then(() => debug.init(event, context))
8+
// Run the Lambda
9+
.then(() => lambda(event, context))
10+
// On success
11+
.then((responseBody) => [200, responseBody])
12+
// On failure
13+
.catch((e) => {
14+
// Print debug messages
15+
debug.flush(e);
16+
return [500, { error: e.message }];
17+
})
18+
// Return HTTP response
19+
.then(([statusCode, body]) => ({
20+
statusCode,
21+
headers: {
22+
"Access-Control-Allow-Origin": "*",
23+
"Access-Control-Allow-Credentials": true,
24+
},
25+
body: JSON.stringify(body),
26+
}))
27+
// Cleanup debugger
28+
.finally(debug.end);
29+
};
30+
}

list.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import * as dynamoDbLib from "./libs/dynamodb-lib";
2-
import { success, failure } from "./libs/response-lib";
1+
import handler from "./libs/handler-lib";
2+
import dynamoDb from "./libs/dynamodb-lib";
33

4-
export async function main(event, context) {
4+
export const main = handler(async (event, context) => {
55
const params = {
66
TableName: process.env.tableName,
77
// 'KeyConditionExpression' defines the condition for the query
@@ -16,11 +16,8 @@ export async function main(event, context) {
1616
}
1717
};
1818

19-
try {
20-
const result = await dynamoDbLib.call("query", params);
21-
// Return the matching list of items in response body
22-
return success(result.Items);
23-
} catch (e) {
24-
return failure({ status: false });
25-
}
26-
}
19+
const result = await dynamoDb.query(params);
20+
21+
// Return the matching list of items in response body
22+
return result.Items;
23+
});

mocks/delete-event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"pathParameters": {
3-
"id": "578eb840-f70f-11e6-9d1a-1359b3b22944"
3+
"id": "2b82e2c0-793f-11ea-9602-6d514f529482"
44
},
55
"requestContext": {
66
"identity": {

mocks/get-event.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"pathParameters": {
3-
"id": "578eb840-f70f-11e6-9d1a-1359b3b22944"
3+
"id": "2b82e2c0-793f-11ea-9602-6d514f529482"
44
},
55
"requestContext": {
66
"identity": {

0 commit comments

Comments
 (0)