import { Observable , pipe , of , EMPTY } from 'rxjs' ; import { mergeMap , catchError , map } from 'rxjs/operators' ; import { pipeFromArray } from '@marblejs/core/dist/+internal/utils' ; import { HttpRequest } from '../http.interface' ; import { HttpEffectResponse } from '../effects/http.effects.interface' ; import { RouteCombinerConfig , RouteEffectGroup , ErrorSubject } from './http.router.interface' ; export const isRouteEffectGroup = ( item ) : item is RouteEffectGroup => Array . isArray ( item . effects ) && Array . isArray ( item . middlewares ) ; export const isRouteCombinerConfig = ( item ) : item is RouteCombinerConfig => Array . isArray ( item . effects ) ; export const decorateEffect = ( stream , errorSubject ) => { stream . pipe = ( ... operations ) : Observable < any > => pipe ( mergeMap ( ( request ) => pipeFromArray ( [ ... operations , map ( ( res ) => ( { ... res , request } ) ) , catchError ( ( error ) => { errorSubject . next ( { error , request } ) ; return EMPTY ; } ) , ] ) ( of ( request ) ) as Observable < HttpEffectResponse > ) ) ( stream ) ; return stream ; } ; export const decorateMiddleware = ( stream , errorSubject ) => { stream . pipe = ( ... operations ) : Observable < any > => pipe ( mergeMap ( ( request ) => pipeFromArray ( [ ... operations , catchError ( ( error ) => { errorSubject . next ( { error , request } ) ; return EMPTY ; } ) , ] ) ( of ( request ) ) as Observable < HttpRequest > ) ) ( stream ) ; return stream ; } ;