Gathering detailed insights and metrics for @fastify/type-provider-typebox
Gathering detailed insights and metrics for @fastify/type-provider-typebox
Gathering detailed insights and metrics for @fastify/type-provider-typebox
Gathering detailed insights and metrics for @fastify/type-provider-typebox
npm install @fastify/type-provider-typebox
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
154 Stars
140 Commits
27 Forks
13 Watching
8 Branches
38 Contributors
Updated on 23 Nov 2024
Minified
Minified + Gzipped
TypeScript (53.99%)
JavaScript (46.01%)
Cumulative downloads
Total Downloads
Last day
1.3%
15,427
Compared to previous day
Last week
6.2%
86,927
Compared to previous week
Last month
-5.7%
367,271
Compared to previous month
Last year
90.9%
3,916,493
Compared to previous year
1
A Type Provider for Typebox
1npm i @sinclair/typebox # Required as peer dependency 2npm i @fastify/type-provider-typebox
This package provides enhanced support for TypeBox by integrating it with the Fastify Type Provider infrastructure. It provides a re-export of the TypeBox Type.*
builder for convenience as well as providing additional utility types and optional validation infrastructure that can be useful when leveraging TypeBox with Fastify.
1import Fastify from 'fastify' 2import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox' 3 4const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>()
Note that the following will not work:
1import Fastify from 'fastify' 2import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox' 3 4const fastify = Fastify() 5 6fastify.withTypeProvider<TypeBoxTypeProvider>()
1import Fastify from 'fastify' 2import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' 3 4// This package re-export Typebox package 5// but you can also use any builders imported 6// directly from @sinclair/typebox 7import { Type } from '@sinclair/typebox' 8 9 10const fastify = Fastify().withTypeProvider<TypeBoxTypeProvider>() 11 12fastify.post('/', { 13 schema: { 14 body: Type.Object({ 15 x: Type.String(), 16 y: Type.Number(), 17 z: Type.Boolean() 18 }) 19 } 20}, (req) => { 21 // The `x`, `y`, `z` types are automatically inferred 22 const { x, y, z } = req.body 23})
1import { 2 FastifyReply, 3 FastifyRequest, 4 RawRequestDefaultExpression, 5 RawServerDefault, 6 RawReplyDefaultExpression, 7 ContextConfigDefault 8} from 'fastify'; 9import { RouteGenericInterface } from 'fastify/types/route'; 10import { FastifySchema } from 'fastify/types/schema'; 11import { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; 12 13export type FastifyRequestTypebox<TSchema extends FastifySchema> = FastifyRequest< 14 RouteGenericInterface, 15 RawServerDefault, 16 RawRequestDefaultExpression<RawServerDefault>, 17 TSchema, 18 TypeBoxTypeProvider 19>; 20 21export type FastifyReplyTypebox<TSchema extends FastifySchema> = FastifyReply< 22 RawServerDefault, 23 RawRequestDefaultExpression, 24 RawReplyDefaultExpression, 25 RouteGenericInterface, 26 ContextConfigDefault, 27 TSchema, 28 TypeBoxTypeProvider 29> 30 31export const CreateProductSchema = { 32 body: Type.Object({ 33 name: Type.String(), 34 price: Type.Number(), 35 }), 36 response: { 37 201: Type.Object({ 38 id: Type.Number(), 39 }), 40 }, 41}; 42 43export const CreateProductHandler = ( 44 req: FastifyRequestTypebox<typeof CreateProductSchema>, 45 reply: FastifyReplyTypebox<typeof CreateProductSchema> 46) => { 47 // The `name` and `price` types are automatically inferred 48 const { name, price } = req.body; 49 50 // The response body type is automatically inferred 51 reply.status(201).send({ id: 'string-value' }); 52 // ^? error TS2322: Type 'string' is not assignable to type 'number'. 53};
Note When using plugin types, withTypeProvider is not required in order to register the plugin
1import { Type, FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox' 2 3const plugin: FastifyPluginAsyncTypebox = async function(fastify, _opts) { 4 fastify.get('/', { 5 schema: { 6 body: Type.Object({ 7 x: Type.String(), 8 y: Type.Number(), 9 z: Type.Boolean() 10 }) 11 } 12 }, (req) => { 13 /// The `x`, `y`, and `z` types are automatically inferred 14 const { x, y, z } = req.body 15 }); 16}
TypeBox provides an optional type compiler that perform very fast runtime type checking for data received on routes. Note this compiler is limited to types expressable through the TypeBox Type.*
namespace only. To enable this compiler, you can call .setValidatorCompiler(...)
with the TypeBoxValidatorCompiler
export provided by this package.
1import { Type, TypeBoxTypeProvider, TypeBoxValidatorCompiler } from '@fastify/type-provider-typebox' 2import Fastify from 'fastify' 3 4const fastify = Fastify().setValidatorCompiler(TypeBoxValidatorCompiler) 5 6fastify.withTypeProvider<TypeBoxTypeProvider>().get('/', { 7 schema: { 8 querystring: Type.Object({ 9 x: Type.String(), 10 y: Type.String(), 11 z: Type.String() 12 }) 13 } 14}, (req) => { 15 const { x, y, z } = req.query 16})
For additional information on this compiler, please refer to the TypeBox documentation located here
No vulnerabilities found.
No security vulnerabilities found.