-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
45 lines (40 loc) · 987 Bytes
/
index.ts
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
import * as UUID from 'uuid/v4';
import 'ts-polyfill/lib/es2015-collection'; // Polyfill for Map()
import AsyncHook,
{
setSharedVariable,
getSharedVariable,
} from './async-hooks';
/**
* Middleware that enable Async hooks and set the unique logging transaction id
* @param req request
* @param res response
* @param next next callback function
*/
const ExpressLoggingUtilityMiddleware = (req, res, next) => {
const loggingId = UUID();
AsyncHook(loggingId, {
loggingId,
});
next();
};
/**
* Set variables per each request context
* @param {any} key Variable name
* @param {any} value variable value
*/
const set = (key: any, value: any) => {
return setSharedVariable(key, value);
};
/**
* Get variables related to current context
* @param {any} key Name of the variable to be retrieved
*/
const get = (key: any) => {
return getSharedVariable(key);
};
export = {
set,
get,
middleware: ExpressLoggingUtilityMiddleware,
};