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

added processBeforeResponse flag to ExpressReceiver for FaaS use case #444

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
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