import { Injectable } from '@angular/core' ; import { getStatusText , InMemoryDbService , ParsedRequestUrl , RequestInfo , RequestInfoUtilities , ResponseOptions , STATUS , } from 'angular-in-memory-web-api' ; @ Injectable ( ) export class InMemoryDataService implements InMemoryDbService { async createDb ( ) { const response = await fetch ( 'assets/data/accounts.json' ) ; const accounts = await response . json ( ) ; const notificationsResponse = await fetch ( 'assets/data/notifications.json' ) ; const notifications = await notificationsResponse . json ( ) ; const subscriptionResponse = await fetch ( 'assets/data/subscription.json' ) ; const subscription = await subscriptionResponse . json ( ) ; const symbols = [ { id : 1 , code : 'MSFT' , entity : 'Microsoft' , } , { id : 2 , code : 'AAPL' , entity : 'Apple' , } , { id : 3 , code : 'GOOG' , entity : 'Google' , } , ] ; return { accounts , symbols , notifications , subscription } ; } parseRequestUrl ( url , utils ) { const newUrl = url . replace ( /\/notifications\/own/ , '/notifications' ) . replace ( /\/datapower\/serviceproxy/ , '/serviceproxy' ) . replace ( /\/nas\/cluster/ , '/cluster' ) . replace ( /\/layer7\/my.cnf/ , '/mycnf' ) ; const parsed = utils . parseRequestUrl ( newUrl ) ; return parsed ; } put ( reqInfo ) { const collectionName = reqInfo . collectionName ; if ( collectionName === 'accounts' ) { ( reqInfo . req as any ) . body = { id : reqInfo . id , ... ( reqInfo . req as any ) . body } ; } else if ( collectionName === 'notifications' || collectionName === 'subscription' ) { reqInfo . collection [ 0 ] . map ( ( item ) => { if ( item . id === reqInfo . id ) { return { id : reqInfo . id , ... ( reqInfo . req as any ) . body } ; } return item ; } ) ; const options = { status : STATUS . ACCEPTED , } ; return reqInfo . utils . createResponse$ ( ( ) => { return this . finishOptions ( options , reqInfo ) ; } ) ; } else { return undefined ; } } post ( reqInfo ) { const collectionName = reqInfo . collectionName ; if ( collectionName === 'notifications' || collectionName === 'subscription' ) { reqInfo . collection [ 0 ] . push ( ( reqInfo . req as any ) . body ) ; reqInfo . collection [ 1 ] = reqInfo . collection [ 1 ] + 1 ; const options = { status : STATUS . CREATED , } ; return reqInfo . utils . createResponse$ ( ( ) => { return this . finishOptions ( options , reqInfo ) ; } ) ; } else { return undefined ; } } private finishOptions ( options , { headers , url } ) { options . statusText = getStatusText ( options . status ) ; options . headers = headers ; options . url = url ; return options ; } }