import { HttpErrorResponse } from '@angular/common/http' ; import { Observable } from 'rxjs/Observable' ; import { of } from 'rxjs/observable/of' ; import { _throw } from 'rxjs/observable/throw' ; import { delay , dematerialize , materialize } from 'rxjs/operators' ; import { environment } from 'environments/environment' ; /**
 * this simulates the behaviour of Angular's http module:
 * if the status code is not a 2XX, it will return a failing Observable
 */ export function response < T = undefined > ( status ) { return responseBody < T > ( undefined , status ) ; } /**
 * this simulates the behaviour of Angular's http module:
 * if the status code is not a 2XX, it will return a failing Observable
 */ export function responseBody < T = undefined > ( body , status = 200 , error ? ) { if ( status >= 200 && status < 300 ) { return of ( body ) . pipe ( delay ( environment . httpDelay ) ) ; } else { return _throw ( new HttpErrorResponse ( { status , error } ) ) . pipe ( materialize ( ) , delay ( environment . httpDelay ) , dematerialize ( ) ) ; } } export function errorBackend < T = undefined > ( message , code ) { return responseBody ( undefined , code , { code , message } ) ; }