Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add apiVersion option to Client constructor #2877

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

tequdev
Copy link
Contributor

@tequdev tequdev commented Jan 16, 2025

High Level Overview of Change

Fixes #2872

Context of Change

  • Changed to allow the apiVersion to be specified from Client constructor.
    • If you specify the apiVersion from the constructor, the type definitions for the request and requestAll responses will also be based on that apiVersion (due to Generics).

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • [x ] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Refactor (non-breaking change that only restructures code)
  • Tests (You added tests for code that already exists, or your new feature included in this PR)
  • Documentation Updates
  • Release

Did you update HISTORY.md?

  • [x ] Yes
  • No, this change does not impact library users

Test Plan

Copy link

coderabbitai bot commented Jan 16, 2025

Walkthrough

The pull request introduces enhanced API versioning support for the XRPL.js library's Client class. It adds a generic apiVersion option to the client constructor and related methods, allowing developers to specify and work with different API versions more flexibly. The changes primarily focus on improving type safety and configurability of the client's interaction with the XRPL API, while also updating documentation to reflect these enhancements.

Changes

File Change Summary
packages/xrpl/HISTORY.md Added documentation for new apiVersion option in Client constructor; deprecated setTransactionFlagsToNumber in favor of convertTxFlagsToNumber
packages/xrpl/src/client/index.ts Updated ClientOptions interface to support generic API versioning; modified Client class to accept and utilize API version parameter; updated method signatures for request and requestAll to use dynamic API versioning
packages/xrpl/src/models/methods/accountTx.ts Changed AccountTxResponseBase to an exported interface for broader accessibility
packages/xrpl/src/models/methods/subscribe.ts Updated OnEventToListenerMap to include a generic API version parameter for enhanced flexibility
packages/xrpl/src/models/methods/tx.ts Updated TxResponse interface to support dynamic API versioning
packages/xrpl/src/sugar/autofill.ts Enhanced multiple functions to accept a generic type parameter for API versioning
packages/xrpl/src/sugar/getFeeXrp.ts Updated getFeeXrp function to support a generic API version parameter
packages/xrpl/src/sugar/getOrderbook.ts Modified requestAllOffers to accept a generic API version parameter
packages/xrpl/src/sugar/submit.ts Updated submitRequest, getSignedTx, and waitForFinalTransactionOutcome to include a generic API version parameter

Assessment against linked issues

