import {Middleware, Injectable, Inject, Config, Session} from '@extollo/lib' import {ColorPalette} from '../../configs/app.config' /** * SiteTheme Middleware * -------------------------------------------- * Determines the correct color theme for the request. */ @Injectable() export class SiteTheme extends Middleware { @Inject() protected readonly config!: Config @Inject() protected readonly session!: Session public async apply() { const themes = this.config.get('app.colors') as Record const themeKeys = Object.keys(themes) const existingThemeName = this.session.get('theme.name') const forceNewTheme = Boolean(this.request.input('forceNewTheme')) const explicitTheme = String(this.request.input('theme') || '') if ( explicitTheme && themeKeys.includes(explicitTheme) ) { this.session.set('theme.name', explicitTheme) return } if ( existingThemeName && !forceNewTheme ) { return } const themeName = themeKeys[Math.floor(Math.random() * themeKeys.length)] this.session.set('theme.name', themeName) } }