Skip to content

Commit

Permalink
Fix slackapi#168 Give listeners and middleware access to the logger
Browse files Browse the repository at this point in the history
  • Loading branch information
seratch committed Dec 24, 2019
1 parent d8f0a87 commit 1c0e33d
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 2 deletions.
50 changes: 50 additions & 0 deletions src/App.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,56 @@ describe('App', () => {
});
});

describe('logger', () => {

it('should be available in middleware/listener args', async () => {
// Arrange
const App = await importApp(overrides); // tslint:disable-line:variable-name
const fakeLogger = createFakeLogger();
const app = new App({
logger: fakeLogger,
receiver: fakeReceiver,
authorize: sinon.fake.resolves(dummyAuthorizationResult),
});
app.use(({ logger, body }) => {
logger.info(body);
});

app.event('app_home_opened', ({ logger, event }) => {
logger.debug(event);
});

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
assert.isTrue(fakeLogger.info.called);
assert.isTrue(fakeLogger.debug.called);
});
});

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 @@ -411,14 +411,15 @@ 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, 'body' | 'payload'> & {
const listenerArgs: Pick<AnyMiddlewareArgs, 'logger' | 'body' | 'payload'> & {
/** Say function might be set below */
say?: SayFn
/** Respond function might be set below */
respond?: RespondFn,
/** Ack function might be set below */
ack?: AckFn<any>,
} = {
logger: this.logger,
body: bodyArg,
payload:
(type === IncomingEventType.Event) ?
Expand Down
18 changes: 17 additions & 1 deletion src/middleware/builtin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'mocha';
import { assert } from 'chai';
import sinon from 'sinon';
import { ErrorCode } from '../errors';
import { Override, delay, wrapToResolveOnFirstCall } from '../test-helpers';
import { Override, delay, wrapToResolveOnFirstCall, createFakeLogger } from '../test-helpers';
import rewiremock from 'rewiremock';
import {
SlackEventMiddlewareArgs,
Expand Down Expand Up @@ -433,11 +433,13 @@ describe('ignoreSelf()', () => {
});

describe('onlyCommands', () => {
const logger = createFakeLogger();

it('should detect valid requests', async () => {
const payload: SlashCommand = { ...validCommandPayload };
const fakeNext = sinon.fake();
onlyCommands({
logger,
payload,
command: payload,
body: payload,
Expand All @@ -454,6 +456,7 @@ describe('onlyCommands', () => {
const payload: any = {};
const fakeNext = sinon.fake();
onlyCommands({
logger,
payload,
action: payload,
command: undefined,
Expand All @@ -469,9 +472,12 @@ describe('onlyCommands', () => {
});

describe('matchCommandName', () => {
const logger = createFakeLogger();

function buildArgs(fakeNext: NextMiddleware): SlackCommandMiddlewareArgs & { next: any, context: any } {
const payload: SlashCommand = { ...validCommandPayload };
return {
logger,
payload,
command: payload,
body: payload,
Expand All @@ -498,9 +504,12 @@ describe('matchCommandName', () => {

describe('onlyEvents', () => {

const logger = createFakeLogger();

it('should detect valid requests', async () => {
const fakeNext = sinon.fake();
const args: SlackEventMiddlewareArgs<'app_mention'> & { event?: SlackEvent } = {
logger,
payload: appMentionEvent,
event: appMentionEvent,
message: null as never, // a bit hackey to sartisfy TS compiler
Expand All @@ -524,6 +533,7 @@ describe('onlyEvents', () => {
const payload: SlashCommand = { ...validCommandPayload };
const fakeNext = sinon.fake();
onlyEvents({
logger,
payload,
command: payload,
body: payload,
Expand All @@ -538,8 +548,11 @@ describe('onlyEvents', () => {
});

describe('matchEventType', () => {
const logger = createFakeLogger();

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

describe('subtype', () => {
const logger = createFakeLogger();

function buildArgs(): SlackEventMiddlewareArgs<'message'> & { event?: SlackEvent } {
return {
logger,
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 @@ -8,6 +8,7 @@ import { InteractiveMessage } from './interactive-message';
import { DialogSubmitAction, DialogValidation } from './dialog-action';
import { MessageAction } from './message-action';
import { SayFn, SayArguments, RespondFn, AckFn } from '../utilities';
import { Logger } from '@slack/logger';

/**
* All known actions from Slack's Block Kit interactive components, message actions, dialogs, and legacy interactive
Expand All @@ -34,6 +35,7 @@ export type SlackAction = BlockAction | InteractiveMessage | DialogSubmitAction
* this case `ElementAction` must extend `BasicElementAction`.
*/
export interface SlackActionMiddlewareArgs<Action extends SlackAction = SlackAction> {
logger: Logger;
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,10 +1,12 @@
import { StringIndexed } from '../helpers';
import { SayFn, RespondFn, RespondArguments, AckFn } from '../utilities';
import { Logger } from '@slack/logger';

/**
* Arguments which listeners and middleware receive to process a slash command from Slack.
*/
export interface SlackCommandMiddlewareArgs {
logger: Logger;
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 @@ -2,11 +2,13 @@ export * from './base-events';
import { SlackEvent, BasicSlackEvent } from './base-events';
import { StringIndexed } from '../helpers';
import { SayFn } from '../utilities';
import { Logger } from '@slack/logger';

/**
* Arguments which listeners and middleware receive to process an event from Slack's Events API.
*/
export interface SlackEventMiddlewareArgs<EventType extends string = string> {
logger: Logger;
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
@@ -1,11 +1,13 @@
import { Option } from '@slack/types';
import { StringIndexed, XOR } from '../helpers';
import { AckFn } from '../utilities';
import { Logger } from '@slack/logger';

/**
* Arguments which listeners and middleware receive to process an options request from Slack
*/
export interface SlackOptionsMiddlewareArgs<Source extends OptionsSource = OptionsSource> {
logger: Logger;
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,5 +1,6 @@
import { StringIndexed } from '../helpers';
import { RespondArguments, AckFn } from '../utilities';
import { Logger } from '@slack/logger';

/**
* Known view action types
Expand All @@ -10,6 +11,7 @@ export type SlackViewAction = ViewSubmitAction | ViewClosedAction;
* Arguments which listeners and middleware receive to process a view submission event from Slack.
*/
export interface SlackViewMiddlewareArgs<ViewActionType extends SlackViewAction = SlackViewAction> {
logger: Logger;
payload: ViewOutput;
view: this['payload'];
body: ViewActionType;
Expand Down

0 comments on commit 1c0e33d

Please sign in to comment.