{"id":14969120,"url":"https://github.com/fastify/fastify-type-provider-typebox","last_synced_at":"2025-04-14T08:57:33.190Z","repository":{"id":37959772,"uuid":"434422643","full_name":"fastify/fastify-type-provider-typebox","owner":"fastify","description":"A Type Provider for Typebox","archived":false,"fork":false,"pushed_at":"2025-04-02T10:49:30.000Z","size":150,"stargazers_count":166,"open_issues_count":21,"forks_count":27,"subscribers_count":12,"default_branch":"main","last_synced_at":"2025-04-07T02:09:14.283Z","etag":null,"topics":["fastify","fastify-plugin"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@fastify/type-provider-typebox","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fastify.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"fastify","open_collective":"fastify"}},"created_at":"2021-12-03T00:58:16.000Z","updated_at":"2025-04-02T10:49:34.000Z","dependencies_parsed_at":"2023-10-19T11:25:49.794Z","dependency_job_id":"d6450402-a714-4c31-86d1-b974f88ca6d5","html_url":"https://github.com/fastify/fastify-type-provider-typebox","commit_stats":{"total_commits":113,"total_committers":27,"mean_commits":4.185185185185185,"dds":0.7787610619469026,"last_synced_commit":"efbaf0467f60c2294d784bb8712e34da3609f225"},"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-type-provider-typebox","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-type-provider-typebox/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-type-provider-typebox/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fastify%2Ffastify-type-provider-typebox/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fastify","download_url":"https://codeload.github.com/fastify/fastify-type-provider-typebox/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248852123,"owners_count":21171839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin"],"created_at":"2024-09-24T13:41:10.202Z","updated_at":"2025-04-14T08:57:33.171Z","avatar_url":"https://github.com/fastify.png","language":"TypeScript","readme":"# @fastify/type-provider-typebox\n\n[![CI](https://github.com/fastify/fastify-type-provider-typebox/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/fastify/fastify-type-provider-typebox/actions/workflows/ci.yml)\n[![NPM version](https://img.shields.io/npm/v/@fastify/type-provider-typebox)](https://www.npmjs.com/package/@fastify/type-provider-typebox)\n[![neostandard javascript style](https://img.shields.io/badge/code_style-neostandard-brightgreen?style=flat)](https://github.com/neostandard/neostandard)\n\nA Type Provider for [Typebox](https://github.com/sinclairzx81/typebox).\n\n## Installation\n\n```sh\nnpm i @sinclair/typebox # Required as peer dependency\nnpm i @fastify/type-provider-typebox\n```\n\n### Compatibility\n\n| Plugin version | Fastify version |\n| ---------------|-----------------|\n| `\u003e=5.x`        | `^5.x`          |\n| `\u003e=1.x \u003c5.x`   | `^4.x`          |\n\n\nPlease note that if a Fastify version is out of support, then so are the corresponding versions of this plugin\nin the table above.\nSee [Fastify's LTS policy](https://github.com/fastify/fastify/blob/main/docs/Reference/LTS.md) for more details.\n\n## Overview\n\nThis package provides enhanced support for TypeBox by integrating it with the Fastify [Type Provider](https://fastify.dev/docs/latest/Reference/Type-Providers/) 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.\n\n## Usage\n\n```ts\nimport Fastify from 'fastify'\nimport { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox'\n\nconst fastify = Fastify().withTypeProvider\u003cTypeBoxTypeProvider\u003e()\n```\n\nNote that the following will not work:\n\n```ts\nimport Fastify from 'fastify'\nimport { Type, TypeBoxTypeProvider } from '@fastify/type-provider-typebox'\n\nconst fastify = Fastify()\n\nfastify.withTypeProvider\u003cTypeBoxTypeProvider\u003e()\n```\n\n## Example\n\n```ts\nimport Fastify from 'fastify'\nimport { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'\n\n// This package re-export Typebox package\n// but you can also use any builders imported\n// directly from @sinclair/typebox\nimport { Type } from '@sinclair/typebox'\n\n\nconst fastify = Fastify().withTypeProvider\u003cTypeBoxTypeProvider\u003e()\n\nfastify.post('/', {\n  schema: {\n    body: Type.Object({\n      x: Type.String(),\n      y: Type.Number(),\n      z: Type.Boolean()\n    })\n  }\n}, (req) =\u003e {\n  // The `x`, `y`, `z` types are automatically inferred\n  const { x, y, z } = req.body\n})\n```\n\n## Type definition of FastifyRequest \u0026 FastifyReply + TypeProvider\n```ts\nimport type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';\nimport { Type } from '@fastify/type-provider-typebox';\nimport type {\n  ContextConfigDefault,\n  FastifyBaseLogger,\n  FastifyInstance,\n  FastifyReply,\n  FastifyRequest,\n  RawReplyDefaultExpression,\n  RawRequestDefaultExpression,\n  RawServerDefault,\n} from 'fastify';\nimport type { RouteGenericInterface } from 'fastify/types/route';\nimport type { FastifySchema } from 'fastify/types/schema';\n\nexport type FastifyTypeBox = FastifyInstance\u003c\n  RawServerDefault,\n  RawRequestDefaultExpression,\n  RawReplyDefaultExpression,\n  FastifyBaseLogger,\n  TypeBoxTypeProvider\n\u003e;\n\nexport type FastifyRequestTypeBox\u003cTSchema extends FastifySchema\u003e = FastifyRequest\u003c\n  RouteGenericInterface,\n  RawServerDefault,\n  RawRequestDefaultExpression,\n  TSchema,\n  TypeBoxTypeProvider\n\u003e;\n\nexport type FastifyReplyTypeBox\u003cTSchema extends FastifySchema\u003e = FastifyReply\u003c\n  RouteGenericInterface,\n  RawServerDefault,\n  RawRequestDefaultExpression,\n  RawReplyDefaultExpression,\n  ContextConfigDefault,\n  TSchema,\n  TypeBoxTypeProvider\n\u003e;\n\nexport const CreateProductSchema = {\n  body: Type.Object({\n    name: Type.String(),\n    price: Type.Number(),\n  }),\n  response: {\n    201: Type.Object({\n      id: Type.Number(),\n    }),\n  },\n};\n\nexport const CreateProductHandler = (\n  req: FastifyRequestTypeBox\u003ctypeof CreateProductSchema\u003e,\n  reply: FastifyReplyTypeBox\u003ctypeof CreateProductSchema\u003e,\n) =\u003e {\n  // The `name` and `price` types are automatically inferred\n  const { name, price } = req.body;\n\n  // The response body type is automatically inferred\n  reply.status(201).send({ id: 'string-value' });\n  //                       ^? error TS2322: Type 'string' is not assignable to type 'number'.\n};\n```\n\n\n## Plugin definition\n\n\u003e **Note**\n\u003e When using plugin types, withTypeProvider is not required in order to register the plugin\n\n```ts\nimport { Type, FastifyPluginAsyncTypebox } from '@fastify/type-provider-typebox'\n\nconst plugin: FastifyPluginAsyncTypebox = async function(fastify, _opts) {\n  fastify.get('/', {\n    schema: {\n      body: Type.Object({\n        x: Type.String(),\n        y: Type.Number(),\n        z: Type.Boolean()\n      })\n    }\n  }, (req) =\u003e {\n    /// The `x`, `y`, and `z` types are automatically inferred\n    const { x, y, z } = req.body\n  });\n}\n```\n\n## Type Compiler\n\nTypeBox 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.\n\n```ts\nimport { Type, TypeBoxTypeProvider, TypeBoxValidatorCompiler } from '@fastify/type-provider-typebox'\nimport Fastify from 'fastify'\n\nconst fastify = Fastify().setValidatorCompiler(TypeBoxValidatorCompiler)\n\nfastify.withTypeProvider\u003cTypeBoxTypeProvider\u003e().get('/', {\n  schema: {\n    querystring: Type.Object({\n      x: Type.String(),\n      y: Type.String(),\n      z: Type.String()\n    })\n  }\n}, (req) =\u003e {\n  const { x, y, z } = req.query\n})\n```\n\nFor additional information on this compiler, please refer to the TypeBox documentation located [here](https://github.com/sinclairzx81/typebox#Compiler).\n\n## License\n\nLicensed under [MIT](./LICENSE).\n","funding_links":["https://github.com/sponsors/fastify","https://opencollective.com/fastify"],"categories":["TypeScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-type-provider-typebox","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffastify%2Ffastify-type-provider-typebox","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffastify%2Ffastify-type-provider-typebox/lists"}