Skip to content

Commit

Permalink
feat(SessionStore): support custom session store (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin authored Apr 13, 2020
1 parent 291ea13 commit 1792773
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
16 changes: 16 additions & 0 deletions packages/bottender/src/__tests__/getSessionStore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import MemorySessionStore from '../session/MemorySessionStore';
import getBottenderConfig from '../shared/getBottenderConfig';
import getSessionStore from '../getSessionStore';
import { SessionDriver } from '../types';
Expand Down Expand Up @@ -72,3 +73,18 @@ it('should get RedisSessionStore when assigning redis as driver', () => {
});
expect(getSessionStore().constructor.name).toEqual('RedisSessionStore');
});

it('should get provided SessionStore when assigning custom driver', () => {
const memory2SessionStore = new MemorySessionStore();

getBottenderConfigMocked.mockReturnValueOnce({
session: {
driver: 'memory2',
stores: {
memory2: memory2SessionStore,
},
},
});

expect(getSessionStore()).toEqual(memory2SessionStore);
});
11 changes: 10 additions & 1 deletion packages/bottender/src/getSessionStore.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import warning from 'warning';

import SessionStore from './session/SessionStore';
import getBottenderConfig from './shared/getBottenderConfig';

function getSessionStore() {
function getSessionStore(): SessionStore {
const { session } = getBottenderConfig();

const sessionDriver = (session && session.driver) || 'memory';
Expand Down Expand Up @@ -44,6 +45,14 @@ function getSessionStore() {
);
}
default: {
// Support custom session stores by returning the session store instance
const customSessionStore:
| SessionStore
| undefined = (storesConfig as any)[sessionDriver];
if (customSessionStore) {
return customSessionStore;
}

warning(
false,
`Received unknown driver: ${sessionDriver}, so fallback it to \`memory\` driver.`
Expand Down
43 changes: 24 additions & 19 deletions packages/bottender/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ConsoleEvent, { ConsoleRawEvent } from './console/ConsoleEvent';
import Context from './context/Context';
import LineEvent from './line/LineEvent';
import MessengerEvent from './messenger/MessengerEvent';
import SessionStore from './session/SessionStore';
import SlackEvent from './slack/SlackEvent';
import TelegramEvent from './telegram/TelegramEvent';
import TwilioClient from './whatsapp/TwilioClient';
Expand Down Expand Up @@ -81,26 +82,30 @@ export enum SessionDriver {
}

export type SessionConfig = {
driver: SessionDriver;
driver: string;
expiresIn?: number;
stores: {
memory?: {
maxSize?: number;
};
file?: {
dirname?: string;
};
redis?: {
port?: number;
host?: string;
password?: string;
db?: number;
};
mongo?: {
url?: string;
collectionName?: string;
};
};
stores:
| {
memory?: {
maxSize?: number;
};
file?: {
dirname?: string;
};
redis?: {
port?: number;
host?: string;
password?: string;
db?: number;
};
mongo?: {
url?: string;
collectionName?: string;
};
}
| {
[P in Exclude<string, SessionDriver>]: SessionStore;
};
};

export type BottenderConfig = {
Expand Down

0 comments on commit 1792773

Please sign in to comment.