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

Commit cc02859

Browse files
authored
fix: Do NOT encapsulate by default - breaking change - v0.5 (#147)
Changes default `config.dataEncapsulation` from `true` to `false`. This is a breaking change that affects almost all pre-existing apps. See the CHANGELOG.MD. Also fixes README error in issue #146. Closes #146
1 parent 22c2d97 commit cc02859

16 files changed

+234
-161
lines changed

CHANGELOG.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ 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.5.0"></a>
12+
## 0.5.0 (2017-10-05)
13+
**BREAKING CHANGE**: HTTP response data no longer wrapped in object w/ `data` property by default.
14+
15+
In this release, the `dataEncapsulation` configuration default changed from `false` to `true`. The HTTP response body holds the data values directly rather than an object that encapsulates those values, `{data: ...}`. This is a **breaking change that affects almost all existing apps!**
16+
17+
Changing the default to `false` is a **breaking change**. Pre-existing apps that did not set this property explicitly will be broken because they expect encapsulation and are probably mapping
18+
the HTTP response results from the `data` property like this:
19+
```
20+
.map(data => data.data as Hero[])
21+
```
22+
**To migrate, simply remove that line everywhere.**
23+
24+
If you would rather keep the web api's encapsulation, `{data: ...}`, set `dataEncapsulation` to `true` during configuration as in the following example:
25+
```
26+
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, { dataEncapsulation: true })
27+
```
28+
29+
We made this change because
30+
* the [security flaw](http://stackoverflow.com/questions/3503102/what-are-top-level-json-arrays-and-why-are-they-a-security-risk)
31+
that data encapsulation tried to overcome has been fixed in most (if not all) browsers supported by Angular v2+.
32+
* data encapsulation is uncommon in modern web APIs.
33+
* removing the `.map` step simplifies usage of `HttpClient` in most scenarios.
34+
1135
<a id="0.4.6"></a>
1236
## 0.4.6 (2017-09-13)
1337
- improves README
@@ -63,6 +87,12 @@ to intercept Angular's attempt to call browser's XHR
6387
**Theme: Support `HttpClient` and add tests**.
6488
See PR #130.
6589

90+
The 0.4.0 release was a major overhaul of this library.
91+
92+
You don't have to change your existing application _code_ if your app uses this library without customizations.
93+
94+
But this release's **breaking changes** affect developers who used the customization features or loaded application files with SystemJS.
95+
6696
**BREAKING CHANGES**: Massive refactoring.
6797
Many low-level and customization options have changed.
6898
Apps that stuck with defaults should be (mostly) OK.

README.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,24 @@ 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 supports `HttpClient`!**
11-
>Release v0.4.0 (8 Sept 2017) was a major overhaul of this library.
12-
>
13-
>You don't have to change your existing application _code_ if your app uses this library without customizations.
14-
>
15-
>
16-
>The v0.4.0 release introduced **breaking changes** that affect developers who used the customization features or loaded application files with SystemJS.
17-
>
18-
>**Read this README and the [CHANGELOG](https://github.com/angular/in-memory-web-api/blob/master/CHANGELOG.md/#0.4.0)**
19-
to learn what's new and about **breaking changes**
20-
>
21-
>The new **unit tests** are a good way to see the in-memory web api in action.
10+
11+
## Important recent changes
12+
13+
### HTTP response data no longer wrapped in object w/ `data` property
14+
15+
As of v0.5.0 (5 October 2017), the `dataEncapsulation` configuration default changed from `false` to `true`. The HTTP response body holds the data values directly rather than an object that encapsulates those values, `{data: ...}`. This is a **breaking change that affects almost all existing apps!**
16+
17+
See the [CHANGELOG](https://github.com/angular/in-memory-web-api/blob/master/CHANGELOG.md/#0.5.0) for the reason behind this change and how to quickly fix your code or revert to encapsulation.
18+
19+
### v0.4 supports `HttpClient`
20+
Release v0.4.0 (8 Sept 2017) was a major overhaul of this library.
21+
22+
The v0.4.0 release introduced **breaking changes** that affect developers who used the customization features or loaded application files with SystemJS.
23+
24+
**Read this README and the [CHANGELOG](https://github.com/angular/in-memory-web-api/blob/master/CHANGELOG.md/#0.4.0)**
25+
to learn what's new and about other **breaking changes**.
26+
27+
---
2228

2329
## Use cases
2430

@@ -120,7 +126,7 @@ in your root `AppModule.imports`
120126
calling the `forRoot` static method with this service class and an optional configuration object:
121127
```ts
122128
import { HttpClientModule } from '@angular/common/http';
123-
import { HttpClientInMemoryWebApiModule } from 'http-angular-in-memory-web-api';
129+
import { HttpClientInMemoryWebApiModule } from 'angular-in-memory-web-api';
124130

125131
import { InMemHeroService } from '../app/hero.service';
126132

@@ -148,7 +154,7 @@ You can still use the in-memory web api with the older `Http` module.
148154

149155
```ts
150156
import { HttpModule } from '@angular/http';
151-
import { HttpInMemoryWebApiModule } from 'http-angular-in-memory-web-api';
157+
import { HttpInMemoryWebApiModule } from 'angular-in-memory-web-api';
152158

153159
import { InMemHeroService } from '../app/hero.service';
154160

@@ -438,7 +444,7 @@ Follow these steps for updating the library.
438444

439445
- update `CHANGELOG.md` to record the change. Call out _breaking changes_.
440446

441-
- update `READM.md` if usage or interfaces change.
447+
- update `README.md` if usage or interfaces change.
442448

443449
- consider updating the dependency versions in `package.json`.
444450

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

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

gulpfile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ gulp.task('clean', function() {
5959
'./http-backend.service.*',
6060
'./http-client-backend.service.*',
6161
'./interfaces.*',
62+
'./http-in-memory-web-api.module.*',
63+
'./http-client-in-memory-web-api.module.*',
6264
'./in-memory-web-api.module.*',
6365
'./index.*',
6466
'./bundles/in-memory-web-api.umd.js'

interfaces.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ export declare abstract class InMemoryBackendConfigArgs {
4747
*/
4848
caseSensitiveSearch?: boolean;
4949
/**
50-
* true (default) encapsulate content in a `data` property inside the response body. false: put content directly inside the response body
50+
* false (default) put content directly inside the response body.
51+
* true: encapsulate content in a `data` property inside the response body, `{ data: ... }`.
5152
*/
5253
dataEncapsulation?: boolean;
5354
/**

interfaces.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interfaces.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

interfaces.metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"__symbolic":"module","version":3,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":180,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"},"UriInfo":{"__symbolic":"interface"}}},{"__symbolic":"module","version":1,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":180,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"},"UriInfo":{"__symbolic":"interface"}}}]
1+
[{"__symbolic":"module","version":3,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":181,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"},"UriInfo":{"__symbolic":"interface"}}},{"__symbolic":"module","version":1,"metadata":{"HeadersCore":{"__symbolic":"interface"},"InMemoryDbService":{"__symbolic":"class","members":{"createDb":[{"__symbolic":"method"}]}},"InMemoryBackendConfigArgs":{"__symbolic":"class"},"InMemoryBackendConfig":{"__symbolic":"class","decorators":[{"__symbolic":"call","expression":{"__symbolic":"reference","module":"@angular/core","name":"Injectable"}}],"members":{"__ctor__":[{"__symbolic":"constructor","parameters":[{"__symbolic":"reference","name":"InMemoryBackendConfigArgs"}]}]}},"parseUri":{"__symbolic":"function"},"ParsedRequestUrl":{"__symbolic":"interface"},"PassThruBackend":{"__symbolic":"interface"},"removeTrailingSlash":{"__symbolic":"function","parameters":["path"],"value":{"__symbolic":"error","message":"Expression form not supported","line":181,"character":22}},"RequestCore":{"__symbolic":"interface"},"RequestInfo":{"__symbolic":"interface"},"RequestInfoUtilities":{"__symbolic":"interface"},"ResponseOptions":{"__symbolic":"interface"},"UriInfo":{"__symbolic":"interface"}}}]

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular-in-memory-web-api",
3-
"version": "0.4.6",
3+
"version": "0.5.0",
44
"description": "An in-memory web api for Angular demos and tests",
55
"main": "bundles/in-memory-web-api.umd.js",
66
"module": "index.js",

src/app/hero-in-mem-data-override.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ export class HeroInMemDataOverrideService extends HeroInMemDataService {
6161
console.log('HTTP GET override');
6262

6363
const collection = villains.slice();
64+
const dataEncapsulation = reqInfo.utils.getConfig().dataEncapsulation;
6465
const id = reqInfo.id;
6566

6667
// tslint:disable-next-line:triple-equals
6768
const data = id == undefined ? collection : reqInfo.utils.findById(collection, id);
6869

6970
const options: ResponseOptions = data ?
7071
{
71-
body: { data },
72+
body: dataEncapsulation ? { data } : data,
7273
status: STATUS.OK
7374
} :
7475
{

0 commit comments

Comments
 (0)