fastify-zod-openapi
Version:
Fastify plugin for zod-openapi
162 lines (161 loc) • 6.32 kB
text/typescript
import { FastifyPluginAsync, FastifyPluginCallback, FastifyPluginOptions, FastifySchema, FastifySchemaCompiler, FastifyTypeProvider, RawServerBase, RawServerDefault } from "fastify";
import * as z from "zod/v4";
import { ZodError, ZodType } from "zod/v4";
import { CreateDocumentOptions, ZodObjectInput, ZodOpenApiComponentsObject, ZodOpenApiRequestBodyObject, ZodOpenApiResponsesObject, oas31 } from "zod-openapi";
import { ComponentRegistry } from "zod-openapi/api";
import * as _fastify_error0 from "@fastify/error";
import { FastifySchemaValidationError, FastifySerializerCompiler } from "fastify/types/schema.js";
import * as core from "zod/v4/core";
import { $ZodType } from "zod/v4/core";
import * as _fastify_swagger0 from "@fastify/swagger";
import { FastifyDynamicSwaggerOptions } from "@fastify/swagger";
//#region src/validationError.d.ts
declare class RequestValidationError extends Error implements FastifySchemaValidationError {
keyword: core.$ZodIssueCode;
instancePath: string;
schemaPath: string;
message: string;
params: {
issue: core.$ZodIssue;
error: ZodError;
};
cause: core.$ZodIssue;
constructor(keyword: core.$ZodIssueCode, instancePath: string, schemaPath: string, message: string, params: {
issue: core.$ZodIssue;
error: ZodError;
});
}
declare const ResponseSerializationError_base: _fastify_error0.FastifyErrorConstructor<{
code: "FST_ERR_RESPONSE_SERIALIZATION";
statusCode: 500;
}, [any?, any?, any?]>;
declare class ResponseSerializationError extends ResponseSerializationError_base {
method: string;
url: string;
cause: ZodError;
constructor(method: string, url: string, options: {
cause: ZodError;
});
}
//#endregion
//#region src/plugin.d.ts
declare const FASTIFY_ZOD_OPENAPI_CONFIG: unique symbol;
interface FastifyZodOpenApiOpts {
components?: ZodOpenApiComponentsObject;
documentOpts?: CreateDocumentOptions;
}
interface FastifyZodOpenApiConfig {
registry: ComponentRegistry;
documentOpts?: CreateDocumentOptions;
fastifyComponents: {
responses: Map<string, {
referenceObject: oas31.ReferenceObject;
path: string[];
}>;
requestBodies: Map<string, {
referenceObject: oas31.ReferenceObject;
path: string[];
}>;
};
}
declare module 'fastify' {
interface FastifySchema {
[FASTIFY_ZOD_OPENAPI_CONFIG]?: FastifyZodOpenApiConfig;
}
interface FastifyValidationResult {
errors?: RequestValidationError[];
}
}
declare module 'openapi-types' {
namespace OpenAPIV3 {
interface Document {
[FASTIFY_ZOD_OPENAPI_CONFIG]?: FastifyZodOpenApiConfig;
}
}
}
interface FastifyZodOpenApiTypeProvider extends FastifyTypeProvider {
validator: unknown extends z.infer<this['schema']> ? this['schema'] extends {
content: infer TContent;
} ? TContent extends Record<string, {
schema: infer TSchema;
}> ? z.infer<TSchema> : unknown : unknown : z.infer<this['schema']>;
serializer: z.input<this['schema']>;
}
type FastifyZodOpenApi = FastifyPluginAsync<FastifyZodOpenApiOpts>;
declare const fastifyZodOpenApiPlugin: FastifyZodOpenApi;
/**
* FastifyPluginCallbackZodOpenApi with Zod automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginCallbackZodOpenApi } from "fastify-zod-openapi"
*
* const plugin: FastifyPluginCallbackZodOpenApi = (fastify, options, done) => {
* done()
* }
* ```
*/
type FastifyPluginCallbackZodOpenApi<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = FastifyPluginCallback<Options, Server, FastifyZodOpenApiTypeProvider>;
/**
* FastifyPluginAsyncZodOpenApi with Zod automatic type inference
*
* @example
* ```typescript
* import { FastifyPluginAsyncZodOpenApi } from "fastify-zod-openapi"
*
* const plugin: FastifyPluginAsyncZodOpenApi = async (fastify, options) => {
* }
* ```
*/
type FastifyPluginAsyncZodOpenApi<Options extends FastifyPluginOptions = Record<never, never>, Server extends RawServerBase = RawServerDefault> = FastifyPluginAsync<Options, Server, FastifyZodOpenApiTypeProvider>;
//#endregion
//#region src/serializerCompiler.d.ts
interface SerializerOptions {
components?: Record<string, $ZodType>;
stringify?: (value: unknown) => string;
fallbackSerializer?: FastifySerializerCompiler<$ZodType>;
}
declare const createSerializerCompiler: (opts?: SerializerOptions) => FastifySerializerCompiler<$ZodType>;
/**
* Enables zod-openapi schema response validation
*
* @example
* ```typescript
* import Fastify from 'fastify'
*
* const server = Fastify().setSerializerCompiler(serializerCompiler)
* ```
*/
declare const serializerCompiler: FastifySerializerCompiler<$ZodType<unknown, unknown, core.$ZodTypeInternals<unknown, unknown>>>;
//#endregion
//#region src/transformer.d.ts
type Transform = NonNullable<FastifyDynamicSwaggerOptions['transform']>;
type TransformObject = NonNullable<FastifyDynamicSwaggerOptions['transformObject']>;
type FastifyZodOpenApiSchema = Omit<FastifySchema, 'response' | 'headers' | 'querystring' | 'body' | 'params'> & {
response?: ZodOpenApiResponsesObject;
headers?: ZodObjectInput;
querystring?: ZodObjectInput;
body?: $ZodType | ZodOpenApiRequestBodyObject;
params?: ZodObjectInput;
};
declare const fastifyZodOpenApiTransform: Transform;
declare const fastifyZodOpenApiTransformObject: TransformObject;
declare const fastifyZodOpenApiTransformers: {
transform: _fastify_swagger0.SwaggerTransform<FastifySchema>;
transformObject: _fastify_swagger0.SwaggerTransformObject;
};
//#endregion
//#region src/validatorCompiler.d.ts
/**
* Enables zod-openapi schema validation
*
* @example
* ```typescript
* import Fastify from 'fastify'
*
* const server = Fastify().setValidatorCompiler(validatorCompiler)
* ```
*/
declare const validatorCompiler: FastifySchemaCompiler<ZodType>;
//#endregion
export { FASTIFY_ZOD_OPENAPI_CONFIG, FastifyPluginAsyncZodOpenApi, FastifyPluginCallbackZodOpenApi, FastifyZodOpenApi, FastifyZodOpenApiOpts, FastifyZodOpenApiSchema, FastifyZodOpenApiTypeProvider, RequestValidationError, ResponseSerializationError, SerializerOptions, createSerializerCompiler, fastifyZodOpenApiPlugin, fastifyZodOpenApiTransform, fastifyZodOpenApiTransformObject, fastifyZodOpenApiTransformers, serializerCompiler, validatorCompiler };