Skip to content

Commit 5c80127

Browse files
committed
Fix #26 - Add rawData to composite response when raw:true option is given
1 parent 1962f26 commit 5c80127

6 files changed

+37
-8
lines changed

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ This adapter just add a light serialize and deserialize layer over DSHttpAdapter
2323

2424
| package | requirement |
2525
| ------------ | ------------- |
26-
| js-data | >= 3.0.0-rc.7 |
27-
| js-data-http | >= 3.0.0-rc.2 |
26+
| js-data | >= 3.0.0 |
27+
| js-data-http | >= 3.0.0 |
2828

2929
By design it is not compatible with JS-Data v2, for a full integration on v2, you should use: [js-data-jsonapi](https://github.com/BlairAllegroTech/js-data-jsonapi).
3030

@@ -170,6 +170,7 @@ store.findAll('User', {}, {
170170
}).then((response) => {
171171
console.log(response.data); // Return the Records
172172
console.log(response.meta); // Return the JSONApi Metas
173+
console.log(response.rawData); // Return the full response (should be discarded as soon as possible to save memory)
173174
})
174175
```
175176

@@ -207,10 +208,13 @@ Deserialization and serialization hooks, see above.
207208
* Custom hooks
208209
* By default update PATCH changes instead of PUT the record
209210

211+
### What may work
212+
213+
* Error handling (need intensive testing)
214+
210215
### What is remaining
211216

212217
* ManyToMany
213-
* Error handling
214218

215219
## License
216220

dist/js-data-jsonapi-light.js

+4-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/js-data-jsonapi-light.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/adapter.ts

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ export class JsonApiAdapter extends HttpAdapter{
3232

3333
private handleResponse (opts?:any) { return function (response:any): Promise<any> {
3434
if (opts && opts.raw) {
35+
response.rawData = response.data.rawData;
3536
response.meta = response.data.meta;
3637
response.data = response.data.result;
3738
}

src/deserializer.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ export function jsonApiDeserialize(mapper:Mapper, res:any, opts:any){
125125

126126
return {
127127
result: outputDatas,
128-
meta: res.data.meta
128+
meta: res.data.meta,
129+
rawData: res.data
129130
};
130131
}

test/unit/composite-response.spec.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ interface DSJsonApiLightResponse {
99
}
1010

1111
describe('Composite Response :', () => {
12-
describe('when resources are fetched with a compositeResponse option', () => {
12+
describe('when resources are fetched with a raw option', () => {
1313
const
1414
USER_LENGTH = 1,
15+
INCLUDED_LENGTH = 2,
1516
USER = {
1617
ID: 'e81fea3d-6379-4137-8068-7d70a90a1a7c',
1718
EMAIL: 'james@example.com'
@@ -33,6 +34,7 @@ describe('Composite Response :', () => {
3334

3435
it('should return a composite response in the findAll promise', () => {
3536
expect(response).to.have.property('data');
37+
expect(response).to.have.property('rawData');
3638
expect(response).to.have.property('meta');
3739
})
3840

@@ -47,5 +49,24 @@ describe('Composite Response :', () => {
4749
expect(response.meta.count).to.equal(META.COUNT);
4850
expect(response.meta.about).to.deep.equal(META.ABOUT);
4951
})
52+
53+
it('should return the raw response in the rawData property of the composite response', () => {
54+
expect(response.rawData).to.be.an('object');
55+
expect(response.rawData).to.have.property('data');
56+
expect(response.rawData).to.have.property('meta');
57+
expect(response.rawData).to.have.property('links');
58+
expect(response.rawData).to.have.property('included');
59+
expect(response.rawData.data[0]).to.be.an('object');
60+
expect(response.rawData.data[0].id).to.equal(USER.ID);
61+
expect(response.rawData.data[0].type).to.equal('User');
62+
expect(response.rawData.data[0]).to.have.property('attributes');
63+
expect(response.rawData.data[0]).to.have.property('relationships');
64+
expect(response.rawData.data[0].attributes).to.be.an('object');
65+
expect(response.rawData.data[0].relationships).to.be.an('object');
66+
expect(response.rawData.data[0].attributes.email).to.equal(USER.EMAIL);
67+
expect(response.rawData.included)
68+
.to.be.an('array')
69+
.and.to.have.lengthOf(INCLUDED_LENGTH);
70+
})
5071
})
5172
});

0 commit comments

Comments
 (0)