-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.js
93 lines (86 loc) · 3.01 KB
/
utilities.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const https = require('https');
const Joi = require('@hapi/joi');
const empty = require('is-empty');
module.exports = {
// eslint-disable-next-line object-shorthand
customError: function (message, errorCode) { // eslint-disable-line func-names
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message || 'The requested resource couldn\'t be found';
this.errorCode = errorCode || 404;
},
// eslint-disable-next-line arrow-body-style
validateBody: (schema) => {
return (req, res, next) => {
const result = Joi.validate(req.body, schema, { abortEarly: false });
if (result.error) {
return next(result.error);
}
req.body = result.value;
return next();
};
},
// eslint-disable-next-line arrow-body-style
validateQuery: (schema) => {
return (req, res, next) => {
const result = Joi.validate(req.query, schema, { abortEarly: false });
if (result.error) {
return next(result.error);
}
req.query = result.value;
return next();
};
},
// eslint-disable-next-line arrow-body-style
validateParams: (schema) => {
return (req, res, next) => {
const result = Joi.validate(req.params, schema, { abortEarly: false });
if (result.error) {
return next(result.error);
}
req.params = result.value;
return next();
};
},
// eslint-disable-next-line arrow-body-style
notification: (config) => {
return {
// eslint-disable-next-line arrow-body-style
sendSlackNotification: (channelId, message) => {
return new Promise((resolve, reject) => {
try {
const notificationsConfig = config && config.notifications;
if (empty(notificationsConfig)) {
throw new Error('Notification Config is Missing');
}
const { slackHostName, slackToken } = notificationsConfig;
if (empty(slackHostName) || empty(slackToken)) {
throw new Error('Slack Host Name & Slack Token is required');
}
if (empty(channelId) || empty(message)) {
throw new Error('channelId Or message is Missing');
}
const options = {
hostname: slackHostName,
port: 443,
path: `/api/chat.postMessage?token=${slackToken}&channel=${channelId}&username=elnotifier&text=${encodeURI(message)}`,
method: 'POST',
};
const req = https.request(options, (response) => {
// Continuously update stream with data
let body = '';
// eslint-disable-next-line no-return-assign
response.on('data', d => body += d);
// Data reception is done, do whatever with it!
response.on('end', () => resolve(body));
});
req.on('error', error => reject(error));
req.end();
} catch (error) {
reject(error);
}
});
},
};
},
};