Skip to content

Commit 08789dc

Browse files
committed
Adding our Serverless API
1 parent 1619d18 commit 08789dc

22 files changed

Lines changed: 9861 additions & 4170 deletions

billing.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import stripePackage from "stripe";
2+
import handler from "./libs/handler-lib";
3+
import { calculateCost } from "./libs/billing-lib";
4+
5+
export const main = handler(async (event, context) => {
6+
const { storage, source } = JSON.parse(event.body);
7+
const amount = calculateCost(storage);
8+
const description = "Scratch charge";
9+
10+
// Load our secret key from the environment variables
11+
const stripe = stripePackage(process.env.stripeSecretKey);
12+
13+
await stripe.charges.create({
14+
source,
15+
amount,
16+
description,
17+
currency: "usd"
18+
});
19+
return { status: true };
20+
});

create.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as uuid from "uuid";
2+
import handler from "./libs/handler-lib";
3+
import dynamoDb from "./libs/dynamodb-lib";
4+
5+
export const main = handler(async (event, context) => {
6+
const data = JSON.parse(event.body);
7+
const params = {
8+
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
17+
Item: {
18+
userId: event.requestContext.identity.cognitoIdentityId,
19+
noteId: uuid.v1(),
20+
content: data.content,
21+
attachment: data.attachment,
22+
createdAt: Date.now()
23+
}
24+
};
25+
26+
await dynamoDb.put(params);
27+
28+
return params.Item;
29+
});

delete.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import handler from "./libs/handler-lib";
2+
import dynamoDb from "./libs/dynamodb-lib";
3+
4+
export const main = handler(async (event, context) => {
5+
const params = {
6+
TableName: process.env.tableName,
7+
// 'Key' defines the partition key and sort key of the item to be removed
8+
// - 'userId': Identity Pool identity id of the authenticated user
9+
// - 'noteId': path parameter
10+
Key: {
11+
userId: event.requestContext.identity.cognitoIdentityId,
12+
noteId: event.pathParameters.id
13+
}
14+
};
15+
16+
await dynamoDb.delete(params);
17+
18+
return { status: true };
19+
});

env.example

Lines changed: 0 additions & 8 deletions
This file was deleted.

get.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import handler from "./libs/handler-lib";
2+
import dynamoDb from "./libs/dynamodb-lib";
3+
4+
export const main = handler(async (event, context) => {
5+
const params = {
6+
TableName: process.env.tableName,
7+
// 'Key' defines the partition key and sort key of the item to be retrieved
8+
// - 'userId': Identity Pool identity id of the authenticated user
9+
// - 'noteId': path parameter
10+
Key: {
11+
userId: event.requestContext.identity.cognitoIdentityId,
12+
noteId: event.pathParameters.id
13+
}
14+
};
15+
16+
const result = await dynamoDb.get(params);
17+
if ( ! result.Item) {
18+
throw new Error("Item not found.");
19+
}
20+
21+
// Return the retrieved item
22+
return result.Item;
23+
});

libs/billing-lib.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function calculateCost(storage) {
2+
const rate = storage <= 10
3+
? 4
4+
: storage <= 100
5+
? 2
6+
: 1;
7+
8+
return rate * storage * 100;
9+
}

libs/dynamodb-lib.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import AWS from "aws-sdk";
2+
3+
const client = new AWS.DynamoDB.DocumentClient();
4+
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export default function handler(lambda) {
2+
return function (event, context) {
3+
return Promise.resolve()
4+
// Run the Lambda
5+
.then(() => lambda(event, context))
6+
// On success
7+
.then((responseBody) => [200, responseBody])
8+
// On failure
9+
.catch((e) => {
10+
return [500, { error: e.message }];
11+
})
12+
// Return HTTP response
13+
.then(([statusCode, body]) => ({
14+
statusCode,
15+
headers: {
16+
"Access-Control-Allow-Origin": "*",
17+
"Access-Control-Allow-Credentials": true,
18+
},
19+
body: JSON.stringify(body),
20+
}));
21+
};
22+
}

list.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import handler from "./libs/handler-lib";
2+
import dynamoDb from "./libs/dynamodb-lib";
3+
4+
export const main = handler(async (event, context) => {
5+
const params = {
6+
TableName: process.env.tableName,
7+
// 'KeyConditionExpression' defines the condition for the query
8+
// - 'userId = :userId': only return items with matching 'userId'
9+
// partition key
10+
// 'ExpressionAttributeValues' defines the value in the condition
11+
// - ':userId': defines 'userId' to be Identity Pool identity id
12+
// of the authenticated user
13+
KeyConditionExpression: "userId = :userId",
14+
ExpressionAttributeValues: {
15+
":userId": event.requestContext.identity.cognitoIdentityId
16+
}
17+
};
18+
19+
const result = await dynamoDb.query(params);
20+
21+
// Return the matching list of items in response body
22+
return result.Items;
23+
});

mocks/billing-event.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"body": "{\"source\":\"tok_visa\",\"storage\":21}",
3+
"requestContext": {
4+
"identity": {
5+
"cognitoIdentityId": "USER-SUB-1234"
6+
}
7+
}
8+
}

0 commit comments

Comments
 (0)