2.0.0 • Published 10 months ago

@joshuaavalon/fastify-plugin-typebox v2.0.0

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
10 months ago

@joshuaavalon/fastify-plugin-typebox

It uses @sinclair/typebox to handle validation and serialization.

Getting Started

This is a ESM only module. You must be using ESM in order to use this.

npm install @joshuaavalon/fastify-plugin-typebox
import fastify from "fastify";
import fileRoutesPlugin from "@joshuaavalon/fastify-plugin-typebox";

const app = await fastify();
await app.register(plugin);
app.post(
  "/",
  {
    schema: {
      body: Type.Object({
        a: Type.Transform(Type.String())
          .Decode((v) => Number.parseInt(v))
          .Encode((v) => v.toString())
      }),
      response: {
        200: Type.Object({
          success: Type.Boolean(),
          a: Type.Transform(Type.String())
            .Decode((v) => Number.parseInt(v))
            .Encode((v) => v.toString())
        })
      }
    }
  },
  async function handler(req, res) {
    const { a } = req.body;
    res.send({ success: Number.isInteger(a), a });
  }
);
import type { StaticDecode, TSchema } from "@sinclair/typebox";

declare module "fastify" {
  interface FastifyTypeProviderDefault {
    output: this["input"] extends TSchema
      ? StaticDecode<this["input"]>
      : unknown;
  }
}
1.1.0

11 months ago

2.0.0

10 months ago

1.0.0

1 year ago