bun-ws-router
Version:
Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.
28 lines • 1.36 kB
TypeScript
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.
*
* PURPOSE: Ensures all published messages conform to their schemas, preventing
* runtime errors for subscribers expecting specific message formats.
*
* FLOW: Extract type → Construct message → Validate → Publish (or log error)
*
* @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 metaOrOpts - Optional metadata or options object with origin tracking
* @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, metaOrOpts?: Partial<z.infer<Schema["shape"]["meta"]>> | {
origin?: string;
key?: string;
}): boolean;
//# sourceMappingURL=publish.d.ts.map