Skip to content

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

@hpx7

Description

@hpx7

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" },
        ],
      });
    },
  );

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions