import type { ZodObject, ZodRawShape, ZodType, z as zType } from "zod"; /** * Minimal interface for Zod instance to avoid circular type references. * WARNING: Using `typeof z` directly causes TypeScript declaration generation to fail * with stack overflow errors. This interface captures only the methods we actually use. */ interface ZodLike { object: (...args: any[]) => any; string: (...args: any[]) => any; number: (...args: any[]) => any; literal: (...args: any[]) => any; union: (...args: any[]) => any; discriminatedUnion: (...args: any[]) => any; optional: (...args: any[]) => any; record: (...args: any[]) => any; any: (...args: any[]) => any; enum: (...args: any[]) => any; instanceof: (...args: any[]) => any; ZodType?: any; } /** * Type helper utilities for better cross-package type inference */ type BaseMessageShape = { type: zType.ZodLiteral; meta: ZodObject<{ timestamp: zType.ZodOptional; correlationId: zType.ZodOptional; }>; }; type MessageWithPayloadShape = BaseMessageShape & { payload: P; }; type MessageWithExtendedMetaShape = { type: zType.ZodLiteral; meta: ZodObject<{ timestamp: zType.ZodOptional; correlationId: zType.ZodOptional; } & M>; }; type MessageWithPayloadAndMetaShape = { type: zType.ZodLiteral; meta: ZodObject<{ timestamp: zType.ZodOptional; correlationId: zType.ZodOptional; } & M>; payload: P; }; /** * Factory function to create messageSchema using the consumer's Zod instance. * * CRITICAL: This factory pattern is required to fix discriminated union support. * Without it, the library and consumer use different Zod instances, causing * instanceof checks to fail and discriminatedUnion to throw runtime errors. * * The factory pattern ensures: * - Both library and app use the same Zod instance (no dual package hazard) * - Discriminated unions work correctly with proper instanceof checks * - Type inference flows through without manual type assertions * - Schemas are composable and can be used in unions * * @param zod - The Zod instance from the consuming application * @returns Object with messageSchema function and related utilities * * @example Basic usage: * ```typescript * import { z } from "zod"; * import { createMessageSchema } from "bun-ws-router/zod"; * * const { messageSchema } = createMessageSchema(z); * const PingSchema = messageSchema("PING"); * ``` * * @example Singleton pattern (recommended for apps): * ```typescript * // schemas/factory.ts * export const { messageSchema, createMessage } = createMessageSchema(z); * * // schemas/messages.ts * import { messageSchema } from "./factory"; * const LoginSchema = messageSchema("LOGIN", { username: z.string() }); * ``` * * @example With discriminated unions: * ```typescript * const PingSchema = messageSchema("PING"); * const PongSchema = messageSchema("PONG"); * * // This now works correctly! * const MessageUnion = z.discriminatedUnion("type", [PingSchema, PongSchema]); * ``` */ export declare function createMessageSchema(zod: ZodLike): { messageSchema: { (messageType: T): ZodObject>; >(messageType: T, payload: P): ZodObject>; (messageType: T, payload: P): ZodObject>>; (messageType: T, payload: undefined, meta: M): ZodObject>; , M extends ZodRawShape>(messageType: T, payload: P, meta: M): ZodObject>; (messageType: T, payload: P, meta: M): ZodObject, M>>; }; MessageMetadataSchema: any; ErrorCode: any; ErrorMessage: ZodObject>, zType.core.$strip>; createMessage: (schema: T, payload: T["shape"]["payload"] extends ZodType ? zType.infer : undefined, meta?: Partial>) => zType.ZodSafeParseResult>; }; type MessageSchemaType = ZodObject<{ type: zType.ZodLiteral; meta: ZodType; payload?: ZodType; }>; export type MessageSchema = [ P ] extends [never] ? ZodObject> : ZodObject>; export type AnyMessageSchema = ZodObject<{ type: zType.ZodLiteral; meta: ZodType; payload?: ZodType; }>; export {}; //# sourceMappingURL=schema.d.ts.map