@hvedinich/utils 0.0.61 → 0.0.63
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hvedinich/utils",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.63",
|
|
4
4
|
"description": "utils module",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"axios": "^0.27.2",
|
|
22
22
|
"express": "4.17.1",
|
|
23
23
|
"graphql-playground-middleware-express": "1.7.20",
|
|
24
|
-
"jsonwebtoken": "8.5.1"
|
|
24
|
+
"jsonwebtoken": "8.5.1",
|
|
25
|
+
"pino": "^9.5.0"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|
|
27
28
|
"eslint": "^7.2.0",
|
package/src/log/index.js
CHANGED
|
@@ -1,25 +1,49 @@
|
|
|
1
|
-
const
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
if (ctx
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
const pino = require('pino');
|
|
2
|
+
|
|
3
|
+
const ctxToString = ctx => {
|
|
4
|
+
if (!ctx) return { uid: 'unknown' };
|
|
5
|
+
return ctx.uid ? { uid: ctx.uid, permission: ctx.permission } : { uid: 'unknown' };
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
const PinoLevelToSeverityLookup = {
|
|
9
|
+
debug: 'DEBUG',
|
|
10
|
+
info: 'INFO',
|
|
11
|
+
error: 'ERROR',
|
|
8
12
|
};
|
|
9
13
|
module.exports = class Logger {
|
|
10
|
-
constructor(name) {
|
|
11
|
-
this.
|
|
14
|
+
constructor(name, environment) {
|
|
15
|
+
this.logger = pino({
|
|
16
|
+
name,
|
|
17
|
+
mixin() {
|
|
18
|
+
return { environment };
|
|
19
|
+
},
|
|
20
|
+
level: 'debug',
|
|
21
|
+
timestamp: pino.stdTimeFunctions.isoTime,
|
|
22
|
+
formatters: {
|
|
23
|
+
level(label, number) {
|
|
24
|
+
return {
|
|
25
|
+
severity: PinoLevelToSeverityLookup[label] || PinoLevelToSeverityLookup.info,
|
|
26
|
+
level: number,
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
log: object => {
|
|
30
|
+
const ctx = ctxToString(object?.ctx);
|
|
31
|
+
|
|
32
|
+
return { ...object, ctx };
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
});
|
|
12
36
|
}
|
|
13
37
|
|
|
14
|
-
error(str, ctx) {
|
|
15
|
-
|
|
38
|
+
error(str, ctx, fields) {
|
|
39
|
+
this.logger.error({ ctx, fields }, str);
|
|
16
40
|
}
|
|
17
41
|
|
|
18
|
-
info(str, ctx) {
|
|
19
|
-
|
|
42
|
+
info(str, ctx, fields) {
|
|
43
|
+
this.logger.info({ ctx, fields }, str);
|
|
20
44
|
}
|
|
21
45
|
|
|
22
|
-
debug(str, ctx) {
|
|
23
|
-
|
|
46
|
+
debug(str, ctx, fields) {
|
|
47
|
+
this.logger.debug({ ctx, fields }, str);
|
|
24
48
|
}
|
|
25
49
|
};
|
package/src/server/index.js
CHANGED
|
@@ -2,7 +2,10 @@ const express = require('express');
|
|
|
2
2
|
const expressPlayground = require('graphql-playground-middleware-express').default;
|
|
3
3
|
const axios = require('axios');
|
|
4
4
|
const { ApolloServer, ApolloError } = require('apollo-server-express');
|
|
5
|
-
const {
|
|
5
|
+
const {
|
|
6
|
+
guestCtx,
|
|
7
|
+
token: { EXPIRED, UNAUTHORIZED },
|
|
8
|
+
} = require('../constants');
|
|
6
9
|
const { addContext } = require('../utils/contextUtils');
|
|
7
10
|
|
|
8
11
|
const logError = (err, logger = console) => {
|
|
@@ -11,15 +14,21 @@ const logError = (err, logger = console) => {
|
|
|
11
14
|
description.path = err.path && err.path[0] ? err.path[0] : 'unknown';
|
|
12
15
|
description.message = err.message;
|
|
13
16
|
description.serviceName = err.extensions.serviceName;
|
|
14
|
-
description.stack =
|
|
15
|
-
&& err.extensions.exception.stacktrace
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
description.stack =
|
|
18
|
+
err.extensions && err.extensions.exception && err.extensions.exception.stacktrace
|
|
19
|
+
? JSON.stringify(err.extensions.exception.stacktrace)
|
|
20
|
+
: 'unknown';
|
|
18
21
|
} finally {
|
|
19
|
-
logger.error(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
logger.error(
|
|
23
|
+
`Server error`,
|
|
24
|
+
{},
|
|
25
|
+
{
|
|
26
|
+
path: description.path,
|
|
27
|
+
message: description.message,
|
|
28
|
+
serviceName: description.serviceName,
|
|
29
|
+
stack: description.stack,
|
|
30
|
+
},
|
|
31
|
+
);
|
|
23
32
|
}
|
|
24
33
|
};
|
|
25
34
|
|
|
@@ -44,9 +53,7 @@ class Server {
|
|
|
44
53
|
this.app.get('/_status', healthCheck || defaultCHealthCheck);
|
|
45
54
|
}
|
|
46
55
|
|
|
47
|
-
initGQL({
|
|
48
|
-
gateway, schema, context, options, logger, errorHandler,
|
|
49
|
-
}) {
|
|
56
|
+
initGQL({ gateway, schema, context, options, logger, errorHandler }) {
|
|
50
57
|
if (!gateway && !schema) {
|
|
51
58
|
console.error('Schema not found');
|
|
52
59
|
return;
|
|
@@ -54,7 +61,7 @@ class Server {
|
|
|
54
61
|
|
|
55
62
|
const serverOptions = {
|
|
56
63
|
context: context || defaultContext,
|
|
57
|
-
formatError:
|
|
64
|
+
formatError: err => {
|
|
58
65
|
logError(err, logger);
|
|
59
66
|
if (errorHandler) return errorHandler(err);
|
|
60
67
|
if (err.name === 'TokenExpiredError') {
|
|
@@ -67,8 +74,11 @@ class Server {
|
|
|
67
74
|
return new ApolloError(err.extensions.sanitized, err.extensions.code, err.extensions);
|
|
68
75
|
}
|
|
69
76
|
if (err.extensions && err.extensions.exception && err.extensions.exception.sanitized) {
|
|
70
|
-
return new ApolloError(
|
|
71
|
-
err.extensions.exception.
|
|
77
|
+
return new ApolloError(
|
|
78
|
+
err.extensions.exception.sanitized,
|
|
79
|
+
err.extensions.exception.code,
|
|
80
|
+
err.extensions.exception,
|
|
81
|
+
);
|
|
72
82
|
}
|
|
73
83
|
if (err.name === 'GraphQLError' || err.name === 'ValidationError') {
|
|
74
84
|
return new ApolloError(err.message, err.name);
|
|
@@ -94,9 +104,7 @@ class Server {
|
|
|
94
104
|
}
|
|
95
105
|
|
|
96
106
|
addEndpoints(endpoints) {
|
|
97
|
-
endpoints.forEach(({
|
|
98
|
-
path, method, handler, proxy,
|
|
99
|
-
}) => {
|
|
107
|
+
endpoints.forEach(({ path, method, handler, proxy }) => {
|
|
100
108
|
if (proxy) {
|
|
101
109
|
this.app.use(path, proxy);
|
|
102
110
|
} else {
|
|
@@ -115,8 +123,8 @@ const waitServices = async (services = {}, logger = console) => {
|
|
|
115
123
|
while (shouldWait) {
|
|
116
124
|
try {
|
|
117
125
|
// eslint-disable-next-line no-await-in-loop
|
|
118
|
-
const statuses = await Promise.all(
|
|
119
|
-
.map(async ({ wait, name, url }) => {
|
|
126
|
+
const statuses = await Promise.all(
|
|
127
|
+
Object.values(services).map(async ({ wait, name, url }) => {
|
|
120
128
|
if (!wait) {
|
|
121
129
|
logger.info(`${name}: ready "true"`);
|
|
122
130
|
return true;
|
|
@@ -128,15 +136,16 @@ const waitServices = async (services = {}, logger = console) => {
|
|
|
128
136
|
}
|
|
129
137
|
logger.info(`${name}: ready "false"`);
|
|
130
138
|
return false;
|
|
131
|
-
})
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
132
141
|
if (statuses.every(Boolean)) {
|
|
133
142
|
shouldWait = false;
|
|
134
143
|
}
|
|
135
|
-
// eslint-disable-next-line no-await-in-loop
|
|
144
|
+
// eslint-disable-next-line no-await-in-loop, no-promise-executor-return
|
|
136
145
|
await new Promise(res => setTimeout(res, 5000));
|
|
137
146
|
} catch (err) {
|
|
138
147
|
logger.debug(err);
|
|
139
|
-
// eslint-disable-next-line no-await-in-loop
|
|
148
|
+
// eslint-disable-next-line no-await-in-loop, no-promise-executor-return
|
|
140
149
|
await new Promise(res => setTimeout(res, 5000));
|
|
141
150
|
}
|
|
142
151
|
}
|
|
@@ -149,10 +158,14 @@ let isHealthyQueue = true;
|
|
|
149
158
|
*/
|
|
150
159
|
const checkIsAlive = () => isHealthyQueue;
|
|
151
160
|
|
|
152
|
-
const setQueueHealthCheck =
|
|
161
|
+
const setQueueHealthCheck = value => {
|
|
153
162
|
isHealthyQueue = value;
|
|
154
163
|
};
|
|
155
164
|
|
|
156
165
|
module.exports = {
|
|
157
|
-
Server,
|
|
166
|
+
Server,
|
|
167
|
+
waitServices,
|
|
168
|
+
express,
|
|
169
|
+
checkIsAlive,
|
|
170
|
+
setQueueHealthCheck,
|
|
158
171
|
};
|
|
@@ -76,7 +76,7 @@ describe('buildDBModel', () => {
|
|
|
76
76
|
const removeResult = { deletedCount: 1 };
|
|
77
77
|
mockSchema.deleteOne.mockResolvedValue(removeResult);
|
|
78
78
|
|
|
79
|
-
const result = await dbModel.remove(mockId);
|
|
79
|
+
const result = await dbModel.remove({ id: mockId });
|
|
80
80
|
expect(result).toEqual(removeResult);
|
|
81
81
|
expect(mockSchema.deleteOne).toHaveBeenCalledWith({ id: mockId });
|
|
82
82
|
});
|
|
@@ -39,7 +39,7 @@ const buildDBModel = schema => {
|
|
|
39
39
|
* @param {String} id - The id of the document to remove.
|
|
40
40
|
* @returns {Promise<Object>} - The result of the remove operation.
|
|
41
41
|
*/
|
|
42
|
-
const remove =
|
|
42
|
+
const remove = filter => schema.deleteOne(filter);
|
|
43
43
|
|
|
44
44
|
/**
|
|
45
45
|
* Counts the number of documents matching a filter.
|