Coder Social home page Coder Social logo

Comments (11)

mcollina avatar mcollina commented on July 30, 2024 2

@delvedor I'm +1 on the idea.

from fastify.

delvedor avatar delvedor commented on July 30, 2024 1

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.

delvedor avatar delvedor commented on July 30, 2024 1

My bad, I thought that use numbers as key in objects was incorrect, but I was wrong :D

from fastify.

delvedor avatar delvedor commented on July 30, 2024

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.

allevo avatar allevo commented on July 30, 2024

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.

allevo avatar allevo commented on July 30, 2024

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.

mcollina avatar mcollina commented on July 30, 2024

@allevo the approach is fine for me.. @delvedor what do you think?

from fastify.

mcollina avatar mcollina commented on July 30, 2024

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.

mcollina avatar mcollina commented on July 30, 2024

@allevo, would you like to fire up a PR?

from fastify.

allevo avatar allevo commented on July 30, 2024

@mcollina I'll start as soon as possible

from fastify.

delvedor avatar delvedor commented on July 30, 2024

Closed in #96.

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.