UNPKG

886 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const uuid_1 = require("uuid");
4const fp = require("fastify-plugin");
5const logger_session_1 = require("./logger.session");
6const logger_constants_1 = require("./logger.constants");
7function requestContext(fastify, opts, next) {
8 fastify.addHook('onRequest', (req, res, done) => {
9 const requestId = req.headers[logger_constants_1.REQUEST_ID_HEADER] || uuid_1.v4();
10 logger_session_1.cls.bindEmitter(req.raw);
11 logger_session_1.cls.bindEmitter(res.res);
12 logger_session_1.cls.runWith(() => {
13 done();
14 }, {
15 [logger_constants_1.REQUEST_INFO]: { requestId }
16 });
17 });
18 next();
19}
20exports.requestContextPlugin = fp(requestContext, {
21 fastify: '3.x',
22 name: 'fastify-request-context',
23});
24//# sourceMappingURL=logger.middleware.js.map
\No newline at end of file