-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapplication.ts
More file actions
59 lines (53 loc) · 1.77 KB
/
Copy pathapplication.ts
File metadata and controls
59 lines (53 loc) · 1.77 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import assert from 'node:assert';
import { EggCore } from '@eggjs/core';
import type { SessionConfig } from '../../config/config.default.js';
export type SessionStore = Required<SessionConfig>['store'];
export type SessionStoreOrAppSessionStoreClass = SessionStore | {
new(app: Application): SessionStore;
};
export default class Application extends EggCore {
/**
* set session external store
*
* ```js
* app.sessionStore = {
* get(key): Promise<unknown>,
* set(key, data): Promise<void>,
* destroy(key): Promise<void>,
* };
*
* app.sessionStore = class SessionStore {
* constructor(app) {
* }
* get(key): Promise<unknown>,
* set(key, data): Promise<void>,
* destroy(key): Promise<void>,
* }
* ```
* @param {Class|Object} store session store class or instance
*/
set sessionStore(store: SessionStoreOrAppSessionStoreClass | null | undefined) {
if (this.config.session.store && this.config.env !== 'unittest') {
this.coreLogger.warn('[@eggjs/session] sessionStore already exists and will be overwrite');
}
// support this.sessionStore = null to disable external store
if (!store) {
this.config.session.store = undefined;
this.coreLogger.info('[@eggjs/session] sessionStore is disabled');
return;
}
if (typeof store === 'function') {
store = new store(this);
}
assert(typeof store.get === 'function', 'store.get must be function');
assert(typeof store.set === 'function', 'store.set must be function');
assert(typeof store.destroy === 'function', 'store.destroy must be function');
this.config.session.store = store;
}
/**
* get sessionStore instance
*/
get sessionStore(): SessionStore | undefined {
return this.config.session.store;
}
}