Skip to content

Infer params type from URL #226

@paulius-valiunas

Description

@paulius-valiunas

Prerequisites

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

🚀 Feature Proposal

The params property on a request could be inferred from the URL, as long as it's passed as a string literal.

Motivation

This would prevent param names from being mistyped in the schema property or the URL, thus breaking type safety. It would also make code more concise as you wouldn't need to manually write another schema.

It was previously implemented here: https://github.com/sinclairzx81/fastify-typebox#Params

Example

(excuse me for using Typebox syntax for the schemas, I hope it's readable enough)

    server.get(
      "/queries/:queryId",
      {
        schema: {
          response: { [200]: Type.String() },
          params: Type.Object({ queryId: Type.String() }),
        },
      },
      async (req, res) => {
        await res.send(req.params.queryId);
      },
    );
  };

The above code should work without params: Type.Object({ queryId: Type.String() }):

    server.get(
      "/queries/:queryId",
      {
        schema: {
          response: { [200]: Type.String() },
        },
      },
      async (req, res) => {
        await res.send(req.params.queryId);
      },
    );
  };

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