@@ -74,16 +74,18 @@ import { InMemoryDbService } from 'angular-in-memory-web-api';
74
74
export class InMemHeroService implements InMemoryDbService {
75
75
createDb() {
76
76
let heroes = [
77
- { id: ' 1 ' , name: ' Windstorm' },
78
- { id: ' 2 ' , name: ' Bombasto' },
79
- { id: ' 3 ' , name: ' Magneta' },
80
- { id: ' 4 ' , name: ' Tornado' }
77
+ { id: 1 , name: ' Windstorm' },
78
+ { id: 2 , name: ' Bombasto' },
79
+ { id: 3 , name: ' Magneta' },
80
+ { id: 4 , name: ' Tornado' }
81
81
];
82
82
return {heroes };
83
83
}
84
84
}
85
85
```
86
86
87
+ > This library _ currently_ assumes that every collection has a primary key called ` id ` .
88
+
87
89
Register this module and your service implementation in ` AppModule.imports `
88
90
calling the ` forRoot ` static method with this service class and optional configuration object:
89
91
``` ts
@@ -222,7 +224,15 @@ The `InMemoryDbService` method name must be the same as the HTTP method name but
222
224
This service calls it with an ` HttpMethodInterceptorArgs ` object.
223
225
For example, your HTTP GET interceptor would be called like this:
224
226
e.g., ` yourInMemDbService["get"](interceptorArgs) ` .
225
- Your method must ** return an ` Observable<Response> ` ** which _ should be "cold"_ .
227
+
228
+ Your method must return either:
229
+
230
+ * ` Observable<Response> ` - your code has handled the request and the response is available from this
231
+ observable. It _ should be "cold"_ .
232
+
233
+ * ` null ` /` undefined ` - your code decided not to intervene,
234
+ perhaps because you wish to intercept only certain paths for the given HTTP method.
235
+ The service continues with its default processing of the HTTP request.
226
236
227
237
The ` HttpMethodInterceptorArgs ` (as of this writing) are:
228
238
``` ts
@@ -233,25 +243,23 @@ passThruBackend: ConnectionBackend; // pass through backend, if it exists
233
243
```
234
244
## Examples
235
245
236
- The file ` examples/hero-data.service.ts ` is an example of a Hero-oriented ` InMemoryDbService ` ,
237
- derived from the [ HTTP Client] ( https://angular.io/docs/ts/latest/guide/server-communication.html )
238
- sample in the Angular documentation.
246
+ The file ` src/app/hero-in-mem-data.service.ts ` is an example of a Hero-oriented ` InMemoryDbService ` ,
247
+ such as you might see in an HTTP sample in the Angular documentation.
239
248
240
249
To try it, add the following line to ` AppModule.imports `
241
250
``` ts
242
- InMemoryWebApiModule .forRoot (HeroDataService )
251
+ InMemoryWebApiModule .forRoot (HeroInMemDataService )
243
252
```
244
253
245
- That file also has a ` HeroDataOverrideService ` derived class that demonstrates overriding
246
- the ` parseUrl ` method and it has a "cold" HTTP GET interceptor.
254
+ See the ` src/app/hero-in-mem-data-override.service.ts ` class that demonstrates overriding
255
+ the ` parseUrl ` method. It also has a "cold" HTTP GET interceptor.
247
256
248
257
Add the following line to ` AppModule.imports ` to see this version of the data service in action:
249
258
``` ts
250
- InMemoryWebApiModule .forRoot (HeroDataOverrideService )
259
+ InMemoryWebApiModule .forRoot (HeroInMemDataOverrideService )
251
260
```
252
261
253
- # To Do
254
- * add tests (shameful omission!)
262
+ The tests (see below) exercise these examples.
255
263
256
264
# Build Instructions
257
265
@@ -282,7 +290,7 @@ compiling your application project.
282
290
283
291
- ` npm run tsc ` to confirm the project compiles w/o error (sanity check)
284
292
285
- -- NO TESTS YET ... BAD --
293
+ - ` npm test ` to build and run tests (see "Testing" below)
286
294
287
295
- ` gulp build `
288
296
- commit and push
@@ -297,3 +305,26 @@ compiling your application project.
297
305
298
306
[ travis-badge ] : https://travis-ci.org/angular/in-memory-web-api.svg?branch=master
299
307
[ travis-badge-url ] : https://travis-ci.org/angular/in-memory-web-api
308
+
309
+ ## Testing
310
+
311
+ The "app" for this repo is not a real app.
312
+ It's an Angular data service (` HeroService ` ) and a bunch of tests.
313
+
314
+ > Note that the ` tsconfig.json ` produces a ` commonjs ` module.
315
+ That's what _ Angular specs require_ .
316
+ But when building for an app, it should be a ` es2015 ` module,
317
+ as is the ` tsconfig-ngc.json ` for AOT-ready version of this library.
318
+
319
+ These tests are a work-in-progress, as tests often are.
320
+
321
+ The ` src/ ` folder is divided into
322
+ - ` app/ ` - the test "app" and its tests
323
+ - ` in-mem/ ` - the source code for the in-memory web api library
324
+
325
+ > A real app would reference the in-memory web api node module;
326
+ these tests reference the library source files.
327
+
328
+ The ` karma-test-shim.js ` add ` in-mem ` to the list of app folders that SystemJS should resolve.
329
+
330
+
0 commit comments