Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR introduces a breaking change to fix discriminated union support by implementing a factory pattern that eliminates the dual package hazard issue. Previously,
messageSchemaschemas would fail when used with Zod'sdiscriminatedUniondue to the library using a different Zod instance than the application.Key Changes:
createMessageSchema()factory pattern for both Zod and ValibotBreaking Changes
❌ Removed Exports
All direct exports from the main package and adapter modules have been removed:
✅ New Required Pattern
The factory pattern is now mandatory to avoid dual package hazards:
Migration Guide
Step 1: Update Imports
Replace all imports from
"bun-ws-router"with explicit adapter imports:Step 2: Create Factory Instance
Add factory creation at the top of your schema files:
Step 3: Update Schema Definitions
No changes needed to existing schema definitions or handlers:
Why This Change?
The previous implementation suffered from a "dual package hazard":
instanceofchecks in discriminated unionstypecould not be extracted"The factory pattern ensures both library and application use the same validator instance.
New Capabilities
Discriminated Unions Now Work
Consistent API for Payload and Meta Parameters
The API now uses consistent raw shapes for both payload and meta parameters:
Improved Type Inference
Full TypeScript support with proper type narrowing in union handlers:
Files Changed
Core Changes
v0.4.1for this patch releaseshared/: Updated all shared types and router logic for factory patternzod/: Implemented factory pattern withcreateMessageSchema()and API consistencyvalibot/: Implemented factory pattern with Valibot support and shape helpersindex.ts: Added deprecation warnings for old importsDocumentation
README.md: Updated all examples to use factory pattern and consistent APIdocs/: Comprehensive updates across 6 documentation files:message-schemas.md: Updated 10+ code examples to use raw shapesexamples.md: Fixed 13+ instances across chat, auth, and notification examplesapi-reference.md: Updated core API documentation examplesadvanced-usage.md: Fixed schema versioning and compression examplescore-concepts.md,deployment.md,valibot-integration.md: API consistency fixesexample/: All 6 example files reviewed and verified for accuracyBREAKING_CHANGES.md: Detailed breaking change documentationMIGRATION.md: Step-by-step migration guideTesting
cross-package-types.test.tsfor comprehensive discriminated union testingmessageSchema-types.test.tsfor discriminated union testingcreateMessagefunction with simplified type signatureExamples
Error Handling
Applications using deprecated imports will receive clear error messages:
Testing
Performance Impact
This change fixes a fundamental issue that prevented the library from working with Zod's discriminated unions, a critical feature for complex message routing patterns. While breaking, the migration path is straightforward and the benefits are significant for type safety and developer experience.