You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 26, 2024. It is now read-only.
- more refactoring
- can substitute your own id generator, `geniD`
- parseUrl -> parseRequestUrl
- refactored parseuri -> parseUri as independent function
- moved interceptorArgs into RequestInfo
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.
8
+
9
+
## Use cases
10
+
11
+
* Demo apps that need to simulate CRUD data persistence operations without a real server.
12
+
You won't have to build and start a test server.
13
+
14
+
* Whip up prototypes and proofs of concept.
15
+
16
+
* Share examples with the community in a web coding environment such as Plunker or CodePen.
17
+
Create Angular issues and StackOverflow answers supported by live code.
18
+
19
+
* Simulate operations against data collections that aren't yet implemented on your dev/test server.
20
+
You can pass requests thru to the dev/test server for collections that are supported.
21
+
22
+
* Write unit test apps that read and write data.
23
+
Avoid the hassle of intercepting multiple http calls and manufacturing sequences of responses.
24
+
The in-memory data store resets for each test so there is no cross-test data pollution.
25
+
26
+
* End-to-end tests. If you can toggle the app into test mode
27
+
using the in-mem web api, you won't disturb the real database.
28
+
This can be especially useful for CI (continuous integration) builds.
5
29
6
-
It intercepts Angular `Http` requests that would otherwise go to the remote server
7
-
via the Angular `XHRBackend` service
8
30
9
31
>**LIMITATIONS**
10
32
>
@@ -35,7 +57,7 @@ Examples:
35
57
```
36
58
// for requests to an `api` base URL that gets heroes from a 'heroes' collection
37
59
GET api/heroes // all heroes
38
-
GET api/heroes/42 // the character with id=42
60
+
GET api/heroes/42 // the hero with id=42
39
61
GET api/heroes?name=^j // 'j' is a regex; returns heroes whose name starting with 'j' or 'J'
40
62
GET api/heroes.json/42 // ignores the ".json"
41
63
```
@@ -86,17 +108,16 @@ export class InMemHeroService implements InMemoryDbService {
86
108
87
109
>This library _currently_ assumes that every collection has a primary key called `id`.
88
110
89
-
Register this module and your service implementation in `AppModule.imports`
111
+
Register this module and your data store service implementation in `AppModule.imports`
90
112
calling the `forRoot` static method with this service class and optional configuration object:
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.
156
+
157
+
See also the example source code in the official Angular.io documentation such as the
158
+
[HttpClient](https://angular.io/guide/http) guide and the
159
+
[Tour of Heroes](https://angular.io/tutorial/toh-pt6).
114
160
115
161
# Bonus Features
116
162
Some features are not readily apparent in the basic usage example.
@@ -129,7 +175,7 @@ Here's how it reasons:
129
175
1. If it looks like a [command](#commands), process as a command
130
176
2. If the [HTTP method is overridden](#method-override)
131
177
3. If the resource name (after the api base path) matches one of the configured collections, process that
132
-
4. If not but the `Config.passThruUnknownUrl` flag is `true`, try to [pass the request along to a real _XHRBackend_](#passthru).
178
+
4. If not but the `Config.passThruUnknownUrl` flag is `true`, try to [pass the request along to a real _XHR_](#passthru).
133
179
5. Return a 404.
134
180
135
181
See the `handleRequest` method implementation for details.
@@ -158,57 +204,77 @@ The following example matches all names start with the letter 'j' or 'J' in the
158
204
Set `config.caseSensitiveSearch = true` if needed.
159
205
160
206
<aid="passthru"></a>
161
-
## Pass thru to a live _XHRBackend_
207
+
## Pass thru to a live server
162
208
163
209
If an existing, running remote server should handle requests for collections
164
210
that are not in the in-memory database, set `Config.passThruUnknownUrl: true`.
165
-
This service will forward unrecognized requests via a base version of the Angular `XHRBackend`.
211
+
Then this service will forward unrecognized requests to the remote server
212
+
via the Angular default `XHR` backend (it depends on whether your using `Http` or `HttpClient`).
166
213
167
-
## _parseUrl_ and your override
214
+
## _parseRequestUrl_ and your override
168
215
169
-
The `parseUrl` parses the request URL into a `ParsedUrl` object.
170
-
`ParsedUrl` is a public interface whose properties guide the in-memory web api
216
+
The `parseRequestUrl` parses the request URL into a `ParsedRequestUrl` object.
217
+
`ParsedRequestUrl` is a public interface whose properties guide the in-memory web api
171
218
as it processes the request.
172
219
173
-
### Default _parseUrl_
220
+
### Default _parseRequestUrl_
174
221
175
222
Default parsing depends upon certain values of `config`: `apiBase`, `host`, and `urlRoot`.
176
223
Read the source code for the complete story.
177
224
178
-
Configuring the `apiBase` yields the most interesting changes to `parseUrl` behavior:
225
+
Configuring the `apiBase` yields the most interesting changes to `parseRequestUrl` behavior:
179
226
180
227
* For `apiBase=undefined` and `url='http://localhost/api/customers/42'`
0 commit comments