Skip to content

Commit

Permalink
Fix slackapi#354 Add client to the list of args send through to lis…
Browse files Browse the repository at this point in the history
…teners
  • Loading branch information
seratch committed Feb 4, 2020
1 parent 6d450c1 commit 7a4281a
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 1 deletion.
48 changes: 48 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,54 @@ describe('App', () => {
});
});

describe('client', () => {

it('should be available in middleware/listener args', async () => {
// Arrange
const App = await importApp(mergeOverrides( // tslint:disable-line:variable-name
withNoopAppMetadata(),
withSuccessfulBotUserFetchingWebClient('B123', 'U123'),
));
const app = new App({
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});
app.use(async ({ client }) => {
await client.auth.test();
});
app.event('app_home_opened', async ({ client }) => {
await client.auth.test();
});

const receiverEvents = [
{
body: {
type: 'event_callback',
token: 'XXYYZZ',
team_id: 'TXXXXXXXX',
api_app_id: 'AXXXXXXXXX',
event: {
type: 'app_home_opened',
event_ts: '1234567890.123456',
user: 'UXXXXXXX1',
text: 'hello friends!',
tab: 'home',
view: {},
},
},
respond: noop,
ack: noop,
},
];

// Act
receiverEvents.forEach(event => fakeReceiver.emit('message', event));
await delay();

// Assert
});
});

