From f5b8ffa758db1d469285449f12fea68ba9b46620 Mon Sep 17 00:00:00 2001 From: Idan Harel Date: Thu, 15 Dec 2016 16:04:50 +0200 Subject: [PATCH] Added backward compatability Update README.md Removed console logger Added default logger even when it's specified --- lib/viber-bot.js | 16 ++++++++++++++-- test/viber-bot-test.js | 10 ++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/viber-bot.js b/lib/viber-bot.js index 755cfe3..2a82c26 100644 --- a/lib/viber-bot.js +++ b/lib/viber-bot.js @@ -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`); } @@ -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); diff --git a/test/viber-bot-test.js b/test/viber-bot-test.js index 7ae0e0d..c0b84d2 100644 --- a/test/viber-bot-test.js +++ b/test/viber-bot-test.js @@ -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",