-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserverless.js
64 lines (53 loc) · 1.59 KB
/
serverless.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
const defaults = require('lodash.defaults');
const cloneDeep = require('lodash.clonedeep');
const setupTransmission = require('./lib');
const defaultConfig = {
proxyObj: module.parent,
proxyKey: "exports",
presets: [],
modifiers: [],
}
const getCustomContext = logger => {
try {
const context = logger.external.sentry.context;
if(typeof context !== "function") {
throw new Error("Context must be a function");
}
return context;
} catch(err) {
return () => {}
}
}
const setupServerless = function(userConfig) {
const {
logger,
proxyObj,
proxyKey,
presets,
modifiers,
} = defaults(userConfig, defaultConfig);
const sentryContext = getCustomContext(logger);
const loggerInstance = setupTransmission(logger);
const presetsMiddlewares = presets.map(preset => require(`./lib/presets/${preset}`));
proxyObj[proxyKey] = new Proxy(proxyObj[proxyKey], {
get(target, propKey) {
const origMethod = target[propKey];
return function (event, context, callback) {
try {
const loggerEvent = cloneDeep(event);
const loggerContext = cloneDeep(context);
modifiers.forEach(modifier => modifier(loggerEvent, loggerContext));
presetsMiddlewares.forEach(presetMiddleware => {
presetMiddleware(loggerInstance, loggerEvent, loggerContext, sentryContext);
});
return origMethod(event, context, callback)
} catch (err) {
loggerInstance.error(err);
callback(err);
}
};
}
});
return loggerInstance
}
module.exports = setupServerless