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

Fix #868 Add ability to use a custom express app and/or router to ExpressReceiver #1084

Merged
merged 1 commit into from
Aug 25, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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