Skip to content

Commit

Permalink
Added backward compatability
Browse files Browse the repository at this point in the history
Update README.md

Removed console logger

Added default logger even when it's specified
  • Loading branch information
Idan Harel committed Dec 15, 2016
1 parent 0763b8c commit f5b8ffa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
16 changes: 14 additions & 2 deletions lib/viber-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,19 @@ const REQUIRED_CONFIGURATION_FIELDS = ["authToken", "name", "avatar"];
const SUBSCRIBED_EVENTS = ["subscribed", "unsubscribed", "conversation_started", "message", "delivered", "seen"];
const API_URL = "https://chatapi.viber.com/pa";

function ViberBot(configuration) {
function ViberBot(loggerOrConfiguration, configuration) {

// backward compatibility: we are still allowing ctor as (logger, configuration);
// newer should use (configuration) with logger property in it.
let logger;
if (!configuration) {
// no logger, treat loggerOrConfiguration as configuration
configuration = loggerOrConfiguration;
logger = configuration.logger || NoopLogger;
} else {
logger = loggerOrConfiguration || NoopLogger;
}

if (!configuration) {
throw new Error(`Invalid configuration`);
}
Expand All @@ -36,7 +48,7 @@ function ViberBot(configuration) {
this.name = configuration.name;
this.avatar = configuration.avatar;

this._logger = configuration.logger || NoopLogger;
this._logger = logger;
this._client = new ViberClient(this._logger, this, API_URL, configuration.registerToEvents || SUBSCRIBED_EVENTS);
this._middleware = new Middleware(this._logger, new MessageValidator(this._logger, this.authToken));
this._messageFactory = new MessageFactory(this._logger);
Expand Down
10 changes: 10 additions & 0 deletions test/viber-bot-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ exports.creatingBotObject = {
test.done();
},

createBotWithLoggerAsOption: test => {
new ViberBot({
logger: TestEnvironmentConfiguration.MockLogger,
authToken: "123AB",
name: "Test",
avatar: "http://avatar.com/image.jpg"
});
test.done();
},

createBotWithMissingAuthTokenField: test => {
test.throws(() => new ViberBot(TestEnvironmentConfiguration.MockLogger, {
name: "Test",
Expand Down

0 comments on commit f5b8ffa

Please sign in to comment.