describe('say()', () => {

function createChannelContextualReceiverEvents(channelId: string): ReceiverEvent[] {
Expand Down
3 changes: 2 additions & 1 deletion src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ export default class App {
// Set body and payload (this value will eventually conform to AnyMiddlewareArgs)
// NOTE: the following doesn't work because... distributive?
// const listenerArgs: Partial<AnyMiddlewareArgs> = {
const listenerArgs: Pick<AnyMiddlewareArgs, 'logger' | 'body' | 'payload'> & {
const listenerArgs: Pick<AnyMiddlewareArgs, 'logger' | 'client' | 'body' | 'payload'> & {
/** Say function might be set below */
say?: SayFn
/** Respond function might be set below */
Expand All @@ -439,6 +439,7 @@ export default class App {
ack?: AckFn<any>,
} = {
logger: this.logger,
client: this.client,
body: bodyArg,
payload:
(type === IncomingEventType.Event) ?
Expand Down
13 changes: 13 additions & 0 deletions src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { onlyCommands, onlyEvents, matchCommandName, matchEventType, subtype } from './builtin';
import { SlashCommand } from '../types/command/index';
import { SlackEvent, AppMentionEvent, BotMessageEvent } from '../types/events/base-events';
import { WebClient } from '@slack/web-api';

describe('matchMessage()', () => {
function initializeTestCase(pattern: string | RegExp): Mocha.AsyncFunc {
Expand Down Expand Up @@ -434,12 +435,14 @@ describe('ignoreSelf()', () => {

describe('onlyCommands', () => {
const logger = createFakeLogger();
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

it('should detect valid requests', async () => {
const payload: SlashCommand = { ...validCommandPayload };
const fakeNext = sinon.fake();
onlyCommands({
logger,
client,
payload,
command: payload,
body: payload,
Expand All @@ -457,6 +460,7 @@ describe('onlyCommands', () => {
const fakeNext = sinon.fake();
onlyCommands({
logger,
client,
payload,
action: payload,
command: undefined,
Expand All @@ -473,11 +477,13 @@ describe('onlyCommands', () => {

describe('matchCommandName', () => {
const logger = createFakeLogger();
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

function buildArgs(fakeNext: NextMiddleware): SlackCommandMiddlewareArgs & { next: any, context: any } {
const payload: SlashCommand = { ...validCommandPayload };
return {
logger,
client,
payload,
command: payload,
body: payload,
Expand Down Expand Up @@ -505,11 +511,13 @@ describe('matchCommandName', () => {
describe('onlyEvents', () => {

const logger = createFakeLogger();
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

it('should detect valid requests', async () => {
const fakeNext = sinon.fake();
const args: SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } = {
logger,
client,
payload: appMentionEvent,
event: appMentionEvent,
message: null as never, // a bit hackey to sartisfy TS compiler
Expand All @@ -534,6 +542,7 @@ describe('onlyEvents', () => {
const fakeNext = sinon.fake();
onlyEvents({
logger,
client,
payload,
command: payload,
body: payload,
Expand All @@ -549,10 +558,12 @@ describe('onlyEvents', () => {

describe('matchEventType', () => {
const logger = createFakeLogger();
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

function buildArgs(): SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } {
return {
logger,
client,
payload: appMentionEvent,
event: appMentionEvent,
message: null as never, // a bit hackey to sartisfy TS compiler
Expand Down Expand Up @@ -585,10 +596,12 @@ describe('matchEventType', () => {

describe('subtype', () => {
const logger = createFakeLogger();
const client = new WebClient(undefined, { logger, slackApiUrl: undefined });

function buildArgs(): SlackEventMiddlewareArgs<'message'> & { event?: SlackEvent } {
return {
logger,
client,
payload: botMessageEvent,
event: botMessageEvent,
message: botMessageEvent,
Expand Down
2 changes: 2 additions & 0 deletions src/types/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { DialogSubmitAction, DialogValidation } from './dialog-action';
import { MessageAction } from './message-action';
import { SayFn, SayArguments, RespondFn, AckFn } from '../utilities';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';

/**
* All known actions from Slack's Block Kit interactive components, message actions, dialogs, and legacy interactive
Expand Down Expand Up @@ -36,6 +37,7 @@ export type SlackAction = BlockAction | InteractiveMessage | DialogSubmitAction
*/
export interface SlackActionMiddlewareArgs<Action extends SlackAction = SlackAction> {
logger: Logger;
client: WebClient;
payload: (
Action extends BlockAction<infer ElementAction> ? ElementAction :
Action extends InteractiveMessage<infer InteractiveAction> ? InteractiveAction :
Expand Down
2 changes: 2 additions & 0 deletions src/types/command/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { StringIndexed } from '../helpers';
import { SayFn, RespondFn, RespondArguments, AckFn } from '../utilities';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';

/**
* Arguments which listeners and middleware receive to process a slash command from Slack.
*/
export interface SlackCommandMiddlewareArgs {
logger: Logger;
client: WebClient;
payload: SlashCommand;
command: this['payload'];
body: this['payload'];
Expand Down
2 changes: 2 additions & 0 deletions src/types/events/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { SlackEvent, BasicSlackEvent } from './base-events';
import { StringIndexed } from '../helpers';
import { SayFn } from '../utilities';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';

/**
* Arguments which listeners and middleware receive to process an event from Slack's Events API.
*/
export interface SlackEventMiddlewareArgs<EventType extends string = string> {
logger: Logger;
client: WebClient;
payload: EventFromType<EventType>;
event: this['payload'];
message: EventType extends 'message' ? this['payload'] : never;
Expand Down
2 changes: 2 additions & 0 deletions src/types/options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { Option } from '@slack/types';
import { StringIndexed, XOR } from '../helpers';
import { AckFn } from '../utilities';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';

/**
* Arguments which listeners and middleware receive to process an options request from Slack
*/
export interface SlackOptionsMiddlewareArgs<Source extends OptionsSource = OptionsSource> {
logger: Logger;
client: WebClient;
payload: OptionsRequest<Source>;
body: this['payload'];
options: this['payload'];
Expand Down
2 changes: 2 additions & 0 deletions src/types/view/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StringIndexed } from '../helpers';
import { RespondArguments, AckFn } from '../utilities';
import { Logger } from '@slack/logger';
import { WebClient } from '@slack/web-api';

/**
* Known view action types
Expand All @@ -12,6 +13,7 @@ export type SlackViewAction = ViewSubmitAction | ViewClosedAction;
*/
export interface SlackViewMiddlewareArgs<ViewActionType extends SlackViewAction = SlackViewAction> {
logger: Logger;
client: WebClient;
payload: ViewOutput;
view: this['payload'];
body: ViewActionType;
Expand Down

0 comments on commit 7a4281a

Please sign in to comment.