import { z } from "zod" ; import zodToJsonSchema from "zod-to-json-schema" ; import type { SwaggerOptions } from "fastify-swagger" ; export type JsonSchema < $id extends string > = { $id : $id ; } ; type Target = `openApi3` | `jsonSchema7` ; export type Options = { readonly target ? : Target ; } ; export type JsonSchemas < $id extends string > = { schemas : JsonSchema < $id > [ ] ; $ref : < $$id extends $id > ( $id : $$id ) => { $ref : `${ $$id }#` ; } ; } ; export const buildJsonSchema = < $id extends string > ( ZodSchema , $id , { target = `jsonSchema7` } = { } ) : JsonSchema < $id > => ( { $id , ... zodToJsonSchema ( ZodSchema , { target } ) , } ) ; export const buildJsonSchemas = < $id extends string > ( zodSchemas , { target = `jsonSchema7` } = { } ) : JsonSchemas < $id > => ( { schemas : Object . entries ( zodSchemas ) . reduce < JsonSchemas < $id > [ `schemas` ] > ( ( schemas , [ $id , ZodSchema ] ) => [ buildJsonSchema ( ZodSchema as z . ZodType < unknown > , $id as $id , { target } ) , ... schemas , ] , [ ] ) , $ref : ( $id ) => ( { $ref : `${ $id }#` , } ) , } ) ; export const withRefResolver = ( options ) : SwaggerOptions => ( { ... options , refResolver : { buildLocalReference : ( json ) => json . $id , } , } as unknown as SwaggerOptions ) ;