TypeDoc all the thngs

This commit is contained in:
2021-03-25 08:50:13 -05:00
parent 7cb0546b01
commit fad1184afe
52 changed files with 976 additions and 3 deletions

View File

@@ -1,10 +1,17 @@
import {NoSessionKeyError, Session, SessionData, SessionNotLoadedError} from "./Session";
import {Injectable} from "@extollo/di";
/**
* Implementation of the session driver that stores session data in memory.
* This is the default, for compatibility, but it is recommended that you replace
* this driver with one with a persistent backend.
*/
@Injectable()
export class MemorySession extends Session {
/** Mapping of session key to session data object. */
private static sessionsByID: {[key: string]: SessionData} = {}
/** Get a particular session by ID. */
private static getSession(id: string) {
if ( !this.sessionsByID[id] ) {
this.sessionsByID[id] = {} as SessionData
@@ -13,11 +20,15 @@ export class MemorySession extends Session {
return this.sessionsByID[id]
}
/** Store the given session data by its ID. */
private static setSession(id: string, data: SessionData) {
this.sessionsByID[id] = data
}
/** The ID of this session. */
protected sessionID?: string
/** The associated data for this session. */
protected data?: SessionData
constructor() { super() }

View File

@@ -2,38 +2,59 @@ import {Injectable, Inject} from "@extollo/di"
import {ErrorWithContext} from "@extollo/util"
import {Request} from "../lifecycle/Request"
/**
* Type alias describing some inflated session data.
*/
export type SessionData = {[key: string]: any}
/**
* Error thrown when a session is requested for a key that does not exist.
*/
export class NoSessionKeyError extends ErrorWithContext {
constructor() {
super('No session ID has been set.')
}
}
/**
* Error thrown when a session operation is performed before the session has been loaded.
*/
export class SessionNotLoadedError extends ErrorWithContext {
constructor() {
super('Cannot access session data; data is not loaded.')
}
}
/**
* An abstract class representing a session driver.
* Some implementation of this is injected into the request.
*/
@Injectable()
export abstract class Session {
@Inject()
protected readonly request!: Request
/** Get the unique key of this session. */
public abstract getKey(): string
/** Set a unique key of this session. */
public abstract setKey(key: string): void
/** Load the session data from the respective backend. */
public abstract load(): void | Promise<void>
/** Save the session data into the respective backend. */
public abstract persist(): void | Promise<void>
/** Get the loaded session data as an object. */
public abstract getData(): SessionData
/** Bulk set an object as the session data. */
public abstract setData(data: SessionData): void
/** Get a value from the session by key. */
public abstract get(key: string, fallback?: any): any
/** Set a value in the session by key. */
public abstract set(key: string, value: any): void
}

View File

@@ -13,10 +13,15 @@ import {Session} from "./Session";
import {Logging} from "../../service/Logging";
import {Config} from "../../service/Config";
/**
* A dependency injection factory that matches the abstract Session class
* and produces an instance of the configured session driver implementation.
*/
export class SessionFactory extends AbstractFactory {
protected readonly logging: Logging
protected readonly config: Config
/** True if we have printed the memory session warning at least once. */
private static loggedMemorySessionWarningOnce = false
constructor() {
@@ -52,6 +57,11 @@ export class SessionFactory extends AbstractFactory {
return meta
}
/**
* Return the instantiable class of the configured session backend.
* @protected
* @return Instantiable<Session>
*/
protected getSessionClass() {
const SessionClass = this.config.get('server.session.driver', MemorySession)
if ( SessionClass === MemorySession && !SessionFactory.loggedMemorySessionWarningOnce ) {