/**
 * Hero-oriented InMemoryDbService with method overrides.
 */ import { Injectable } from '@angular/core' ; import { RequestInfo , RequestInfoUtilities , ParsedRequestUrl } from 'angular-in-memory-web-api' ; import { Hero , Villain } from './model' ; /** In-memory database data */ interface Db { [ collectionName ] : any [ ] ; } @ Injectable ( ) export class InMemoryDataService { /** True if in-mem service is intercepting; all requests pass thru when false. */ active = true ; /** In-memory database data */ db : Db = { } ; /** Create the in-memory database on start or by command */ createDb ( reqInfo ? ) { this . db = getDbData ( ) ; if ( reqInfo ) { const body = reqInfo . utils . getJsonBody ( reqInfo . req ) || { } ; if ( body . clear === true ) { for ( const coll in this . db ) { this . db [ coll ] . length = 0 ; } } this . active = ! ! body . active ; } return this . db ; } param url param utils parseRequestUrl ( url , utils ) { const parsed = utils . parseRequestUrl ( url ) ; parsed . collectionName = this . active ? mapCollectionName ( parsed . collectionName ) : undefined ; return parsed ; } } param name function mapCollectionName ( name ) { return ( ( { hero : 'heroes' , villain : 'villains' } as any ) [ name ] || name ) ; } /**
 * Development data
 */ function getDbData ( ) { const heroes = [ { id : 11 , name : 'Maxwell Smart' , saying : 'Missed it by that much.' } , { id : 12 , name : 'Bullwinkle J. Moose' , saying : 'Watch me pull a rabbit out of a hat.' } , { id : 13 , name : 'Muhammad Ali' , saying : 'Float like a butterfly, sting like a bee.' } , { id : 14 , name : 'Eleanor Roosevelt' , saying : 'No one can make you feel inferior without your consent.' } ] ; const villains = [ { id : 21 , name : 'Dr. Evil' , saying : 'One million dollars!' } , { id : 22 , name : 'Agent Smith' , saying : 'Human beings are a disease.' } , { id : 23 , name : 'Natasha Fatale' , saying : 'You can say that again, dahling.' } , { id : 24 , name : 'Goldfinger' , saying : 'No, I expect you to die!' } , { id : 25 , name : 'West Witch' , saying : 'I\'ll get you, my pretty, and your little dog too!' } , { id : 26 , name : 'Tony Montana' , saying : 'Say hello to my little friend.' } , ] ; return { heroes , villains } as Db ; }