From 279b97bb592da1bd19923386a94051e7ddddc2a7 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Tue, 21 Jan 2020 10:44:20 -0800 Subject: [PATCH] Fix #377 ExpressReceiver's RespondFn implementation doesn't accept a string --- src/ExpressReceiver.spec.ts | 16 ++++++++++++++++ src/ExpressReceiver.ts | 7 +++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/ExpressReceiver.spec.ts b/src/ExpressReceiver.spec.ts index 3b3a58621..454b4b89d 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 = { @@ -379,4 +381,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 ca2f094ca..7036c5da0 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); });