-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Closed
Milestone
Description
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the feature has not already been requested
🚀 Feature Proposal
Change handler's reply type definition to accept RouteGeneric type as first parameter, to be uniformed with that of the request object.
Motivation
Fastify offers a way to define a schema and generate a type out of it, which is great. However, I find it hard to inject that type in the reply object without being verbose when the handler is not defined in the route definition.
Example
This is how it is now:
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { IncomingMessage, Server, ServerResponse } from 'http';
import { HandlerRequest, HandlerResponse, route_schema } from './schema';
const handler = (
request: FastifyRequest<{ Body: HandlerRequest }>,
reply: FastifyReply<Server, IncomingMessage, ServerResponse, { Reply: HandlerResponse }>,
): void => {
const payload = request.body;
reply.status(StatusCodes.OK).send({ hello: payload.name });
};
export const registerHandler = (fastify: FastifyInstance): void => {
fastify.route<{ Body: HandlerRequest; Reply: HandlerResponse }>({
method: 'POST',
url: '/some-url',
schema: route_schema.schema,
handler,
});
};This is what I propose:
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
import { HandlerRequest, HandlerResponse, route_schema } from './schema';
const handler = (
request: FastifyRequest<{ Body: HandlerRequest }>,
reply: FastifyReply<{ Reply: HandlerResponse }>,
): void => {
const payload = request.body;
reply.status(StatusCodes.OK).send({ hello: payload.name });
};
export const registerHandler = (fastify: FastifyInstance): void => {
fastify.route<{ Body: HandlerRequest; Reply: HandlerResponse }>({
method: 'POST',
url: '/some-url',
schema: route_schema.schema,
handler,
});
};Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels