Skip to content

Commit 7b51b0c

Browse files
committed
implemented options
1 parent 7d3654b commit 7b51b0c

File tree

3 files changed

+65
-26
lines changed

3 files changed

+65
-26
lines changed

index.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,56 @@ const onFinished = require('on-finished');
66
const template = require('./src/templates/default');
77
const sendMessage = require('./src/sendMessage');
88
const { printWarning } = require('./src/utils/print');
9+
const validateOptions = require('./src/utils/options');
10+
const { isError, is4xx, is5xx } = require('./src/utils/statusCode');
911

1012
/**
11-
* @param options Middleware's options
12-
* @param options.botToken Bot's token, sent by BotFather
13-
* @param options.chatId ChatID of a Telegram chat to send notifications to
13+
* @param {object} config Middleware's configuration
14+
* @param {String} config.botToken Bot's token, sent by BotFather
15+
* @param {Number} config.chatId ChatID of a Telegram chat to send notifications to
16+
* @param {object} opts Middleware's options
17+
* @param {boolean} opts.enable4xx If true enables 4xx status notifications
18+
* @param {boolean} opts.sound4xx If true enables 4xx status notifications sounds
19+
* @param {boolean} opts.enable5xx If true enables 5xx status notifications
20+
* @param {boolean} opts.sound5xx If true enables 5xx status notifications sounds
21+
* @param {Array} opts.exclude An array of statusCodes to exclude (this has the highest priority)
1422
*/
15-
const telegramMiddleware = (options) => (req, res, next) => {
16-
if (!options) options = {};
17-
const { botToken } = options;
18-
const { chatId } = options;
23+
const telegramMiddleware = (config, opts) => (req, res, next) => {
24+
if (!config) config = {};
25+
const { botToken, chatId } = config;
1926

2027
if (!botToken) printWarning('botToken not provided, errors get ignored');
2128
if (!chatId) printWarning('chatId not provided, errors get ignored');
2229

30+
let options = validateOptions(opts);
31+
2332
if (botToken && chatId) {
2433
onFinished(res, (err, _) => {
25-
if (err || res.statusCode >= 400) {
26-
const text = template(req, res, options);
27-
28-
let query = {
29-
text,
30-
chat_id: chatId,
31-
parse_mode: 'Markdown',
32-
};
33-
34-
// If statusCode is less than 500, then this error is not critical, we disable notification
35-
if (res.statusCode < 500) query.disable_notification = true;
36-
37-
query = querify(query);
38-
39-
try {
40-
sendMessage(botToken, query);
41-
} catch (e) {
42-
console.log(e);
43-
next(e);
34+
if ((is4xx(res) && options.enable4xx) || (is5xx(res) && options.enable5xx)) {
35+
if (!options.exclude.includes(res.statusCode)) {
36+
if (err || isError(res)) {
37+
const text = template(req, res, config);
38+
39+
let query = {
40+
text,
41+
chat_id: chatId,
42+
parse_mode: 'Markdown',
43+
};
44+
45+
// Set sound notification according to the options
46+
if ((is4xx(res) && !options.sound4xx) || (is5xx(res) && !options.sound5xx)) {
47+
query.disable_notification = true;
48+
}
49+
50+
query = querify(query);
51+
52+
try {
53+
sendMessage(botToken, query);
54+
} catch (e) {
55+
console.log(e);
56+
next(e);
57+
}
58+
}
4459
}
4560
}
4661
});

src/utils/options.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const validateOptions = (options) => {
2+
if (!options) options = {};
3+
4+
options = Object.assign({
5+
enable4xx: true,
6+
sound4xx: false,
7+
enable5xx: true,
8+
sound5xx: true,
9+
exclude: [],
10+
}, options);
11+
12+
if (!Array.isArray(options.exclude)) {
13+
options.exclude = [];
14+
}
15+
16+
return options;
17+
};
18+
19+
module.exports = validateOptions;

src/utils/statusCode.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const is4xx = (res) => res.statusCode >= 400 && res.statusCode < 500;
2+
const is5xx = (res) => res.statusCode >= 500;
3+
const isError = (res) => res.statusCode >= 400;
4+
5+
module.exports = { is4xx, is5xx, isError };

0 commit comments

Comments
 (0)