Giter VIP home page Giter VIP logo

Comments (22)

typicode avatar typicode commented on July 16, 2024 15

For nested resources to work, you need to structure your db.json this way:

{
  "environments": [
    { "id": 12 },
    { "id": 19 }
  ],
  "reports": [
    { "id": 17, "environmentId": 12 }
    { "id": 25, "environmentId": 12 }
  ]
}

Then /environments/12/reports should return reports that have environmentId === 12

from json-server.

typicode avatar typicode commented on July 16, 2024 2

@patotoma have you tried custom routes https://github.com/typicode/json-server#add-custom-routes? You can map many different kind of routes and for example nested resources.

"/some/nested/route/posts/:postId/comments/:commentId": "/comments/:commentId"

/some/nested/route/posts/1/comments/2 will rewrite to /comments/2

from json-server.

Legitcoder avatar Legitcoder commented on July 16, 2024 2

Documentation should state there's only one level of nesting. Was trying to figure out why I can't go two levels down, found out through here. Would've been searching and hacking at json-server for a while if I hadn't seen this. You can use ?id=3, but it only works for get. Doesn't work for delete. Still json-server is great since you don't have to wait on a back-end to be fully complete to get started.

from json-server.

jderijke avatar jderijke commented on July 16, 2024 1

Love to see nested routes too. Even if it is going down 2 levels instead of 1, that would already be nice.

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

Ah. kk. Thanks for the update. I'll try that and close this if it works.

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

Worked like a charm. Would you be averse to a PR clarifying nested requests in README.md? I can have it submitted in a few minutes if you want it.

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

One more question: should /environments/12/reports/17 work? I thought it was earlier today, but now I'm running it and no dice. 404 error.

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

Hrmmm... looks like not. The nested routes are being handled statically. I'll take a look at it tonight and see if I can't add nested routing ad infinitum.

from json-server.

typicode avatar typicode commented on July 16, 2024

You're right, one level of nesting is supported.
Except if there's many people that need infinite nesting, it's not wished for the moment.

Mainly to limit code complexity and also based on the idea that if you know the report id, you can directly make a request to get it /reports/17

However a workaround could be to use filters, there's no limit to how much you can use:

/reports?id=17&environmentId=12&...

There's also an (old) article but still interesting about deep nesting:
http://weblog.jamisbuck.org/2007/2/5/nesting-resources

Do you need deep nesting because of a particular front-end library?

PS: thanks for the PR, I'll take a look at it as soon as I can :)

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

Thanks for the resource! I'm definitely gonna be going over that.

That being said, the API I'm mocking isn't in my control, I'm building a front-end for it, and I'm just trying to mimic it as closely as possible. It's not that difficult of an update, but if that feature isn't needed in the library as a whole, I'll just source my fork with the changes needed, no worries.

Let me know if there needs to be any changes in that PR, I'm happy to jump in and fix stuff.

from json-server.

typicode avatar typicode commented on July 16, 2024

Great.

Not sure if it's the best way to fill the gaps for your API, but using the project as a module you could do something like this:

var jsonServer = require('json-server')

var server = jsonServer.create()
server.use(jsonServer.defaults)

// Simple hack
server.use('/environments/:environmentId/reports/:reportId', function (req, res) {
  res.redirect('/reports/' + req.params.reportId)
})

server.use(jsonServer.router('db.json'))

server.listen(3000)

I've not tested code, but it should work.

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

Ok, so here's what I got: https://github.com/therebelrobot/json-server/tree/feature/nested-calls

I rebuilt router.js quite a bit, it was hard-coding the routes and duplicating code from show to list. I changed the name of list to parse, and routed all calls through it (it now checks all the ids and manages sub-resources). I split out the show function to find (which handles the actual object finding in lowdb) and present (which handles sorting, paging, etc), making things a little more modular. Also added most internal objects that were in list to req._internal so it could be called by subsequent middleware.

I ran all of it through the test suite, also added 4 more tests for more nested calls. Everything is passing 100%.

Also added a coverage folder, and verified >99% code coverage of the new router.

I know you weren't looking at this kind of feature, but if you want a PR for it after all, lemme know :)

from json-server.

ir-g avatar ir-g commented on July 16, 2024

@typicode @therebelrobot

I would love to see this PR'd in as I really would love to see nested calls working, even if it was just an optional feature.

I wouldn't normally comment, but this feature would be super-useful.

from json-server.

therebelrobot avatar therebelrobot commented on July 16, 2024

@isaacrg As per @typicode's desire to keep things simple, I've forked the repo and republished it. Here it is: https://github.com/therebelrobot/json-mock

I'll close this issue now :)

from json-server.

typicode avatar typicode commented on July 16, 2024

The feross/standard is a good point, FWIW the project now follows it. Maybe it can save you time.

from json-server.

dschu-lab avatar dschu-lab commented on July 16, 2024

I would also love to see this feature implemented. I'm currently trying to mock an API which is not in my control, and they are using multiple nested resources, even if the id of an element is already known. 😞

from json-server.

gEndelf avatar gEndelf commented on July 16, 2024

+1

from json-server.

patotoma avatar patotoma commented on July 16, 2024

+1 for this feature, @therebelrobot's fork is two years old and I would like much more to see it done here

from json-server.

patotoma avatar patotoma commented on July 16, 2024

@typicode hey man thanks for quick response. Have to say that after a while of using json-server I found out why you made it like this. It wants kind of different thinking that I was used to but now I find using nested routing as bad practice. Route rewrites can be used to map existing apis but from now on I am going to use top level routing only. My team is currently working on a service that will generate db.json for you, so that could be cool. BTW: thanks for your great work!

from json-server.

ennisa-ire avatar ennisa-ire commented on July 16, 2024

Hey it would be a nice feature to have a deep nested resource feature, the routes approach doesnt cut it. Are there plans to implement it? This would bring already a great service to the next level :)

from json-server.

lbadi avatar lbadi commented on July 16, 2024

+1

from json-server.

ghaschel avatar ghaschel commented on July 16, 2024

This would be nice to have

from json-server.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.