@@ -12,6 +12,7 @@ import { debug as Debug } from 'debug';
1212import { createServer } from 'http' ;
1313import { promisify } from 'util' ;
1414import { logger as log } from './logger' ;
15+ import { MESSAGES } from './messages' ;
1516
1617
1718let _config : any = { } ;
@@ -26,25 +27,25 @@ let jsonParser = promisify(bodyParser.json({ limit: '1mb' }));
2627 */
2728const requestHandler = ( request , response ) => {
2829
29- log . info ( `Request recived, ' ${ request . method } : ${ request . url } '` ) ;
30+ log . info ( MESSAGES . REQUEST_RECEIVED ) ;
3031 debug ( '_config' , _config ) ;
3132 // Explicitly remove or override the X-Powered-By header
3233 response . setHeader ( 'X-Powered-By' , '' ) ;
3334 return Promise . resolve ( ) . then ( ( ) => {
3435 // Should be a POST call.
3536 if ( request . method && request . method !== 'POST' ) {
36- debug ( 'Only POST call is supported.' ) ;
37+ debug ( MESSAGES . ONLY_POST_SUPPORTED ) ;
3738 return Promise . reject ( {
38- body : `Only POST call is supported.` ,
39+ body : `Only POST requests are supported.` ,
3940 statusCode : 400 ,
4041 statusMessage : 'Not allowed' ,
4142 } ) ;
4243 }
4344 } ) . then ( ( ) => {
4445 // validate endpoint
45- debug ( ` ${ request . url } invoked` ) ;
46+ debug ( MESSAGES . REQUEST_INVOKED ( request . url ) ) ;
4647 if ( _config && _config . listener && request . url !== _config . listener . endpoint ) {
47- debug ( 'url authentication failed' ) ;
48+ debug ( MESSAGES . URL_AUTH_FAILED ) ;
4849 return Promise . reject ( {
4950 body : `${ request . url } not found.` ,
5051 statusCode : 404 ,
@@ -53,12 +54,12 @@ const requestHandler = (request, response) => {
5354 }
5455 } ) . then ( ( ) => {
5556 // verify authorization
56- debug ( 'validating basic auth' , _config . listener ) ;
57+ debug ( MESSAGES . VALIDATING_BASIC_AUTH , _config . listener ) ;
5758 if ( _config && _config . listener && _config . listener . basic_auth ) {
58- debug ( 'validating basic auth' ) ;
59+ debug ( MESSAGES . VALIDATING_BASIC_AUTH ) ;
5960 const creds = BasicAuth ( request ) ;
6061 if ( ! creds || ( creds . name !== _config . listener . basic_auth . user || creds . pass !== _config . listener . basic_auth . pass ) ) {
61- debug ( 'basic auth failed' ) ;
62+ debug ( MESSAGES . BASIC_AUTH_FAILED ) ;
6263 debug (
6364 'expected %O but received %O' ,
6465 _config . listener . basic_auth ,
@@ -73,12 +74,12 @@ const requestHandler = (request, response) => {
7374 }
7475 } ) . then ( ( ) => {
7576 // validate custom headers
76- debug ( 'validate custom headers' ) ;
77+ debug ( MESSAGES . VALIDATING_CUSTOM_HEADERS ) ;
7778 if ( _config && _config . listener ) {
7879 for ( const headerKey in _config . listener . headers ) {
79- debug ( 'validating headers' ) ;
80+ debug ( MESSAGES . VALIDATING_HEADERS ) ;
8081 if ( request . headers [ headerKey ] !== _config . listener . headers [ headerKey ] ) {
81- debug ( ` ${ headerKey } was not found in req headers` ) ;
82+ debug ( MESSAGES . HEADER_NOT_FOUND ( headerKey ) ) ;
8283 return Promise . reject ( {
8384 body : 'Header key mismatch.' ,
8485 statusCode : 417 ,
@@ -88,7 +89,7 @@ const requestHandler = (request, response) => {
8889 }
8990 }
9091 } ) . then ( async ( ) => {
91- debug ( 'parsing json' ) ;
92+ debug ( MESSAGES . PARSING_JSON ) ;
9293 try {
9394 if ( _config . reqBodyLimit ) {
9495 jsonParser = promisify ( bodyParser . json ( { limit : _config . reqBodyLimit } ) ) ;
@@ -103,13 +104,13 @@ const requestHandler = (request, response) => {
103104 locale = body . data . locale ;
104105 }
105106 debug ( '_config.listener.actions[type]' , _config . listener . actions [ type ] ) ;
106- debug ( 'event' , event ) ;
107+ debug ( MESSAGES . EVENT , event ) ;
107108 // validate event:type
108109 if (
109110 ! _config . listener . actions [ type ] ||
110111 _config . listener . actions [ type ] . indexOf ( event ) === - 1
111112 ) {
112- debug ( ` ${ event } : ${ type } not defined for processing` ) ;
113+ debug ( MESSAGES . EVENT_NOT_DEFINED ( event , type ) ) ;
113114 return Promise . reject ( {
114115 body : `${ event } :${ type } not defined for processing` ,
115116 statusCode : 403 ,
@@ -137,9 +138,9 @@ const requestHandler = (request, response) => {
137138 }
138139 data . event = event ;
139140 _notify ( data ) . then ( ( data ) => {
140- debug ( 'Data [_notify]' , data ) ;
141+ debug ( MESSAGES . DATA_RECEIVED_NOTIFY , data ) ;
141142 } ) . catch ( ( error ) => {
142- debug ( 'Error [_notify]' , error ) ;
143+ debug ( MESSAGES . ERROR_OCCURRED_NOTIFY , error ) ;
143144 } ) ;
144145 return Promise . resolve ( { statusCode : 200 , statusMessage : 'OK' , body : data } ) ;
145146 } catch ( err ) {
@@ -150,7 +151,7 @@ const requestHandler = (request, response) => {
150151 } ) ;
151152 }
152153 } ) . then ( ( value ) => {
153- debug ( 'Value' , value ) ;
154+ debug ( MESSAGES . VALUE , value ) ;
154155 response . setHeader ( 'Content-Type' , 'application/json' ) ;
155156 response . statusCode = value . statusCode ;
156157 response . statusMessage = value . statusMessage ;
@@ -162,7 +163,7 @@ const requestHandler = (request, response) => {
162163 response . end ( JSON . stringify ( safeBody ) ) ;
163164 return ;
164165 } ) . catch ( ( error ) => {
165- debug ( 'Error' , error ) ;
166+ debug ( MESSAGES . ERROR , error ) ;
166167 const safeError = {
167168 statusCode : error . statusCode || 500 ,
168169 statusMessage : error . statusMessage || 'Internal Server Error' ,
0 commit comments