8000 docs: refine README setup and commands sections · cHullaert/in-memory-web-api@d8c7a03 · GitHub
[go: up one dir, main page]

Skip to content

Commit d8c7a03

Browse files
committed
docs: refine README setup and commands sections
1 parent d23942a commit d8c7a03

File tree

1 file changed

+46
-27
lines changed

1 file changed

+46
-27
lines changed

README.md

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,10 @@ Examples:
8383
GET api/heroes?name=^j // 'j' is a regex; returns heroes whose name starting with 'j' or 'J'
8484
GET api/heroes.json/42 // ignores the ".json"
8585
```
86-
<a id="commands"></a>
87-
## Commands
8886

89-
The service also accepts "commands" that can, for example, reconfigure the service and reset the database.
87+
The in-memory web api service processes these requests against a "database" - a set of named collections - that you define during setup.
9088

91-
When the last segment of the _api base path_ is "commands", the `collectionName` is treated as the _command_.
92-
Example URLs:
93-
```
94-
commands/resetdb // Reset the "database" to its original state
95-
commands/config // Get or update this service's config object
96-
```
97-
98-
Usage:
99-
```
100-
http.post('commands/resetdb', undefined);
101-
http.get('commands/config');
102-
http.post('commands/config', '{"delay":1000}');
103-
```
104-
105-
## Basic usage
89+
## Basic setup
10690

10791
Create an `InMemoryDataService` class that implements `InMemoryDbService`.
10892

@@ -126,7 +110,17 @@ export class InMemHeroService implements InMemoryDbService {
126110
}
127111
```
128112

129-
>This library _currently_ assumes that every collection has a primary key called `id`.
113+
**Notes**
114+
115+
* The in-memory web api library _currently_ assumes that every collection has a primary key called `id`.
116+
117+
* The `createDb` method can be synchronous or asynchronous.
118+
It would have to be asynchronous if you initialized your in-memory database service from a JSON file.
119+
Return the database _object_, an _observable_ of that object, or a _promise_ of that object. The tests include an example of all three.
120+
121+
* The client can send a [`resetDb` command](#commands) request which calls your `createDb` again, passing in the command request information.
122+
123+
### Import the in-memory web api module
130124

131125
Register your data store service implementation with the `HttpClientInMemoryWebApiModule`
132126
in your root `AppModule.imports`
@@ -155,12 +149,7 @@ to ensure that the in-memory backend provider supersedes the Angular version.
155149

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

158-
* The `createDb` method can be synchronous or asynchronous.
159-
so you can initialize your in-memory database service from a JSON file.
160-
Return the database object, an observable of that object, or a promise of that object.
161-
The in-memory web api service calls `createDb` (a) when it handles the _first_ `HttpClient` (or `Http`) request and (b) when it receives a `POST resetdb` request.
162-
163-
### Using with the older Angular _Http_ module
152+
### Setup for the older Angular _Http_ module
164153

165154
You can still use the in-memory web api with the older `Http` module.
166155

@@ -180,10 +169,10 @@ imports: [
180169
})
181170
export class AppModule { ... }
182171
```
183-
### Using both Angular HTTP modules
172+
### Setup for both Angular HTTP modules
184173

185174
Perhaps you have a hybrid app with BOTH Angular modules
186-
because you're migrating to `HttpClient` from 'Http`.
175+
because you're migrating to `HttpClient` from `Http`.
187176
Or perhaps you've used this library before and you don't have time
188177
at this moment to re-do your module setup.
189178

@@ -224,6 +213,8 @@ See also the example source code in the official Angular.io documentation such a
224213
# Advanced Features
225214
Some features are not readily apparent in the basic usage described above.
226215

216+
## Configuration arguments
217+
227218
The `InMemoryBackendConfigArgs` defines a set of options. Add them as the second `forRoot` argument:
228219
```ts
229220
InMemoryWebApiModule.forRoot(InMemHeroService, { delay: 500 }),
@@ -275,6 +266,34 @@ that are not in the in-memory database, set `Config.passThruUnknownUrl: true`.
275266
Then this service will forward unrecognized requests to the remote server
276267
via the Angular default `XHR` backend (it depends on whether your using `Http` or `HttpClient`).
277268

269+
<a id="commands"></a>
270+
## Commands
271+
272+
The in-memory web api service accepts "commands" that can, for example, reconfigure the service and reset the database.
273+
274+
When the last segment of the _api base path_ is "commands", the `collectionName` is treated as the _command_.
275+
276+
Example URLs:
277+
```
278+
commands/resetdb // Reset the "database" to its original state
279+
commands/config // Get or update this service's config object
280+
```
281+
282+
Usage:
283+
```
284+
http.post('commands/resetdb', undefined);
285+
http.get('commands/config');
286+
http.post('commands/config', '{"delay":1000}');
287+
```
288+
289+
The in-memory web api calls your `InMemoryDbService` data service class's `createDb` method on two occasions.
290+
291+
1. when it handles the _first_ HTTP request
292+
1. when it receives a `resetdb` command.
293+
294+
In the command case, the service passes in a `RequestInfo` object,
295+
enabling the `createDb` logic to adjust its behavior per the client request. See the tests for examples.
296+
278297
## _parseRequestUrl_ and your override
279298

280299
The `parseRequestUrl` parses the request URL into a `ParsedRequestUrl` object.

0 commit comments

Comments
 (0)
0