@villedemontreal/correlation-id 5.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +119 -0
  3. package/dist/src/config/configs.d.ts +16 -0
  4. package/dist/src/config/configs.js +26 -0
  5. package/dist/src/config/configs.js.map +1 -0
  6. package/dist/src/config/constants.d.ts +27 -0
  7. package/dist/src/config/constants.js +27 -0
  8. package/dist/src/config/constants.js.map +1 -0
  9. package/dist/src/config/init.d.ts +15 -0
  10. package/dist/src/config/init.js +34 -0
  11. package/dist/src/config/init.js.map +1 -0
  12. package/dist/src/index.d.ts +3 -0
  13. package/dist/src/index.js +25 -0
  14. package/dist/src/index.js.map +1 -0
  15. package/dist/src/middleware/correlationIdMiddleware.d.ts +8 -0
  16. package/dist/src/middleware/correlationIdMiddleware.js +43 -0
  17. package/dist/src/middleware/correlationIdMiddleware.js.map +1 -0
  18. package/dist/src/middleware/correlationIdMiddleware.test.d.ts +1 -0
  19. package/dist/src/middleware/correlationIdMiddleware.test.js +306 -0
  20. package/dist/src/middleware/correlationIdMiddleware.test.js.map +1 -0
  21. package/dist/src/services/correlationIdService.d.ts +68 -0
  22. package/dist/src/services/correlationIdService.js +166 -0
  23. package/dist/src/services/correlationIdService.js.map +1 -0
  24. package/dist/src/services/correlationIdService.test.d.ts +1 -0
  25. package/dist/src/services/correlationIdService.test.js +215 -0
  26. package/dist/src/services/correlationIdService.test.js.map +1 -0
  27. package/dist/src/utils/logger.d.ts +11 -0
  28. package/dist/src/utils/logger.js +54 -0
  29. package/dist/src/utils/logger.js.map +1 -0
  30. package/dist/src/utils/testingConfigurations.d.ts +9 -0
  31. package/dist/src/utils/testingConfigurations.js +18 -0
  32. package/dist/src/utils/testingConfigurations.js.map +1 -0
  33. package/package.json +62 -0
  34. package/src/config/configs.ts +26 -0
  35. package/src/config/constants.ts +40 -0
  36. package/src/config/init.ts +33 -0
  37. package/src/index.ts +9 -0
  38. package/src/middleware/correlationIdMiddleware.test.ts +335 -0
  39. package/src/middleware/correlationIdMiddleware.ts +45 -0
  40. package/src/services/correlationIdService.test.ts +255 -0
  41. package/src/services/correlationIdService.ts +255 -0
  42. package/src/utils/logger.ts +53 -0
  43. package/src/utils/testingConfigurations.ts +14 -0
