UNPKG

fastify-type-provider-zod

Version:

Zod Type Provider for Fastify@5

68 lines (67 loc) 2.77 kB
import type { SwaggerTransform, SwaggerTransformObject } from "@fastify/swagger"; import type { FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifySchema, FastifySchemaCompiler, FastifyTypeProvider, RawServerBase, RawServerDefault } from "fastify"; import type { FastifySerializerCompiler } from "fastify/types/schema"; import { z } from "zod/v4"; export interface ZodTypeProvider extends FastifyTypeProvider { validator: this["schema"] extends z.ZodTypeAny ? z.output<this["schema"]> : unknown; serializer: this["schema"] extends z.ZodTypeAny ? z.input<this["schema"]> : unknown; } interface Schema extends FastifySchema { hide?: boolean; } type CreateJsonSchemaTransformOptions = { skipList?: readonly string[]; schemaRegistry?: z.core.$ZodRegistry<{ id?: string | undefined; }>; }; export declare const createJsonSchemaTransform: ({ skipList, schemaRegistry }: CreateJsonSchemaTransformOptions) => SwaggerTransform<Schema>; export declare const jsonSchemaTransform: SwaggerTransform<Schema>; type CreateJsonSchemaTransformObjectOptions = { schemaRegistry?: z.core.$ZodRegistry<{ id?: string | undefined; }>; }; export declare const createJsonSchemaTransformObject: ({ schemaRegistry }: CreateJsonSchemaTransformObjectOptions) => SwaggerTransformObject; export declare const jsonSchemaTransformObject: SwaggerTransformObject; export declare const validatorCompiler: FastifySchemaCompiler<z.ZodTypeAny>; type ReplacerFunction = (this: any, key: string, value: any) => any; export type ZodSerializerCompilerOptions = { replacer?: ReplacerFunction; }; export declare const createSerializerCompiler: (options?: ZodSerializerCompilerOptions) => FastifySerializerCompiler<z.ZodTypeAny | { properties: z.ZodTypeAny; }>; export declare const serializerCompiler: ReturnType<typeof createSerializerCompiler>; /** * FastifyPluginCallbackZod with Zod automatic type inference * * @example * ```typescript * import { FastifyPluginCallbackZod } from "fastify-type-provider-zod" * * const plugin: FastifyPluginCallbackZod = (fastify, options, done) => { * done() * } * ``` */ export type FastifyPluginCallbackZod< Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault > = FastifyPluginCallback<Options, Server, ZodTypeProvider>; /** * FastifyPluginAsyncZod with Zod automatic type inference * * @example * ```typescript * import { FastifyPluginAsyncZod } from "fastify-type-provider-zod" * * const plugin: FastifyPluginAsyncZod = async (fastify, options) => { * } * ``` */ export type FastifyPluginAsyncZod< Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault > = FastifyPluginAsync<Options, Server, ZodTypeProvider>; export {};