import { createInstance , createInstances } from '@boostercloud/framework-common-helpers' ; import { BoosterConfig , Class , EntityInterface , EventFilter , EventSearchResponse , FilterFor , FinderByKeyFunction , Logger , ReadModelInterface , ReadOnlyNonEmptyArray , Searcher , SearcherFunction , SequenceKey , UUID , } from '@boostercloud/framework-types' ; import { BoosterEventDispatcher } from './booster-event-dispatcher' ; import { BoosterGraphQLDispatcher } from './booster-graphql-dispatcher' ; import { buildLogger } from './booster-logger' ; import { BoosterScheduledCommandDispatcher } from './booster-scheduled-command-dispatcher' ; import { BoosterSubscribersNotifier } from './booster-subscribers-notifier' ; import { Importer } from './importer' ; import { EventStore } from './services/event-store' ; import { BoosterRocketDispatcher } from './booster-rocket-dispatcher' ; /**
 * Main class to interact with Booster and configure it.
 * Sensible defaults are used whenever possible:
 * - `provider`: `Provider.AWS`
 * - `appName`: `new-booster-app`
 * - `region`: 'eu-west-1'
 *
 */ export class Booster { public static readonly configuredEnvironments : Set < string > = new Set < string > ( ) ; private static logger : Logger ; public static readonly config = new BoosterConfig ( checkAndGetCurrentEnv ( ) ) ; /**
     * Avoid creating instances of this class
     */ private constructor ( ) { } public static configureCurrentEnv ( configurator ) { configurator ( this . config ) ; } param environment param configurator public static configure ( environment , configurator ) { this . configuredEnvironments . add ( environment ) ; if ( this . config . environmentName === environment ) { configurator ( this . config ) ; } } /**
     * Initializes the Booster project
     */ public static start ( codeRootPath ) { const projectRootPath = codeRootPath . replace ( new RegExp ( this . config . codeRelativePath + '$' ) , '' ) ; this . config . userProjectRootPath = projectRootPath ; Importer . importUserProjectFiles ( codeRootPath ) ; this . logger = buildLogger ( this . config . logLevel ) ; this . config . validate ( ) ; } param readModelClass public static readModel < TReadModel extends ReadModelInterface > ( readModelClass ) { const searchFunction = async ( readModelName , filters , limit ? , afterCursor ? , paginatedVersion ? ) => { const searchResult = await this . config . provider . readModels . search ( this . config , this . logger , readModelName , filters , limit , afterCursor , paginatedVersion ) ; if ( ! Array . isArray ( searchResult ) ) { return { ... searchResult , items : createInstances ( readModelClass , searchResult . items ) , } ; } return createInstances ( readModelClass , searchResult ) ; } ; const finderByIdFunction = async ( readModelName , id , sequenceKey ? ) => { const readModels = await this . config . provider . readModels . fetch ( this . config , this . logger , readModelName , id , sequenceKey ) ; if ( sequenceKey ) { return readModels as ReadOnlyNonEmptyArray < TReadModel > ; } return readModels [ 0 ] as TReadModel ; } ; return new Searcher ( readModelClass , searchFunction , finderByIdFunction ) ; } public static async events ( filters ) { const events = await this . config . provider . events . search ( this . config , this . logger , filters ) ; return events . map ( ( event ) => { const eventMetadata = this . config . events [ event . type ] ; event . value = createInstance ( eventMetadata . class , event . value ) ; return event ; } ) ; } param entityClass param entityID public static async entity < TEntity extends EntityInterface > ( entityClass , entityID ) { const eventStore = new EventStore ( this . config , this . logger ) ; const entitySnapshotEnvelope = await eventStore . fetchEntitySnapshot ( entityClass . name , entityID ) ; return entitySnapshotEnvelope ? createInstance ( entityClass , entitySnapshotEnvelope . value ) : undefined ; } /**
     * Dispatches event messages to your application.
     */ public static dispatchEvent ( rawEvent ) { return BoosterEventDispatcher . dispatch ( rawEvent , this . config , this . logger ) ; } public static serveGraphQL ( request ) { return new BoosterGraphQLDispatcher ( this . config , this . logger ) . dispatch ( request ) ; } public static triggerScheduledCommand ( request ) { return new BoosterScheduledCommandDispatcher ( this . config , this . logger ) . dispatch ( request ) ; } public static notifySubscribers ( request ) { return new BoosterSubscribersNotifier ( this . config , this . logger ) . dispatch ( request ) ; } public static dispatchRocket ( request ) { return new BoosterRocketDispatcher ( this . config , this . logger ) . dispatch ( request ) ; } } function checkAndGetCurrentEnv ( ) { const env = process . env . BOOSTER_ENV ; if ( ! env || env . trim ( ) . length == 0 ) { throw new Error ( 'Booster environment is missing. You need to provide an environment to configure your Booster project' ) ; } return env ; } export async function boosterEventDispatcher ( rawEvent ) { return Booster . dispatchEvent ( rawEvent ) ; } export async function boosterServeGraphQL ( rawRequest ) { return Booster . serveGraphQL ( rawRequest ) ; } export async function boosterTriggerScheduledCommand ( rawRequest ) { return Booster . triggerScheduledCommand ( rawRequest ) ; } export async function boosterNotifySubscribers ( rawRequest ) { return Booster . notifySubscribers ( rawRequest ) ; } export async function boosterRocketDispatcher ( rawRequest ) { return Booster . dispatchRocket ( rawRequest ) ; }