Skip to content

Commit

Permalink
Merge pull request #444 from stevengill/processBeforeResponse
Browse files Browse the repository at this point in the history
added processBeforeResponse flag to ExpressReceiver for FaaS use case
  • Loading branch information
stevengill authored Mar 30, 2020
2 parents 628cda8 + 0f9c342 commit 235e792
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const packageJson = require('../package.json'); // tslint:disable-line:no-requir
export interface AppOptions {
signingSecret?: ExpressReceiverOptions['signingSecret'];
endpoints?: ExpressReceiverOptions['endpoints'];
processBeforeResponse?: ExpressReceiverOptions['processBeforeResponse'];
agent?: Agent;
clientTls?: Pick<SecureContextOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'>;
convoStore?: ConversationStore | false;
Expand Down Expand Up @@ -184,6 +185,7 @@ export default class App {
logLevel = undefined,
ignoreSelf = true,
clientOptions = undefined,
processBeforeResponse = false,
}: AppOptions = {}) {

if (typeof logger === 'undefined') {
Expand Down Expand Up @@ -249,6 +251,7 @@ export default class App {
this.receiver = new ExpressReceiver({
signingSecret,
endpoints,
processBeforeResponse,
logger: this.logger,
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/ExpressReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ describe('ExpressReceiver', () => {
signingSecret: 'my-secret',
logger: noopLogger,
endpoints: { events: '/custom-endpoint' },
processBeforeResponse: true,
});
assert.isNotNull(receiver);
});
Expand All @@ -58,7 +59,7 @@ describe('ExpressReceiver', () => {
});

describe('built-in middleware', () => {
describe('ssl_check requset handler', () => {
describe('ssl_check request handler', () => {
it('should handle valid requests', async () => {
// Arrange
// tslint:disable-next-line: no-object-literal-type-assertion
Expand Down
32 changes: 26 additions & 6 deletions src/ExpressReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface ExpressReceiverOptions {
endpoints?: string | {
[endpointType: string]: string;
};
processBeforeResponse?: boolean;
}

/**
Expand All @@ -30,11 +31,13 @@ export default class ExpressReceiver implements Receiver {
private server: Server;
private bolt: App | undefined;
private logger: Logger;
private processBeforeResponse: boolean;

constructor({
signingSecret = '',
logger = new ConsoleLogger(),
endpoints = { events: '/slack/events' },
processBeforeResponse = false,
}: ExpressReceiverOptions) {
this.app = express();
// TODO: what about starting an https server instead of http? what about other options to create the server?
Expand All @@ -47,6 +50,7 @@ export default class ExpressReceiver implements Receiver {
this.requestHandler.bind(this),
];

this.processBeforeResponse = processBeforeResponse;
this.logger = logger;
const endpointList: string[] = typeof endpoints === 'string' ? [endpoints] : Object.values(endpoints);
for (const endpoint of endpointList) {
Expand All @@ -64,25 +68,41 @@ export default class ExpressReceiver implements Receiver {
// tslint:disable-next-line: align
}, 3001);

let storedResponse = undefined;
const event: ReceiverEvent = {
body: req.body,
ack: async (response: any): Promise<void> => {
ack: async (response): Promise<void> => {
if (isAcknowledged) {
throw new ReceiverMultipleAckError();
}
isAcknowledged = true;
if (!response) {
res.send('');
} else if (typeof response === 'string') {
res.send(response);
if (this.processBeforeResponse) {
if (!response) {
storedResponse = '';
} else {
storedResponse = response;
}
} else {
res.json(response);
if (!response) {
res.send('');
} else if (typeof response === 'string') {
res.send(response);
} else {
res.json(response);
}
}
},
};

try {
await this.bolt?.processEvent(event);
if (storedResponse !== undefined) {
if (typeof storedResponse === 'string') {
res.send(storedResponse);
} else {
res.json(storedResponse);
}
}
} catch (err) {
res.send(500);
throw err;
Expand Down

0 comments on commit 235e792

Please sign in to comment.