From a9f3ff2aee6dfe840386bee9d90aa3211d9a91f3 Mon Sep 17 00:00:00 2001 From: kohta ito Date: Thu, 1 Aug 2019 21:53:48 +0900 Subject: [PATCH] Pass clientOptions from constructor to client --- src/App.spec.ts | 27 +++++++++++++++++++++++++++ src/App.ts | 11 ++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/App.spec.ts b/src/App.spec.ts index 8c60c9085..cac4273d9 100644 --- a/src/App.spec.ts +++ b/src/App.spec.ts @@ -8,6 +8,7 @@ import rewiremock from 'rewiremock'; import { ErrorCode } from './errors'; import { Receiver, ReceiverEvent, SayFn, NextMiddleware } from './types'; import { ConversationStore } from './conversation-store'; +import { LogLevel } from '@slack/logger'; describe('App', () => { describe('constructor', () => { @@ -155,6 +156,32 @@ describe('App', () => { assert(fakeConversationContext.firstCall.calledWith(dummyConvoStore)); }); }); + it('with clientOptions', async () => { + const fakeConstructor = sinon.fake() + const overrides = mergeOverrides( + withNoopAppMetadata(), + { + '@slack/web-api': { + WebClient: class { + constructor() { + fakeConstructor(...arguments) + } + }, + } + } + ) + const App = await importApp(overrides); + + const clientOptions = { slackApiUrl: 'proxy.slack.com' }; + new App({ authorize: noopAuthorize, signingSecret: '', logLevel: LogLevel.ERROR, clientOptions }); + + assert.ok(fakeConstructor.called); + + const [token, options] = fakeConstructor.lastCall.args; + assert.strictEqual(undefined, token, 'token should be undefined'); + assert.strictEqual(clientOptions.slackApiUrl, options.slackApiUrl); + assert.strictEqual(LogLevel.ERROR, options.logLevel, 'override logLevel'); + }) // TODO: tests for ignoreSelf option // TODO: tests for logger and logLevel option // TODO: tests for providing botId and botUserId options diff --git a/src/App.ts b/src/App.ts index 4178ed037..a89a6c1cd 100644 --- a/src/App.ts +++ b/src/App.ts @@ -1,5 +1,5 @@ import util from 'util'; -import { WebClient, ChatPostMessageArguments, addAppMetadata } from '@slack/web-api'; +import { WebClient, ChatPostMessageArguments, addAppMetadata, WebClientOptions } from '@slack/web-api'; import { Logger, LogLevel, ConsoleLogger } from '@slack/logger'; import ExpressReceiver, { ExpressReceiverOptions } from './ExpressReceiver'; import { @@ -50,6 +50,7 @@ export interface AppOptions { logger?: Logger; logLevel?: LogLevel; ignoreSelf?: boolean; + clientOptions?: WebClientOptions; } export { LogLevel, Logger } from '@slack/logger'; @@ -127,14 +128,18 @@ export default class App { logger = new ConsoleLogger(), logLevel = LogLevel.INFO, ignoreSelf = true, + clientOptions = undefined, }: AppOptions = {}) { this.logger = logger; this.logger.setLevel(logLevel); this.errorHandler = defaultErrorHandler(this.logger); - // TODO: other webclient options (such as proxy) - this.client = new WebClient(undefined, { logLevel }); + let options = { logLevel }; + if (clientOptions !== undefined) { + options = { ...clientOptions, ...options }; + } + this.client = new WebClient(undefined, options); if (token !== undefined) { if (authorize !== undefined) {