3.6.88 • Published 10 months ago

@taktikorg/alias-suscipit v3.6.88

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Fastify Type Provider Zod

NPM Version NPM Downloads Build Status

How to use?

import Fastify from "fastify";
import { serializerCompiler, validatorCompiler, ZodTypeProvider } from "fastify-type-provider-zod";
import z from "zod";

const app = Fastify()

// Add schema validator and serializer
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);

app.withTypeProvider<ZodTypeProvider>().route({
  method: "GET",
  url: "/",
  // Define your schema
  schema: {
    querystring: z.object({
      name: z.string().min(4),
    }),
    response: {
      200: z.string(),
    },
  },
  handler: (req, res) => {
    res.send(req.query.name);
  },
});

app.listen({ port: 4949 });

How to use together with @fastify/swagger

import fastify from 'fastify';
import fastifySwagger from '@fastify/swagger';
import fastifySwaggerUI from '@fastify/swagger-ui';
import { z } from 'zod';

import {
  jsonSchemaTransform,
  createJsonSchemaTransform,
  serializerCompiler,
  validatorCompiler,
  ZodTypeProvider,
} from 'fastify-type-provider-zod';

const app = fastify();
app.setValidatorCompiler(validatorCompiler);
app.setSerializerCompiler(serializerCompiler);

app.register(fastifySwagger, {
  openapi: {
    info: {
      title: 'SampleApi',
      description: 'Sample backend service',
      version: '1.0.0',
    },
    servers: [],
  },
  transform: jsonSchemaTransform,
  // You can also create transform with custom skiplist of endpoints that should not be included in the specification:
  //
  // transform: createJsonSchemaTransform({
  //   skipList: [ '/documentation/static/*' ]
  // })
});

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

const LOGIN_SCHEMA = z.object({
  username: z.string().max(32).describe('Some description for username'),
  password: z.string().max(32),
});

app.after(() => {
  app.withTypeProvider<ZodTypeProvider>().route({
    method: 'POST',
    url: '/login',
    schema: { body: LOGIN_SCHEMA },
    handler: (req, res) => {
      res.send('ok');
    },
  });
});

async function run() {
  await app.ready();

  await app.listen({
    port: 4949,
  });

  console.log(`Documentation running at http://localhost:4949/documentation`);
}

run();

How to create a plugin?

import { z } from 'zod';
import { FastifyPluginAsyncZod } from 'fastify-type-provider-zod';

