Skip to content

Commit

Permalink
Merge pull request #4 from vitoladev/docs/fastify-request-type
Browse files Browse the repository at this point in the history
doc: type definition of FastifyRequest with TypeProvider
  • Loading branch information
RafaelGSS authored Mar 21, 2022
2 parents 91cc21f + 079bfa6 commit 4703e77
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,38 @@ fastify.get('/', {
const { x, y, z } = req.body
})
```

## Type definition of FastifyRequest + TypeProvider
```ts
import {
FastifyReply,
FastifyRequest,
RawRequestDefaultExpression,
RawServerDefault,
} from 'fastify';
import { Type } from '@sinclair/typebox';
import { RouteGenericInterface } from 'fastify/types/route';
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';

export type FastifyRequestTypebox<TSchema> = FastifyRequest<
RouteGenericInterface,
RawServerDefault,
RawRequestDefaultExpression<RawServerDefault>,
TSchema,
TypeBoxTypeProvider
>;

export const CreateProductSchema = {
body: Type.Object({
name: Type.String(),
price: Type.Number(),
}),
};

export const CreateProductHandler = (
req: FastifyRequestTypebox<typeof CreateProductSchema>
) => {
// The `name` and `price` types are automatically inferred
const { name, price } = req.body;
};
```

0 comments on commit 4703e77

Please sign in to comment.