GithubHelp home page GithubHelp logo

dwyl / content Goto Github PK

View Code? Open in Web Editor NEW
21.0 21.0 1.0 152 KB

πŸ”Œ A Plug to add Content Negotiation to any Phoenix App so you can render HTML or JSON for the same route.

License: GNU General Public License v2.0

Elixir 100.00%

content's People

Contributors

arhell avatar dependabot[bot] avatar luchoturtle avatar nelsonic avatar simonlab avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

luchoturtle

content's Issues

Phoenix can implicitly render responses based off `Accept` header

Phoenix can already implicitly render different responses based off the Accept header or through forcing a type through the put_accept plug.

def index(conn, _params) do
  doors = Access.list_doors()
  render(conn, :index, doors: doors)
end

Will render "index.json" or "index.html" implicitly.

See https://gist.github.com/chrismccord/31340f08d62de1457454.

In the Phoenix Docs: https://hexdocs.pm/phoenix/Phoenix.Controller.html#render/3

Specifically;

template - which may be an atom or a string. If an atom, like :index, it will render a template with the same format as the one returned by get_format/1. For example, for an HTML request, it will render the "index.html" template. If the template is a string, it must contain the extension too, like "index.json"

"CompileError - undefined function browser/2" when adding content plug to router.ex

Following the instructions in the readme file, I was faced with this error (CompileError) undefined function browser/2 (expected HitsWeb.Router to define such a function or for it to be imported, but none are available) when adding the content plug to the router pipeline.

The error could be obvious to some but as someone who is starting out, it's not clear in the README that one has to also change the pipe_through to pipe_through(:any) in the scope method inside therouter.ex file as well.

I can create a PR with this small change to be more clear to a newbie audience πŸ˜„ .

Any URL should return JSON when ".json" is appended

As a person using the @dwyl App who also wants to build my own custom interface,
I want to be able to append .json to any URL in the App
and see a JSON representation of the content (data)
without having to manually construct an API request.
i.e. use the existing session/JWT and return the data for the given (logged in) person.

Reddit does this for most of their URLs. e.g: reddit.com/r/politics/comments/gpuwml/trump_rips_columbia_as_disgraceful_institution

image

But appending the .json to the end of the URL:
reddit.com/r/politics/comments/gpuwml/trump_rips_columbia_as_disgraceful_institution/.json

image

Todo

[SPIKE] Create Content Negotiation Plug that *Any* Phoenix App can use!

We have show that content negotiation is fairly straightforward in a Phoenix App in our tutorial: https://github.com/dwyl/phoenix-content-negotiation-tutorial
but our conclusion is that it's quite cumbersome.

We need a simplified way of doing content negotiation in our App(s)
whereby we only add one line to the router.ex and one line to each controller.

I think it's possible, but I don't know for sure. πŸ’­
I would prefer to avoid including Phoenix as a Dependency ... because of maintenance and RAM.
So this issue is a "SPIKE" to:

  • explore how to do content negotiation in a reusable plug.

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.