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

fastify-type-provider-yup

v5.0.0

Published

Yup type provider for Fastify v5

Downloads

61

Readme

fastify-type-provider-yup

NPM Version CI

Getting Started

pnpm add fastify-type-provider-yup
yarn add fastify-type-provider-yup
npm i fastify-type-provider-yup

| Package | Version | Fastify Compatibility | | ------- | ------- | --------------------- | | pnpm add fastify-type-provider-yup@4 | 4.x | 4.x | | pnpm add fastify-type-provider-yup@5 | 5.x | 5.x |

How to use

See Examples folder for more details.

Register Fastify plugin:

import { yupPlugin } from 'fastify-type-provider-yup';
import fp from 'fastify-plugin';

export const example = async (fastify) => {
  fastify.register(fp(yupPlugin));
};

Manually registered with Typescript:

import Fastify from "fastify";
import {
  serializerCompiler,
  validatorCompiler,
  type YupTypeProvider
} from 'fastify-type-provider-yup';
import * as yup from yup

const server = Fastify()

server.withTypeProvider<withTypeProvider>().route({
  method: 'POST',
  url: '/',
  schema: {
    body: yup.object({
      foo: yup.string().required()
    }),
    querystring: yup.object({
      page: yup.number().default(1)
    }),
    response: {
      200: yup.string()
    }
  },
  handler: async (reques, reply) => {
    const { foo } = request.body
    const { page } = request.querystring
    return `Received ${foo} for page ${page}`
  }
})

server.listen({port: 1337, host: '0.0.0.0'})

Swagger

import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import Fastify from 'fastify';
import * as yup from 'yup';
import {
  type YupTypeProvider,
  validatorCompiler,
  serializerCompiler,
  jsonSchemaTransformer,
} from 'fastify-type-provider-yup';
import { extendSchema } from '@sodaru/yup-to-json-schema';
import { Schema, addMethod } from 'yup';

extendSchema({ addMethod, Schema });

const app = Fastify({ logger: true });
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);

app.register(fastifySwagger, {
  openapi: {
    info: {
      title: 'SampleApi',
      description: 'Sample backend service',
      version: '1.0.0',
    },
    servers: [],
  },
  transform: jsonSchemaTransformer,
});

app.register(fastifySwaggerUI, {
  routePrefix: '/docs',
});

app.after(() => {
  app.withTypeProvider<YupTypeProvider>().route({
    url: '/',
    method: 'POST',
    schema: {
      description: 'Description details',
      tags: ['home'],
      body: yup.object({
        page: yup.number().default(1),
      }),
      response: {
        200: yup.object({
          page: yup.string().example('1'),
        }),
      },
    },
    handler: async (request, reply) => {
      const { page } = request.body;

      return {
        page: String(page),
      };
    },
  });
});

app.listen({ port: 8080, host: '0.0.0.0' });

Aknowledgements

Module heavilty inspired by:

Benchmarks

Run:

Go to folder /benchmarks and run the benchmark server:

node (both|serializer|validation)-(json|yup|zod).benchmark.cjs

Then run:

autocannon http://localhost:3000

Find below the results using a MacBook Pro (16-inch, Apple M1 Pro, 2021, Sonoma 14.6.1) with 16 GB of RAM.

Serializer Results (Avg.)

| Name | Version | Req/sec | Latency (ms) | Bytes/sec | Requests| | --- | --- | --- | --- | --- | --- | | fastify-type-provider-yup | 0.0.5 | 65,521.46 (84.58 %) | 0.01 | 12.3 MB | 721k req in 11.01s, 135 MB read | | fastify-type-provider-zod | 2.0.0 | 77,119.00 (99.55 %) | 0.01 | 14.5 MB | 805k req in 11.01 s, 151 MB read | | fastify (raw) | 4.23.0 | 77,464.55 | 0.01 | 14.6 MB | 854k req in 11.01 s, 161 MB read |

Validation Results (Avg.)

| Name | Version | Req/sec | Latency (ms) | Bytes/sec | Requests| | --- | --- | --- | --- | --- | --- | | fastify-type-provider-yup | 0.0.5 | 63,902.55 (89.74 %) | 0.01 | 11.6 MB | 703k req in 11.01s, 127 MB read | | fastify-type-provider-zod | 2.0.0 | 70,893.10 (99.56 %) | 0.01 | 12.8 MB | 780k req in 11.01 s, 141 MB read | | fastify (raw) | 4.23.0 | 71,207.28 | 0.01 | 12.9 MB | 783k req in 11.02 s, 142 MB read |

Validation And Serialization Results (Avg.)

| Name | Version | Req/sec | Latency (ms) | Bytes/sec | Requests| | --- | --- | --- | --- | --- | --- | | fastify-type-provider-yup | 0.0.5 | 57,133.10 (73.38 %) | 0.01 | 10.5 MB | 629k req in 11.01s, 115 MB read | | fastify-type-provider-zod | 2.0.0 | 51,389.82 (66.00 %) | 0.01 | 9.4 MB | 565k req in 11.01 s, 103 MB read | | fastify (raw) | 4.23.0 | 77,861.82 | 0.01 | 14.2 MB | 856k req in 11.01 s, 157 MB read |