import { Observable } from 'rxjs/internal/Observable' ; import { of } from 'rxjs/internal/observable/of' ; import { OperatorFunction } from 'rxjs/internal/types' ; import { switchMap , map , catchError , startWith , take } from 'rxjs/operators' ; export type Status = 'succeeded' | 'failed' | 'pending' ; export interface Http < Req , Res > { status : Status ; req : Req ; res ? : Res ; error ? : any ; } param withAjax export function http < Req , Res > ( withAjax ) { return switchMap ( ( req ) => withAjax ( req ) . pipe ( map ( res => ( { status : 'succeeded' as Status , req , res } ) ) , startWith ( { status : 'pending' as Status , req } ) , catchError ( res => of ( { status : 'failed' as Status , req , res , error : res . xhr . response } ) ) , take ( 2 ) ) ) ; }