import { Request, Response, Application } from "express"; import { Db } from "mongodb"; import { Middleware } from "@swirl/api"; import { RedisConnection } from "./redis"; import Mailer from "./mail/mailer"; import { JobRunner } from "./job"; import { MessageSet } from "./mail/message-set"; import { AdminMessageType } from "./mail/admin-templates"; declare module "express" { interface Request { /** The information about the Admin this request belongs to. */ admin: AdminData; } } /** Contains an admin context. */ export default class Admin { /** The options for this admin. */ readonly options: AdminOptions; /** The cached mongo connection pool. */ private cachedDatabase; /** The active job runners that are managed by this admin. */ readonly runners: JobRunner[]; /** The job names currently being used, maped to the jobs they require. */ readonly jobMap: Map; /** The message set for the builting admin messages */ readonly messages: MessageSet; /** Handles serving the admin interface via Express.js */ router: Application; /** Construct given the admin options. */ constructor(options: AdminOptions); /** Returns a promise for the database this admin uses. */ db(): Promise; /** * Creates a middleware that injects the admin information onto requests. * @param app the application to register the app mountpath from */ inject(app?: Application): (req: Request, res: Response, next: Function) => void; /** * Creates a middleware that validates user sessions before running a request. * @param groups if specified only users belong to any of these groups will be allowed to access the handler. */ secure(...groups: string[]): Middleware; /** * Runs a job on its specified schedule. * @param jobPath the absolute path to the job to run. */ runJob(jobPath: string): void; } /** Options used to create an admin. */ export interface AdminOptions { /** The Mongo DB connection URL that this admin will use. */ mongoUrl: string; /** The Redis connection URL that this admin will use. */ redisUrl: string; /** The URL to redirect to after a user logs in. */ loginRedirect?: string; /** The URL to redirect to after a user logs out. */ logoutRedirect?: string; /** The base path of the application on the server. */ basePath?: string; /** The domain of the session cookies (if not set then the app domain is used) */ cookieDomain?: string; } /** Contains data about the admin this api is running under. */ export interface AdminData { /** The Mongo DB database containing the admin information. */ mongo: Db; /** * A redis client connected to the admin's database. * This client is automatically closed when the response is finished. */ redis: RedisConnection; /** The mailer to send emails with this admin's settings. */ mail: Mailer; /** The Admin instance that was injected onto the request. */ instance: Admin; /** The path that the admin instance was mounted at. */ mountPath: string; /** The path that the user app was mounted at. */ userMountPath: string; /** The options originally passed when constructing the admin object */ readonly options: AdminOptions; }