Created
February 1, 2021 17:59
-
-
Save RafaelGSS/8a4932d8680512f6ba87d7e43a588422 to your computer and use it in GitHub Desktop.
Fastify + Typebox Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Type, Static } from '@sinclair/typebox'; | |
export const SendTextMessage = Type.Object({ | |
body: Type.String(), | |
chatId: Type.Number(), | |
options: Type.Optional( | |
Type.Object({ previewUrl: Type.Optional(Type.Boolean()) }), | |
), | |
}); | |
export type SendTextMessage = Static<typeof SendTextMessage>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Fastify from 'fastify'; | |
import { SendTextMessage } from './incoming-request.dto'; | |
const fastify = Fastify(); | |
fastify.post( | |
'/message', | |
{ | |
schema: SendTextMessage, | |
handler: async ( | |
{ body }: FastifyRequest<{ Body: SendTextMessage }>, | |
reply: FastifyReply, | |
): Promise<void> => { | |
reply.code(200).send({}); | |
}, | |
}, | |
); | |
fastify.listen(3000, (err) => { | |
if (err) throw err; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment