import { Injectable } from '@angular/core' ; import { HttpRequest , HttpResponse , HttpHandler , HttpEvent , HttpInterceptor , HTTP_INTERCEPTORS } from '@angular/common/http' ; import { Observable , of , throwError } from 'rxjs' ; import { delay , mergeMap , materialize , dematerialize } from 'rxjs/operators' ; import { User } from '@app/_models' ; const users = [ { id : 1 , username : 'test' , password : 'test' , firstName : 'Test' , lastName : 'User' } ] ; @ Injectable ( ) export class FakeBackendInterceptor implements HttpInterceptor { intercept ( request , next ) { const { url , method , headers , body } = request ; return of ( null ) . pipe ( mergeMap ( handleRoute ) ) . pipe ( materialize ( ) ) . pipe ( delay ( 500 ) ) . pipe ( dematerialize ( ) ) ; function handleRoute ( ) { switch ( true ) { case url . endsWith ( '/users/authenticate' ) && method === 'POST' : return authenticate ( ) ; case url . endsWith ( '/users' ) && method === 'GET' : return getUsers ( ) ; default : return next . handle ( request ) ; } } function authenticate ( ) { const { username , password } = body ; const user = users . find ( x => x . username === username && x . password === password ) ; if ( ! user ) return error ( 'Username or password is incorrect' ) ; return ok ( { id : user . id , username : user . username , firstName : user . firstName , lastName : user . lastName } ) ; } function getUsers ( ) { if ( ! isLoggedIn ( ) ) return unauthorized ( ) ; return ok ( users ) ; } function ok ( body ? ) { return of ( new HttpResponse ( { status : 200 , body } ) ) ; } function error ( message ) { return throwError ( { error : { message } } ) ; } function unauthorized ( ) { return throwError ( { status : 401 , error : { message : 'Unauthorised' } } ) ; } function isLoggedIn ( ) { return headers . get ( 'Authorization' ) === `Basic ${ window . btoa ( 'test:test' ) }` ; } } } export let fakeBackendProvider = { provide : HTTP_INTERCEPTORS , useClass : FakeBackendInterceptor , multi : true } ;