Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@foodsy-app/fastify-typebox

Package Overview
Dependencies
Maintainers
4
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@foodsy-app/fastify-typebox

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

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
increased by900%
Maintainers
4
Weekly downloads
 
Created
Source

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

This plugin uses 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. If you notice any problems, please post a issue.

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 | undefined
    const id = request.params?.id;

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

Keywords

FAQs

Package last updated on 22 Jul 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc