bun-ws-router
Version:
A simple and efficient WebSocket router for Bun with Zod/Valibot message validation.
19 lines • 1.41 kB
TypeScript
import type { ServerWebSocket } from "bun";
import * as v from "valibot";
import type { InferOutput } from "valibot";
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 Valibot 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 extends v.ObjectSchema<infer TEntries, any> ? TEntries extends Record<string, any> ? "payload" extends keyof TEntries ? InferOutput<TEntries["payload"]> : unknown : unknown : unknown, meta?: Partial<Schema extends v.ObjectSchema<infer TEntries, any> ? TEntries extends Record<string, any> ? "meta" extends keyof TEntries ? InferOutput<TEntries["meta"]> : unknown : unknown : unknown>): boolean;
//# sourceMappingURL=publish.d.ts.map