-
Notifications
You must be signed in to change notification settings - Fork 568
/
store.ts
32 lines (31 loc) · 1.11 KB
/
store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { Provide, Scope, ScopeEnum, Types } from '@midwayjs/core';
@Provide()
@Scope(ScopeEnum.Singleton)
export class SessionStoreManager {
private sessionStoreClz:
| (new (...args) => any)
| ((session: any) => new (...args) => any);
private sessionStore;
private sessionStoreOptions: any;
setSessionStore(sessionStore, options = {}) {
this.sessionStoreClz = sessionStore;
this.sessionStoreOptions = options;
}
getSessionStore(session?) {
if (!this.sessionStore && this.sessionStoreClz) {
if (Types.isClass(this.sessionStoreClz)) {
this.sessionStore = new (this.sessionStoreClz as new (...args) => any)(
this.sessionStoreOptions
);
} else if (typeof this.sessionStoreClz === 'function') {
// 因为 session 扩展的规范是传入 express-session 对象,所以这里需要传入 session
this.sessionStore = new ((
this.sessionStoreClz as (session: any) => new (...args) => any
)(session))(this.sessionStoreOptions);
} else {
this.sessionStore = this.sessionStoreClz;
}
}
return this.sessionStore;
}
}