Skip to content

Commit

Permalink
Fixup PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Henning Vogt committed Oct 13, 2019
1 parent 5e100db commit e8dc6b2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 48 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ Which will work for:
```
If this option is not set it will fall back to `total`.

In addition, if your server doesn't provide a count field, you can set *total count* to `null`, and the provider will
assume the total count is the same as the length of the data array:
In addition, if your server doesn't provide a count field, you can set *total
count* to `null`, and the provider will assume the total count is the same as
the length of the data array:

``` javascript
const dataProvider = jsonapiClient('http://localhost:3000', { total: null });
Expand Down Expand Up @@ -150,4 +151,5 @@ Default: `brackets`
Options: `indices`, `repeat`, `comma`

## Contributors
* [TMiguelT](https://github.com/TMiguelT)
* [hootbah](https://github.com/hootbah)
28 changes: 11 additions & 17 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,24 +131,18 @@ export default (apiUrl, userSettings = {}) => (type, resource, params) => {

return axios({ url, ...options })
.then((response) => {
// Do some validation of the total parameter if a list was requested
let total;
if ([GET_LIST, GET_MANY, GET_MANY_REFERENCE].includes(type)){
if (settings.total === null){
// If the user explicitly provided no total field, then just count the number of objects returned

// For all collection requests get the total count.
if ([GET_LIST, GET_MANY, GET_MANY_REFERENCE].includes(type)) {
// When meta data and the 'total' setting is provided try
// to get the total count.
if (response.data.meta && settings.total) {
total = response.data.data.length;
}
else if ('meta' in response.data && settings.total in response.data.meta){
// If the user specified a count field, and it's present, then just use that
total = response.data.meta[settings.total];
}
else if (!('meta' in response.data) || !(settings.total in response.data.meta)){
// The third option: the server doesn't return a total property at all, so we have to throw an exception
throw new Error(`The JSON API response did not contain the field "${settings.total}" in the meta object.
Consider either setting the "total" setting to null for default behaviour, changing the "total" setting to
point to the correct meta field, or ensuring your JSON API server is actually returned a "total" meta
property.`)
}

// Use the length of the data array as a fallback.
total = total || response.data.data.length;
}

switch (type) {
Expand All @@ -159,7 +153,7 @@ export default (apiUrl, userSettings = {}) => (type, resource, params) => {
{ id: value.id },
value.attributes,
)),
total
total,
};
}

Expand All @@ -169,7 +163,7 @@ export default (apiUrl, userSettings = {}) => (type, resource, params) => {
{ id: value.id },
value.attributes,
)),
total
total,
};
}

Expand Down
44 changes: 15 additions & 29 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,36 +228,22 @@ describe('GET_MANY', () => {
});
});

// This test should work exactly the same as the normal GET_LIST test, but the returned data has no
// meta field, and thus no count variable. We set the count variable to null in the client
describe("GET_LIST with {total: null}", () => {
it("contains a total property", () => {
nock("http://api.example.com")
// This test should work exactly the same as the normal GET_LIST test, but the
// returned data has no meta field, and thus no count variable. We set the
// count variable to null in the client
describe('GET_LIST with {total: null}', () => {
it('contains a total property', () => {
nock('http://api.example.com')
.get(/users.*sort=name.*/)
.reply(200, getListNoMeta)

const noMetaClient = jsonapiClient("http://api.example.com", {
total: null
})

return expect(
noMetaClient("GET_LIST", "users", {
pagination: {page: 1, perPage: 25},
sort: {field: "name", order: "ASC"}
})
).to.eventually.have.property("total").that.is.equal(5)
})
});
.reply(200, getListNoMeta);

describe("GET_LIST with incorrect \"total\"", () => {
it("throws an Error in this situation", () => {
nock("http://api.example.com")
.get(/users.*sort=name.*/)
.reply(200, getListNoMeta)
const noMetaClient = jsonapiClient('http://api.example.com', {
total: null,
});

return expect(client("GET_LIST", "users", {
pagination: {page: 1, perPage: 25},
sort: {field: "name", order: "ASC"}
})).to.be.rejectedWith(Error)
})
return expect(noMetaClient('GET_LIST', 'users', {
pagination: { page: 1, perPage: 25 },
sort: { field: 'name', order: 'ASC' },
})).to.eventually.have.property('total').that.is.equal(5);
});
});

0 comments on commit e8dc6b2

Please sign in to comment.