Skip to content

Commit 7700377

Browse files
committed
module update
1 parent de68853 commit 7700377

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@contentstack/webhook-listener",
3-
"version": "0.0.1",
4-
"description": "HTTP web server that listens to Contentstack webhooks. This is part of Contentstack DataSync.",
3+
"version": "1.0.0",
4+
"description": "HTTP web server that listens to Contentstack webhooks. This is part of Contentstack DataSync",
55
"main": "./dist",
66
"types": "./typings",
77
"homepage": "https://www.contentstack.com/docs/guide/synchronization/contentstack-datasync",
@@ -43,10 +43,10 @@
4343
"devDependencies": {
4444
"@types/basic-auth": "^1.1.2",
4545
"@types/node": "^10.12.2",
46-
"winston": "^3.1.0",
47-
"jest": "^23.6.0",
46+
"jest": "^24.8.0",
4847
"rimraf": "^2.6.2",
4948
"supertest": "^3.3.0",
50-
"typescript": "^3.2.2"
49+
"typescript": "^3.2.2",
50+
"winston": "^3.1.0"
5151
}
5252
}

src/index.ts

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
'use strict';
88

99
import { debug as Debug } from 'debug';
10-
import { merge as _merge } from 'lodash';
10+
import { merge } from 'lodash';
1111
import { createListener } from './core';
1212
import { defaultConfig } from './defaults';
1313
import { logger as log, setLogger } from './logger';
1414

1515
const debug = Debug('webhook:listener');
1616
let notify;
1717
let 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
*/
4041
export 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

Comments
 (0)