Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

About X-Total-Count #67

Closed
binali-rustamov opened this issue Apr 28, 2015 · 6 comments
Closed

About X-Total-Count #67

binali-rustamov opened this issue Apr 28, 2015 · 6 comments

Comments

@binali-rustamov
Copy link
Contributor

May we provide information in X-Total-Count like JSON parameter?
In my case: I use ExtJS for build my application. Currently I have problem with getting information from response header.
In my opinion: We can make json-server response like
{ count: .... , data: .... }

@typicode
Copy link
Owner

There's many way to return a slice of an array, and you're right that's a valid way to do.
There was also a discussion about pagination here: #57

For the moment, I prefer to return an array and meta in headers. But maybe a mock server implementing JSON API would work better with ExtJS (never used this lib)... can't remember right now other mock server names, but there is fortunejs.

Sorry about that.

binali-rustamov added a commit to binali-rustamov/json-server that referenced this issue Apr 29, 2015
@typicode typicode closed this as completed May 4, 2015
@a-ursino
Copy link

Hi @typicode
we can enable this (a count property in the body response) via a flag?
Several (request) solution doesn't provide a way to access to header data.

@Luwangel
Copy link

Is there any solution to do that?

@lowdev
Copy link

lowdev commented Aug 23, 2019

I found a solution by creating server.js :

Const jsonServer = require('json-server')
.....
....
....

router.render = function(req, res) {
Var newData = {
TotalCount: res.get('X-Total-Count'),
Result: res.locals.data
}
res.json(newData)
}

@Luwangel
Copy link

With your solution, you'll not have the "total count", but the number of returned items.

I found a solution using lowdb. It's not perfect because we need to call the db twice (one for the initial request, one for the total count).

For example, I want to count the number of users.

const db = await low(myStorage);

const countItems = resource =>
  db
    .get(resource)
    .map('id')
    .value().length;

router.render = (req, res) => {
  res.jsonp({
    items: res.locals.data.items,
    totalItems: countItems('users') // parse url to get the desired resource
  });
};

@uniquejava
Copy link

uniquejava commented Nov 2, 2020

I found a solution by creating server.js :

Const jsonServer = require('json-server')
.....
....
....

router.render = function(req, res) {
Var newData = {
TotalCount: res.get('X-Total-Count'),
Result: res.locals.data
}
res.json(newData)
}

@lowdev, Thank you, This should be the accepted answer.

I was searching for how to get X-Total-Count from render, your answer inspired me. Here is some slightly formatted code.

router.render = (req, res) => {
  // if it's a GET request and result is an Array
  if (req.method === 'GET' && Array.isArray(res.locals.data)) {
    res.json({
      total: res.get('X-Total-Count'),
      list: res.locals.data,
    })
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants