Skip to content

Commit

Permalink
Merge pull request #379 from seratch/issue-377
Browse files Browse the repository at this point in the history
Fix #377 ExpressReceiver's RespondFn implementation doesn't accept a string
  • Loading branch information
seratch authored Jan 22, 2020
2 parents 6117d4e + d83d279 commit 7c89750
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/ExpressReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import ExpressReceiver, {
verifySignatureAndParseBody,
} from './ExpressReceiver';

import { RespondArguments } from './types/utilities';

describe('ExpressReceiver', () => {

const noopLogger: Logger = {
Expand Down Expand Up @@ -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: [] });
});
});

});
7 changes: 5 additions & 2 deletions src/ExpressReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
});
Expand Down

0 comments on commit 7c89750

Please sign in to comment.