-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add getSessionStore api (#633)
- Loading branch information
1 parent
562661c
commit dbaf4d2
Showing
8 changed files
with
154 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import getBottenderConfig from '../shared/getBottenderConfig'; | ||
import getSessionStore from '../getSessionStore'; | ||
import { SessionDriver } from '../types'; | ||
|
||
jest.mock('../shared/getBottenderConfig'); | ||
|
||
const getBottenderConfigMocked = getBottenderConfig as jest.MockedFunction< | ||
typeof getBottenderConfig | ||
>; | ||
|
||
it('should get MemorySessionStore by default', () => { | ||
getBottenderConfigMocked.mockReturnValueOnce({}); | ||
expect(getSessionStore().constructor.name).toEqual('MemorySessionStore'); | ||
}); | ||
|
||
it('should get MemorySessionStore when assigning memory as driver', () => { | ||
getBottenderConfigMocked.mockReturnValueOnce({ | ||
session: { | ||
driver: SessionDriver.Memory, | ||
stores: { | ||
memory: { | ||
maxSize: 500, | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(getSessionStore().constructor.name).toEqual('MemorySessionStore'); | ||
}); | ||
|
||
it('should get FileSessionStore when assigning file as driver', () => { | ||
getBottenderConfigMocked.mockReturnValueOnce({ | ||
session: { | ||
driver: SessionDriver.File, | ||
stores: { | ||
file: { | ||
dirname: '.sessions', | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(getSessionStore().constructor.name).toEqual('FileSessionStore'); | ||
}); | ||
|
||
it('should get MongoSessionStore when assigning mongo as driver', () => { | ||
getBottenderConfigMocked.mockReturnValueOnce({ | ||
session: { | ||
driver: SessionDriver.Mongo, | ||
stores: { | ||
mongo: { | ||
url: 'mongodb://localhost:27017', | ||
collectionName: 'sessions', | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(getSessionStore().constructor.name).toEqual('MongoSessionStore'); | ||
}); | ||
|
||
it('should get RedisSessionStore when assigning redis as driver', () => { | ||
getBottenderConfigMocked.mockReturnValueOnce({ | ||
session: { | ||
driver: SessionDriver.Redis, | ||
stores: { | ||
redis: { | ||
port: 6379, | ||
host: '127.0.0.1', | ||
password: 'auth', | ||
db: 0, | ||
}, | ||
}, | ||
}, | ||
}); | ||
expect(getSessionStore().constructor.name).toEqual('RedisSessionStore'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import warning from 'warning'; | ||
|
||
import getBottenderConfig from './shared/getBottenderConfig'; | ||
|
||
function getSessionStore() { | ||
const bottenderConfig = getBottenderConfig(); | ||
|
||
const { session } = bottenderConfig; | ||
|
||
const sessionDriver = (session && session.driver) || 'memory'; | ||
|
||
const storesConfig = (session && session.stores) || {}; | ||
|
||
switch (sessionDriver) { | ||
case 'memory': { | ||
const MemorySessionStore = require('./session/MemorySessionStore') | ||
.default; | ||
|
||
return new MemorySessionStore( | ||
storesConfig.memory || {}, | ||
session && session.expiresIn | ||
); | ||
} | ||
case 'file': { | ||
const FileSessionStore = require('./session/FileSessionStore').default; | ||
|
||
return new FileSessionStore( | ||
storesConfig.file || {}, | ||
session && session.expiresIn | ||
); | ||
} | ||
case 'mongo': { | ||
const MongoSessionStore = require('./session/MongoSessionStore').default; | ||
|
||
return new MongoSessionStore( | ||
storesConfig.mongo || {}, | ||
session && session.expiresIn | ||
); | ||
} | ||
case 'redis': { | ||
const RedisSessionStore = require('./session/RedisSessionStore').default; | ||
|
||
return new RedisSessionStore( | ||
storesConfig.redis || {}, | ||
session && session.expiresIn | ||
); | ||
} | ||
default: { | ||
warning( | ||
false, | ||
`Received unknown driver: ${sessionDriver}, so fallback it to \`memory\` driver.` | ||
); | ||
const MemorySessionStore = require('./session/MemorySessionStore') | ||
.default; | ||
|
||
return new MemorySessionStore( | ||
storesConfig.memory || {}, | ||
session && session.expiresIn | ||
); | ||
} | ||
} | ||
} | ||
|
||
export default getSessionStore; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters