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

Option "generateMethods" to generate endpoint with specific available methods only #385

Open
pimvanderheijden opened this issue Jul 31, 2018 · 2 comments

Comments

@pimvanderheijden
Copy link

We are relying heavily on express-restify-mongoose. Great module!

For some endpoints of our micro-services we need to exclude specific methods, like allow GET's but not PUT, DELETE etc.

Would be amazing to have an option like "generateMethods" in the restify.serve function. The option will then expect an array of methods to be generated, which by default will be something like:

[
	"delete"
	"deleteOne"
	"get"
	"getOne"
	"getCount"
	"getShallow"
        "patch"
        "put"
]

I'm planning on creating a PR soon. Are you open to accommodating this feature?

@pimvanderheijden pimvanderheijden changed the title Option "generateMethods" to generate endpoint fro specific methods only Option "generateMethods" to generate endpoint with specific available methods only Jul 31, 2018
@Zertz
Copy link
Collaborator

Zertz commented Jul 31, 2018

Glad it's working for you! :)

I think preMiddleware should cover that use case,

preMiddleware: (req, res, next) => {
  if (["delete", "put"].includes(req.method.toLowerCase())) {
    return res.sendStatus(403)
  }

  next()
}

@pimvanderheijden
Copy link
Author

pimvanderheijden commented Jul 31, 2018

Yes, we're doing something similar now:

_.each @mongoose.connection.models, (Model) =>
    name = Model.collection.name

    debug "Setting up rest endpoints for collection #{name}"

    resitfy.serve @app, Model,
        name:       name

        preCreate: (req, res, next) =>
            return req.res.status(400).json { message: "POST not available" }

        preUpdate: (req, res, next) =>
            return req.res.status(400).json { message: "PUT/PATCH not available" }

       preDelete: (req, res, next) =>
            return req.res.status(400).json { message: "DELETE not available" }

Doing this differently for specific models takes some more effort, and especially when doing it over and over again in different ways in different projects, it would be great setting it as an option. I think it's a common use case anyway.

Also, the problem for me is that the routes are created in Express. They are now only blocked inside the middleware pipeline. Instead of a 400 (or 404), I would really like to see for example the express "Cannot POST ..." error for routes that are non-existent.

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