-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
103 lines (82 loc) · 3.54 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
const utilities = require('@apitraffic/utilities');
const package = require('./package.json');
module.exports.getContext = function(){
return utilities.context.getStore();
}
module.exports.getRequestManager = function(){
return utilities.context.getStore().RequestManager;
}
module.exports.tag = function(key, value){
utilities.context.getStore().RequestManager.tag(key, value);
}
module.exports.trace = function(content){
utilities.context.getStore().RequestManager.trace(content);
}
/**
* ApiTraffic Express middleware function.
* @param {{interceptOutbound?:boolean}} options - Configuration options.
* @returns {Function} - apitraffic-express middleware.
*/
function apiTraffic(options = {
interceptOutbound : true,
host : "",
token : "",
bucket : "",
debug : false
}){
// Set things up...
utilities.setup(options, utilities.context);
return (req, res, next) => {
// make sure the request context is setup with the RequestManager so it can be uses anywhere in the request...
utilities.context.enterWith({
RequestManager: new utilities.RequestManager({package : {name: package.name, version : package.version}})
});
const originalSend = res.send
res.send = function sendOverWrite(body) {
this.apiTrafficBody = body
originalSend.call(this, body)
}
res.on('finish', function onceFinish() {
// this is what we will do if the host is to be skipped...
//return next();
try{
const apiTrafficOptions = {
version: utilities.context.getStore().RequestManager.package.version,
sdk: utilities.context.getStore().RequestManager.package.name
};
let body = null;
if(req.method.toUpperCase() !== 'GET' && req.method.toUpperCase() !== 'OPTIONS'){
body = JSON.stringify(req.body);
}
// TODO: Account for other body types other than JSON...
const apiTrafficPayload = {
contextSid : utilities.context.getStore().RequestManager.contextSid,
direction : "in",
request: {
received: utilities.context.getStore().RequestManager.requestReceivedAt,
ip : req.ip,
url : `${req.protocol}://${req.headers['host']}${req.originalUrl}`,
method: req.method.toUpperCase(),
headers : req.headers,
body : body
},
response : {
headers : res.getHeaders(),
status : res.statusCode,
responseTime : utilities.getDuration(utilities.context.getStore().RequestManager.requestStartTime),
body : res.apiTrafficBody
},
tags : utilities.context.getStore().RequestManager.getTagArray(),
traces : utilities.context.getStore().RequestManager.getTraces()
};
// call the function to log all now...
// we will not await the response b/c we want to fire and forget...
utilities.sendToApiTraffic(apiTrafficOptions, apiTrafficPayload);
}catch(e){
console.log(e);
}
});
next();
};
}
module.exports.middleware = apiTraffic;