Add basic memory-based session driver
This commit is contained in:
39
src/http/session/Session.ts
Normal file
39
src/http/session/Session.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import {Injectable, Inject} from "@extollo/di"
|
||||
import {ErrorWithContext} from "@extollo/util"
|
||||
import {Request} from "../lifecycle/Request"
|
||||
|
||||
export type SessionData = {[key: string]: any}
|
||||
|
||||
export class NoSessionKeyError extends ErrorWithContext {
|
||||
constructor() {
|
||||
super('No session ID has been set.')
|
||||
}
|
||||
}
|
||||
|
||||
export class SessionNotLoadedError extends ErrorWithContext {
|
||||
constructor() {
|
||||
super('Cannot access session data; data is not loaded.')
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export abstract class Session {
|
||||
@Inject()
|
||||
protected readonly request!: Request
|
||||
|
||||
public abstract getKey(): string
|
||||
|
||||
public abstract setKey(key: string): void
|
||||
|
||||
public abstract load(): void | Promise<void>
|
||||
|
||||
public abstract persist(): void | Promise<void>
|
||||
|
||||
public abstract getData(): SessionData
|
||||
|
||||
public abstract setData(data: SessionData): void
|
||||
|
||||
public abstract get(key: string, fallback?: any): any
|
||||
|
||||
public abstract set(key: string, value: any): void
|
||||
}
|
||||
Reference in New Issue
Block a user