import { HasInfrastructure , ProviderLibrary , RocketDescriptor } from '@boostercloud/framework-types' ; import { DynamoDB } from 'aws-sdk' ; import { requestFailed , requestSucceeded } from './library/api-gateway-io' ; import { deleteConnectionData , fetchConnectionData , sendMessageToConnection , storeConnectionData , } from './library/connections-adapter' ; import { rawEventsToEnvelopes , readEntityEventsSince , readEntityLatestSnapshot , storeEvents , } from './library/events-adapter' ; import { searchEvents } from './library/events-searcher-adapter' ; import { rawGraphQLRequestToEnvelope } from './library/graphql-adapter' ; import { deleteReadModel , fetchReadModel , rawReadModelEventsToEnvelopes , storeReadModel , } from './library/read-models-adapter' ; import { searchReadModel } from './library/read-models-searcher-adapter' ; import { rawScheduledInputToEnvelope } from './library/scheduled-adapter' ; import { deleteAllSubscriptions , deleteSubscription , fetchSubscriptions , subscribeToReadModel , } from './library/subscription-adapter' ; const dynamoDB = new DynamoDB . DocumentClient ( { maxRetries : 10 , httpOptions : { timeout : 2000 , } , } ) ; export function loadInfrastructurePackage ( packageName ) { return require ( packageName ) ; } /**
 * `Provider` is a function that accepts a list of rocket names and returns an
 * object compatible with the `ProviderLibrary` defined in the `framework-types` package.
 * The rocket names are passed to the infrastructure package, which loads them dynamically
 * to extend the AWS functionality. Rockets are typically distributed in separate node packages.
 */ export const Provider = ( rockets ? ) : ProviderLibrary => { return { events : { rawToEnvelopes : rawEventsToEnvelopes , forEntitySince : readEntityEventsSince . bind ( null , dynamoDB ) , latestEntitySnapshot : readEntityLatestSnapshot . bind ( null , dynamoDB ) , search : searchEvents . bind ( null , dynamoDB ) , store : storeEvents . bind ( null , dynamoDB ) , } , readModels : { rawToEnvelopes : rawReadModelEventsToEnvelopes , fetch : fetchReadModel . bind ( null , dynamoDB ) , search : searchReadModel . bind ( null , dynamoDB ) , store : storeReadModel . bind ( null , dynamoDB ) , delete : deleteReadModel . bind ( null , dynamoDB ) , subscribe : subscribeToReadModel . bind ( null , dynamoDB ) , fetchSubscriptions : fetchSubscriptions . bind ( null , dynamoDB ) , deleteSubscription : deleteSubscription . bind ( null , dynamoDB ) , deleteAllSubscriptions : deleteAllSubscriptions . bind ( null , dynamoDB ) , } , graphQL : { rawToEnvelope : rawGraphQLRequestToEnvelope , handleResult : requestSucceeded , } , api : { requestSucceeded , requestFailed , } , connections : { storeData : storeConnectionData . bind ( null , dynamoDB ) , fetchData : fetchConnectionData . bind ( null , dynamoDB ) , deleteData : deleteConnectionData . bind ( null , dynamoDB ) , sendMessage : sendMessageToConnection , } , scheduled : { rawToEnvelope : rawScheduledInputToEnvelope , } , infrastructure : ( ) => { const infrastructurePackageName = require ( '../package.json' ) . name + '-infrastructure' ; let infrastructure ; try { infrastructure = loadInfrastructurePackage ( infrastructurePackageName ) ; } catch ( e ) { throw new Error ( `The AWS infrastructure package could not be loaded. The following error was thrown: ${ e . message }. Please ensure that one of the following actions has been done:\n` + `  - It has been specified in your "devDependencies" section of your "package.json" file. You can do so by running 'npm install --save-dev ${ infrastructurePackageName }'\n` + `  - Or it has been installed globally. You can do so by running 'npm install -g ${ infrastructurePackageName }'` ) ; } return infrastructure . Infrastructure ( rockets ) ; } , } ; } ; export * from './constants' ;