-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
55 lines (48 loc) · 1.63 KB
/
index.js
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
'use strict';
let Promise = require('bluebird');
let path = require('path');
// Setup config
require('dotenv').config();
// Setup AWS clients
let AWS = require('aws-sdk');
if(!AWS.config.region) {
AWS.config.region = process.env.AWS_DEFAULT_REGION;
}
let dynamodb = new AWS.DynamoDB.DocumentClient();
let dynamodbPromised = Promise.promisifyAll(dynamodb);
// Setup Plaid client
let plaid = require('plaid');
Promise.promisifyAll(plaid);
let plaidClient = new plaid.Client(process.env.PLAID_CLIENT_ID, process.env.PLAID_CLIENT_SECRET, plaid.environments[process.env.PLAID_ENV]);
// Define options
let options = {
plaidClient: plaidClient,
dynamodb: dynamodbPromised,
stage: process.env.SERVERLESS_STAGE,
accountsTable: process.env.ACCOUNTS_TABLE
};
// Load lambdas
let lambdaPath = path.join(__dirname, "lambda");
require("fs").readdirSync(lambdaPath).forEach(function(file) {
let moduleName = path.parse(file).name;
let mod = require(`lambda/${moduleName}`);
let workflow = new mod(options);
module.exports[moduleName] = (event, context) => {
let statePromise = Promise.resolve({event: event, context: context});
workflow.run(statePromise)
.then(context.succeed)
.catch((err) => {
if(err.stack) {
console.error(err.stack);
} else {
console.error(err);
}
context.succeed({
statusCode: 500,
headers: {
"Access-Control-Allow-Origin" : "*"
}
});
});
};
});