const plugin: FastifyPluginAsyncZod = async function (fastify, _opts) {
  fastify.route({
    method: 'GET',
    url: '/',
    // Define your schema
    schema: {
      querystring: z.object({
        name: z.string().min(4),
      }),
      response: {
        200: z.string(),
      },
    },
    handler: (req, res) => {
      res.send(req.query.name);
    },
  });
};
ReactiveXcoercibleframerString.prototype.trimreact-componentES5linkbyteLengthUint8Arraysequenceuser-streamsObject.fromEntriesvpctypedarraycss nestingpredictabletimechromiumdeep-cloneoptimistbinariesreact-hook-formerror-handlingSetECMAScript 2019stylingAsyncIteratorlockfilecloudwatchredactroutingJSON-SchemahardlinksschemeObject.keystesting256arktypereusepnpm9ArrayBuffer#slicecryptutilityprettydayjsprocessdeterministiceffect-tsES2020forEachredirectES7readtddpositivecolumnspawnflagspreprocessoraccessorless compileres7ttymkdirpstatelessasciizodgroupBysetexit-codetoolkitespreevariablesenvironmentshttppreserve-symlinksec2browserlistPushsymlinkproxyes-abstractdataviewdependency managerspinneres8symbolscomparespecoptimizersyntaxincludesdataemitprotobufdescriptorstesterreact animationArray.prototype.includesTypeBoxmetadatahelpersmixinsirqpoint-freeiambddoperating-systembuffersshimtypedauthsetPrototypeOfiefixed-widtherrortelephonegesturesemrboundbootstrap lessshebangstyleguideexpressaccessibilityeslintpluginsorthascurliteratorObject.getPrototypeOfparserassertionFloat32Arrayarraybundlerairbnbnested cssasteriskshookformserviceSymbol.toStringTagpackagesinternalbyteOffsetfile systemjavascriptworkflowtoArrayqueueMicrotaskcommandercss variablees-shim APIcall-boundregexpeverycolorsidentifiersES2017pushvalidateposesettingsECMAScript 2018styleestreeexecescapeECMAScript 2020constECMAScript 2017clonearraysspringharmonylrureadableminimalrouteprototypeuninstallfilterStreamebstoolsjson-schema-validationfromquerystringPromisejshintStreamsvestsymbolcompilerjsonschemaimportfunction.lengthcjkeventEmitterinputnativeyamldeletepackage.jsonfunctionargvgroupsymlinksES2019make dirECMAScript 3form-validationArrayobjreact-hooksclass-validatordirdeepObservablenodeconsoleURLSearchParamsfantasy-landisWebSocketsWeakSetpatchfindLastIndexbootstrap csseast-asian-widthsignalsbannerkinesisrapidECMAScript 2023back-endwalkequalmergeglobal objectECMAScript 2021resolveserializeruntimephonees2015globalThissuperstructignorecolourendpointawsdirectorymkdirsless cssTypeScript[[Prototype]]@@toStringTaglazynamesUint32ArraymockingsuperagentdependenciescolumnsRxJSoutputloadbalancingUint8ClampedArrayinspectprivate datarobuststableHyBilistenerscollectionworkermacosObject.valuesstringcryptonpmignoreencryptionelectron3dassertInt8ArrayoptionlanguageagentkeyECMAScript 2016safesyntaxerrorstylesheetprunewhichbindpolyfillpopmotionsideInt32ArraymatchAllajvlinuxsharedarraybufferformatinvariantFloat64ArrayArray.prototype.findLastcharactertrimdateserverString.prototype.matchAllcontainscodestrimEndTypedArraytoStringTagmatchwhatwgeventDispatchermulti-packageObject.definePropertytypesformbundlingReactiveExtensionsperformanttext_.extendobjectdropprotobyteequalityjoiUnderscorecloudformationgenericsrgbsnstypeofenvironmentInt16Arraylastgetter
3.6.88

10 months ago

3.6.87

10 months ago

3.6.86

10 months ago

3.6.85

10 months ago

3.6.84

10 months ago

2.6.84

10 months ago

2.6.83

10 months ago

2.6.82

11 months ago

2.6.81

11 months ago

2.5.81

11 months ago

2.5.80

11 months ago

2.5.79

11 months ago

2.5.78

11 months ago

2.5.77

11 months ago

2.5.76

11 months ago

2.5.75

11 months ago

2.5.74

11 months ago

2.5.73

11 months ago

2.5.72

11 months ago

2.5.71

11 months ago

2.5.70

11 months ago

2.5.69

11 months ago

2.5.68

11 months ago

2.5.67

11 months ago

2.5.66

11 months ago

2.5.65

11 months ago

2.5.64

11 months ago

2.5.63

11 months ago

2.5.62

11 months ago

2.5.61

11 months ago

2.5.60

11 months ago

2.5.59

11 months ago

2.5.58

11 months ago

2.5.57

11 months ago

2.5.56

11 months ago

1.5.56

11 months ago

1.5.55

11 months ago

1.5.54

12 months ago

1.5.53

12 months ago

1.5.52

12 months ago

1.5.51

12 months ago

1.5.50

12 months ago

1.5.49

12 months ago

1.4.49

12 months ago

1.4.48

12 months ago

1.4.47

12 months ago

1.3.47

12 months ago

1.3.46

12 months ago

1.3.45

12 months ago

1.3.44

12 months ago

1.3.43

12 months ago

1.3.42

12 months ago

1.3.41

12 months ago

1.3.40

12 months ago

1.3.39

12 months ago

1.3.38

12 months ago

1.3.37

12 months ago

1.3.36

1 year ago

1.3.35

1 year ago

1.2.35

1 year ago

1.2.34

1 year ago

1.2.33

1 year ago

1.2.32

1 year ago

1.2.31

1 year ago

1.2.30

1 year ago

1.2.29

1 year ago

1.2.28

1 year ago

1.2.27

1 year ago

1.2.26

1 year ago

1.2.25

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.20

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago