@@ -6,41 +6,56 @@ const onFinished = require('on-finished');
66const template = require ( './src/templates/default' ) ;
77const sendMessage = require ( './src/sendMessage' ) ;
88const { 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 } ) ;
0 commit comments