Skip to content
0:00 / 0:00

#fastify#typebox#typescript

Type-safe Fastify

Adventures in Nodeland - Matteo Collina

4.4K subscribers • 1 video on Hallway

YouTube

Intermediate. Assumes working knowledge.

9.1K views3 years ago2 recommendations

Web developmentProgramming languagesTypeScriptNode.js, Deno and BunAPI design

Matteo Collina demonstrates how to achieve end-to-end type safety and runtime validation in Fastify using TypeBox.

What if I told you Fastify has first-class typescript support, allowing for fully typed requests and replies? And you could use those types to actually validate the incoming data, so you avoid potential security and consistency issues? This is the video for you. We will cover how to integrate npmjs.com/package/@sinclair/typebox with Fastify using github.com/fastify/fastify-type-provider-typebox! All the code in the video is published at github.com/mcollina/type-safe-fastify.

Why it's here

2 people recommend this

What people say. Recommended for learning how to implement runtime validation and automatic OpenAPI generation in Fastify.

u/sinclair_zx81in r/typescript • Recommends

On “TypeBox: Template Literals + Conditional Types at Runtime”

Hi. The types should be OpenAPI 3.1 compliant by default (but you can add specific schema representations for previous versions). Also there's a few frameworks using TypeBox for auto OpenAPI doc generation + service contracts. Have a watch of [video

u/pacioniin r/node • Recommends

On “Well-Structured, Scalable Fastify Project with TypeScript - Seeking Advice”

Watch a couple of videos of Matteo Collina on YouTube (link to this video and read the doc (https://fastify.dev/docs/latest/Reference/TypeScript/). I think that the idea behind fastify is that everything should be a plugin, so you develop your system not with routes, models etc... but with business separation (for example users, products, etc...). To have an idea you can follow this https://github.com/fastify/fastify/blob/main/examples/typescript-server.ts

u/sinclair_zx81in r/typescript • Recommends

On “I'm looking to use my openapi spec to dyanamically create types”

> Fastify is great, but I want the openapi spec to be the one true source of truth, it provides the documentation, and the typing. Fastify does this in reverse, and thus I think misses the mark a little. link to this video Watch till the end where Fastify will produce openapi spec metadata automatically; you still treat openapi as a source of truth.

u/sinclair_zx81in r/typescript • Recommends

On “Introducing TypeBox 1.0: A Runtime Type System for JavaScript”

TypeBox types are represented as Json Schema so you can assign them directly to OpenAPI schematics. In other words, you don't need to transform TypeBox to Json Schema because TypeBox \*is\* Json Schema. Frameworks like Fastify, Elysia, Feathers and many others have integrated OpenAPI infrastructure and can accept raw Json Schema on routes. This schema is then used to generate OpenAPI schematics behind the scenes which in turn is used for auto documentation generation. This is quite common in many modern server frameworks. If it's helpful, have a watch of this video to get an idea of how TB is used for OpenAPI spec generation in Fastify.