"integrity": "sha1-l6ERlkmyEa0zaR2fn0hqjsn74KM=",
"dev": true
},
+ "angular-in-memory-web-api": {
+ "version": "0.11.0",
+ "resolved": "https://registry.npmjs.org/angular-in-memory-web-api/-/angular-in-memory-web-api-0.11.0.tgz",
+ "integrity": "sha512-QV1qYHm+Zd+wrvlcPLnAcqqGpOmCN1EUj4rRuYHpek8+QqFFdxBNuPZOJCKvU7I97z5QSKHsdc6PNKlpUQr3UA=="
+ },
"ansi-colors": {
"version": "3.2.4",
"resolved": "https://registry.npmjs.org/ansi-colors/-/ansi-colors-3.2.4.tgz",
"@angular/platform-browser": "~9.1.5",
"@angular/platform-browser-dynamic": "~9.1.5",
"@angular/router": "~9.1.5",
+ "angular-in-memory-web-api": "^0.11.0",
"rxjs": "~6.5.5",
"tslib": "^1.10.0",
"zone.js": "~0.10.2"
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
+import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
+import { InMemoryDataService } from './in-memory-data.service';
+
import { AppComponent } from './app.component';
import { HeroesComponent } from './heroes/heroes.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
BrowserModule,
AppRoutingModule,
FormsModule,
- HttpClientModule
+ HttpClientModule,
+
+ // The HttpClientInMemoryWebApiModule module intercepts HTTP requests
+ // and returns simulated server responses.
+ // Remove it when a real server is ready to receive requests.
+ HttpClientInMemoryWebApiModule.forRoot(
+ InMemoryDataService, { dataEncapsulation: false }
+ )
],
providers: [],
bootstrap: [AppComponent]
--- /dev/null
+import { TestBed } from '@angular/core/testing';
+
+import { InMemoryDataService } from './in-memory-data.service';
+
+describe('InMemoryDataService', () => {
+ let service: InMemoryDataService;
+
+ beforeEach(() => {
+ TestBed.configureTestingModule({});
+ service = TestBed.inject(InMemoryDataService);
+ });
+
+ it('should be created', () => {
+ expect(service).toBeTruthy();
+ });
+});
--- /dev/null
+import { Injectable } from '@angular/core';
+import { InMemoryDbService } from 'angular-in-memory-web-api';
+import { Hero } from './hero';
+
+@Injectable({
+ providedIn: 'root',
+})
+export class InMemoryDataService implements InMemoryDbService {
+ createDb() {
+ const heroes = [
+ { id: 11, name: 'Dr Nice' },
+ { id: 12, name: 'Narco' },
+ { id: 13, name: 'Bombasto' },
+ { id: 14, name: 'Celeritas' },
+ { id: 15, name: 'Magneta' },
+ { id: 16, name: 'RubberMan' },
+ { id: 17, name: 'Dynama' },
+ { id: 18, name: 'Dr IQ' },
+ { id: 19, name: 'Magma' },
+ { id: 20, name: 'Tornado' }
+ ];
+ return {heroes};
+ }
+
+ // Overrides the genId method to ensure that a hero always has an id.
+ // If the heroes array is empty,
+ // the method below returns the initial number (11).
+ // if the heroes array is not empty, the method below returns the highest
+ // hero id + 1.
+ genId(heroes: Hero[]): number {
+ return heroes.length > 0 ? Math.max(...heroes.map(hero => hero.id)) + 1 : 11;
+ }
+}