Comments (11)
@delvedor I'm +1 on the idea.
from fastify.
Since we must support also swagger, I think the best (and more elegant) solution will be something like:
out: {
'200': {},
'500': {}
}
And so on.
Maybe we can rename out
in response
.
Since we are building the schema at compilation time we can check if the user is passing directly the schema or the different status codes, and create the function(s) accordingly.
An even better solution could be:
out: {
'2xx': {},
'4xx': {},
'404': {}
'5xx': {}
}
from fastify.
My bad, I thought that use numbers as key in objects was incorrect, but I was wrong :D
from fastify.
Hi, thank you for the interest!
The out
schema is not applied to all the routes, but only in the routes where you pass it as parameter.
Example:
fastify.get('/no-schema', (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.get('/schema', {
out: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}, (req, reply) => {
reply.send({ hello: 'world' })
})
The out schema is applied in every case, not matter of the status code and there is no way to set it for all the routes. However, you could store it in a variable and pass it to all the routes.
const schema = {
out: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
fastify.get('/route', schema, (req, reply) => {
reply.send({ hello: 'world' })
})
fastify.get('/other-route', schema, (req, reply) => {
reply.send({ hello: 'world' })
})
If you want to use an out schema for the "correct" response and support also a completely different object, you must use the .serializer
method of reply
, that forces reply
to use a custom serializer.
Example:
const schema = {
out: {
type: 'object',
properties: {
hello: { type: 'string' }
}
}
}
fastify.get('/route', schema, (req, reply) => {
if (someError) {
reply
.serializer(JSON.stringify)
.send({ error: 'message' })
return
}
reply.send({ hello: 'world' })
})
@mcollina could be a nice idea support different output schema based on the status code, it will be useful for the swagger generator as well :)
from fastify.
Maybe a replacement for default serializer is enough.
I'd like to be sure to have all my 5xx and 4xx response bodies with the same structure.
from fastify.
Can we declare a specification for this issue ?
Maybe the out
key could be used only for 200 and, if specified, the out500
or out5xx
could be used for 5xx responses.
In that way, the back compatibility is kept.
Let me know if this approach is fine for you
from fastify.
@allevo the approach is fine for me.. @delvedor what do you think?
from fastify.
I'm 👍 with the change, the only things I do not like is that we need '
, do you see any way around that?
from fastify.
@allevo, would you like to fire up a PR?
from fastify.
@mcollina I'll start as soon as possible
from fastify.
Closed in #96.
from fastify.
Related Issues (20)
- Backport webdav methods from next to current branch HOT 7
- OPTIONS HTTP method body processing HOT 3
- Dependency update for v5 HOT 1
- Merge main into next HOT 4
- Scoped FastifyRequest Customization via Generic Parameter in FastifyInstance HOT 1
- Flaky test on N|Solid 20 MacOS HOT 2
- Implement simple B-Tree for faster string comparisons HOT 9
- Throwing error in setErrorHandler after a JSON parse SyntaxError causes app to crash
- Where is the documentation for <Reply>.sendFile? HOT 2
- Switch to ajv/2020 for fastify 5 HOT 7
- Fastify v5 coordination HOT 7
- Does Validation-and-Serialization document's outdated?
- Should we drop Node.js v18 for v5? HOT 11
- `disableErrorLogging` option HOT 1
- Request hangs if you `await reply.status` in an async handler. HOT 15
- 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
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.