Skip to content

Commit

Permalink
Get rid of next! in App.use()
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Mar 27, 2020
1 parent edfcfbc commit 1225079
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
18 changes: 9 additions & 9 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { WebClientOptions, WebClient } from '@slack/web-api';
// TODO: swap out rewiremock for proxyquire to see if it saves execution time
// Utility functions
const noop = (() => Promise.resolve(undefined));
const noopMiddleware = async ({ next }: { next: NextFn; }) => { await next!(); };
const noopMiddleware = async ({ next }: { next: NextFn; }) => { await next(); };
const noopAuthorize = (() => Promise.resolve({}));

// Dummies (values that have no real behavior but pass through the system opaquely)
Expand Down Expand Up @@ -347,8 +347,8 @@ describe('App', () => {
// Arrange
app.use(fakeFirstMiddleware);
app.use(async ({ next }) => {
await next!();
await next!();
await next();
await next();
});
app.use(fakeSecondMiddleware);
app.error(fakeErrorHandler);
Expand All @@ -367,7 +367,7 @@ describe('App', () => {
await delay(100);
changed = true;

await next!();
await next();
});

await fakeReceiver.sendEvent(dummyReceiverEvent);
Expand All @@ -381,7 +381,7 @@ describe('App', () => {

app.use(async ({ next }) => {
try {
await next!();
await next();
} catch (err) {
caughtError = err;
}
Expand Down Expand Up @@ -713,7 +713,7 @@ describe('App', () => {

app.use(async ({ next }) => {
await ackFn();
await next!();
await next();
});
app.shortcut({ callback_id: 'message_action_callback_id' }, async ({ }) => { await shortcutFn(); });
app.shortcut(
Expand Down Expand Up @@ -857,7 +857,7 @@ describe('App', () => {
});
app.use(async ({ logger, body, next }) => {
logger.info(body);
await next!();
await next();
});

app.event('app_home_opened', async ({ logger, event }) => {
Expand Down Expand Up @@ -905,7 +905,7 @@ describe('App', () => {
});
app.use(async ({ logger, body, next }) => {
logger.info(body);
await next!();
await next();
});

app.event('app_home_opened', async ({ logger, event }) => {
Expand Down Expand Up @@ -971,7 +971,7 @@ describe('App', () => {
});
app.use(async ({ client, next }) => {
await client.auth.test();
await next!();
await next();
});
const clients: WebClient[] = [];
app.event('app_home_opened', async ({ client }) => {
Expand Down
15 changes: 13 additions & 2 deletions src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
RespondArguments,
} from './types';
import { IncomingEventType, getTypeAndConversation, assertNever } from './helpers';
import { NonListenerMiddlewareArgs, NonListenerMiddleware } from './types/middleware';
import {
CodedError,
asCodedError,
Expand Down Expand Up @@ -275,8 +276,18 @@ export default class App {
*
* @param m global middleware function
*/
public use(m: Middleware<AnyMiddlewareArgs>): this {
this.middleware.push(m);
public use(m: NonListenerMiddleware): this {
const middleware: Middleware<AnyMiddlewareArgs> = (args) => {
const _args: NonListenerMiddlewareArgs = {
context: args.context,
logger: args.logger,
client: args.client,
next: args.next!,
...args,
};
return m(_args);
};
this.middleware.push(middleware);
return this;
}

Expand Down
7 changes: 4 additions & 3 deletions src/conversation-store.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Middleware, AnyMiddlewareArgs } from './types';
import { Middleware } from './types';
import { Logger } from '@slack/logger';
import { getTypeAndConversation } from './helpers';
import { NonListenerMiddlewareArgs } from './types/middleware';

/**
* Storage backend used by the conversation context middleware
Expand Down Expand Up @@ -54,7 +55,7 @@ export class MemoryStore<ConversationState = any> implements ConversationStore<C
export function conversationContext<ConversationState = any>(
store: ConversationStore<ConversationState>,
logger: Logger,
): Middleware<AnyMiddlewareArgs> {
): Middleware<NonListenerMiddlewareArgs> {
return async (args) => {
const { body, context, next } = args;
const { conversationId } = getTypeAndConversation(body as any);
Expand All @@ -74,7 +75,7 @@ export function conversationContext<ConversationState = any>(
} else {
logger.debug('No conversation ID for incoming event');
// TODO: remove the non-null assertion operator
await next!();
await next();
}
};
}
7 changes: 4 additions & 3 deletions src/middleware/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
} from '../types';
import { ActionConstraints, ViewConstraints, ShortcutConstraints } from '../App';
import { ContextMissingPropertyError } from '../errors';
import { NonListenerMiddlewareArgs } from '../types/middleware';

/**
* Middleware that filters out any event that isn't an action
Expand Down Expand Up @@ -263,7 +264,7 @@ export function matchEventType(type: string): Middleware<SlackEventMiddlewareArg
};
}

export function ignoreSelf(): Middleware<AnyMiddlewareArgs> {
export function ignoreSelf(): Middleware<NonListenerMiddlewareArgs> {
return async (args) => {
// When context does not have a botId in it, then this middleware cannot perform its job. Bail immediately.
if (args.context.botId === undefined) {
Expand Down Expand Up @@ -384,7 +385,7 @@ function isViewBody(
}

function isEventArgs(
args: AnyMiddlewareArgs,
): args is SlackEventMiddlewareArgs {
args: NonListenerMiddlewareArgs,
): args is NonListenerMiddlewareArgs & SlackEventMiddlewareArgs {
return (args as SlackEventMiddlewareArgs).event !== undefined;
}
14 changes: 14 additions & 0 deletions src/types/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ export interface Middleware<Args> {
(args: Args & AllMiddlewareArgs): Promise<void>;
}

interface NextForSure {
body: any;
context: Context;
logger: Logger;
client: WebClient;
next: NextFn;
}

export type NonListenerMiddlewareArgs = AnyMiddlewareArgs & NextForSure;

export interface NonListenerMiddleware {
(args: NonListenerMiddlewareArgs): Promise<void>;
}

export interface Context extends StringIndexed {
}

Expand Down

0 comments on commit 1225079

Please sign in to comment.