yonathan06 / fastify-now

File based routing for fastify https://github.com/fastify/fastify
MIT License
72 stars 8 forks source link

Support Fastify V3 #4

Closed yonathan06 closed 4 years ago

yonathan06 commented 4 years ago

NowRequestHandler

No longer part of fastify module type, need to be imported directly from fastify-now

import { NowRequestHandler } from 'fastify-now';

also, must include request types in typescript:

export const POST: NowRequestHandler<{ Body: { name: string }; Params: { id: string } }> = async (
  req,
  rep,
) => {
  if (req.body.name === 'Jon Doe') {
    /**
     * in async function, you can return undefined if you already sent a response
     * then it won't try to send a response again with the returned value;
     */
    rep.status(400).send({ message: 'Name can not be Jon Doe' });
    return;
  }
  return { userId: req.params.id };
};