3.6.88 • Published 1 year ago

@taktikorg/alias-suscipit v3.6.88

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

3.6.87

1 year ago

3.6.86

1 year ago

3.6.85

1 year ago

3.6.84

1 year ago

2.6.84

1 year ago

2.6.83

1 year ago

2.6.82

1 year ago

2.6.81

1 year ago

2.5.81

1 year ago

2.5.80

1 year ago

2.5.79

1 year ago

2.5.78

1 year ago

2.5.77

1 year ago

2.5.76

1 year ago

2.5.75

1 year ago

2.5.74

1 year ago

2.5.73

1 year ago

2.5.72

1 year ago

2.5.71

1 year ago

2.5.70

1 year ago

2.5.69

1 year ago

2.5.68

1 year ago

2.5.67

1 year ago

2.5.66

1 year ago

2.5.65

1 year ago

2.5.64

1 year ago

2.5.63

1 year ago

2.5.62

1 year ago

2.5.61

1 year ago

2.5.60

1 year ago

2.5.59

1 year ago

2.5.58

1 year ago

2.5.57

1 year ago

2.5.56

1 year ago

1.5.56

1 year ago

1.5.55

1 year ago

1.5.54

1 year ago

1.5.53

1 year ago

1.5.52

1 year ago

1.5.51

1 year ago

1.5.50

1 year ago

1.5.49

1 year ago

1.4.49

1 year ago

1.4.48

1 year ago

1.4.47

1 year ago

1.3.47

1 year ago

1.3.46

1 year ago

1.3.45

1 year ago

1.3.44

1 year ago

1.3.43

1 year ago

1.3.42

1 year ago

1.3.41

1 year ago

1.3.40

1 year ago

1.3.39

1 year ago

1.3.38

1 year ago

1.3.37

1 year 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