Skip to content

Commit 30ec4ef

Browse files
committed
fix data array return
1 parent 176064a commit 30ec4ef

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src/services/json-api-datastore.service.spec.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ describe('JsonApiDatastore', () => {
5151
const req = httpMock.expectOne(BASE_URL + 'authors');
5252
expect(req.request.method).toEqual('GET');
5353
expect(req.request.url).toEqual(BASE_URL + 'authors');
54-
req.flush({data: getAuthorData()});
54+
req.flush({data: [getAuthorData()]});
5555
httpMock.verify();
5656
});
5757

@@ -62,7 +62,7 @@ describe('JsonApiDatastore', () => {
6262
expect(req.request.url).toEqual(BASE_URL + 'authors');
6363
expect(req.request.headers.get('Content-type')).toEqual('application/vnd.api+json');
6464
expect(req.request.headers.get('Accept')).toEqual('application/vnd.api+json');
65-
req.flush({data: getAuthorData()});
65+
req.flush({data: [getAuthorData()]});
6666
httpMock.verify();
6767
});
6868

@@ -92,7 +92,7 @@ describe('JsonApiDatastore', () => {
9292
encodeURIComponent('include') + '=comments&' +
9393
encodeURIComponent('filter[title][keyword]') + '=Tolkien'
9494
);
95-
req.flush({data: getAuthorData()});
95+
req.flush({data: [getAuthorData()]});
9696
httpMock.verify();
9797
});
9898

@@ -104,7 +104,7 @@ describe('JsonApiDatastore', () => {
104104
expect(req.request.url).toEqual(BASE_URL + 'authors');
105105
expect(req.request.headers.get('authorization')).toBeTruthy();
106106
expect(req.request.headers.get('authorization')).toEqual('Bearer');
107-
req.flush({data: getAuthorData()});
107+
req.flush({data: [getAuthorData()]});
108108
httpMock.verify();
109109
});
110110

@@ -117,7 +117,7 @@ describe('JsonApiDatastore', () => {
117117
expect(req.request.url).toEqual(BASE_URL + 'authors');
118118
expect(req.request.headers.get('authorization')).toBeTruthy();
119119
expect(req.request.headers.get('authorization')).toEqual('Basic');
120-
req.flush({data: getAuthorData()});
120+
req.flush({data: [getAuthorData()]});
121121
httpMock.verify();
122122
});
123123

@@ -132,7 +132,7 @@ describe('JsonApiDatastore', () => {
132132
const req = httpMock.expectOne(BASE_URL + 'authors');
133133
expect(req.request.method).toEqual('GET');
134134
expect(req.request.url).toEqual(BASE_URL + 'authors');
135-
req.flush({data: getAuthorData()});
135+
req.flush({data: [getAuthorData()]});
136136
httpMock.verify();
137137
});
138138

@@ -147,7 +147,7 @@ describe('JsonApiDatastore', () => {
147147
expect(req.request.method).toEqual('GET');
148148
expect(req.request.url).toEqual(BASE_URL + 'authors');
149149
req.flush({
150-
data: getAuthorData(),
150+
data: [getAuthorData()],
151151
meta: {
152152
page: {
153153
number: 1,
@@ -169,7 +169,7 @@ describe('JsonApiDatastore', () => {
169169

170170
const req = httpMock.expectOne(BASE_URL + 'books');
171171
req.flush({
172-
data: getSampleBook(1, '1'),
172+
data: [getSampleBook(1, '1')],
173173
links: ['http://www.example.org']
174174
});
175175
httpMock.verify();
@@ -183,7 +183,7 @@ describe('JsonApiDatastore', () => {
183183

184184
const req = httpMock.expectOne(BASE_URL + 'authors');
185185

186-
req.flush({ foo: 'bar' }, { status: 500, statusText: 'Server Error' });
186+
req.flush({ foo: 'bar', data: [] }, { status: 500, statusText: 'Server Error' });
187187
httpMock.verify();
188188
});
189189

@@ -194,8 +194,11 @@ describe('JsonApiDatastore', () => {
194194
'books?' +
195195
qs.stringify({ arrayParam: [4, 5, 6] }, { arrayFormat: 'brackets' })
196196
);
197-
req.flush({});
198-
expect(req.request.url.split('?')[1]).toEqual(qs.stringify({ arrayParam: [4, 5, 6] }, { arrayFormat: 'brackets' }));
197+
req.flush({data: []});
198+
expect(req.request.url.split('?')[1]).toEqual(qs.stringify(
199+
{ arrayParam: [4, 5, 6] },
200+
{ arrayFormat: 'brackets' }
201+
));
199202
httpMock.verify();
200203
});
201204

@@ -206,7 +209,7 @@ describe('JsonApiDatastore', () => {
206209
'books?' +
207210
qs.stringify({ arrayParam: [4, 5, 6] }, { arrayFormat: 'brackets' })
208211
);
209-
req.flush({});
212+
req.flush({data: []});
210213
expect(req.request.url.split('?')[1]).toEqual(qs.stringify(
211214
{ arrayParam: [4, 5, 6] },
212215
{ arrayFormat: 'brackets' })

src/services/json-api-datastore.service.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,16 @@ export class JsonApiDatastore {
164164
private extractQueryData<T extends JsonApiModel>(res: any, modelType: ModelType<T>, withMeta = false): T[] | JsonApiQueryData<T> {
165165
let body: any = res;
166166
let models: T[] = [];
167-
let model: T = new modelType(this, body.data);
168-
this.addToStore(model);
167+
let model: T;
168+
body.data.map((_data: any) => {
169+
model = new modelType(this, _data);
170+
this.addToStore(model);
171+
});
172+
models.push(model);
169173
if (body.included) {
170174
model.syncRelationships(body.data, body.included, 0);
171175
this.addToStore(model);
172176
}
173-
models.push(model);
174177
if (withMeta && withMeta === true) {
175178
return new JsonApiQueryData(models, this.parseMeta(body, modelType));
176179
} else {

0 commit comments

Comments
 (0)