fastify-zod-openapi
Version:
Fastify plugin for zod-openapi
148 lines (141 loc) • 6.74 kB
text/typescript
import { FastifyTypeProvider, FastifyPluginAsync, FastifyPluginOptions, RawServerBase, RawServerDefault, FastifyPluginCallback, FastifySchema, FastifySchemaCompiler } from 'fastify';
import * as zod from 'zod';
import { ZodIssueCode, ZodIssue, ZodError, ZodType, z, ZodTypeAny, ZodObject } from 'zod';
import { ZodOpenApiComponentsObject, CreateDocumentOptions, ZodOpenApiResponsesObject, ZodObjectInputType, ZodOpenApiParameters, oas31 } from 'zod-openapi';
import { ComponentsObject } from 'zod-openapi/api';
import * as _fastify_error from '@fastify/error';
import { FastifySchemaValidationError, FastifySerializerCompiler } from 'fastify/types/schema';
import { FastifyDynamicSwaggerOptions } from '@fastify/swagger';
declare class RequestValidationError extends Error implements FastifySchemaValidationError {
keyword: ZodIssueCode;
instancePath: string;
schemaPath: string;
message: string;
params: {
issue: ZodIssue;
error: ZodError;
};
cause: ZodIssue;
constructor(keyword: ZodIssueCode, instancePath: string, schemaPath: string, message: string, params: {
issue: ZodIssue;
error: ZodError;
});
}
declare const ResponseSerializationError_base: _fastify_error.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;
});
}
declare const FASTIFY_ZOD_OPENAPI_CONFIG: unique symbol;
declare const FASTIFY_ZOD_OPENAPI_COMPONENTS: unique symbol;
interface FastifyZodOpenApiOpts {
components?: ZodOpenApiComponentsObject;
documentOpts?: CreateDocumentOptions;
}
interface FastifyZodOpenApiConfig {
components: ComponentsObject;
documentOpts?: CreateDocumentOptions;
}
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_COMPONENTS]?: ComponentsObject;
}
}
}
interface FastifyZodOpenApiTypeProvider extends FastifyTypeProvider {
validator: this['schema'] extends ZodType ? z.infer<this['schema']> : unknown;
serializer: this['schema'] extends ZodType ? z.input<this['schema']> : unknown;
}
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>;
interface SerializerOptions {
components?: Record<string, ZodTypeAny>;
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<any, zod.ZodTypeDef, any>>;
type Transform = NonNullable<FastifyDynamicSwaggerOptions['transform']>;
type TransformObject = NonNullable<FastifyDynamicSwaggerOptions['transformObject']>;
type FastifyResponseSchema = ZodType | Record<string, unknown>;
type FastifySwaggerSchemaObject = Omit<oas31.SchemaObject, 'required'> & {
required?: string[] | boolean;
};
type FastifyZodOpenApiSchema = Omit<FastifySchema, 'response' | 'headers' | 'querystring' | 'body' | 'params'> & {
response?: ZodOpenApiResponsesObject;
headers?: ZodObjectInputType;
querystring?: ZodObjectInputType;
body?: ZodObjectInputType;
params?: ZodObjectInputType;
};
declare const isZodType: (object: unknown) => object is ZodType;
declare const isZodObject: (object: unknown) => object is ZodObject<any, any, any, any, any>;
declare const createParams: (querystring: ZodObject<any, any, any, any, any>, type: keyof ZodOpenApiParameters, components: ComponentsObject, path: string[], doucmentOpts?: CreateDocumentOptions) => Record<string, FastifySwaggerSchemaObject | oas31.ReferenceObject>;
declare const createResponseSchema: (schema: FastifyResponseSchema, components: ComponentsObject, path: string[], documentOpts?: CreateDocumentOptions) => unknown;
declare const createContent: (content: unknown, components: ComponentsObject, path: string[], documentOpts?: CreateDocumentOptions) => unknown;
declare const createResponse: (response: unknown, components: ComponentsObject, path: string[], documentOpts?: CreateDocumentOptions) => unknown;
declare const fastifyZodOpenApiTransform: Transform;
declare const fastifyZodOpenApiTransformObject: TransformObject;
/**
* Enables zod-openapi schema validation
*
* @example
* ```typescript
* import Fastify from 'fastify'
*
* const server = Fastify().setValidatorCompiler(validatorCompiler)
* ```
*/
declare const validatorCompiler: FastifySchemaCompiler<ZodType>;
export { FASTIFY_ZOD_OPENAPI_COMPONENTS, FASTIFY_ZOD_OPENAPI_CONFIG, type FastifyPluginAsyncZodOpenApi, type FastifyPluginCallbackZodOpenApi, type FastifyZodOpenApi, type FastifyZodOpenApiOpts, type FastifyZodOpenApiSchema, type FastifyZodOpenApiTypeProvider, RequestValidationError, ResponseSerializationError, type SerializerOptions, createContent, createParams, createResponse, createResponseSchema, createSerializerCompiler, fastifyZodOpenApiPlugin, fastifyZodOpenApiTransform, fastifyZodOpenApiTransformObject, isZodObject, isZodType, serializerCompiler, validatorCompiler };