Code Monkey home page Code Monkey logo

Comments (10)

ethanresnick avatar ethanresnick commented on July 23, 2024

it seems as though the way to do it is to replace the resource with undefined, but that logic is only applied to Collections

Yes, this is intentional, if I remember my thought process correctly. Basically, it's ok to remove a single resource from a response that returns a collection, because the resulting response (just the collection minus one item) is still a valid response. By contrast, a single resource request that doesn't return a resource needs to be an error (probably, but not necessarily, a 404; could also be a 403 etc).

So, I think the way to do what you want is to have your beforeRender transform throw an APIError with the appropriate details. Try that, and let me know if for some reason it doesn't work :)

from json-api.

carlbennettnz avatar carlbennettnz commented on July 23, 2024

What I'm doing to getting /users which right now returns all users, but should return just the authenticated user (or more, depending on privileges). beforeRender is being called once on each of the users found.

Is this what you're talking about with throwing an APIError?

var APIError = require('json-api').types.Error

module.exports = {
    urlTemplates: {
        self: 'https://localhost:3000/users/{id}',
        relationship: 'https://localhost:3000/users/{ownerId}/relationships/{path}'
    },

    beforeRender: function(resource) {
        throw new APIError(500, undefined, 'Nope')
    }
}

This just hangs for me. Same result if I return the error.

from json-api.

ethanresnick avatar ethanresnick commented on July 23, 2024

Ahh, ok. Then the fact that returning undefined doesn't work is just a bug. I think I know where it's coming from, and will push a fix in a sec.

from json-api.

ethanresnick avatar ethanresnick commented on July 23, 2024

This is fixed now, so you should be able to just return undefined for the resources you want to remove. See the commit message if you're curious about what the bug was.

Edit: of course, you'll have to update to the newest version, 2.8.2

from json-api.

carlbennettnz avatar carlbennettnz commented on July 23, 2024

Works perfectly. Thanks so much!

from json-api.

ethanresnick avatar ethanresnick commented on July 23, 2024

Absolutely! Please lmk if you hit any other problems. Ideas for the library are also always welcome!

from json-api.

carlbennettnz avatar carlbennettnz commented on July 23, 2024

Do you want me to report hangs when I find them? I've bumped into a few while doing things wrong. Now that I have things set up correctly it's all working fine, but it might be nice to give proper errors in those cases.

from json-api.

ethanresnick avatar ethanresnick commented on July 23, 2024

Yeah, reporting those would be much appreciated! I definitely coded this library for the happy path, so the code isn't great about throwing errors where it should (though that's starting to improve as we add more tests).

from json-api.

carlbennettnz avatar carlbennettnz commented on July 23, 2024

Sweet, I'll open some more issues. I'll send in a PR or two as well If I manage to get my head around generators

from json-api.

ethanresnick avatar ethanresnick commented on July 23, 2024

Awesome! Thank you! I'll take a look at those issues later tonight :)

from json-api.

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.