bun-ws-router
Version:
Lightweight client/server WebSocket router for Bun with type-safe Zod/Valibot validation.
126 lines • 5.99 kB
TypeScript
import type { GenericSchema, LiteralSchema, NumberSchema, ObjectSchema, OptionalSchema, StringSchema } from "valibot";
/**
* Minimal interface for Valibot instance to avoid circular type references.
* WARNING: Using `typeof v` directly causes TypeScript declaration generation to fail
* with stack overflow errors. This interface captures only the methods we actually use.
*/
interface ValibotLike {
object: (...args: any[]) => any;
strictObject: (...args: any[]) => any;
string: (...args: any[]) => any;
number: (...args: any[]) => any;
literal: (...args: any[]) => any;
union: (...args: any[]) => any;
optional: (...args: any[]) => any;
record: (...args: any[]) => any;
any: (...args: any[]) => any;
picklist: (...args: any[]) => any;
pipe: (...args: any[]) => any;
integer: (...args: any[]) => any;
minValue: (...args: any[]) => any;
safeParse: (...args: any[]) => any;
}
/**
* Shape helper types for better cross-package type inference
* These mirror the Zod implementation patterns exactly
*/
type BaseMessageEntries<T extends string> = {
type: LiteralSchema<T, undefined>;
meta: ObjectSchema<{
timestamp: OptionalSchema<NumberSchema<undefined>, undefined>;
correlationId: OptionalSchema<StringSchema<undefined>, undefined>;
}, undefined>;
};
type MessageWithPayloadEntries<T extends string, P extends Record<string, GenericSchema>> = BaseMessageEntries<T> & {
payload: ObjectSchema<P, undefined>;
};
type MessageWithExtendedMetaEntries<T extends string, M extends Record<string, GenericSchema>> = {
type: LiteralSchema<T, undefined>;
meta: ObjectSchema<{
timestamp: OptionalSchema<NumberSchema<undefined>, undefined>;
correlationId: OptionalSchema<StringSchema<undefined>, undefined>;
} & M, undefined>;
};
type MessageWithPayloadAndMetaEntries<T extends string, P extends Record<string, GenericSchema>, M extends Record<string, GenericSchema>> = {
type: LiteralSchema<T, undefined>;
meta: ObjectSchema<{
timestamp: OptionalSchema<NumberSchema<undefined>, undefined>;
correlationId: OptionalSchema<StringSchema<undefined>, undefined>;
} & M, undefined>;
payload: ObjectSchema<P, undefined>;
};
/**
* Factory function to create messageSchema using the consumer's Valibot instance.
*
* CRITICAL: This factory pattern is required to fix discriminated union support.
* Without it, the library and consumer use different Valibot instances, causing
* instanceof checks to fail and discriminatedUnion to throw runtime errors.
*
* The factory pattern ensures:
* - Both library and app use the same Valibot instance (no dual package hazard)
* - Validation works correctly with proper instanceof checks
* - Type inference flows through without manual type assertions
* - Schemas are composable and can be used in unions
*
* @param valibot - The Valibot instance from the consuming application
* @returns Object with messageSchema function and related utilities
*
* @example Basic usage:
* ```typescript
* import * as v from "valibot";
* import { createMessageSchema } from "bun-ws-router/valibot";
*
* const { messageSchema } = createMessageSchema(v);
* const PingSchema = messageSchema("PING");
* ```
*
* @example Singleton pattern (recommended for apps):
* ```typescript
* // schemas/factory.ts
* export const { messageSchema, createMessage } = createMessageSchema(v);
*
* // schemas/messages.ts
* import { messageSchema } from "./factory";
* const LoginSchema = messageSchema("LOGIN", { username: v.string() });
* ```
*
* @example With discriminated unions:
* ```typescript
* const PingSchema = messageSchema("PING");
* const PongSchema = messageSchema("PONG");
*
* // This now works correctly!
* const MessageUnion = v.union([PingSchema, PongSchema]);
* ```
*/
export declare function createMessageSchema(valibot: ValibotLike): {
messageSchema: {
<T extends string>(messageType: T): ObjectSchema<BaseMessageEntries<T>, undefined>;
<T extends string, P extends ObjectSchema<Record<string, GenericSchema>, undefined>>(messageType: T, payload: P): ObjectSchema<MessageWithPayloadEntries<T, P["entries"]>, undefined>;
<T extends string, P extends Record<string, GenericSchema>>(messageType: T, payload: P): ObjectSchema<MessageWithPayloadEntries<T, P>, undefined>;
<T extends string, M extends Record<string, GenericSchema>>(messageType: T, payload: undefined, meta: M): ObjectSchema<MessageWithExtendedMetaEntries<T, M>, undefined>;
<T extends string, P extends ObjectSchema<Record<string, GenericSchema>, undefined>, M extends Record<string, GenericSchema>>(messageType: T, payload: P, meta: M): ObjectSchema<MessageWithPayloadAndMetaEntries<T, P["entries"], M>, undefined>;
<T extends string, P extends Record<string, GenericSchema>, M extends Record<string, GenericSchema>>(messageType: T, payload: P, meta: M): ObjectSchema<MessageWithPayloadAndMetaEntries<T, P, M>, undefined>;
};
MessageMetadataSchema: any;
ErrorCode: any;
ErrorMessage: ObjectSchema<MessageWithPayloadEntries<"ERROR", {
code: any;
message: any;
context: any;
}>, undefined>;
createMessage: (schema: MessageSchemaType, payload?: unknown, meta?: Record<string, unknown>) => any;
};
type MessageSchemaType = ObjectSchema<{
type: LiteralSchema<string, undefined>;
meta: ObjectSchema<any, undefined>;
payload?: ObjectSchema<any, undefined>;
}, undefined>;
export type MessageSchema<T extends string, P extends Record<string, GenericSchema> = never> = [P] extends [never] ? ObjectSchema<BaseMessageEntries<T>, undefined> : ObjectSchema<MessageWithPayloadEntries<T, P>, undefined>;
export type AnyMessageSchema = ObjectSchema<{
type: LiteralSchema<string, undefined>;
meta: ObjectSchema<any, undefined>;
payload?: ObjectSchema<any, undefined>;
}, undefined>;
export {};
//# sourceMappingURL=schema.d.ts.map