mschae / trailing_format_plug

An elixir plug to support legacy APIs that use a rails-like trailing format: http://api.dev/resources.json
25 stars 15 forks source link

Pass in accepted formats via options #14

Closed msmykowski closed 7 years ago

msmykowski commented 7 years ago

I would like to use options to pass in the accepted format types. For instance, if I have a json endpoint, I would like to allow /api/example.json as a valid request. However, I would not want /api/example.html or any other extension to hit my json endpoint.

This pull request first checks to see if there are any options. If not, then all formats are accepted and stripped of their format.

If options are passed, it checks if the format of the request matches any of the formats in the options. The requests that do match will have their formats stripped. Requests that do not match are ignored and the conn is simply returned.

mschae commented 7 years ago

Unless I'm missing something this mimics the :accepts Plug of the Phoenix router.

Please use that plug or - if you don't want to use Phoenix - write another plug for content negatiation.

I don't believe negotiating formats is in the scope of this Plug.

Sorry I have to decline your change at this point. Thank you for your contribution and understanding.

msmykowski commented 7 years ago

Oh you are completely right. I was not aware of how the :accepts Plug worked. Thank you for the information!