npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@foodsy-app/fastify-typebox

v5.3.0

Published

Plugin for Fastify to prevent having to write duplicate type definitions for schemas

Downloads

21

Readme

@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:

diff --git a/node_modules/fastify/types/instance.d.ts b/node_modules/fastify/types/instance.d.ts
index 06380bd..558338d 100644
--- a/node_modules/fastify/types/instance.d.ts
+++ b/node_modules/fastify/types/instance.d.ts
@@ -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
# Or if you are using Yarn
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";

// Available request methods are
// .get, .head, .post, .put, .delete, .options, .patch and .all
server.get(
  "/secret-route/:id",
  {
    schema: server.typeboxSchema({
      // You can also type querystring, headers and response
      params: Type.Object({
        id: Type.String(),
      }),
      body: Type.Object({
        user: Type.Object({
          name: Type.String(),
          age: Type.Number({ minimum: 18 }),
        }),
      }),
    }),
  },
  (request, reply) => {
    // `id` is inferred as string
    const id = request.params.id;

    // `user` is inferred as the object we described!
    const user = request.body.user;
  }
);