Skip to content

Commit

Permalink
Fix #868 Add ability to use a custom express app and/or router to Exp…
Browse files Browse the repository at this point in the history
…ressReceiver
  • Loading branch information
seratch committed Aug 25, 2021
1 parent 02d601d commit 8975e85
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
31 changes: 31 additions & 0 deletions src/receivers/ExpressReceiver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Request, Response } from 'express';
import { Readable } from 'stream';
import { EventEmitter } from 'events';
import { ErrorCode, CodedError, ReceiverInconsistentStateError } from '../errors';
import { Application, IRouter } from 'express';

import ExpressReceiver, {
respondToSslCheck,
Expand Down Expand Up @@ -79,6 +80,36 @@ describe('ExpressReceiver', function () {
});
assert.isNotNull(receiver);
});
it('should accept custom Express app / router', async () => {
const app: Application = {
use: sinon.fake(),
} as unknown as Application;
const router: IRouter = {
get: sinon.fake(),
post: sinon.fake(),
use: sinon.fake(),
} as unknown as IRouter;
const receiver = new ExpressReceiver({
signingSecret: 'my-secret',
logger: noopLogger,
endpoints: { events: '/custom-endpoint' },
processBeforeResponse: true,
clientId: 'my-clientId',
clientSecret: 'my-client-secret',
stateSecret: 'state-secret',
scopes: ['channels:read'],
installerOptions: {
authVersion: 'v2',
userScopes: ['chat:write'],
},
app: app,
router: router,
});
assert.isNotNull(receiver);
assert((app.use as any).calledOnce);
assert((router.get as any).called);
assert((router.post as any).calledOnce);
});
});

describe('#start()', function () {
Expand Down
12 changes: 8 additions & 4 deletions src/receivers/ExpressReceiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { createServer, Server, ServerOptions } from 'http';
import { createServer as createHttpsServer, Server as HTTPSServer, ServerOptions as HTTPSServerOptions } from 'https';
import { ListenOptions } from 'net';
import express, { Request, Response, Application, RequestHandler, Router } from 'express';
import express, { Request, Response, Application, RequestHandler, Router, IRouter } from 'express';
import rawBody from 'raw-body';
import querystring from 'querystring';
import crypto from 'crypto';
Expand Down Expand Up @@ -33,6 +33,8 @@ export interface ExpressReceiverOptions {
installationStore?: InstallProviderOptions['installationStore']; // default MemoryInstallationStore
scopes?: InstallURLOptions['scopes'];
installerOptions?: InstallerOptions;
app?: Application;
router?: IRouter;
}

// Additional Installer Options
Expand Down Expand Up @@ -63,7 +65,7 @@ export default class ExpressReceiver implements Receiver {

private processBeforeResponse: boolean;

public router: Router;
public router: IRouter;

public installer: InstallProvider | undefined = undefined;

Expand All @@ -79,8 +81,10 @@ export default class ExpressReceiver implements Receiver {
installationStore = undefined,
scopes = undefined,
installerOptions = {},
app = undefined,
router = undefined,
}: ExpressReceiverOptions) {
this.app = express();
this.app = app !== undefined ? app : express();

if (typeof logger !== 'undefined') {
this.logger = logger;
Expand All @@ -99,7 +103,7 @@ export default class ExpressReceiver implements Receiver {
this.processBeforeResponse = processBeforeResponse;

const endpointList = typeof endpoints === 'string' ? [endpoints] : Object.values(endpoints);
this.router = Router();
this.router = router !== undefined ? router : Router();
endpointList.forEach((endpoint) => {
this.router.post(endpoint, ...expressMiddleware);
});
Expand Down

0 comments on commit 8975e85

Please sign in to comment.