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

Allow the use of nested resource key #47

Closed
ingro opened this issue Feb 5, 2015 · 5 comments
Closed

Allow the use of nested resource key #47

ingro opened this issue Feb 5, 2015 · 5 comments

Comments

@ingro
Copy link

ingro commented Feb 5, 2015

Hello and thanks for your great work!

I have a little problem. We use a structure like this:

{
  "data": [
    {
      "id": 1,
      "name": "John"
    },
    {
      "id": 2,
      "name": "Jake"
    },
  ],
  "meta": {
    "total": 2,
    "page": 1
  }
}

Now I can write the structure in my generate.js with the "data" and "meta" keys and it return correctly when I call /user.

Anyway when I try to get a single element of the collection, say /user/1, it returns me an empty object with a 404 http code.

I understand that this is a non-conventional request, but do you think that a situation like that could be handled via some config file or options passed to the CLI?

Thank you.

@typicode
Copy link
Owner

typicode commented Feb 5, 2015

Hi Ingro, always happy to know my projects are appreciated, thanks :)

Not really sure to undersdand your example, because I don't see user in your code. What does generate.js outputs and how do you get /user?

@typicode
Copy link
Owner

typicode commented Feb 6, 2015

Oh I think I'm getting it. When you get /user, the response is the structure you've shown:

{
  "data": [
    {
      "id": 1,
      "name": "John"
    },
// ...

And because user list is in data, you can't get /user/1.

@ingro
Copy link
Author

ingro commented Feb 6, 2015

Yeah sorry it wasn't clear on the first thought, that was the response I expect from the server. I need the meta data to manage the pagination on the client application, but as you guessed I can't get a specific user structuring the data in this way.

Completely out of scope but, great avatar choice! :)

@typicode
Copy link
Owner

typicode commented Feb 6, 2015

Thanks, good choice for Radiohead :)

Unfortunately, I don't see a way to use that kind of structure with this project. It would mean creating some all new routes.
And POST, DELETE would need to update the meta properties too.

The current approach in JSON Server for pagination, is to use _start, _end query parameters and the X-TOTAL-COUNT header.

Sorry about that.

Maybe you can have a look at dyson or interfake.

Also, I think it wouldn't take too long to create a custom fake server using Express. Actually, if you look at src/router.js you'll see that the code is quite simple.

Hope it will help.

@ingro
Copy link
Author

ingro commented Feb 6, 2015

Well thanks for your suggestions, I will look at the resources you linked me, I think you can close this issue, keep up the good work! :D

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

2 participants