bun-ws-router
Version:
Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.
125 lines • 5.24 kB
TypeScript
import type { ZodObject, ZodRawShape, ZodType, z as zType } from "zod";
/**
* Minimal interface for Zod instance to avoid circular type references.
* WARNING: Using `typeof z` directly causes TypeScript declaration generation to fail
* with stack overflow errors. This interface captures only the methods we actually use.
*/
interface ZodLike {
object: (...args: any[]) => any;
string: (...args: any[]) => any;
number: (...args: any[]) => any;
literal: (...args: any[]) => any;
union: (...args: any[]) => any;
discriminatedUnion: (...args: any[]) => any;
optional: (...args: any[]) => any;
record: (...args: any[]) => any;
any: (...args: any[]) => any;
enum: (...args: any[]) => any;
instanceof: (...args: any[]) => any;
ZodType?: any;
}
/**
* Type helper utilities for better cross-package type inference
*/
type BaseMessageShape<T extends string> = {
type: zType.ZodLiteral<T>;
meta: ZodObject<{
timestamp: zType.ZodOptional<zType.ZodNumber>;
correlationId: zType.ZodOptional<zType.ZodString>;
}>;
};
type MessageWithPayloadShape<T extends string, P extends ZodType> = BaseMessageShape<T> & {
payload: P;
};
type MessageWithExtendedMetaShape<T extends string, M extends ZodRawShape> = {
type: zType.ZodLiteral<T>;
meta: ZodObject<{
timestamp: zType.ZodOptional<zType.ZodNumber>;
correlationId: zType.ZodOptional<zType.ZodString>;
} & M>;
};
type MessageWithPayloadAndMetaShape<T extends string, P extends ZodType, M extends ZodRawShape> = {
type: zType.ZodLiteral<T>;
meta: ZodObject<{
timestamp: zType.ZodOptional<zType.ZodNumber>;
correlationId: zType.ZodOptional<zType.ZodString>;
} & M>;
payload: P;
};
/**
* Factory function to create messageSchema using the consumer's Zod instance.
*
* CRITICAL: This factory pattern is required to fix discriminated union support.
* Without it, the library and consumer use different Zod instances, causing
* instanceof checks to fail and discriminatedUnion to throw runtime errors.
*
* The factory pattern ensures:
* - Both library and app use the same Zod instance (no dual package hazard)
* - Discriminated unions work correctly with proper instanceof checks
* - Type inference flows through without manual type assertions
* - Schemas are composable and can be used in unions
*
* @param zod - The Zod instance from the consuming application
* @returns Object with messageSchema function and related utilities
*
* @example Basic usage:
* ```typescript
* import { z } from "zod";
* import { createMessageSchema } from "bun-ws-router/zod";
*
* const { messageSchema } = createMessageSchema(z);
* const PingSchema = messageSchema("PING");
* ```
*
* @example Singleton pattern (recommended for apps):
* ```typescript
* // schemas/factory.ts
* export const { messageSchema, createMessage } = createMessageSchema(z);
*
* // schemas/messages.ts
* import { messageSchema } from "./factory";
* const LoginSchema = messageSchema("LOGIN", { username: z.string() });
* ```
*
* @example With discriminated unions:
* ```typescript
* const PingSchema = messageSchema("PING");
* const PongSchema = messageSchema("PONG");
*
* // This now works correctly!
* const MessageUnion = z.discriminatedUnion("type", [PingSchema, PongSchema]);
* ```
*/
export declare function createMessageSchema(zod: ZodLike): {
messageSchema: {
<T extends string>(messageType: T): ZodObject<BaseMessageShape<T>>;
<T extends string, P extends ZodObject<ZodRawShape>>(messageType: T, payload: P): ZodObject<MessageWithPayloadShape<T, P>>;
<T extends string, P extends ZodRawShape>(messageType: T, payload: P): ZodObject<MessageWithPayloadShape<T, ZodObject<P>>>;
<T extends string, M extends ZodRawShape>(messageType: T, payload: undefined, meta: M): ZodObject<MessageWithExtendedMetaShape<T, M>>;
<T extends string, P extends ZodObject<ZodRawShape>, M extends ZodRawShape>(messageType: T, payload: P, meta: M): ZodObject<MessageWithPayloadAndMetaShape<T, P, M>>;
<T extends string, P extends ZodRawShape, M extends ZodRawShape>(messageType: T, payload: P, meta: M): ZodObject<MessageWithPayloadAndMetaShape<T, ZodObject<P>, M>>;
};
MessageMetadataSchema: any;
ErrorCode: any;
ErrorMessage: ZodObject<MessageWithPayloadShape<"ERROR", ZodObject<{
code: any;
message: any;
context: any;
}, zType.core.$strip>>, zType.core.$strip>;
createMessage: <T extends MessageSchemaType>(schema: T, payload: T["shape"]["payload"] extends ZodType ? zType.infer<T["shape"]["payload"]> : undefined, meta?: Partial<zType.infer<T["shape"]["meta"]>>) => zType.ZodSafeParseResult<zType.core.output<T>>;
};
type MessageSchemaType = ZodObject<{
type: zType.ZodLiteral<string>;
meta: ZodType;
payload?: ZodType;
}>;
export type MessageSchema<T extends string, P extends ZodType = never> = [
P
] extends [never] ? ZodObject<BaseMessageShape<T>> : ZodObject<MessageWithPayloadShape<T, P>>;
export type AnyMessageSchema = ZodObject<{
type: zType.ZodLiteral<string>;
meta: ZodType;
payload?: ZodType;
}>;
export {};
//# sourceMappingURL=schema.d.ts.map