Skip to content

Commit

Permalink
Merge branch 'master' into add-view-closed-support
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane DeWael authored Oct 7, 2019
2 parents a7cd1d7 + 2e11041 commit c4114be
Showing 2 changed files with 30 additions and 10 deletions.
20 changes: 13 additions & 7 deletions src/App.ts
Original file line number Diff line number Diff line change
@@ -44,6 +44,8 @@ const packageJson = require('../package.json'); // tslint:disable-line:no-requir
export interface AppOptions {
signingSecret?: ExpressReceiverOptions['signingSecret'];
endpoints?: ExpressReceiverOptions['endpoints'];
agent?: ExpressReceiverOptions['agent']; // also WebClientOptions['agent']
clientTls?: ExpressReceiverOptions['clientTls']; // also WebClientOptions['tls']
convoStore?: ConversationStore | false;
token?: AuthorizeResult['botToken']; // either token or authorize
botId?: AuthorizeResult['botId']; // only used when authorize is not defined, shortcut for fetching
@@ -53,7 +55,7 @@ export interface AppOptions {
logger?: Logger;
logLevel?: LogLevel;
ignoreSelf?: boolean;
clientOptions?: WebClientOptions;
clientOptions?: Pick<WebClientOptions, 'slackApiUrl'>;
}

export { LogLevel, Logger } from '@slack/logger';
@@ -127,6 +129,8 @@ export default class App {
constructor({
signingSecret = undefined,
endpoints = undefined,
agent = undefined,
clientTls = undefined,
receiver = undefined,
convoStore = undefined,
token = undefined,
@@ -143,11 +147,13 @@ export default class App {
this.logger.setLevel(logLevel);
this.errorHandler = defaultErrorHandler(this.logger);

let options = { logLevel };
if (clientOptions !== undefined) {
options = { ...clientOptions, ...options };
}
this.client = new WebClient(undefined, options);
this.client = new WebClient(undefined, {
agent,
logLevel,
logger,
tls: clientTls,
slackApiUrl: clientOptions !== undefined ? clientOptions.slackApiUrl : undefined,
});

if (token !== undefined) {
if (authorize !== undefined) {
@@ -182,7 +188,7 @@ export default class App {
);
} else {
// Create default ExpressReceiver
this.receiver = new ExpressReceiver({ signingSecret, logger, endpoints });
this.receiver = new ExpressReceiver({ signingSecret, logger, endpoints, agent, clientTls });
}
}

20 changes: 17 additions & 3 deletions src/ExpressReceiver.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { EventEmitter } from 'events';
import { Receiver, ReceiverEvent, ReceiverAckTimeoutError } from './types';
import { createServer, Server } from 'http';
import { createServer, Server, Agent } from 'http';
import { SecureContextOptions } from 'tls';
import express, { Request, Response, Application, RequestHandler, NextFunction } from 'express';
import axios from 'axios';
import axios, { AxiosInstance } from 'axios';
import rawBody from 'raw-body';
import querystring from 'querystring';
import crypto from 'crypto';
@@ -18,6 +19,8 @@ export interface ExpressReceiverOptions {
endpoints?: string | {
[endpointType: string]: string;
};
agent?: Agent;
clientTls?: Pick<SecureContextOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'>;
}

/**
@@ -30,17 +33,28 @@ export default class ExpressReceiver extends EventEmitter implements Receiver {

private server: Server;

private axios: AxiosInstance;

constructor({
signingSecret = '',
logger = new ConsoleLogger(),
endpoints = { events: '/slack/events' },
agent = undefined,
clientTls = undefined,
}: ExpressReceiverOptions) {
super();

this.app = express();
this.app.use(this.errorHandler.bind(this));
// TODO: what about starting an https server instead of http? what about other options to create the server?
this.server = createServer(this.app);
this.axios = axios.create(Object.assign(
{
httpAgent: agent,
httpsAgent: agent,
},
clientTls,
));

const expressMiddleware: RequestHandler[] = [
verifySignatureAndParseBody(logger, signingSecret),
@@ -87,7 +101,7 @@ export default class ExpressReceiver extends EventEmitter implements Receiver {

if (req.body && req.body.response_url) {
event.respond = (response): void => {
axios.post(req.body.response_url, response)
this.axios.post(req.body.response_url, response)
.catch((e) => {
this.emit('error', e);
});

0 comments on commit c4114be

Please sign in to comment.