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

Commit 956637e

Browse files
authored
fix: clone request data (#165)
closes #164
1 parent 7fc1ca0 commit 956637e

8 files changed

+46
-18
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ 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.2"></a>
12+
## 0.5.2 (2017-12-10)
13+
No longer modify the request data coming from client. Fixes #164
14+
1115
<a id="0.5.1"></a>
1216
## 0.5.1 (2017-10-21)
1317
Support Angular v5.

backend.service.js

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

backend.service.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.

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

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

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.5.1",
3+
"version": "0.5.2",
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/in-mem/backend.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ export abstract class BackendService {
201201
}
202202

203203
protected bodify(data: any) {
204-
const body = this.clone(data);
205-
return this.config.dataEncapsulation ? { data: body } : body;
204+
return this.config.dataEncapsulation ? { data } : data;
206205
}
207206

208207
protected clone(data: any) {
@@ -433,7 +432,7 @@ export abstract class BackendService {
433432
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, `'${collectionName}' with id='${id}' not found`);
434433
}
435434
return {
436-
body: this.bodify(data),
435+
body: this.bodify(this.clone(data)),
437436
headers: headers,
438437
status: STATUS.OK
439438
};
@@ -585,7 +584,7 @@ export abstract class BackendService {
585584
// Create entity
586585
// Can update an existing entity too if post409 is false.
587586
protected post({ collection, collectionName, headers, id, req, resourceUrl, url }: RequestInfo): ResponseOptions {
588-
const item = this.getJsonBody(req);
587+
const item = this.clone(this.getJsonBody(req));
589588

590589
// tslint:disable-next-line:triple-equals
591590
if (item.id == undefined) {
@@ -629,7 +628,7 @@ export abstract class BackendService {
629628
// Update existing entity
630629
// Can create an entity too if put404 is false.
631630
protected put({ collection, collectionName, headers, id, req, url }: RequestInfo): ResponseOptions {
632-
const item = this.getJsonBody(req);
631+
const item = this.clone(this.getJsonBody(req));
633632
// tslint:disable-next-line:triple-equals
634633
if (item.id == undefined) {
635634
return this.createErrorResponseOptions(url, STATUS.NOT_FOUND, `Missing '${collectionName}' id`);

src/in-mem/http-backend.service.spec.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,20 @@ describe('Http Backend Service', () => {
169169
);
170170
}));
171171

172+
it('can generate the id when add a hero with no id', async(() => {
173+
const hero = new Hero(null, 'SuperDooper');
174+
http.post('api/heroes', hero)
175+
.map(res => res.json())
176+
.subscribe(
177+
replyHero => {
178+
expect(replyHero.id).toBeTruthy('added hero should have an id');
179+
expect(replyHero).not.toBe(hero,
180+
'reply hero should not be the request hero');
181+
},
182+
failure
183+
);
184+
}));
185+
172186
it('can get nobodies (empty collection)', async(() => {
173187
http.get('api/nobodies')
174188
.map(res => res.json())

src/in-mem/http-client-backend.service.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,19 @@ describe('HttpClient Backend Service', () => {
213213
);
214214
}));
215215

216+
it('can generate the id when add a hero with no id', async(() => {
217+
const hero = new Hero(null, 'SuperDooper');
218+
http.post<Hero>('api/heroes', hero)
219+
.subscribe(
220+
replyHero => {
221+
expect(replyHero.id).toBeTruthy('added hero should have an id');
222+
expect(replyHero).not.toBe(hero,
223+
'reply hero should not be the request hero');
224+
},
225+
failure
226+
);
227+
}));
228+
216229
it('can get nobodies (empty collection)', async(() => {
217230
http.get<Hero[]>('api/nobodies')
218231
.subscribe(

0 commit comments

Comments
 (0)