UNPKG

@ws-kit/zod

Version:

Zod validator adapter for WS-Kit with runtime schema validation and full TypeScript inference

71 lines 2.73 kB
/** * withZod() plugin: adds Zod validation capability to the router. * * Composes core plugins (withMessaging + withRpc) with Zod-specific validation: * - Validates inbound payloads against Zod schemas * - Optionally validates outbound payloads (send, reply, progress) * - Adds ctx.payload (validated) to event handlers * - Provides .rpc() method for request-response handlers * * Once plugged, the router gains: * - ctx.send() - Fire-and-forget unicast (from withMessaging) * - ctx.reply() / ctx.error() / ctx.progress() - RPC methods (from withRpc) * - ctx.payload - Validated message payload * - router.rpc() - Request-response handler registration * * Validation errors are routed to router.onError() or custom onValidationError hook. */ import type { ConnectionData, ProgressOptions as CoreProgressOptions, ReplyOptions as CoreReplyOptions } from "@ws-kit/core"; export interface WithZodOptions { /** * Validate outgoing payloads (send, reply, publish). * Default: true * Set to false for ultra-hot paths where performance is critical. * Per-schema override: message({..., options: { validateOutgoing: false }}) */ validateOutgoing?: boolean; /** * Hook for validation errors (inbound/outbound). * If provided, called instead of routing to router.onError(). */ onValidationError?: (error: Error & { code: string; details: any; }, context: { type: string; direction: "inbound" | "outbound"; payload: unknown; }) => void | Promise<void>; } export interface ReplyOptions extends CoreReplyOptions { /** * Whether to validate the outgoing payload. * Default: uses plugin validateOutgoing setting */ validate?: boolean; } export interface ProgressOptions extends CoreProgressOptions { /** * Whether to validate the outgoing payload. * Default: uses plugin validateOutgoing setting */ validate?: boolean; } /** * The runtime API surface this plugin adds: * - Capability marker for type narrowing ({ validation: true }) * - rpc() for registering RPC handlers (does NOT overwrite router.on) * * Note: Type-level on()/rpc() overloads are supplied by Router’s ValidationAPI * when the { validation: true } capability is present. We only add the runtime * rpc() implementation; router.on remains untouched to avoid collisions. */ interface WithZodCapability { readonly validation: true; readonly __caps: { validation: true; }; } export declare function withZod<TContext extends ConnectionData = ConnectionData>(options?: WithZodOptions): import("@ws-kit/core").Plugin<TContext, WithZodCapability>; export {}; //# sourceMappingURL=plugin.d.ts.map