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)
- Enhance Readme with Dynamically Fetched Contributors' Avatars HOT 2
- Ecosystem.md, missing @fastify/kafka, maybe other core plugins missing too HOT 1
- Pass descriptive error message when "attachValidation: true" is used HOT 6
- Cant we make this an options to be passed for useSemicolonDefault to find-my-way? HOT 4
- Drop security, governance, coc, and contributing files from this repo? HOT 4
- Cannot addHook(), keeps returning error. HOT 6
- Register plugin within namespace HOT 8
- Failed building the ajv validation schema due to error strict mode HOT 3
- Provide better "hover" debuggability in VSCode HOT 2
- live-server on port 8080 doesn't work with parameter routes HOT 2
- Error with Fastify version 4.* in TypeScript: 'register' method expects 0 arguments, but got 1 in 'fastifyRoutes' HOT 4
- feat: Decorater Hook HOT 4
- Dependency AJV is not maintained HOT 6
- Multiple endpoints graphql using Fastify and Mercurius trigger error ' The decorator 'graphql' has already been added! HOT 1
- Return Response.body from a fetch call as a stream HOT 3
- preSerialization are not executed when content-type is set HOT 8
- Fastify response log does not include request details HOT 5
- decorateReply hook does not match overload call HOT 2
- Incorrect Dependencies Checking HOT 1
- Getting Started should probably include a ESM example for the 'Your First Plugin' section 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.