UNPKG

bun-ws-router

Version:

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

109 lines 4.47 kB
import type { ZodObject, ZodRawShape, ZodTypeAny } from "zod"; import { z } from "zod"; /** * Base schema for message metadata. * Provides common fields that are available on all messages. * Can be extended for specific message types. */ export declare const MessageMetadataSchema: ZodObject<{ clientId: z.ZodOptional<z.ZodString>; timestamp: z.ZodOptional<z.ZodNumber>; correlationId: z.ZodOptional<z.ZodString>; }, z.core.$strip>; /** * Base message schema that all specific message types extend. * Defines the minimum structure required for routing. */ export declare const MessageSchema: ZodObject<{ type: z.ZodString; meta: ZodObject<{ clientId: z.ZodOptional<z.ZodString>; timestamp: z.ZodOptional<z.ZodNumber>; correlationId: z.ZodOptional<z.ZodString>; }, z.core.$strip>; }, z.core.$strip>; /** * Standard error codes for WebSocket communication. * Used in ErrorMessage payloads for consistent error handling. */ export declare const ErrorCode: z.ZodEnum<{ INVALID_MESSAGE_FORMAT: "INVALID_MESSAGE_FORMAT"; VALIDATION_FAILED: "VALIDATION_FAILED"; UNSUPPORTED_MESSAGE_TYPE: "UNSUPPORTED_MESSAGE_TYPE"; AUTHENTICATION_FAILED: "AUTHENTICATION_FAILED"; AUTHORIZATION_FAILED: "AUTHORIZATION_FAILED"; RESOURCE_NOT_FOUND: "RESOURCE_NOT_FOUND"; RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED"; INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR"; }>; export type ErrorCode = z.infer<typeof ErrorCode>; /** * Creates a type-safe WebSocket message schema with optimized overloads. * * The schema includes: * - A literal type field for routing messages * - Metadata for tracking client info and message context * - Optional payload for the message data * * Types are fully inferred for use with WebSocketRouter handlers. */ export declare function messageSchema<T extends string>(messageType: T): ZodObject<{ type: z.ZodLiteral<T>; meta: typeof MessageMetadataSchema; }>; export declare function messageSchema<T extends string, P extends Record<string, ZodTypeAny> | ZodTypeAny>(messageType: T, payload: P): ZodObject<{ type: z.ZodLiteral<T>; meta: typeof MessageMetadataSchema; payload: P extends Record<string, ZodTypeAny> ? ZodObject<P> : P; }>; export declare function messageSchema<T extends string, M extends ZodRawShape>(messageType: T, payload: undefined, meta: ZodObject<M>): ZodObject<{ type: z.ZodLiteral<T>; meta: ZodObject<typeof MessageMetadataSchema.shape & M>; }>; export declare function messageSchema<T extends string, P extends Record<string, ZodTypeAny> | ZodTypeAny, M extends ZodRawShape>(messageType: T, payload: P, meta: ZodObject<M>): ZodObject<{ type: z.ZodLiteral<T>; meta: ZodObject<typeof MessageMetadataSchema.shape & M>; payload: P extends Record<string, ZodTypeAny> ? ZodObject<P> : P; }>; /** * Standard error message schema for consistent error responses. */ export declare const ErrorMessage: ZodObject<{ type: z.ZodLiteral<"ERROR">; meta: typeof MessageMetadataSchema; payload: ZodObject<{ code: z.ZodEnum<{ INVALID_MESSAGE_FORMAT: "INVALID_MESSAGE_FORMAT"; VALIDATION_FAILED: "VALIDATION_FAILED"; UNSUPPORTED_MESSAGE_TYPE: "UNSUPPORTED_MESSAGE_TYPE"; AUTHENTICATION_FAILED: "AUTHENTICATION_FAILED"; AUTHORIZATION_FAILED: "AUTHORIZATION_FAILED"; RESOURCE_NOT_FOUND: "RESOURCE_NOT_FOUND"; RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED"; INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR"; }>; message: z.ZodOptional<z.ZodString>; context: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>; }, z.core.$strip>; }, z.core.$strip>; /** * Creates a validated WebSocket message from a schema. * * @example * ```typescript * const EchoSchema = messageSchema("ECHO", { text: z.string() }); * const message = createMessage(EchoSchema, { text: "Hello" }); * * if (message.success) { * ws.send(JSON.stringify(message.data)); * } * ``` */ export declare function createMessage<T extends MessageSchemaType>(schema: T, payload: T["shape"]["payload"] extends ZodTypeAny ? z.infer<T["shape"]["payload"]> : undefined, meta?: Partial<z.infer<T["shape"]["meta"]>>): z.ZodSafeParseResult<z.core.output<T>>; type MessageSchemaType = ZodObject<{ type: z.ZodLiteral<string>; meta: ZodTypeAny; payload?: ZodTypeAny; }>; export {}; //# sourceMappingURL=schema.d.ts.map