Skip to content

Conversation

@rekmarks
Copy link
Member

@rekmarks rekmarks commented Oct 20, 2025

Explanation

References

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed, highlighting breaking changes as necessary
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

Note

Introduce generic, type-safe MiddlewareContext and JsonRpcEngineV2.create() factory with improved type inference; update server, utils/exports, tests, and docs accordingly.

  • Engine V2:
    • Add JsonRpcEngineV2.create() static factory; make constructor private and enforce non-empty middleware.
    • Enhance generics: middleware now typed with Context, ResultConstraint; improved request/notification typing and composition.
    • Support context-typed handle()/asMiddleware(); validate incompatible contexts at type level; new internal invalid-engine typing.
  • MiddlewareContext:
    • Make MiddlewareContext generic with strict key/value typing; add assertGet/set typings, EmptyContext, and MergeContexts util.
  • Utils/Types:
    • Re-export Json, JsonRpcParams; add UnionToIntersection type.
    • Extend JsonRpcEngineError with cross-version isInstance().
  • Server:
    • Construct engine via create() when passed middleware; clarify/adjust param validation and id handling.
  • Compatibility utils:
    • Type propagateToContext to accept typed context; minor param-handling tweaks.
  • Tests:
    • Update to use create() and new helper middlewares; add coverage for context typing, invalid compositions, and new behaviors.
  • Docs:
    • Rewrite/expand README examples for create(), typed middleware/context, composition patterns, and error semantics.

Written by Cursor Bugbot for commit 75f61e7. This will update automatically on new commits. Configure here.

@rekmarks rekmarks requested a review from mcmire October 20, 2025 21:49
@rekmarks rekmarks changed the base branch from main to rekm/json-rpc-engine-rewrite-next October 20, 2025 21:49
@rekmarks rekmarks force-pushed the rekm/temp-context-generic-factory branch from 83ab221 to a45a3c5 Compare October 21, 2025 19:14
@rekmarks rekmarks changed the title feat: Type-safe context inference? feat: Type-safe context inference Oct 22, 2025
Copy link
Contributor

@mcmire mcmire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had one question about representing an invalid engine better, as well as some other comments. None of this is blocking though. I think this works really well.


it('type errors and returns undefined when getting unknown keys', () => {
const context = new MiddlewareContext<{ test: string }>();
// @ts-expect-error - foo is not a valid key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Would it be worth adding tsd or tstyche tests at some point for some of these classes? This would allow us to write assertions at the type level.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're squeaking by with @ts-expect-error for now, although I'm not opposed to the idea if we think it becomes warranted.

@rekmarks rekmarks marked this pull request as ready for review October 24, 2025 06:01
@rekmarks rekmarks requested review from a team as code owners October 24, 2025 06:01
@rekmarks rekmarks merged commit 471ff5e into rekm/json-rpc-engine-rewrite-next Oct 24, 2025
11 of 12 checks passed
@rekmarks rekmarks deleted the rekm/temp-context-generic-factory branch October 24, 2025 06:02
@rekmarks rekmarks changed the title feat: Type-safe context inference feat(json-rpc-engine): Type-safe context inference Oct 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants