Comments (15)
I would recommend fastify/fastify-accepts.
from fastify.
The PR was already merged #144
If there are other things, I'd prefer move the conversation to fastify-accepts
from fastify.
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.
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.
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.
Maybe a getter on Request object can be an help
from fastify.
yes. Do you want to create the plugin @allevo?
from fastify.
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.
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.
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.
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.
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.
fastify/fastify-accepts
or allevo/fastify-accepts
????
from fastify.
https://github.com/fastify/fastify-accepts
from fastify.
Good job! Would you like to send us a PR for the README of fastify to list fastify-accepts
?
from fastify.
Related Issues (20)
- querystring with property of type number fails HOT 7
- onError hook is called before errorHandler when error is thrown in onRequest hook HOT 7
- Approval Request for Future State Proposal HOT 20
- Expose .writeEarlyHints()
- Logger methods are not bound to this HOT 4
- Whole API breaks when passing non-async function to fastify.register HOT 3
- Documentation and example of webhook signature check (through rawBody) HOT 1
- req.headers or req.query null in a 404 handler HOT 6
- Update reply.then and fastify.then types to use branded types to please typescript-eslint HOT 5
- hasRoute not working properly HOT 7
- Serverless Firebase Functions (v2) Issue: "... failed to start and listen on the port defined provided by the PORT=8080 ..." HOT 2
- import/require fastify module is incredibly slow > 300 ms HOT 3
- Custom format for ajv gets overwritten by ajv-formats HOT 4
- Plugin: Generate endpoints based on file names on a directory. HOT 16
- custom error handler not breaking encapsulation using fastify-plugin HOT 1
- Broken type inferrence with auxilliary hook handlers HOT 2
- listen() registers a callback even when listen() fails, calling the callback twice - once when it fails and again if it succeeds in a later call HOT 5
- Only automatically run workflows when a member approved them or if the PR is created by a member HOT 2
- app.close only triggers when a new request comes, resulting in 503 error HOT 7
- Missing cookie field in headers of upgrade websocket request HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fastify.