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

Adds some WebClient options #278

Merged
merged 2 commits into from
Oct 7, 2019
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
20 changes: 13 additions & 7 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down Expand Up @@ -122,6 +124,8 @@ export default class App {
constructor({
signingSecret = undefined,
endpoints = undefined,
agent = undefined,
clientTls = undefined,
receiver = undefined,
convoStore = undefined,
token = undefined,
Expand All @@ -138,11 +142,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) {
Expand Down Expand Up @@ -177,7 +183,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 });
}
}

Expand Down
22 changes: 18 additions & 4 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';
Expand All @@ -18,6 +19,8 @@ export interface ExpressReceiverOptions {
endpoints?: string | {
[endpointType: string]: string;
};
agent?: Agent;
clientTls?: Pick<SecureContextOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'>;
}

/**
Expand All @@ -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' }
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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I overlooked the necessity to support the respond function as well. Great catch 🙇

{
httpAgent: agent,
httpsAgent: agent,
},
clientTls,
));

const expressMiddleware: RequestHandler[] = [
verifySignatureAndParseBody(logger, signingSecret),
Expand Down Expand Up @@ -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);
});
Expand Down