@foodsy-app/fastify-typebox
Plugin for Fastify to prevent having to write duplicate type definitions for schemas.
@foodsy-app/fastify-typebox
uses typebox, to compose JSON schemas.
Important note before using this plugin!
This plugin requires patch-package to comment existing type declarations for request methods in node_modules/fastify/types/instance.d.ts
since TypeScript does not allow to overwrite declarations.
Create a fastify+3.1.1.patch
file (edit the version based on the Fastify version you have installed) and paste the block below inside:
@@ -66,14 +66,14 @@ export interface FastifyInstance<
ContextConfig = ContextConfigDefault
>(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
- get: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- head: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- post: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- put: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- delete: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- options: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- patch: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
- all: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // get: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // head: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // post: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // put: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // delete: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // options: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // patch: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
+ // all: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
// addHook: overloads
After this, run the following command:
npx patch-package --patch-dir .
Run the same command using the --reverse
flag if you notice any problems.
Tip
Add the patch file to your source control and add the script below to the scripts section in your package.json
so that people who install your project do not have to manually apply the patch:
"scripts": {
"postinstall": "npx patch-package --patch-dir ."
}
Install
npm i @foodsy-app/fastify-typebox @sinclair/typebox
yarn add @foodsy-app/fastify-typebox @sinclair/typebox
Usage
Add it to your project using register
and you are done!
import fastify from "fastify";
fastify.register(require("@foodsy-app/fastify-typebox"));
Examples
This plugin exposes a new method on the FastifyInstance
called typeboxSchema
Basic usage
Notice: Please refer to the typebox repository for a overview of the typebox API.
import { Type } from "@sinclair/typebox";
server.get(
"/secret-route/:id",
{
schema: server.typeboxSchema({
params: Type.Object({
id: Type.String(),
}),
body: Type.Object({
user: Type.Object({
name: Type.String(),
age: Type.Number({ minimum: 18 }),
}),
}),
}),
},
(request, reply) => {
const id = request.params?.id;
const user = request.body?.user;
}
);