Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #377 ExpressReceiver's RespondFn implementation doesn't accept a string #379

Merged
merged 2 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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: [] });
});
});

});
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