diff --git a/src/ExpressReceiver.spec.ts b/src/ExpressReceiver.spec.ts index f5b009897..fed10aaa6 100644 --- a/src/ExpressReceiver.spec.ts +++ b/src/ExpressReceiver.spec.ts @@ -14,6 +14,8 @@ import ExpressReceiver, { verifySignatureAndParseBody, } from './ExpressReceiver'; +import { RespondArguments } from './types/utilities'; + describe('ExpressReceiver', () => { const noopLogger: Logger = { @@ -451,4 +453,18 @@ describe('ExpressReceiver', () => { }); + // Just copied the implementation as the method is private and it's a bit hard to write a unit test + describe('RespondFn implementation', () => { + it('should work with both a string and a RespondArguments', () => { + const respond = (response: string | RespondArguments): void => { + const validResponse: RespondArguments = + (typeof response === 'string') ? { text: response } : response; + assert.equal(validResponse.text, 'hello'); + }; + respond('hello'); + respond({ text: 'hello' }); + respond({ text: 'hello', blocks: [] }); + }); + }); + }); diff --git a/src/ExpressReceiver.ts b/src/ExpressReceiver.ts index ecd25bfc0..39750e681 100644 --- a/src/ExpressReceiver.ts +++ b/src/ExpressReceiver.ts @@ -10,6 +10,7 @@ import crypto from 'crypto'; import tsscmp from 'tsscmp'; import { ErrorCode, errorWithCode } from './errors'; import { Logger, ConsoleLogger } from '@slack/logger'; +import { RespondArguments } from './types/utilities'; // TODO: we throw away the key names for endpoints, so maybe we should use this interface. is it better for migrations? // if that's the reason, let's document that with a comment. @@ -101,8 +102,10 @@ export default class ExpressReceiver extends EventEmitter implements Receiver { }; if (req.body && req.body.response_url) { - event.respond = (response): void => { - this.axios.post(req.body.response_url, response) + event.respond = (response: string | RespondArguments): void => { + const validResponse: RespondArguments = + (typeof response === 'string') ? { text: response } : response; + this.axios.post(req.body.response_url, validResponse) .catch((e) => { this.emit('error', e); });