UNPKG

@breautek/storm

Version:

Object-Oriented REST API framework

161 lines (160 loc) 5.39 kB
import { EventEmitter } from 'events'; import { TokenManager } from './TokenManager'; import { Database } from './Database'; import { Handler } from './Handler'; import { IConfig } from './IConfig'; import { Command } from 'commander'; import { IAuthTokenData } from '@arashi/token'; import { BaseLogger } from '@arashi/logger'; export interface IStormCLIArgs { bind?: string; port?: number; authentication_header?: string; configFile?: string; localConfigFile?: string; custom: Record<string, any>; shard?: number; } /** * Main entry point for the Application. Should be extended and have the abstract methods implemented. */ export declare abstract class Application<TConfig extends IConfig = IConfig, TAuthToken extends IAuthTokenData = IAuthTokenData, TDBConfig = any, TDBConnectionAPI = any> extends EventEmitter { private $logger; private $name; /** * Path to the config directory. * This is used as a fallback and it will be expected that * bt-config.json and bt-local-config.json are found. */ private $configDir; /** * Path to a bt-config.json file */ private $configPath; /** * Path to a bt-local-config.json file */ private $localConfigPath; private $config; private $tokenManager; private $server; private $db; private $socket; private $program; private $promServer; private $usingDeprecatedConfigPath; /** * * @param name The application name * @param configPath @deprecated The directory where bt-config.json and bt-local-config.json can be found. Defaults to current working directory. */ constructor(name: string, configPath?: string); start(): Promise<void>; private $initPrometheus; /** * Default algorithm is to take main port, and add 595 to it. * Still -- it is better to explicitly set the prometheus port. * * @param mainPort * @returns */ getDefaultPortForPrometheus(): number; private $load; protected _initialize(config: TConfig): Promise<void>; protected _createLogger(config: TConfig): BaseLogger; protected _initLogger(config: TConfig): Promise<BaseLogger>; private $connectCW; private $validateCWConfig; getLogger(): BaseLogger; getPort(): number; getVersion(): string; protected _getVersion(): string; private $getVersionString; private $buildArgOptions; getShard(): number; isPrometheusEnabled(): boolean; protected _buildArgOptions(program: Command): void; getProgram(): Command; /** * Override this method to map CLI args to customConfig * @param args */ getConfigFromCLIArgs(args: any): Record<string, any>; /** * The maximum size limit for incoming requests that this service needs to handle. */ getRequestSizeLimit(): number; private $attachHandlers; /** * * @param path The URL API path. E.g. /api/myService/myCommand/ * @param Handler */ attachHandler(path: string, handler: Handler): void; attachHandlerInstance(path: string, handler: Handler): void; close(): Promise<void>; protected _closeDatabase(): Promise<void>; protected _closeSocket(): Promise<void>; /** * Subclasses are expected to attach the API handlers for their service. This will be invoked during application startup. * @returns Promise<void> */ protected abstract _attachHandlers(): Promise<void>; /** * @deprecated Supply the configs via --config and --local-config arguments * * @param path The directory path that contains bt-config.json and bt-local-config.json */ loadConfig(path: string): Promise<TConfig>; private $loadConfig; getConfigFilePath(): string; getLocalConfigFilePath(): string; private $getLocalConfigFilePath; private $getConfigFilePath; /** * @returns the application name */ getName(): string; private $getLogger; /** * @returns the config object. */ getConfig(): TConfig; /** * @returns true if the Application should bind to an IP address */ shouldListen(): boolean; /** * Invoked once the config has been loaded and ready to be used. * * @param config The config object (as defined in bt-config.json/bt-local-config.json) */ protected _onConfigLoad(config: TConfig): void; /** * Sets the TokenManager to be used for authentication. * @param tokenManager */ setTokenManager(tokenManager: TokenManager<TAuthToken>): void; /** * @returns the token manager */ getTokenManager(): TokenManager<TAuthToken>; /** * @returns the database pool. This will need to be casted based on your preferred database dialect. */ getDB(): Database<TDBConfig, TDBConnectionAPI>; /** * @returns command line arguments */ getCmdLineArgs(): IStormCLIArgs; /** * Subclasses are expected to override this to configure their database setup, if the service uses a database. * @param config The bt-config object */ protected _initDB(config: TConfig): Promise<Database<TDBConfig, TDBConnectionAPI>>; protected _onBeforeReadyAsync(): Promise<void>; /** * Invoked when the application is considered ready for operation. */ protected _onReady(): void; }