@autofleet/node-common 1.0.8 → 1.0.9
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.
- package/.gitlab-ci.yml +13 -0
- package/.jest.config.js +8 -0
- package/logger/index.js +30 -21
- package/logger/index.test.js +100 -0
- package/network/index.js +1 -1
- package/network/index.test.js +31 -0
- package/package.json +13 -3
- /package/logger/{test.js → example.js} +0 -0
package/.gitlab-ci.yml
ADDED
package/.jest.config.js
ADDED
package/logger/index.js
CHANGED
|
@@ -1,31 +1,40 @@
|
|
|
1
|
+
require('dotenv').config();
|
|
1
2
|
const winston = require('winston')
|
|
3
|
+
const { env } = process
|
|
2
4
|
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
+
const getLevel = (logLevel) => {
|
|
6
|
+
if (logLevel) return logLevel
|
|
7
|
+
if (env.LOG_LEVEL) return env.LOG_LEVEL
|
|
8
|
+
if (env.NODE_ENV === 'development') return 'debug'
|
|
9
|
+
// test and producion will return info
|
|
10
|
+
return 'info'
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const getTransporters = () => {
|
|
14
|
+
let transporters = [new winston.transports.Console()]
|
|
15
|
+
if (env.USE_LOG_FILES === 'true') {
|
|
16
|
+
transporters = transporters.concat([
|
|
17
|
+
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
|
|
18
|
+
new winston.transports.File({ filename: 'logs/all.log' })
|
|
19
|
+
])
|
|
20
|
+
}
|
|
21
|
+
return transporters
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const createLogger = (level, transporters) => {
|
|
5
25
|
const logger = winston.createLogger({
|
|
6
26
|
level,
|
|
7
27
|
format: winston.format.simple(),
|
|
8
|
-
transports:
|
|
9
|
-
//
|
|
10
|
-
// - Write to all logs with level `info` and below to `combined.log`
|
|
11
|
-
// - Write all logs error (and below) to `error.log`.
|
|
12
|
-
//
|
|
13
|
-
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
|
|
14
|
-
new winston.transports.File({ filename: 'logs/combined.log' })
|
|
15
|
-
]
|
|
28
|
+
transports: transporters
|
|
16
29
|
});
|
|
17
30
|
|
|
18
|
-
//
|
|
19
|
-
// If we're not in production then log to the `console` with the format:
|
|
20
|
-
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
|
|
21
|
-
//
|
|
22
|
-
if (process.env.NODE_ENV !== 'production') {
|
|
23
|
-
logger.add(new winston.transports.Console({
|
|
24
|
-
format: winston.format.simple()
|
|
25
|
-
}));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
31
|
return logger
|
|
29
32
|
}
|
|
30
33
|
|
|
31
|
-
|
|
34
|
+
const getLogger = (logLevel) => {
|
|
35
|
+
const level = getLevel(logLevel)
|
|
36
|
+
const transporters = getTransporters()
|
|
37
|
+
return createLogger(level, transporters)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = getLogger
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
const Logger = require('./index')
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const wait = require('wait-promise');
|
|
4
|
+
|
|
5
|
+
const { env } = process
|
|
6
|
+
|
|
7
|
+
const waitForFileToHaveContent = (filePath) => {
|
|
8
|
+
return wait.limit(30).until(() => {
|
|
9
|
+
if(fs.existsSync(filePath)) {
|
|
10
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
11
|
+
return content != ''
|
|
12
|
+
}
|
|
13
|
+
return false
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
describe('Logger', () => {
|
|
18
|
+
it('it`s default level is info', () => {
|
|
19
|
+
env.NODE_ENV=''
|
|
20
|
+
const logger = Logger()
|
|
21
|
+
expect(logger).toBeDefined();
|
|
22
|
+
expect(logger.level).toBe('info');
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
it('its default develpment level is debug', () => {
|
|
26
|
+
env.NODE_ENV='development'
|
|
27
|
+
const logger = Logger()
|
|
28
|
+
expect(logger.level).toBe('debug');
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
it('its default test level is info', () => {
|
|
32
|
+
env.NODE_ENV='test'
|
|
33
|
+
const logger = Logger()
|
|
34
|
+
expect(logger.level).toBe('info');
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
it('its default production level is info', () => {
|
|
38
|
+
env.NODE_ENV='production'
|
|
39
|
+
const logger = Logger()
|
|
40
|
+
expect(logger.level).toBe('info');
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('its level can be override by LOG_LEVEL env variable', () => {
|
|
44
|
+
env.NODE_ENV='development'
|
|
45
|
+
env.LOG_LEVEL='warn'
|
|
46
|
+
const logger = Logger()
|
|
47
|
+
expect(logger.level).toBe('warn');
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('its level can be override by function variable', () => {
|
|
51
|
+
env.NODE_ENV='production'
|
|
52
|
+
env.LOG_LEVEL='warn'
|
|
53
|
+
const logger = Logger('silly')
|
|
54
|
+
expect(logger.level).toBe('silly');
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
it('its has functions for each level', () => {
|
|
58
|
+
const logger = Logger()
|
|
59
|
+
expect(logger.error).toBeFunction()
|
|
60
|
+
expect(logger.warn).toBeFunction()
|
|
61
|
+
expect(logger.info).toBeFunction()
|
|
62
|
+
expect(logger.verbose).toBeFunction()
|
|
63
|
+
expect(logger.debug).toBeFunction()
|
|
64
|
+
expect(logger.silly).toBeFunction()
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('it can have general file transporters', async () => {
|
|
68
|
+
env.USE_LOG_FILES='true'
|
|
69
|
+
const allPath = 'logs/all.log'
|
|
70
|
+
fs.unlinkSync(allPath)
|
|
71
|
+
const logger = Logger()
|
|
72
|
+
const errorText = "this is error"
|
|
73
|
+
logger.error(errorText)
|
|
74
|
+
|
|
75
|
+
await waitForFileToHaveContent(allPath)
|
|
76
|
+
|
|
77
|
+
expect(fs.existsSync(allPath)).toBe(true)
|
|
78
|
+
|
|
79
|
+
const contents = fs.readFileSync(allPath, 'utf8');
|
|
80
|
+
expect(contents).toBe(`error: ${errorText}\n`)
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
it('it can have general file transporters', async () => {
|
|
84
|
+
env.USE_LOG_FILES='true'
|
|
85
|
+
|
|
86
|
+
const errorPath = 'logs/error.log'
|
|
87
|
+
fs.unlinkSync(errorPath)
|
|
88
|
+
expect(fs.existsSync(errorPath)).toBe(false)
|
|
89
|
+
|
|
90
|
+
const logger = Logger()
|
|
91
|
+
const errorText = "this is error"
|
|
92
|
+
logger.error(errorText)
|
|
93
|
+
|
|
94
|
+
await waitForFileToHaveContent(errorPath)
|
|
95
|
+
|
|
96
|
+
const contents = fs.readFileSync(errorPath, 'utf8');
|
|
97
|
+
expect(contents).toBe(`error: ${errorText}\n`)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
});
|
package/network/index.js
CHANGED
|
@@ -32,7 +32,7 @@ module.exports = class Network {
|
|
|
32
32
|
}
|
|
33
33
|
|
|
34
34
|
validate() {
|
|
35
|
-
if (!this.settings.serviceUrl && !
|
|
35
|
+
if (!this.settings.serviceUrl && !this.settings.serviceName) {
|
|
36
36
|
throw new Error('At least one of the settings Missing serviceUrl or serviceName')
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const nock = require('nock');
|
|
2
|
+
const Network = require('./index');
|
|
3
|
+
|
|
4
|
+
nock('http://www.google.com')
|
|
5
|
+
.get('/resource')
|
|
6
|
+
.reply(200, { one: 1 })
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
nock('https://www.apple.com')
|
|
10
|
+
.get('/resource')
|
|
11
|
+
.reply(200, { two: 2 })
|
|
12
|
+
|
|
13
|
+
describe('Network', () => {
|
|
14
|
+
it('Making requests by service name from ENV', async () => {
|
|
15
|
+
process.env.TEST_SERVICE_HOST = 'www.google.com'
|
|
16
|
+
n = new Network({ serviceName: 'TEST' });
|
|
17
|
+
const response = await n.get('/resource');
|
|
18
|
+
expect(response).toBeDefined();
|
|
19
|
+
expect(response.data).toBeDefined();
|
|
20
|
+
expect(response.data.one).toBe(1);
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('Making requests by service name from ENV', async () => {
|
|
24
|
+
process.env.TEST_SERVICE_HOST = 'www.google.com'
|
|
25
|
+
n = new Network({ serviceUrl: 'https://www.apple.com' });
|
|
26
|
+
const response = await n.get('/resource');
|
|
27
|
+
expect(response).toBeDefined();
|
|
28
|
+
expect(response.data).toBeDefined();
|
|
29
|
+
expect(response.data.two).toBe(2);
|
|
30
|
+
})
|
|
31
|
+
});
|
package/package.json
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autofleet/node-common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.9",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
6
5
|
"scripts": {
|
|
7
|
-
"
|
|
6
|
+
"coverage": "jest --coverage --forceExit --runInBand",
|
|
7
|
+
"test": "jest --forceExit --runInBand",
|
|
8
|
+
"test-auto": "jest --watch --runInBand"
|
|
9
|
+
},
|
|
10
|
+
"jest": {
|
|
11
|
+
"setupTestFrameworkScriptFile": "jest-extended"
|
|
8
12
|
},
|
|
9
13
|
"repository": {
|
|
10
14
|
"type": "git",
|
|
@@ -19,6 +23,12 @@
|
|
|
19
23
|
"dependencies": {
|
|
20
24
|
"axios": "^0.18.0",
|
|
21
25
|
"dotenv": "^5.0.1",
|
|
26
|
+
"jest": "^22.4.3",
|
|
27
|
+
"wait-promise": "^0.4.1",
|
|
22
28
|
"winston": "^3.0.0-rc5"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"jest-extended": "^0.7.1",
|
|
32
|
+
"nock": "^9.2.5"
|
|
23
33
|
}
|
|
24
34
|
}
|
|
File without changes
|