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
Typescript
Module System
Node Version
NPM Version
TypeScript (54.3%)
JavaScript (45.7%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
NOASSERTION License
179 Stars
174 Commits
28 Forks
11 Watchers
3 Branches
40 Contributors
Updated on Jul 02, 2025
Latest Version
5.1.0
Package Id
@fastify/type-provider-typebox@5.1.0
Unpacked Size
28.40 kB
Size
7.74 kB
File Count
19
NPM Version
10.9.0
Node Version
20.18.0
Published on
Dec 03, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
A Type Provider for Typebox.
1npm i @sinclair/typebox # Required as peer dependency 2npm i @fastify/type-provider-typebox
Plugin version | Fastify version |
---|---|
^5.x | ^5.x |
^1.x | ^4.x |
Please note that if a Fastify version is out of support, then so are the corresponding version(s) of this plugin in the table above. See Fastify's LTS policy for more details.
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.
Licensed under MIT.
No vulnerabilities found.
No security vulnerabilities found.