2021-06-03 03:36:25 +00:00
|
|
|
import {Injectable, Inject} from '../../di'
|
2022-03-29 06:14:46 +00:00
|
|
|
import {ErrorWithContext, Safe} from '../../util'
|
2021-06-03 03:36:25 +00:00
|
|
|
import {Request} from '../lifecycle/Request'
|
2021-03-07 19:26:14 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Type alias describing some inflated session data.
|
|
|
|
*/
|
2021-03-07 19:26:14 +00:00
|
|
|
export type SessionData = {[key: string]: any}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Error thrown when a session is requested for a key that does not exist.
|
|
|
|
*/
|
2021-03-07 19:26:14 +00:00
|
|
|
export class NoSessionKeyError extends ErrorWithContext {
|
|
|
|
constructor() {
|
|
|
|
super('No session ID has been set.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* Error thrown when a session operation is performed before the session has been loaded.
|
|
|
|
*/
|
2021-03-07 19:26:14 +00:00
|
|
|
export class SessionNotLoadedError extends ErrorWithContext {
|
|
|
|
constructor() {
|
|
|
|
super('Cannot access session data; data is not loaded.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/**
|
|
|
|
* An abstract class representing a session driver.
|
|
|
|
* Some implementation of this is injected into the request.
|
|
|
|
*/
|
2021-03-07 19:26:14 +00:00
|
|
|
@Injectable()
|
|
|
|
export abstract class Session {
|
|
|
|
@Inject()
|
|
|
|
protected readonly request!: Request
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Get the unique key of this session. */
|
2021-03-07 19:26:14 +00:00
|
|
|
public abstract getKey(): string
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Set a unique key of this session. */
|
2021-03-07 19:26:14 +00:00
|
|
|
public abstract setKey(key: string): void
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Load the session data from the respective backend. */
|
2021-03-07 19:26:14 +00:00
|
|
|
public abstract load(): void | Promise<void>
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Save the session data into the respective backend. */
|
2021-03-07 19:26:14 +00:00
|
|
|
public abstract persist(): void | Promise<void>
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Get the loaded session data as an object. */
|
2021-03-07 19:26:14 +00:00
|
|
|
public abstract getData(): SessionData
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Bulk set an object as the session data. */
|
2021-03-07 19:26:14 +00:00
|
|
|
public abstract setData(data: SessionData): void
|
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Get a value from the session by key. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public abstract get(key: string, fallback?: unknown): any
|
2021-03-07 19:26:14 +00:00
|
|
|
|
2021-03-25 13:50:13 +00:00
|
|
|
/** Set a value in the session by key. */
|
2021-06-03 03:36:25 +00:00
|
|
|
public abstract set(key: string, value: unknown): void
|
2021-07-17 17:49:07 +00:00
|
|
|
|
|
|
|
/** Remove a key from the session data. */
|
|
|
|
public abstract forget(key: string): void
|
2022-03-29 06:14:46 +00:00
|
|
|
|
|
|
|
/** Load a key from the session as a Safe value. */
|
|
|
|
public safe(key: string): Safe {
|
|
|
|
return new Safe(this.get(key))
|
|
|
|
}
|
2021-03-07 19:26:14 +00:00
|
|
|
}
|