import { StreamResponse , DoOnStreamFns } from '../types/plugins' ; import { isFullString } from 'is-what' ; import { isDocModule , isCollectionModule } from './pathHelpers' ; export function logError ( errorMessage ) { console . error ( '[@magnetarjs error]\n' , errorMessage ) ; } export function logErrorAndThrow ( errorMessage ) { logError ( errorMessage ) ; throw new Error ( errorMessage ) ; } export function throwOnIncompleteStreamResponses ( streamInfoPerStore , doOnStreamFns ) { const noStreamLogic = ! Object . keys ( streamInfoPerStore ) . length ; if ( noStreamLogic ) { const errorMessage = 'None of your store plugins have implemented logic to open a stream.' ; logErrorAndThrow ( errorMessage ) ; } const noDoOnStreamLogic = ! Object . values ( doOnStreamFns ) . flat ( ) . length ; if ( noDoOnStreamLogic ) { const errorMessage = 'None of your store plugins have implemented logic to do something with the data coming in from streams.' ; logErrorAndThrow ( errorMessage ) ; } } export function throwIfNoFnsToExecute ( storesToExecute ) { if ( storesToExecute . length === 0 ) { const errorMessage = 'None of your store plugins have implemented this function or you have not defined an executionOrder anywhere.' ; logErrorAndThrow ( errorMessage ) ; } } export function throwIfNolocalStoreName ( localStoreName ) { if ( isFullString ( localStoreName ) ) return ; const errorMessage = `No 'localStoreName' provided.` ; logErrorAndThrow ( errorMessage ) ; } export function throwIfInvalidModulePath ( modulePath , moduleType ) { let errorMessage = '' ; if ( moduleType === 'collection' ) { if ( ! modulePath ) errorMessage = 'You must provide a collection id (or a "path" like so: collection/doc/collection).' ; if ( isDocModule ( modulePath ) ) errorMessage = `Your collection id (or "path") must be of odd segments. The expected pattern is: collection/doc/collection ... Yours was ${ modulePath }` ; } if ( moduleType === 'doc' ) { if ( ! modulePath ) errorMessage = 'You must provide a document id (or a "path" like so: collection/doc).' ; if ( isCollectionModule ( modulePath ) ) errorMessage = `Your doc id (or "path") must be of even segments. The expected pattern is: collection/doc/collection/doc ... Yours was ${ modulePath }` ; } if ( errorMessage ) logErrorAndThrow ( errorMessage ) ; }