-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
70 lines (58 loc) · 1.98 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* This file is an example AWS Lambda function.
*/
const moesif = require('moesif-aws-lambda');
const https = require('https');
console.log('Loading function');
const moesifOptions = {
applicationId: process.env.MOESIF_APPLICATION_ID,
identifyUser: function (event, context) {
return event.requestContext && event.requestContext.identity && event.requestContext.identity.cognitoIdentityId
},
// below is optional: but if you plan to metered billing, moesif have 1 to 1 mapping between company id and subscription id.
//
// - by providing a subscription id directly.
// - or providing a companyId, and during setting up Billing provider, set up mapping from subscription id to company id.
identifyCompany: function (event, context) {
return 'some company id or subscription id';
}
};
var moesifMiddleware = moesif(moesifOptions);
// optional. only if you want to capture outgoing api calls.
moesifMiddleware.startCaptureOutgoing();
exports.handler = function (event, context, callback) {
// Outgoing API call to third party
https.get(
{
host: 'jsonplaceholder.typicode.com',
path: '/posts/1'
},
function(res) {
var body = '';
res.on('data', function(d) {
body += d;
});
res.on('end', function() {
var parsed = JSON.parse(body);
console.log(parsed);
});
}
);
callback(null, {
statusCode: '200',
body: JSON.stringify({key: 'hello world'}),
headers: {
'Content-Type': 'application/json'
}
});
};
// Async Functions
// For more details, please refer to - https://docs.aws.amazon.com/lambda/latest/dg/nodejs-handler.html.
// exports.handler = async (event, context) => {
// const response = {
// statusCode: 200,
// body: JSON.stringify({ message: 'hello world' })
// }
// return response
// }
exports.handler = moesif(moesifOptions, exports.handler);