@@ -0,0 +1,215 @@
1
+ "use strict";
2
+ // tslint:disable max-func-body-length
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ const chai_1 = require("chai");
5
+ const events_1 = require("events");
6
+ const testingConfigurations_1 = require("../utils/testingConfigurations");
7
+ const correlationIdService_1 = require("./correlationIdService");
8
+ (0, testingConfigurations_1.setTestingConfigurations)();
9
+ const uuidMatcher = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
10
+ async function timeout(work, ms = 50) {
11
+ return new Promise(resolve => setTimeout(() => {
12
+ work();
13
+ resolve();
14
+ }, ms));
15
+ }
16
+ describe('Correlation Id Service', () => {
17
+ describe('createNewId', () => {
18
+ it('should return UUID', () => {
19
+ const cid = correlationIdService_1.correlationIdService.createNewId();
20
+ chai_1.assert.match(cid, uuidMatcher);
21
+ });
22
+ });
23
+ describe('withId correlator', () => {
24
+ it('with sync function', () => {
25
+ // GIVEN
26
+ const expectedResult = '⭐';
27
+ // WHEN
28
+ const result = correlationIdService_1.correlationIdService.withId(() => {
29
+ const actual = correlationIdService_1.correlationIdService.getId();
30
+ chai_1.assert.match(actual, uuidMatcher, 'getId() should return a uuid');
31
+ return expectedResult;
32
+ });
33
+ // THEN
34
+ chai_1.assert.strictEqual(result, expectedResult);
35
+ });
36
+ it('with async function', async () => {
37
+ let done = false;
38
+ const promise = correlationIdService_1.correlationIdService.withId(async () => {
39
+ await timeout(() => {
40
+ const actual = correlationIdService_1.correlationIdService.getId();
41
+ chai_1.assert.match(actual, uuidMatcher, 'getId() should return a uuid');
42
+ done = true;
43
+ });
44
+ });
45
+ // "withId" doesn't care about a promise to be floating or not
46
+ chai_1.assert.isFalse(done);
47
+ await promise;
48
+ });
49
+ it('with supplied id', () => {
50
+ const testId = 'id-1';
51
+ correlationIdService_1.correlationIdService.withId(() => {
52
+ const actual = correlationIdService_1.correlationIdService.getId();
53
+ chai_1.assert.strictEqual(actual, testId, 'getId() should return supplied id');
54
+ }, testId);
55
+ });
56
+ it('with bound emitter', () => {
57
+ // GIVEN
58
+ const emitter = new events_1.EventEmitter();
59
+ let receivedValue = 0;
60
+ let receivedCid = '';
61
+ emitter.on('test', value => {
62
+ receivedValue = value;
63
+ receivedCid = correlationIdService_1.correlationIdService.getId();
64
+ });
65
+ // WHEN
66
+ let boundEmitter;
67
+ correlationIdService_1.correlationIdService.withId(() => {
68
+ boundEmitter = correlationIdService_1.correlationIdService.bind(emitter);
69
+ chai_1.assert.strictEqual(boundEmitter, emitter);
70
+ }, 'foo');
71
+ // THEN
72
+ boundEmitter.emit('test', 33);
73
+ chai_1.assert.equal(receivedValue, 33);
74
+ chai_1.assert.equal(receivedCid, 'foo');
75
+ });
76
+ it('with bound function', () => {
77
+ // GIVEN
78
+ const func = (msg) => msg + correlationIdService_1.correlationIdService.getId();
79
+ // WHEN
80
+ let boundFunc;
81
+ correlationIdService_1.correlationIdService.withId(() => {
82
+ boundFunc = correlationIdService_1.correlationIdService.bind(func);
83
+ }, 'foo');
84
+ // THEN
85
+ const result = boundFunc('Bar-');
86
+ chai_1.assert.equal(result, 'Bar-foo');
87
+ });
88
+ it('with bound method', () => {
89
+ // GIVEN
90
+ const instance = {
91
+ title: 'Hello-',
92
+ say(msg) {
93
+ return this.title + msg + correlationIdService_1.correlationIdService.getId();
94
+ }
95
+ };
96
+ // WHEN
97
+ correlationIdService_1.correlationIdService.withId(() => {
98
+ instance.say = correlationIdService_1.correlationIdService.bind(instance.say);
99
+ }, 'foo');
100
+ // THEN
101
+ const result = instance.say('Bar-');
102
+ chai_1.assert.equal(result, 'Hello-Bar-foo');
103
+ });
104
+ it('with resolved promise', async () => {
105
+ let done = false;
106
+ const promise = correlationIdService_1.correlationIdService.withId(() => Promise.resolve(correlationIdService_1.correlationIdService.getId()).then(id => {
107
+ chai_1.assert.match(id, uuidMatcher, 'Promise should resolve correlation id');
108
+ done = true;
109
+ }));
110
+ // "withId" doesn't care about a promise to be floating or not
111
+ chai_1.assert.isFalse(done);
112
+ await promise;
113
+ });
114
+ it('with nested functions', () => {
115
+ correlationIdService_1.correlationIdService.withId(() => {
116
+ const cid1 = correlationIdService_1.correlationIdService.getId();
117
+ chai_1.assert.match(cid1, uuidMatcher, 'correlationIdService.getId() should return a UUID');
118
+ correlationIdService_1.correlationIdService.withId(() => {
119
+ const cid2 = correlationIdService_1.correlationIdService.getId();
120
+ chai_1.assert.notEqual(cid2, cid1, 'correlationIdService.getId() should return a different id for every scope');
121
+ chai_1.assert.match(cid2, uuidMatcher, 'correlationIdService.getId() should return a UUID');
122
+ });
123
+ const cid3 = correlationIdService_1.correlationIdService.getId();
124
+ chai_1.assert.strictEqual(cid3, cid1, 'correlationIdService.getId() should return the same id for the same scope');
125
+ chai_1.assert.match(cid3, uuidMatcher, 'correlationIdService.getId() should return a UUID');
126
+ });
127
+ });
128
+ it('with async function', async function () {
129
+ const duration = 25;
130
+ setSlowThreshold(this, duration);
131
+ // GIVEN
132
+ const expectedResult = '⭐';
133
+ // WHEN
134
+ const result = await correlationIdService_1.correlationIdService.withId(async () => {
135
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
136
+ await timeout(() => {
137
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
138
+ }, duration);
139
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
140
+ return expectedResult;
141
+ }, 'foo');
142
+ // THEN
143
+ chai_1.assert.strictEqual(result, expectedResult);
144
+ });
145
+ });
146
+ describe('withIdAsync correlator', () => {
147
+ it('with async function', async function () {
148
+ const duration = 25;
149
+ setSlowThreshold(this, duration);
150
+ // GIVEN
151
+ const expectedResult = '⭐';
152
+ // WHEN
153
+ const result = await correlationIdService_1.correlationIdService.withIdAsync(async () => {
154
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
155
+ await timeout(() => {
156
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
157
+ }, duration);
158
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
159
+ return expectedResult;
160
+ }, 'foo');
161
+ // THEN
162
+ chai_1.assert.strictEqual(result, expectedResult);
163
+ });
164
+ it('with error in callback', async () => {
165
+ try {
166
+ await correlationIdService_1.correlationIdService.withIdAsync(async () => {
167
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
168
+ throw new Error('some error...');
169
+ }, 'foo');
170
+ chai_1.assert.fail('expected error');
171
+ }
172
+ catch (err) {
173
+ chai_1.assert.equal(err.message, 'some error...');
174
+ }
175
+ });
176
+ it('with error after await', async function () {
177
+ const duration = 25;
178
+ setSlowThreshold(this, duration);
179
+ try {
180
+ await correlationIdService_1.correlationIdService.withIdAsync(async () => {
181
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
182
+ // tslint:disable-next-line: no-empty
183
+ await timeout(() => { }, duration);
184
+ throw new Error('some error...');
185
+ }, 'foo');
186
+ chai_1.assert.fail('expected error');
187
+ }
188
+ catch (err) {
189
+ chai_1.assert.equal(err.message, 'some error...');
190
+ }
191
+ });
192
+ it('with nested async functions', async function () {
193
+ const duration = 25;
194
+ setSlowThreshold(this, 2 * duration);
195
+ await correlationIdService_1.correlationIdService.withIdAsync(async () => {
196
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
197
+ await timeout(async () => {
198
+ await correlationIdService_1.correlationIdService.withIdAsync(async () => {
199
+ await timeout(() => {
200
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'bar');
201
+ }, duration);
202
+ }, 'bar');
203
+ }, duration);
204
+ chai_1.assert.equal(correlationIdService_1.correlationIdService.getId(), 'foo');
205
+ }, 'foo');
206
+ });
207
+ });
208
+ function setSlowThreshold(context, expectedTestDuration) {
209
+ // Cf. https://mochajs.org/#test-duration
210
+ // 10: budgeted test case own processing time
211
+ // ×2: for the estimation to sit around "slow/2" in Mocha scale (and no warning shows up)
212
+ context.slow((expectedTestDuration + 10) * 2);
213
+ }
214
+ });
215
+ //# sourceMappingURL=correlationIdService.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"correlationIdService.test.js","sourceRoot":"","sources":["../../../src/services/correlationIdService.test.ts"],"names":[],"mappings":";AAAA,sCAAsC;;AAEtC,+BAA8B;AAC9B,mCAAsC;AACtC,0EAA0E;AAC1E,iEAA8D;AAE9D,IAAA,gDAAwB,GAAE,CAAC;AAE3B,MAAM,WAAW,GAAW,gEAAgE,CAAC;AAE7F,KAAK,UAAU,OAAO,CAAC,IAA2B,EAAE,KAAa,EAAE;IACjE,OAAO,IAAI,OAAO,CAAO,OAAO,CAAC,EAAE,CACjC,UAAU,CAAC,GAAG,EAAE;QACd,IAAI,EAAE,CAAC;QACP,OAAO,EAAE,CAAC;IACZ,CAAC,EAAE,EAAE,CAAC,CACP,CAAC;AACJ,CAAC;AAED,QAAQ,CAAC,wBAAwB,EAAE,GAAG,EAAE;IACtC,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;QAC3B,EAAE,CAAC,oBAAoB,EAAE,GAAG,EAAE;YAC5B,MAAM,GAAG,GAAG,2CAAoB,CAAC,WAAW,EAAE,CAAC;YAC/C,aAAM,CAAC,KAAK,CAAC,GAAG,EAAE,WAAW,CAAC,CAAC;QACjC,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,QAAQ,CAAC,mBAAmB,EAAE,GAAG,EAAE;QACjC,EAAE,CAAC,oBAAoB,EAAE,GAAG,EAAE;YAC5B,QAAQ;YACR,MAAM,cAAc,GAAW,GAAG,CAAC;YAEnC,OAAO;YACP,MAAM,MAAM,GAAG,2CA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BACjB,aAAM,CAAC,KAAK,CAAC,2CAAoB,CAAC,KAAK,EAAE,EAAE,KAAK,CAAC,CAAC;wBACpD,CAAC,EAAE,QAAQ,CAAC,CAAC;oBACf,CAAC,EAAE,KAAK,CAAC,CAAC;gBACZ,CAAC,EAAE,QAAQ,CAAC,CAAC;gBACb,aAAM,CAAC,KAAK,CAAC,2CAAoB,CAAC,KAAK,EAAE,EAAE,KAAK,CAAC,CAAC;YACpD,CAAC,EAAE,KAAK,CAAC,CAAC;QACZ,CAAC,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,SAAS,gBAAgB,CAAC,OAAsB,EAAE,oBAA4B;QAC5E,yCAAyC;QACzC,6CAA6C;QAC7C,yFAAyF;QACzF,OAAO,CAAC,IAAI,CAAC,CAAC,oBAAoB,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;IAChD,CAAC;AACH,CAAC,CAAC,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { ILogger } from '@villedemontreal/logger';
2
+ /**
3
+ * Creates a Logger.
4
+ */
5
+ export declare function createLogger(name: string): ILogger;
6
+ /**
7
+ * A Logger that uses a dummy cid provider.
8
+ *
9
+ * Only use this when running the tests!
10
+ */
11
+ export declare function getTestingLoggerCreator(): (name: string) => ILogger;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getTestingLoggerCreator = exports.createLogger = void 0;
4
+ const logger_1 = require("@villedemontreal/logger");
5
+ const configs_1 = require("../config/configs");
6
+ let testingLoggerLibInitialised = false;
7
+ /**
8
+ * Creates a Logger.
9
+ */
10
+ function createLogger(name) {
11
+ // ==========================================
12
+ // We use a LazyLogger so the real Logger
13
+ // is only created when the first
14
+ // log is actually performed... At that point,
15
+ // our "configs.loggerCreator" configuration
16
+ // must have been set by the code using our library!
17
+ //
18
+ // This pattern allows calling code to import
19
+ // modules from us in which a logger is
20
+ // created in the global scope :
21
+ //
22
+ // let logger = createLogger('someName');
23
+ //
24
+ // Without a Lazy Logger, the library configurations
25
+ // would at that moment *not* have been set yet
26
+ // (by the calling code) and an Error would be thrown
27
+ // because the "configs.loggerCreator" is required.
28
+ // ==========================================
29
+ return new logger_1.LazyLogger(name, (nameArg) => {
30
+ return configs_1.configs.loggerCreator(nameArg);
31
+ });
32
+ }
33
+ exports.createLogger = createLogger;
34
+ function initTestingLoggerConfigs() {
35
+ const loggerConfig = new logger_1.LoggerConfigs(() => 'test-cid');
36
+ loggerConfig.setLogLevel(logger_1.LogLevel.DEBUG);
37
+ (0, logger_1.initLogger)(loggerConfig);
38
+ }
39
+ /**
40
+ * A Logger that uses a dummy cid provider.
41
+ *
42
+ * Only use this when running the tests!
43
+ */
44
+ function getTestingLoggerCreator() {
45
+ return (name) => {
46
+ if (!testingLoggerLibInitialised) {
47
+ initTestingLoggerConfigs();
48
+ testingLoggerLibInitialised = true;
49
+ }
50
+ return new logger_1.Logger(name);
51
+ };
52
+ }
53
+ exports.getTestingLoggerCreator = getTestingLoggerCreator;
54
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../../../src/utils/logger.ts"],"names":[],"mappings":";;;AAAA,oDAA2G;AAC3G,+CAA4C;AAE5C,IAAI,2BAA2B,GAAG,KAAK,CAAC;AAExC;;GAEG;AACH,SAAgB,YAAY,CAAC,IAAY;IACvC,6CAA6C;IAC7C,yCAAyC;IACzC,iCAAiC;IACjC,8CAA8C;IAC9C,4CAA4C;IAC5C,oDAAoD;IACpD,EAAE;IACF,6CAA6C;IAC7C,uCAAuC;IACvC,gCAAgC;IAChC,EAAE;IACF,yCAAyC;IACzC,EAAE;IACF,oDAAoD;IACpD,+CAA+C;IAC/C,qDAAqD;IACrD,mDAAmD;IACnD,6CAA6C;IAC7C,OAAO,IAAI,mBAAU,CAAC,IAAI,EAAE,CAAC,OAAe,EAAE,EAAE;QAC9C,OAAO,iBAAO,CAAC,aAAa,CAAC,OAAO,CAAC,CAAC;IACxC,CAAC,CAAC,CAAC;AACL,CAAC;AAtBD,oCAsBC;AAED,SAAS,wBAAwB;IAC/B,MAAM,YAAY,GAAkB,IAAI,sBAAa,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,CAAC;IACxE,YAAY,CAAC,WAAW,CAAC,iBAAQ,CAAC,KAAK,CAAC,CAAC;IACzC,IAAA,mBAAU,EAAC,YAAY,CAAC,CAAC;AAC3B,CAAC;AAED;;;;GAIG;AACH,SAAgB,uBAAuB;IACrC,OAAO,CAAC,IAAY,EAAW,EAAE;QAC/B,IAAI,CAAC,2BAA2B,EAAE;YAChC,wBAAwB,EAAE,CAAC;YAC3B,2BAA2B,GAAG,IAAI,CAAC;SACpC;QAED,OAAO,IAAI,eAAM,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC,CAAC;AACJ,CAAC;AATD,0DASC"}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Call this when your need to set
3
+ * *Testing* configurations to the current
4
+ * library, without the need for a calling code
5
+ * to do so.
6
+ *
7
+ * A test Correlation Id will be used!
8
+ */
9
+ export declare function setTestingConfigurations(): void;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.setTestingConfigurations = void 0;
4
+ const init_1 = require("../config/init");
5
+ const logger_1 = require("../utils/logger");
6
+ /**
7
+ * Call this when your need to set
8
+ * *Testing* configurations to the current
9
+ * library, without the need for a calling code
10
+ * to do so.
11
+ *
12
+ * A test Correlation Id will be used!
13
+ */
14
+ function setTestingConfigurations() {
15
+ (0, init_1.init)((0, logger_1.getTestingLoggerCreator)());
16
+ }
17
+ exports.setTestingConfigurations = setTestingConfigurations;
18
+ //# sourceMappingURL=testingConfigurations.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testingConfigurations.js","sourceRoot":"","sources":["../../../src/utils/testingConfigurations.ts"],"names":[],"mappings":";;;AAAA,yCAAsC;AACtC,4CAA0D;AAE1D;;;;;;;GAOG;AACH,SAAgB,wBAAwB;IACtC,IAAA,WAAI,EAAC,IAAA,gCAAuB,GAAE,CAAC,CAAC;AAClC,CAAC;AAFD,4DAEC"}
package/package.json ADDED
@@ -0,0 +1,62 @@
1
+ {
2
+ "name": "@villedemontreal/correlation-id",
3
+ "version": "5.3.2",
4
+ "description": "Express middleware to set a correlation in Express. The correlation id will be consistent across async calls within the handling of a request.",
5
+ "main": "dist/src/index.js",
6
+ "typings": "dist/src",
7
+ "files": [
8
+ "src",
9
+ "dist"
10
+ ],
11
+ "scripts": {
12
+ "start": "node run test",
13
+ "test": "mocha --require ts-node/register src/**/*.test.ts",
14
+ "compile": "node run compile",
15
+ "lint": "echo 'not yet implemented'",
16
+ "lint-fix": "echo 'not yet implemented'",
17
+ "prettier": "echo 'not yet implemented'",
18
+ "prettier-fix": "echo 'not yet implemented'",
19
+ "watch": "echo 'not yet implemented'"
20
+ },
21
+ "keywords": [
22
+ "montreal",
23
+ "cid",
24
+ "correlation ID",
25
+ "request",
26
+ "debug"
27
+ ],
28
+ "author": "Ville de Montréal",
29
+ "license": "MIT",
30
+ "dependencies": {
31
+ "@types/app-root-path": "1.2.4",
32
+ "@types/cls-hooked": "4.3.3",
33
+ "@types/lodash": "4.14.182",
34
+ "@types/uuid": "8.3.4",
35
+ "@villedemontreal/logger": "6.5.6",
36
+ "app-root-path": "3.0.0",
37
+ "cls-hooked": "4.2.2",
38
+ "http-header-fields-typed": "1.3.0",
39
+ "lodash": "4.17.21",
40
+ "semver": "7.3.7",
41
+ "uuid": "8.3.2"
42
+ },
43
+ "devDependencies": {
44
+ "@types/chai": "4.3.1",
45
+ "@types/express": "4.17.13",
46
+ "@types/fs-extra": "9.0.13",
47
+ "@types/mocha": "9.1.1",
48
+ "@types/semver": "7.3.10",
49
+ "@types/supertest": "2.0.12",
50
+ "@villedemontreal/scripting": "2.1.5",
51
+ "@villedemontreal/lint-config": "1.7.7",
52
+ "chai": "4.3.6",
53
+ "express": "4.18.1",
54
+ "fs-extra": "10.1.0",
55
+ "mocha": "9.2.2",
56
+ "mocha-jenkins-reporter": "0.4.7",
57
+ "supertest": "6.2.3",
58
+ "tslint": "6.1.2",
59
+ "ts-node": "^10.8.1",
60
+ "typescript": "4.7.4"
61
+ }
62
+ }
@@ -0,0 +1,26 @@
1
+ import { ILogger } from '@villedemontreal/logger';
2
+
3
+ /**
4
+ * Http Client Config
5
+ */
6
+ export class Configs {
7
+ private loggerCreatorVar: (name: string) => ILogger;
8
+
9
+ /**
10
+ * Sets the Logger creator.
11
+ */
12
+ public setLoggerCreator(loggerCreator: (name: string) => ILogger) {
13
+ this.loggerCreatorVar = loggerCreator;
14
+ }
15
+
16
+ /**
17
+ * The Logger creator
18
+ */
19
+ get loggerCreator(): (name: string) => ILogger {
20
+ if (!this.loggerCreatorVar) {
21
+ throw new Error(`The Logger Creator HAS to be set as a configuration! Please call the init(...) fonction first.`);
22
+ }
23
+ return this.loggerCreatorVar;
24
+ }
25
+ }
26
+ export let configs: Configs = new Configs();
@@ -0,0 +1,40 @@
1
+ import { path as appRoot } from 'app-root-path';
2
+ import * as path from 'path';
3
+
4
+ /**
5
+ * Library constants
6
+ */
7
+ export class Constants {
8
+ /**
9
+ * The library root. When this library is used
10
+ * as a dependency in a project, the "libRoot"
11
+ * will be the path to the dependency folder,
12
+ * inside the "node_modules".
13
+ */
14
+ public libRoot: string;
15
+
16
+ /**
17
+ * The app root. When this library is used
18
+ * as a dependency in a project, the "appRoot"
19
+ * will be the path to the root project!
20
+ */
21
+ public appRoot: string;
22
+
23
+ constructor() {
24
+ // From the "dist/src/config" folder
25
+ this.libRoot = path.normalize(__dirname + '/../../..');
26
+ this.appRoot = appRoot;
27
+ }
28
+
29
+ /**
30
+ * Extra values that we can add to the original Express request.
31
+ */
32
+ get requestExtraVariables() {
33
+ return {
34
+ cidReceivedInRequest: '_cidReceivedInRequest',
35
+ cidNew: '_cidNew'
36
+ };
37
+ }
38
+ }
39
+
40
+ export let constants: Constants = new Constants();
@@ -0,0 +1,33 @@
1
+ import { ILogger } from '@villedemontreal/logger';
2
+ import { configs } from './configs';
3
+
4
+ let libIsInited: boolean = false;
5
+
6
+ /**
7
+ * Inits the library.
8
+ */
9
+ export function init(loggerCreator: (name: string) => ILogger): void {
10
+ if (!loggerCreator) {
11
+ throw new Error(`The Logger Creator is required.`);
12
+ }
13
+ configs.setLoggerCreator(loggerCreator);
14
+
15
+ // ==========================================
16
+ // Set as being "properly initialized".
17
+ // At the very end of the "init()" function!
18
+ // ==========================================
19
+ libIsInited = true;
20
+ }
21
+
22
+ /**
23
+ * Is the library properly initialized?
24
+ *
25
+ * This function MUST be named "isInited()"!
26
+ * Code using this library may loop over all its "@villemontreal"
27
+ * dependencies and, if one of those exports a "isInited" fonction,
28
+ * it will enforce that the lib has been properly initialized before
29
+ * starting...
30
+ */
31
+ export function isInited(): boolean {
32
+ return libIsInited;
33
+ }
package/src/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from './services/correlationIdService';
2
+ export * from './middleware/correlationIdMiddleware';
3
+
4
+ // ==========================================
5
+ // We do not export the configs instance itself,
6
+ // only the "init()" method, so we can define
7
+ // required parameters.
8
+ // ==========================================
9
+ export * from './config/init';