8000 feat: offer `Http` and `HttpClient` only versions of the setup module… · angular/in-memory-web-api@010e676 · GitHub
[go: up one dir, main page]

Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 010e676

Browse files
authored
feat: offer Http and HttpClient only versions of the setup module (#141)
For those who want only one or the other and don’t want to have to include the unused Angular HTTP module. closes #140
1 parent 028747d commit 010e676

34 files changed

+555
-140
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ because this is a development tool, not a production product.
88
We do try to tell you about such changes in this `CHANGELOG.md`
99
and we fix bugs as fast as we can.
1010

11+
<a id="0.4.5"></a>
12+
## 0.4.5 (2017-09-11)
13+
Feature - offer separate `HttpClientInMemoryWebApiModule` and `HttpInMemoryWebApiModule`.
14+
15+
closes #140
16+
1117
<a id="0.4.4"></a>
1218
## 0.4.4 (2017-09-11)
1319
closes #136

README.md

Lines changed: 79 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ that emulates CRUD operations over a RESTy API.
77
It intercepts Angular `Http` and `HttpClient` requests that would otherwise go to the remote server and redirects them to an in-memory data store that you control.
88

99
---
10-
## **v0.4.0 supports `HttpClient`!**
10+
## **v0.4 supports `HttpClient`!**
1111
>Release v0.4.0 (8 Sept 2017) is a major overhaul of this library.
1212
>
1313
>You don't have to change your existing application _code_ if your app uses this library without customizations.
@@ -105,6 +105,7 @@ Usage:
105105
```
106106

107107
## Basic usage
108+
108109
Create an `InMemoryDataService` class that implements `InMemoryDbService`.
109110

110111
At minimum it must implement `createDb` which
@@ -129,17 +130,19 @@ export class InMemHeroService implements InMemoryDbService {
129130

130131
>This library _currently_ assumes that every collection has a primary key called `id`.
131132
132-
Register this module and your data store service implementation in `AppModule.imports`
133-
calling the `forRoot` static method with this service class and optional configuration object:
133+
Register your data store service implementation with the `HttpClientInMemoryWebApiModule`
134+
in your root `AppModule.imports`
135+
calling the `forRoot` static method with this service class and an optional configuration object:
134136
```ts
135-
import { HttpClientModule } from '@angular/common/http';
136-
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
137+
import { HttpClientModule } from '@angular/common/http';
138+
import { HttpClientInMemoryWebApiModule } from 'http-angular-in-memory-web-api';
139+
140+
import { InMemHeroService } from '../app/hero.service';
137141

138-
import { InMemHeroService } from '../app/hero.service';
139142
@NgModule({
140143
imports: [
141144
HttpClientModule,
142-
InMemoryWebApiModule.forRoot(InMemHeroService),
145+
HttpClientInMemoryWebApiModule.forRoot(InMemHeroService),
143146
...
144147
],
145148
...
@@ -154,38 +157,74 @@ the in-memory backed provider supersedes the Angular version.
154157

155158
* You can setup the in-memory web api within a lazy loaded feature module by calling the `.forFeature` method as you would `.forRoot`.
156159

157-
* You can still use the in-memory web api with the older `Http` module.
158-
159-
```ts
160-
import { HttpModule } from '@angular/http';
161-
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
162-
163-
import { InMemHeroService } from '../app/hero.service';
164-
@NgModule({
165-
imports: [
166-
HttpModule,
167-
InMemoryWebApiModule.forRoot(InMemHeroService),
168-
...
169-
],
170-
...
171-
})
172-
export class AppModule { ... }
173-
```
174-
175160
* The `createDb` method can be synchronous or asynchronous.
176161
so you can initialize your in-memory database service from a JSON file.
177162
Return the database object, an observable of that object, or a promise of that object.
178163
The in-mem web api service calls `createDb` (a) when it handles the _first_ `HttpClient` (or `Http`) request and (b) when it receives a `POST resetdb` request.
179164

180-
## In-memory web api examples
181-
The tests (`src/app/*.spec.ts` files) in the [github repo](https://github.com/angular/in-memory-web-api/tree/master/src/app) are a good place to learn how to setup and use this in-memory web api library.
165+
### Using with the older Angular _Http_ module
166+
167+
You can still use the in-memory web api with the older `Http` module.
168+
169+
```ts
170+
import { HttpModule } from '@angular/http';
171+
import { HttpInMemoryWebApiModule } from 'http-angular-in-memory-web-api';
172+
173+
import { InMemHeroService } from '../app/hero.service';
174+
175+
@NgModule({
176+
imports: [
177+
HttpModule,
178+
HttpInMemoryWebApiModule.forRoot(InMemHeroService),
179+
...
180+
],
181+
...
182+
})
183+
export class AppModule { ... }
184+
```
185+
### Using both Angular HTTP modules
186+
187+
Perhaps you have a hybrid app with BOTH Angular modules
188+
because you're migrating to `HttpClient` from 'Http`.
189+
Or perhaps you've used this library before and you don't have time
190+
at this moment to re-do your module setup.
191+
192+
There's a combo-module
193+
(`InMemoryWebApiModule`) that prepares for both of them.
194+
It has the same syntax from pre-`v0.4.0` days and it should "_just work_"
195+
as long as you aren't using the [advanced features described below](#advanced-features).
196+
197+
```ts
198+
import { HttpModule } from '@angular/http';
199+
import { HttpClientModule } from '@angular/common/http';
200+
201+
import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
202+
203+
import { InMemHeroService } from '../app/hero.service';
204+
205+
@NgModule({
206+
imports: [
207+
HttpModule,
208+
HttpClientModule,
209+
InMemoryWebApiModule.forRoot(InMemHeroService),
210+
...
211+
],
212+
...
213+
})
214+
export class AppModule { ... }
215+
```
216+
217+
# Examples
218+
The tests (`src/app/*.spec.ts` files) in the
219+
[github repository](https://github.com/angular/in-memory-web-api/tree/master/src/app)
220+
are a good place to learn how to setup and use this in-memory web api library.
182221

183222
See also the example source code in the official Angular.io documentation such as the
184223
[HttpClient](https://angular.io/guide/http) guide and the
185224
[Tour of Heroes](https://angular.io/tutorial/toh-pt6).
186225

187226
# Advanced Features
188-
Some features are not readily apparent in the basic usage example.
227+
Some features are not readily apparent in the basic usage described above.
189228

190229
The `InMemoryBackendConfigArgs` defines a set of options. Add them as the second `forRoot` argument:
191230
```ts
@@ -343,23 +382,16 @@ createResponse$: (resOptionsFactory: () => ResponseOptions) => Observable<any>;
343382
```
344383
## In-memory Web Api Examples
345384

346-
The file `src/app/hero-in-mem-data.service.ts` is an example of a Hero-oriented `InMemoryDbService`,
347-
such as you might see in an HTTP sample in the Angular documentation.
385+
The [github repository](https://github.com/angular/in-memory-web-api/tree/master/src/app)
386+
demonstrates library usage with tested examples.
348387

349-
To try it, add the following line to `AppModule.imports`
350-
```ts
351-
InMemoryWebApiModule.forRoot(HeroInMemDataService)
352-
```
353-
354-
For examples of overriding service methods,
355-
see the `src/app/hero-in-mem-data-override.service.ts` class.
388+
The `HeroInMemDataService` class (in `src/app/hero-in-mem-data.service.ts`) is a Hero-oriented `InMemoryDbService`
389+
such as you might see in an HTTP sample in the Angular documentation.
356390

357-
Add the following line to `AppModule.imports` to see this version of the data service in action:
358-
```ts
359-
InMemoryWebApiModule.forRoot(HeroInMemDataOverrideService)
360-
```
391+
The `HeroInMemDataOverrideService` class (in `src/app/hero-in-mem-data-override.service.ts`)
392+
demonstrates a few ways to override methods of the base `HeroInMemDataService`.
361393

362-
The tests (see below) exercise these examples.
394+
The tests ([see below](#testing)) exercise these examples.
363395

364396
# Build Instructions
365397

@@ -425,18 +457,18 @@ The `src/` folder is divided into
425457
>A real app would reference the in-memory web api node module;
426458
these tests reference the library source files.
427459

428-
The `karma-test-shim.js` add `in-mem` to the list of app folders that SystemJS should resolve.
460+
The `karma-test-shim.js` adds the `in-mem` folder to the list of folders that SystemJS should resolve.
429461

430462
## Rollup
431463

432464
The gulp "umd" task runs rollup for tree-shaking.
433465

434-
I don't remember it ever working without a lot of warnings.
435-
In v.0.4.x, updated to v.0.49 ... which required updating of the `rollup.config.js`.
466+
I don't remember if it ever worked without a lot of warnings.
467+
The `v0.4.x` release updated to `rollup@0.49` which required updates to the `rollup.config.js`.
436468

437-
Still weirdly runs an unspecified `cjs` rollup config first that I can’t find (which produces numerous warnings) before doing the right thing and running the `umd` config.
469+
Still weirdly runs `cjs` rollup config first that I can’t find (which produces numerous warnings) before doing the right thing and running the `umd` config.
438470

439-
Also does not work if follow instructions and use the `output` property of `rollup.config.js`; does work when config it “wrong” and put the options in the root.
471+
Also does not work if you follow instructions and use the `output` property of `rollup.config.js`; does work when configure it “wrong” and put the options in the root.
440472

441473
Ignoring these issues for now.
442474

bundles/in-memory-web-api.umd.js

Lines changed: 108 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

http-backend.service.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ import { BackendService } from './backend.service';
1515
* Call `forRoot` static method with this service class and optional configuration object:
1616
* ```
1717
* // other imports
18-
* import { HttpModule } from '@angular/http';
19-
* import { InMemoryWebApiModule } from 'angular-in-memory-web-api';
18+
* import { HttpModule } from '@angular/http';
19+
* import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
2020
*
2121
* import { InMemHeroService, inMemConfig } from '../api/in-memory-hero.service';
2222
* @NgModule({
2323
* imports: [
2424
* HttpModule,
25-
* InMemoryWebApiModule.forRoot(InMemHeroService, inMemConfig),
25+
* HttpClientInMemoryWebApiModule.forRoot(InMemHeroService, inMemConfig),
2626
* ...
2727
* ],
2828
* ...

http-backend.service.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0