fastify / fastify

Fast and low overhead web framework, for Node.js

Home Page:https://www.fastify.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type narrow on status().send() chains using Type-Providers schema

hpx7 opened this issue · comments

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

This is related to #4823 (and more specifically #4761 which was rejected).

The stated purpose of Type-Providers is as follows:

Type Providers are a TypeScript only feature that enables Fastify to statically infer type information directly from inline JSON Schema. They are an alternative to specifying generic arguments on routes; and can greatly reduce the need to keep associated types for each schema defined in your project.

However, they don't currently compose well with multiple status code responses (see example). This can be solved by inferring the Reply type from the schema as proposed in #4761.

Motivation

Avoid cumbersome and error-prone duplication when working with Type-Providers and multiple status code response types

Example

fastify
  .withTypeProvider<JsonSchemaToTsProvider>()
  // the below Reply generics should be inferred
  .get<{
    Reply: {
      200: FromSchema<typeof responseSchema>;
      404: { message: string };
    };
  }>(
    "/list",
    {
      schema: {
        response: {
          200: responseSchema,
          404: {
            type: "object",
            properties: { message: { type: "string" } },
            required: ["message"],
          },
        },
      },
    },
    async (request, reply) => {
      // if the Reply generics are omitted, the default is to use the union of all reply types defined in schema (and there is no enforcement on the status code beyond it being a number)
      if (Math.random() < 0.1) {
        return reply.code(404).send({ message: "Not found" });
      }
      return reply.code(200).send({
        data: [
          { id: "1", name: "app1" },
          { id: "2", name: "app2" },
        ],
      });
    },
  );

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests. We use tsd for types.

@mcollina I would be happy to submit a PR for this if the maintainers agree on the proposed functionality. Given #4761 was rejected, I thought I'd ask first

pinging @fox1t and @sinclairzx81. How do you think this should be fixed?

I submitted #5380 as a fix, but please let me know if the approach isn't correct

@hpx7
You absolutely rock! This was always the last piece missing in my otherwise absolutely type-safe Fastify based API.
Thank you for working on it.