Giter Site home page Giter Site logo

accept headers parsing about fastify HOT 15 CLOSED

fastify avatar fastify commented on May 10, 2024
accept headers parsing

from fastify.

Comments (15)

mcollina avatar mcollina commented on May 10, 2024 1

I would recommend fastify/fastify-accepts.

from fastify.

allevo avatar allevo commented on May 10, 2024 1

The PR was already merged #144

If there are other things, I'd prefer move the conversation to fastify-accepts

from fastify.

delvedor avatar delvedor commented on May 10, 2024

How do we want to implement this?
Having the field headers directly into the request object instead of request.req could be useful?

from fastify.

mcollina avatar mcollina commented on May 10, 2024

I think we need to add a bunch of methods to Request, maybe based on https://www.npmjs.com/package/accepts.

Should we do this on a plugin?

from fastify.

delvedor avatar delvedor commented on May 10, 2024

That would be perfect. I think a plugin is better, so if a user do not need it, will not pay that cost.

from fastify.

allevo avatar allevo commented on May 10, 2024

Maybe a getter on Request object can be an help

from fastify.

mcollina avatar mcollina commented on May 10, 2024

yes. Do you want to create the plugin @allevo?

from fastify.

allevo avatar allevo commented on May 10, 2024

yea, but the nightmare is on the serialization.

The another solution could be to implement it in fastify to be described in routes definition.
Like:

fastify.route({
  method: 'GET',
  url: '/',
  schema: { ... },
  beforeHandler: function (request, reply, done) { ... },
  handler: function (request, reply) {
    reply.send({ hello: 'world' })
  },
  accepts: ['application/json', 'text/html'],
})

from fastify.

mcollina avatar mcollina commented on May 10, 2024

No, I prefer this to be handled all in code. You'll need code there anyway.

why the nightmare is on the serialization? what do you mean?

from fastify.

allevo avatar allevo commented on May 10, 2024

For example:

fastify.get('/foobar', (req, res) => {
   switch(req.accept.type(['json', 'html'])) {
    case 'json':
      // handle json response
      break
    case 'html':
      // handle html response
      break
    default:
      // the fallback is text/plain, so no need to specify it above
      // handle plain response
      break
  }
  // 406
})

can be tricky (or not?)
For example the 406 response can be forgotten.

Another approach can be:

fastify.get('/foobar', (req, res) => {
  res.render({ });
})

fastify.decorateReply('render', (obj) => {
   // assuming this == res
  
   switch(res.req.accept.type(['json', 'html'])) { //  <-----  ugly access to req obj HERE
    case 'json':
      // handle json response
      this.json(obj) // NB: this reuses the serialization implemented in fastify
      break
    case 'html':
      // handle html response
      this.type('html')
      this.res.end('<html>' + JSON.stringify(obj) + '</html>')
      break
    default:
      // the fallback is text/plain, so no need to specify it above
      // handle plain response
      break
  }
  // 406
})

where the reply decoration is made into a plugin.

The second way allows us to split the request handler from the response serialization.

from fastify.

mcollina avatar mcollina commented on May 10, 2024

That approach would not work in practice, because you would have to customize both the HTML and other types. So, maybe we can build a general mechanism on top of this to provide that functionality, but let's start simple and add the accept() part to the Request.

from fastify.

delvedor avatar delvedor commented on May 10, 2024

Let's consider that swagger uses the accepts field, so could be nice find a way to set it in a way that another plugin can see that rule.

@allevo if you need a good example on how decorate the Request object check out fastify-multipart.

from fastify.

allevo avatar allevo commented on May 10, 2024

fastify/fastify-accepts or allevo/fastify-accepts ????

from fastify.

allevo avatar allevo commented on May 10, 2024

https://github.com/fastify/fastify-accepts

from fastify.

mcollina avatar mcollina commented on May 10, 2024

Good job! Would you like to send us a PR for the README of fastify to list fastify-accepts?

from fastify.

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.