Skip to content

Comments

Use @hardlydifficult/rest-client and websocket packages#301

Merged
HardlyDifficult merged 1 commit intomainfrom
feat/use-hardlydifficult-rest-client
Feb 20, 2026
Merged

Use @hardlydifficult/rest-client and websocket packages#301
HardlyDifficult merged 1 commit intomainfrom
feat/use-hardlydifficult-rest-client

Conversation

@HardlyDifficult
Copy link
Collaborator

@HardlyDifficult HardlyDifficult commented Feb 20, 2026

Summary

Delegate general-purpose infrastructure to published @hardlydifficult/* npm packages, reducing duplication and establishing a shared foundation:

  • Error hierarchy: CantonError now extends RestClientError from @hardlydifficult/rest-client. All instanceof CantonError checks still work, and errors are also instanceof RestClientError.
  • AuthenticationManager: Replaced ~215 lines of OAuth2 implementation with a thin adapter (~100 lines) that delegates to @hardlydifficult/rest-client's AuthenticationManager. Converts Canton's auth config format to rest-client's discriminated union format.
  • Token refresh: calculateTokenRefreshTime imported from @hardlydifficult/websocket instead of duplicated locally. Re-exported for backward compatibility.

No breaking changes -- public API, method signatures, and all downstream imports are unchanged.

Test plan

  • npx tsc --noEmit -- clean compile, zero errors
  • 347 unit tests passing
  • npm run fix -- lint + format clean
  • npm run build:core -- full build succeeds

Made with Cursor

Summary by CodeRabbit

  • Refactor

    • Restructured authentication system to delegate external request handling to specialized libraries
    • Updated core error handling classes to align with external library interfaces
    • Removed redundant local calculation logic in favor of external library implementations
  • Chores

    • Added two new runtime dependencies: @hardlydifficult/rest-client (v1.0.0) and @hardlydifficult/websocket (v1.0.2)
  • Style

    • Enhanced documentation table formatting and alignment

Note

Medium Risk
Authentication and base error hierarchy behavior now depends on external package implementations, which could subtly change token caching/refresh and error propagation despite unchanged APIs.

Overview
Switches core auth, error, and WebSocket token-refresh infrastructure to delegate to published @hardlydifficult/* packages, reducing in-repo implementations while keeping the SDK’s public surface the same.

AuthenticationManager is rewritten as a thin adapter over @hardlydifficult/rest-client (including config conversion and logger adaptation), CantonError now extends RestClientError for cross-library compatibility, and calculateTokenRefreshTime is imported/re-exported from @hardlydifficult/websocket. Also adds the two new runtime dependencies and fixes minor formatting in CLAUDE.md.

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

Delegate general-purpose infrastructure to published npm packages:

- CantonError now extends RestClientError from @hardlydifficult/rest-client
- AuthenticationManager delegates to rest-client's AuthenticationManager
  via a config adapter (Canton auth format -> rest-client auth format)
- calculateTokenRefreshTime imported from @hardlydifficult/websocket
  instead of duplicated locally

Public API unchanged -- all existing instanceof checks, method signatures,
and downstream imports continue to work.

Co-authored-by: Cursor <cursoragent@cursor.com>
@claude
Copy link

claude bot commented Feb 20, 2026

Claude finished @HardlyDifficult's task —— View job


PR Review: Use @hardlydifficult/rest-client and websocket packages

This is a well-architected refactor that delegates infrastructure concerns to published packages while maintaining backward compatibility. Here's my detailed review:

Excellent Code Quality

Dependencies & Architecture:

  • Clean addition of @hardlydifficult/rest-client@1.0.0 and @hardlydifficult/websocket@1.0.2 to package.json:94655989095fdc8f6c9b1f484f27c0caa6c988bb
  • Smart delegation pattern reduces maintenance burden while maintaining control over public APIs

Error Hierarchy (src/core/errors.ts:8143b891fcc9d638f89a399d0f8a89ee5044e4a1):

  • Excellent inheritance approach: CantonError extends RestClientError
  • Maintains backward compatibility - all instanceof CantonError checks continue to work
  • Errors are now also instanceof RestClientError, providing cross-library compatibility
  • Cleaner constructor removes manual prototype chain management

Authentication Refactor (src/core/auth/AuthenticationManager.ts:3dba3b09f51f8aae124bfd9d7bd7dd28d6371983):

  • Reduces ~177 lines to ~103 lines while maintaining identical public interface
  • convertAuthConfig() elegantly transforms Canton's config to rest-client's discriminated union format
  • toRestLogger() adapter preserves existing logging integration
  • Removes complex OAuth2 implementation, axios dependencies, and manual token lifecycle management

WebSocket Changes (src/core/ws/WebSocketClient.ts:0a968e8ffba3a06f0d13be133b091c0a7cd3a81d):

  • calculateTokenRefreshTime import with re-export maintains backward compatibility
  • Reduces duplication by delegating token refresh logic to shared utility
  • Clean implementation with proper documentation

Security & Performance

Security Improvements:

  • Delegates OAuth2 security to a focused, maintained library
  • Reduces attack surface by removing custom auth implementation
  • Maintains existing security patterns (Bearer tokens, proper headers)

Performance Benefits:

  • Smaller bundle size (220 deletions vs 86 additions)
  • Eliminates duplicate utility functions
  • Leverages optimized implementations from dedicated packages

Backward Compatibility Verified

Public API Preservation:

  • All method signatures unchanged (authenticate(), getBearerToken(), clearToken(), etc.)
  • Error hierarchy maintains instanceof compatibility
  • calculateTokenRefreshTime re-exported for existing imports
  • AuthResponse interface unchanged

No Breaking Changes:

  • Existing client code will work without modification
  • Error handling patterns remain consistent
  • Token refresh scheduling behavior preserved

Testing & Build Status

According to PR description:

  • ✅ 347 unit tests passing
  • ✅ Clean TypeScript compilation (npx tsc --noEmit)
  • ✅ Lint/format clean (npm run fix)
  • ✅ Full build succeeds (npm run build:core)

💡 Minor Observations

  1. CLAUDE.md Change: The table formatting change (line spacing) is cosmetic and maintains consistency
  2. Dependency Trust: The @hardlydifficult/* packages are version 1.0.0+ indicating stable APIs
  3. Error Context: The context property is now inherited from RestClientError, maintaining compatibility with the existing ErrorContext type

🎯 Recommendation: APPROVE

This is an exemplary refactor that:

  • ✅ Reduces code complexity and maintenance burden
  • ✅ Maintains perfect backward compatibility
  • ✅ Improves security through battle-tested OAuth2 implementation
  • ✅ Establishes shared foundation with other projects
  • ✅ Follows clean architecture patterns with proper abstraction layers

No concerns identified. The implementation demonstrates excellent software engineering practices with careful attention to compatibility and maintainability.

@coderabbitai
Copy link

coderabbitai bot commented Feb 20, 2026

📝 Walkthrough

Walkthrough

This change introduces two new external dependencies and refactors core authentication and error handling to delegate to external libraries. The AuthenticationManager now proxies authentication to @hardlydifficult/rest-client, the CantonError class extends RestClientError, and WebSocket token refresh logic is imported from @hardlydifficult/websocket.

Changes

Cohort / File(s) Summary
Documentation & Dependency Updates
CLAUDE.md, package.json
Formatting alignment in documentation table; added two new runtime dependencies (@hardlydifficult/rest-client v1.0.0 and @hardlydifficult/websocket v1.0.2).
Rest-Client Integration
src/core/auth/AuthenticationManager.ts, src/core/errors.ts
AuthenticationManager refactored to delegate authentication logic to external RestClientAuthManager; added conversion layer for auth config and logger adapter. CantonError now extends RestClientError instead of Error, with updated constructor signature to pass code and context to parent class.
WebSocket Library Integration
src/core/ws/WebSocketClient.ts
Removed local implementation of calculateTokenRefreshTime and replaced with re-export from @hardlydifficult/websocket library.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: adopting two external npm packages (@hardlydifficult/rest-client and websocket) as core infrastructure, which aligns directly with the substantial refactoring across AuthenticationManager, error handling, and token refresh logic.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/use-hardlydifficult-rest-client

Comment @coderabbitai help to get the list of available commands and usage tips.

@HardlyDifficult HardlyDifficult merged commit 286e18a into main Feb 20, 2026
7 checks passed
@HardlyDifficult HardlyDifficult deleted the feat/use-hardlydifficult-rest-client branch February 20, 2026 17:40
Copy link
Contributor

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is ON, but it could not run because the branch was deleted or merged before Autofix could start.

// Check if we have a valid token
if (this.isTokenValid() && this.bearerToken) {
return this.bearerToken;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Auth errors no longer match Canton error hierarchy

High Severity

authenticate() delegates directly to this.delegate.authenticate() without wrapping errors. The rest-client's AuthenticationManager throws RestClientError (not AuthenticationError or ApiError). Since CantonError extends RestClientError (not the reverse), these errors fail instanceof CantonError, instanceof AuthenticationError, and instanceof ApiError checks. The documented error-handling pattern in docs/features.md (error instanceof AuthenticationError) will silently miss authentication failures, contradicting the PR's "no breaking changes" claim.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant