UNPKG

bun-ws-router

Version:

A simple and efficient WebSocket router for Bun with Zod/Valibot message validation.

21 lines 1.16 kB
import type { ServerWebSocket } from "bun"; import type { ZodType } from "zod"; import { z } from "zod"; import type { MessageSchemaType } from "./types"; /** * Validates a message against its schema and publishes it to a WebSocket topic. * Complements Bun's native WebSocket PubSub functionality with schema validation. * * @param ws - The ServerWebSocket instance to publish from * @param topic - The topic to publish to (subscribers will receive the message) * @param schema - The Zod schema to validate the message against * @param payload - The payload to include in the message (type inferred from schema) * @param meta - Optional additional metadata to include (type inferred from schema) * @returns True if message was validated and published successfully */ export declare function publish<Schema extends MessageSchemaType>(ws: ServerWebSocket<{ clientId: string; } & Record<string, unknown>>, topic: string, schema: Schema, payload: Schema["shape"] extends { payload: infer P; } ? P extends ZodType ? z.infer<P> : unknown : unknown, meta?: Partial<z.infer<Schema["shape"]["meta"]>>): boolean; //# sourceMappingURL=publish.d.ts.map