elierotenberg / fastify-zod

Zod integration with Fastify
MIT License
212 stars 19 forks source link

RouteHandler does not expose FastifyReply object #29

Closed Woody88 closed 1 year ago

Woody88 commented 2 years ago

Hello,

I would like to know how does fastify-zod expect users to modify the reply status for a specific route?

As of now, the RouteHandler type does not expose the FastifyReply object: https://github.com/elierotenberg/fastify-zod/blob/4b1abf7971113f20fed5ff2013d7b5f06faeb782/src/FastifyZod.ts#L44-L52

I can only think of using the onSend hook to intercept the reply request and modify the status code.

balkarika commented 1 year ago

I would also like know the answer to this question. I like this lib, it can create really nice swagger but unfortunately this is a major deal breaker that it cannot reply with HTTP code other than 200.

elierotenberg commented 1 year ago

Hello,

Sorry for the very late reply.

I have pushed 1.3.1 today, with a proposed solution that is backwards-compatible:

f.zod.get(
  `/item/:id`,
  {
    operationId: `getTodoItem`,
    params: `TodoItemId`,
    response: {
      200: `TodoItem`,
      404: `TodoItemNotFoundError`,
    },
  },
  async ({ params: { id } }, reply) => {
    const item = state.todoItems.find((item) => item.id === id);
    if (item) {
      return item;
    }
    reply.code(404);
    return {
      id,
      message: `item not found`,
    };
  }
);

Does this solve your problem?