Objective Addressed Explanation
Allow specifying API version in Client constructor (#[2872])
Prevent API version errors when connecting to certain networks (#[2872]) It is unclear if the changes automatically handle API version switching upon connection errors.

Possibly related PRs

Suggested reviewers

  • mvadari
  • ckeshava
  • khancode

Poem

🐰 Hop, hop, API version dance!
Flexible client, take your stance
Generic types, a rabbit's delight
XRPL code now shines so bright
Version control with coding grace 🚀

Finishing Touches

  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
packages/xrpl/HISTORY.md (1)

7-9: Move the new Client feature to the "Added" section.

The introduction of the apiVersion option in the Client constructor is a new feature rather than a fix. For clarity and proper documentation, consider moving this entry from the "Fixed" section to the "Added" section under "Unreleased Changes".

Apply this diff to reposition the entry:

 ## Unreleased Changes

-### Fixed
+### Added
 * `Client` supports `apiVersion` option on constructor
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 84943ae and f0c88e4.

📒 Files selected for processing (2)
  • packages/xrpl/HISTORY.md (1 hunks)
  • packages/xrpl/src/client/index.ts (9 hunks)
🔇 Additional comments (8)
packages/xrpl/src/client/index.ts (8)

95-97: Appropriate use of generic parameter in ClientOptions.

The introduction of the ClientAPIVersion generic parameter in the ClientOptions interface enhances type safety and flexibility.


116-121: Documentation for apiVersion is clear and informative.

The added JSDoc comment for the apiVersion property provides a clear description and default value, aiding in code readability and maintainability.


195-197: Correct extension of EventEmitter with generic parameter.

The Client class correctly extends EventEmitter while introducing the ClientAPIVersion generic parameter, maintaining consistency with the interface.


251-254: Constructor correctly utilizes the generic parameter.

The constructor correctly accepts ClientOptions<ClientAPIVersion>, ensuring that the apiVersion is consistently applied throughout the Client instance.


265-266: Proper assignment of apiVersion with fallback to default.

The use of the nullish coalescing operator ensures that apiVersion is assigned appropriately, defaulting to DEFAULT_API_VERSION when not provided.


Line range hint 350-354: Generic parameters enhance request method flexibility.

The updated request method signature uses the ClientAPIVersion type parameter effectively, allowing for type-safe requests corresponding to the client's API version.


473-476: requestAll method correctly integrates generic parameter.

The inclusion of ClientAPIVersion in the requestAll method signature ensures consistent API versioning across batched requests.


501-502: Correct type assertion with ClientAPIVersion.

The type assertion of singleResponse as MarkerResponse<ClientAPIVersion> is appropriate and ensures that the result is correctly typed for further processing.

@@ -222,7 +232,7 @@ class Client extends EventEmitter<EventTypes> {
* API Version used by the server this client is connected to
*
*/
public apiVersion: APIVersion = DEFAULT_API_VERSION
public apiVersion: APIVersion
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Type of apiVersion property should match the generic parameter.

Currently, apiVersion is typed as APIVersion. To ensure consistency and leverage the benefits of the generic parameter, consider typing it as ClientAPIVersion.

Apply this diff to update the type:

-public apiVersion: APIVersion
+public apiVersion: ClientAPIVersion

Committable suggestion skipped: line range outside the PR's diff.

Comment on lines +95 to +97
export interface ClientOptions<
ClientAPIVersion extends APIVersion = typeof DEFAULT_API_VERSION,
> extends ConnectionUserOptions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think this typing change is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this, Client class will not be able to recognize the API version set in the constructor, and the response of request() and requestAll() will only be the types of APIv2 (DEFAULT_API_VERSION).

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/xrpl/src/sugar/submit.ts (1)

162-163: Consider improving type assertion safety.

The TODO comment indicates a need to resolve the type assertion. Consider using type guards or restructuring the code to avoid the type assertion.

-    // TODO: resolve the type assertion below
-    // eslint-disable-next-line @typescript-eslint/consistent-type-assertions -- we know that txResponse is of type TxResponse
-    return txResponse as TxResponse<T, V>
+    if ('result' in txResponse && 'tx_json' in txResponse.result) {
+      return txResponse as TxResponse<T, V>
+    }
+    throw new XrplError('Unexpected response format')
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d31a3ad and 34493ab.

📒 Files selected for processing (6)
  • packages/xrpl/src/client/index.ts (15 hunks)
  • packages/xrpl/src/models/methods/tx.ts (1 hunks)
  • packages/xrpl/src/sugar/autofill.ts (7 hunks)
  • packages/xrpl/src/sugar/getFeeXrp.ts (2 hunks)
  • packages/xrpl/src/sugar/getOrderbook.ts (2 hunks)
  • packages/xrpl/src/sugar/submit.ts (6 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: snippets (22.x)
  • GitHub Check: integration (22.x)
  • GitHub Check: snippets (20.x)
  • GitHub Check: integration (20.x)
  • GitHub Check: snippets (18.x)
  • GitHub Check: integration (18.x)
  • GitHub Check: browser (18.x)
🔇 Additional comments (17)
packages/xrpl/src/sugar/getFeeXrp.ts (1)

18-20: LGTM! Type-safe API version support added.

The generic type parameter is correctly implemented with proper constraints and default value.

packages/xrpl/src/models/methods/tx.ts (1)

96-100: LGTM! Enhanced type safety for API versioning.

The TxResponse interface correctly implements generic type parameters for both transaction type and API version, maintaining type safety throughout the response chain.

packages/xrpl/src/sugar/getOrderbook.ts (1)

145-147: LGTM! Type-safe API version support added.

The requestAllOffers function correctly implements the generic type parameter for API versioning.

packages/xrpl/src/sugar/submit.ts (1)

51-54: LGTM! Comprehensive API version support added.

The generic type parameters are consistently implemented across all functions, maintaining type safety throughout the submission process.

Also applies to: 117-124, 232-235

packages/xrpl/src/sugar/autofill.ts (6)

6-6: LGTM!

The import of APIVersion and DEFAULT_API_VERSION is necessary for the generic type parameters being added to the functions.


95-97: LGTM!

The addition of the generic type parameter V extends APIVersion with a default to DEFAULT_API_VERSION maintains type safety while allowing flexibility in API versions.


221-223: LGTM!

The addition of the generic type parameter V extends APIVersion with a default to DEFAULT_API_VERSION maintains type safety while allowing flexibility in API versions.


241-243: LGTM!

The addition of the generic type parameter V extends APIVersion with a default to DEFAULT_API_VERSION maintains type safety while allowing flexibility in API versions.


262-264: LGTM!

The addition of the generic type parameter V extends APIVersion with a default to DEFAULT_API_VERSION maintains type safety while allowing flexibility in API versions.


325-327: LGTM!

The addition of the generic type parameter V extends APIVersion with a default to DEFAULT_API_VERSION maintains type safety while allowing flexibility in API versions for both functions.

Also applies to: 340-342

packages/xrpl/src/client/index.ts (7)

Line range hint 96-122: LGTM!

The addition of the generic type parameter and apiVersion option is well-documented and provides type-safe API version configuration.


Line range hint 134-146: LGTM!

The type changes properly handle API versioning for paginated responses, particularly for account transactions.


199-201: LGTM!

The addition of the generic type parameter to the Client class provides type safety for API versioning at the class level.


Line range hint 255-270: LGTM!

The constructor properly handles the apiVersion option, defaulting to DEFAULT_API_VERSION if not provided.


354-354: LGTM!

The method now correctly uses the client's API version by default for request types.


399-400: LGTM!

The method now correctly uses the client's API version for paginated response types.


859-859: LGTM!

The method now correctly uses the client's API version for transaction response types.

@Ryan0656

This comment was marked as off-topic.

@Ryan0656

This comment has been minimized.

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.

xrpl.js v4.x throws errors on some networks/servers
3 participants