77'use strict' ;
88
99import { debug as Debug } from 'debug' ;
10- import { merge as _merge } from 'lodash' ;
10+ import { merge } from 'lodash' ;
1111import { createListener } from './core' ;
1212import { defaultConfig } from './defaults' ;
1313import { logger as log , setLogger } from './logger' ;
1414
1515const debug = Debug ( 'webhook:listener' ) ;
1616let notify ;
1717let config : any = { } ;
18+ let appConfig : any = defaultConfig
1819
1920/**
2021 * Register a function that will get called when webhook is triggered.
@@ -38,40 +39,49 @@ export function register(consumer: any) {
3839 * @returns {Promise } Promise object represents http.Server
3940 */
4041export function start ( userConfig : any , customLogger ?: any ) {
41- if ( customLogger ) {
42- setLogger ( customLogger ) ;
43- }
4442 return new Promise ( ( resolve , reject ) => {
45- debug ( 'start called with %O' , userConfig ) ;
46- validateConfig ( userConfig ) ;
47- // Override default with user config
48- if ( userConfig ) {
49- config = _merge ( { } , defaultConfig , userConfig ) ;
50- } else {
51- log . info ( 'Starting listener with default configs' ) ;
52- config = defaultConfig ;
53- }
43+ try {
44+ if ( customLogger ) {
45+ setLogger ( customLogger ) ;
46+ }
47+ debug ( 'start called with %O' , userConfig ) ;
48+ appConfig = merge ( appConfig , userConfig )
49+ validateConfig ( appConfig ) ;
5450
55- if ( ! notify ) {
56- log . error (
57- 'Aborting start of webhook listener, since no function is provided to notify.' ,
58- ) ;
59- return reject (
60- new Error (
61- `Aborting start of webhook listener, since no function is provided to notify.` ,
62- ) ,
63- ) ;
64- }
51+ if ( ! notify ) {
52+ log . error (
53+ 'Aborting start of webhook listener, since no function is provided to notify.' ,
54+ ) ;
55+ return reject (
56+ new Error (
57+ `Aborting start of webhook listener, since no function is provided to notify.` ,
58+ ) ,
59+ ) ;
60+ }
6561
66- debug ( 'starting with config: ' + JSON . stringify ( config ) ) ;
67- const port = process . env . PORT || config . listener . port ;
68- const server = createListener ( config , notify ) . listen ( port , ( ) => {
69- log . info ( `Server running at port ${ port } ` ) ;
70- } ) ;
71- return resolve ( server ) ;
62+ debug ( 'starting with config: ' + JSON . stringify ( appConfig ) ) ;
63+ const port = process . env . PORT || appConfig . listener . port ;
64+ const server = createListener ( appConfig , notify ) . listen ( port , ( ) => {
65+ log . info ( `Server running at port ${ port } ` ) ;
66+ } ) ;
67+ return resolve ( server ) ;
68+ } catch ( error ) {
69+ return reject ( error )
70+ }
7271 } ) ;
7372}
7473
74+ /**
75+ * @public
76+ * @method setConfig
77+ * @description
78+ * Sets listener library's configuration
79+ * @param config Listener lib config
80+ */
81+ export const setConfig = ( config ) => {
82+ appConfig = merge ( appConfig , config )
83+ }
84+
7585/**
7686 * Get configuration.
7787 */
0 commit comments