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

PLT-9368: Strict mode for runtime-lifecycle #180

Merged
merged 11 commits into from
Jan 30, 2024
Merged

PLT-9368: Strict mode for runtime-lifecycle #180

merged 11 commits into from
Jan 30, 2024

Conversation

bjornkihlberg
Copy link
Contributor

@bjornkihlberg bjornkihlberg commented Jan 24, 2024

Examples

Examples of improvements to developer experience
image
image

Summary by CodeRabbit

  • New Features

    • Standardized and simplified API calls by introducing new request objects for method invocations.
    • Expanded the import map to include new mappings for modules under the @marlowe.io namespace, facilitating easier module access and management.
  • Refactor

    • Modified function calls across several modules to use object parameters instead of individual parameters, improving code readability and maintainability.
    • Updated and added new guard definitions for input validation across various endpoints, ensuring type safety and correctness.
  • Documentation

    • Enhanced inline documentation and type definitions to reflect changes in function parameters and request object structures.
  • Bug Fixes

    • Addressed potential type mismatches by introducing strict dynamic type checks and custom error handling for invalid types.

Copy link
Contributor

coderabbitai bot commented Jan 24, 2024

Walkthrough

The overarching change focuses on enhancing type safety and standardizing request object structures across various operations in a Node.js context. This involves transitioning from passing individual parameters to using object parameters with named properties for function calls. Additionally, there's an introduction of new type guards and modifications to support dynamic type checking, reflecting a move towards more robust and error-resistant code practices.

Changes

Files Change Summary
.../applicable-inputs.ts, .../marlowe-object-flow.ts, .../custodian/index.js, .../vesting-flow/index.html, .../lifecycle/src/generic/contracts.ts, .../swap.ada.token.e2e.spec.ts Updated function calls to use object parameters instead of individual parameters.
jsdelivr-npm-importmap.js Added new mappings for modules under @marlowe.io namespace.
.../adapter/src/io-ts.ts, .../runtime/client/rest/src/..., .../runtime/core/src/textEnvelope.ts, .../runtime/lifecycle/src/..., .../wallet/src/browser/index.ts Introduced type guards, request objects, and dynamic type checking enhancements.
.../runtime/client/rest/src/contract/..., .../runtime/client/rest/src/transaction/..., .../runtime/client/rest/src/payout/..., .../runtime/client/rest/src/withdrawal/... Modified request structures and added type validations for contracts, transactions, payouts, and withdrawals.

"In the realm of code, where the rabbits hop,
Changes abound, type safety won't stop.
🎩✨ With a magical tweak, and a structured call,
Our rabbit ensures, robustness for all."

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>.
    • Generate unit-tests for this file.
  • 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 tests 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 generate interesting stats about this repository from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 5

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 76b2336 and 4844746.
Files ignored due to path filters (2)
  • packages/language/core/v1/package.json is excluded by: !**/*.json
  • packages/marlowe-object/package.json is excluded by: !**/*.json
Files selected for processing (25)
  • examples/nodejs/src/experimental-features/applicable-inputs.ts (1 hunks)
  • examples/nodejs/src/marlowe-object-flow.ts (2 hunks)
  • examples/survey-workshop/custodian/index.js (2 hunks)
  • examples/vesting-flow/index.html (1 hunks)
  • jsdelivr-npm-importmap.js (2 hunks)
  • packages/adapter/src/io-ts.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/collection.ts (6 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/singleton.ts (3 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/sources.ts (4 hunks)
  • packages/runtime/client/rest/src/contract/index.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/next/endpoint.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/transaction/endpoints/collection.ts (4 hunks)
  • packages/runtime/client/rest/src/contract/transaction/endpoints/singleton.ts (3 hunks)
  • packages/runtime/client/rest/src/index.ts (15 hunks)
  • packages/runtime/client/rest/src/payout/endpoints/collection.ts (2 hunks)
  • packages/runtime/client/rest/src/payout/endpoints/singleton.ts (1 hunks)
  • packages/runtime/client/rest/src/withdrawal/endpoints/collection.ts (3 hunks)
  • packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts (2 hunks)
  • packages/runtime/core/src/textEnvelope.ts (1 hunks)
  • packages/runtime/lifecycle/src/browser/index.ts (3 hunks)
  • packages/runtime/lifecycle/src/generic/contracts.ts (3 hunks)
  • packages/runtime/lifecycle/src/index.ts (2 hunks)
  • packages/runtime/lifecycle/src/nodejs/index.ts (1 hunks)
  • packages/runtime/lifecycle/test/examples/swap.ada.token.e2e.spec.ts (2 hunks)
  • packages/wallet/src/browser/index.ts (2 hunks)
Additional comments: 60
packages/runtime/lifecycle/src/nodejs/index.ts (1)
  • 36-36: The mkRestClient function is correctly called with the strict parameter, ensuring that the strict mode setting is propagated. This is a good practice for maintaining consistency in how strict type checks are applied across different parts of the application.
packages/runtime/client/rest/src/payout/endpoints/singleton.ts (1)
  • 29-32: The definition of GetPayoutByIdRequestGuard correctly uses assertGuardEqual to ensure that the GetPayoutByIdRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
packages/runtime/core/src/textEnvelope.ts (1)
  • 45-46: The introduction of HexTransactionWitnessSetGuard as a type guard for HexTransactionWitnessSet is a straightforward and effective way to ensure that values assigned to variables of type HexTransactionWitnessSet are indeed strings. This is a good practice for enhancing type safety in the application.
packages/runtime/client/rest/src/contract/index.ts (3)
  • 46-46: The export of SubmitContractRequest from ./endpoints/singleton.js is correctly implemented, ensuring that this type is available for use in other parts of the application. This is a good practice for modularizing type definitions and promoting code reuse.
  • 50-52: The export of transaction-related request types from ./transaction/endpoints/singleton.js is correctly implemented. This modular approach to exporting types enhances code maintainability and readability by clearly separating concerns.
  • 57-57: The addition of GetTransactionsForContractRequest to the exports from ./transaction/endpoints/collection.js is a good practice, ensuring that this type is available for use where needed in the application. It promotes a consistent and modular approach to managing type definitions.
packages/runtime/lifecycle/src/index.ts (1)
  • 75-75: The mkRestClient function is correctly called with the strict parameter, ensuring that the strict mode setting is propagated. This is a good practice for maintaining consistency in how strict type checks are applied across different parts of the application.
examples/survey-workshop/custodian/index.js (2)
  • 25-27: The modification to call restClient.getTransactionsForContract with an object parameter is a good practice. It improves code readability and maintainability by clearly indicating what each parameter represents.
  • 38-41: Similarly, the modification to call restClient.getContractTransactionById with an object parameter enhances code readability and maintainability. Using named properties in function calls makes the code more self-documenting and easier to understand.
packages/runtime/lifecycle/src/browser/index.ts (1)
  • 93-93: The mkRestClient function is correctly called with the strict parameter, ensuring that the strict mode setting is propagated. This is a good practice for maintaining consistency in how strict type checks are applied across different parts of the application, even in a browser environment.
packages/adapter/src/io-ts.ts (2)
  • 100-102: The introduction of the strictDynamicTypeCheck function is a good practice for validating the type of the strict parameter in a dynamically typed context. This function enhances type safety by ensuring that strict is always a boolean.
  • 104-110: The addition of the InvalidTypeError class is a good practice for error handling. It extends the Error class with a constructor that includes parameters for errors and a message, allowing for more descriptive error messages related to type validation failures.
packages/runtime/client/rest/src/contract/next/endpoint.ts (1)
  • 54-66: The definition of GetNextStepsForContractRequestGuard correctly uses assertGuardEqual to ensure that the GetNextStepsForContractRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts (2)
  • 34-41: The definition of GetWithdrawalByIdRequestGuard correctly uses assertGuardEqual to ensure that the GetWithdrawalByIdRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
  • 52-58: Similarly, the definition of SubmitWithdrawalRequestGuard correctly uses assertGuardEqual to ensure that the SubmitWithdrawalRequest type aligns with its runtime validation. This enhances type safety by ensuring that the data structures used in the application are correctly validated at runtime.
packages/runtime/client/rest/src/contract/endpoints/singleton.ts (2)
  • 74-84: The definition of SubmitContractRequestGuard correctly uses assertGuardEqual to ensure that the SubmitContractRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
  • 87-87: The replacement of submitContractViaAxios with submitContract and the update to the PUT type to include new parameters and guards for HexTransactionWitnessSet and HexTransactionWitnessSetGuard are correctly implemented. This enhances the clarity and maintainability of the code by using more descriptive function names and ensuring type safety through the use of guards.
packages/runtime/client/rest/src/contract/transaction/endpoints/singleton.ts (2)
  • 39-49: The definition of GetContractTransactionByIdRequestGuard correctly uses assertGuardEqual to ensure that the GetContractTransactionByIdRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
  • 73-85: Similarly, the definition of SubmitContractTransactionRequestGuard correctly uses assertGuardEqual to ensure that the SubmitContractTransactionRequest type aligns with its runtime validation. This enhances type safety by ensuring that the data structures used in the application are correctly validated at runtime.
packages/runtime/client/rest/src/payout/endpoints/collection.ts (1)
  • 35-47: The definition of GetPayoutsRequestGuard correctly uses assertGuardEqual to ensure that the GetPayoutsRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
packages/runtime/client/rest/src/contract/endpoints/sources.ts (4)
  • 23-28: The definition of CreateContractSourcesRequestGuard correctly uses assertGuardEqual to ensure that the CreateContractSourcesRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
  • 68-73: The definition of GetContractBySourceIdRequestGuard correctly uses assertGuardEqual to ensure that the GetContractBySourceIdRequest type aligns with its runtime validation. This enhances type safety by ensuring that the data structures used in the application are correctly validated at runtime.
  • 106-107: Similarly, the definition of GetContractSourceAdjacencyRequestGuard correctly uses assertGuardEqual to ensure that the GetContractSourceAdjacencyRequest type aligns with its runtime validation. This is a good practice for maintaining type safety and ensuring that the data structures used in the application are correctly validated at runtime.
  • 139-140: The definition of GetContractSourceClosureRequestGuard correctly uses assertGuardEqual to ensure that the GetContractSourceClosureRequest type aligns with its runtime validation. This enhances type safety by ensuring that the data structures used in the application are correctly validated at runtime.
jsdelivr-npm-importmap.js (4)
  • 27-28: The addition of the mapping for @marlowe.io/language-core-v1/contract is correctly implemented.
  • 31-32: The addition of the mapping for @marlowe.io/language-core-v1/inputs is correctly implemented.
  • 35-36: The addition of the mapping for @marlowe.io/language-core-v1/participants is correctly implemented.
  • 83-84: The addition of the mapping for @marlowe.io/marlowe-object/object is correctly implemented.
packages/runtime/client/rest/src/contract/transaction/endpoints/collection.ts (4)
  • 18-18: The addition of AddressBech32Guard is correctly implemented and enhances type safety.
  • 34-39: The addition of ItemRangeGuard, PageGuard, and InputGuard declarations is correctly implemented, improving the structure and type safety of the code.
  • 88-103: The modifications to the GetTransactionsForContractRequest interface and the addition of GetTransactionsForContractRequestGuard are correctly implemented, aligning with best practices for type safety and code maintainability.
  • 145-162: The modifications to the ApplyInputsToContractRequest interface and the addition of ApplyInputsToContractRequestGuard are correctly implemented, enhancing type safety and code structure.
packages/wallet/src/browser/index.ts (3)
  • 3-3: The addition of io-ts import is correctly implemented and is essential for runtime type validation.
  • 6-6: The addition of assertGuardEqual and proxy imports from @marlowe.io/adapter/io-ts is correctly implemented, supporting the new type guard functionality.
  • 27-31: The introduction of SupportedWalletNameGuard using io-ts for type validation is correctly implemented, enhancing type safety within the module.
packages/runtime/lifecycle/test/examples/swap.ada.token.e2e.spec.ts (2)
  • 252-252: The modification to the getContractById function call to pass an object with the contractId property is correctly implemented, aligning with the updated API signature.
  • 271-271: The modification to the getContractById function call within getMarloweStatefromAnActiveContract to pass an object with the contractId property is correctly implemented, enhancing code readability and maintainability.
packages/runtime/lifecycle/src/generic/contracts.ts (3)
  • 59-67: The modification to the getTransactionsForContract function call to pass an object with the contractId property is correctly implemented, aligning with the updated API signature.
  • 137-139: The modification to the createContractSources function call to pass an object with the bundle property is correctly implemented, enhancing code readability and maintainability.
  • 153-156: The modification to the submitContract function call to pass an object with the contractId and txEnvelope properties is correctly implemented, aligning with the updated API signature.
examples/vesting-flow/index.html (2)
  • 143-143: The modification to the getContractById function call to pass an object with the contractId property is correctly implemented, aligning with the updated API signature.
  • 150-157: The modifications to the getTransactionsForContract and getContractTransactionById function calls to pass objects with named properties are correctly implemented, enhancing code readability and maintainability.
examples/nodejs/src/experimental-features/applicable-inputs.ts (1)
  • 125-125: The change to call restClient.getContractById with an object containing contractId as a property, instead of passing contractId directly, aligns with the PR's goal of improving type safety and developer experience by standardizing function calls.
examples/nodejs/src/marlowe-object-flow.ts (1)
  • 588-596: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [591-615]

The modifications to use object parameters for getContractById and createContractSources calls are consistent with the PR's objectives to improve type safety and standardize function calls.

packages/runtime/client/rest/src/contract/endpoints/collection.ts (3)
  • 35-35: The addition of AddressBech32Guard to the imports supports the PR's objective of improving type safety through stricter type checking.
  • 52-52: The addition of RoleNameGuard to the imports further enhances type safety by ensuring role names adhere to expected formats.
  • 80-88: The definition of GetContractsRequestGuard correctly uses the newly imported AddressBech32Guard and RoleNameGuard, aligning with the PR's focus on type safety.
packages/runtime/client/rest/src/index.ts (13)
  • 32-39: The addition of CompatibleRuntimeVersionGuard, RuntimeVersion, InvalidTypeError, and strictDynamicTypeCheck imports aligns with the PR's objective to enhance type safety and introduce strict mode functionality.
  • 108-108: The change in the createContractSources method signature to accept a Sources.CreateContractSourcesRequest object instead of a ContractBundle improves the method's clarity and type safety by using structured request objects.
  • 154-155: The modification of the getContractById method to accept a request object enhances the method's usability and consistency with other SDK methods.
  • 161-161: Updating the submitContract method to take a request object aligns with the PR's goal of improving parameter handling and type safety.
  • 171-171: The change in the getTransactionsForContract method signature to use a request object is a positive step towards standardizing method interfaces across the SDK.
  • 191-191: Modifying the submitContractTransaction method to accept a request object is consistent with the PR's objectives of enhancing type safety and improving developer experience.
  • 202-202: The update to the getContractTransactionById method signature to use a request object instead of individual parameters is a good practice for improving code readability and maintainability.
  • 231-231: Changing the getWithdrawalById method to accept a request object is in line with the PR's aim to standardize method calls and improve type safety.
  • 237-237: The modification of the submitWithdrawal method to use a request object instead of individual parameters aligns with the PR's objectives and enhances the method's clarity.
  • 256-291: The introduction of mkRestClientArgumentDynamicTypeCheck and the modifications to mkRestClient to include dynamic type checking and strict mode are significant improvements. They enforce type safety and provide developers with more informative error messages, aligning with the PR's goals. However, ensure that the performance impact of these checks, especially in non-strict mode, is minimal and documented.
  • 317-327: The implementation of strict type checking in the getContracts method, using Contracts.GetContractsRequestGuard, is a good practice that enhances type safety and error handling in the SDK.
  • 341-351: Implementing strict type checking in the getContractById method using Contract.GetContractByIdRequest guard is consistent with the PR's objectives to improve type safety and developer experience.
  • 355-367: The addition of strict type checking in the buildCreateContractTx method using Contracts.BuildCreateContractTxRequestGuard aligns with the PR's goal of enhancing type safety and improving error handling.

@@ -122,7 +122,7 @@ export async function getApplicableActions(
contractId: ContractId,
environment?: Environment
): Promise<ApplicableAction[]> {
const contractDetails = await restClient.getContractById(contractId);
const contractDetails = await restClient.getContractById({ contractId });
Copy link
Contributor

Choose a reason for hiding this comment

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

The use of await inside getApplicableActions without a try-catch block for error handling could lead to unhandled promise rejections if restClient.getContractById fails. Consider adding error handling to improve robustness.

+ try {
    const contractDetails = await restClient.getContractById({ contractId });
+ } catch (error) {
+   console.error("Failed to get contract details:", error);
+   throw error; // Rethrow or handle as appropriate for your application logic
+ }

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const contractDetails = await restClient.getContractById({ contractId });
try {
const contractDetails = await restClient.getContractById({ contractId });
} catch (error) {
console.error("Failed to get contract details:", error);
throw error; // Rethrow or handle as appropriate for your application logic
}

Comment on lines 588 to 596
contractId: ContractId
): Promise<ValidationResults> {
// First we try to fetch the contract details and the required tags
const contractDetails =
await lifecycle.restClient.getContractById(contractId);
const contractDetails = await lifecycle.restClient.getContractById({
contractId,
});

const scheme = extractSchemeFromTags(contractDetails.tags);

Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [591-615]

Consider adding error handling for the asynchronous calls to getContractById, createContractSources, and getContractSourceById to manage potential failures gracefully.

+ try {
    const contractDetails = await lifecycle.restClient.getContractById({ contractId });
    ...
    const { contractSourceId } = await lifecycle.restClient.createContractSources({ bundle: contractBundle });
    ...
    const initialContract = await lifecycle.restClient.getContractSourceById({ contractSourceId });
+ } catch (error) {
+   console.error("Error fetching contract details or sources:", error);
+   throw error; // Rethrow or handle as appropriate for your application logic
+ }

Comment on lines +26 to +32
strict: unknown = true
) {
if (!strictDynamicTypeCheck(strict)) {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

The validation for the strict parameter uses a custom type check (strictDynamicTypeCheck) but does not correctly check the type of strict. It should directly check if strict is a boolean instead of using a custom function that is not shown in this context.

-  if (!strictDynamicTypeCheck(strict)) {
+  if (typeof strict !== 'boolean') {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
strict: unknown = true
) {
if (!strictDynamicTypeCheck(strict)) {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
strict: unknown = true
) {
if (typeof strict !== 'boolean') {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);

Comment on lines +67 to +71
if (!strictDynamicTypeCheck(strict)) {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

The validation for the strict parameter within the mkRuntimeLifecycle function does not directly check if strict is a boolean. Instead, it relies on a custom type check (strictDynamicTypeCheck) that is not shown in this context. Directly checking the type of strict would be more straightforward and reliable.

-  if (!strictDynamicTypeCheck(strict)) {
+  if (typeof strict !== 'boolean') {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (!strictDynamicTypeCheck(strict)) {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
if (typeof strict !== 'boolean') {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);

Comment on lines +77 to +81
if (!strictDynamicTypeCheck(strict)) {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

The validation for the strict parameter within the mkRuntimeLifecycle function does not directly check if strict is a boolean. Instead, it relies on a custom type check (strictDynamicTypeCheck) that is not shown in this context. Directly checking the type of strict would be more straightforward and reliable.

-  if (!strictDynamicTypeCheck(strict)) {
+  if (typeof strict !== 'boolean') {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if (!strictDynamicTypeCheck(strict)) {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);
if (typeof strict !== 'boolean') {
throw new InvalidTypeError(
[],
`Invalid type for argument 'strict', expected boolean but got ${strict}`
);

@nhenin
Copy link
Collaborator

nhenin commented Jan 29, 2024

@bjornkihlberg can you squash all these let's go commits and add a better description to it ?

Copy link
Collaborator

@hrajchert hrajchert left a comment

Choose a reason for hiding this comment

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

Just checked the code, looks good

Comment on lines 57 to 71
},
"./contract": {
"import": "./dist/esm/contract.js",
"require": "./dist/bundled/cjs/contract.cjs",
"types": "./dist/esm/contract.d.ts"
},
"./participants": {
"import": "./dist/esm/participants.js",
"require": "./dist/bundled/cjs/participants.cjs",
"types": "./dist/esm/participants.d.ts"
},
"./inputs": {
"import": "./dist/esm/inputs.js",
"require": "./dist/bundled/cjs/inputs.cjs",
"types": "./dist/esm/inputs.d.ts"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are you exporting these?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had to in order to be able to use the rest client from the browser. Maybe we can avoid it. I'll look into it.

Comment on lines 51 to 52
import { ContractGuard } from "@marlowe.io/language-core-v1/contract";
import { RoleNameGuard } from "@marlowe.io/language-core-v1/participants";
Copy link
Collaborator

Choose a reason for hiding this comment

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

these should be
import * as G from "@marlowe.io/language-core-v1/guards

G.Contract
G.RoleName

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This may remove the need for some of the exported modules.

`Invalid argument to getContracts(${request})`
);
} else {
const test: typeof request = result.right;
Copy link
Collaborator

Choose a reason for hiding this comment

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

this seems unused

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's unused at runtime but it helps ensure that you are using the correct guard. I think there are better ways of solving this. I'm going to look into it.

Comment on lines 341 to 351
if (strict) {
const result = Contract.GetContractByIdRequest.decode(request);
if (result._tag === "Left") {
throw new InvalidTypeError(
result.left,
`Invalid argument to getContractById(${request})`
);
} else {
const test: typeof request = result.right;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this pattern could be captured by a function assertRequest(methodName, guard, request, strict)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I was thinking the same.

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 2

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ffaa183 and 95c4958.
Files ignored due to path filters (1)
  • packages/marlowe-object/package.json is excluded by: !**/*.json
Files selected for processing (26)
  • changelog.d/20240126_162752_bjorn.wilhelm.kihlberg_PLT_9368.md (1 hunks)
  • examples/nodejs/src/experimental-features/applicable-inputs.ts (1 hunks)
  • examples/nodejs/src/marlowe-object-flow.ts (2 hunks)
  • examples/survey-workshop/custodian/index.js (2 hunks)
  • examples/vesting-flow/index.html (1 hunks)
  • jsdelivr-npm-importmap.js (1 hunks)
  • packages/adapter/src/io-ts.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/collection.ts (7 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/singleton.ts (3 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/sources.ts (4 hunks)
  • packages/runtime/client/rest/src/contract/index.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/next/endpoint.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/transaction/endpoints/collection.ts (4 hunks)
  • packages/runtime/client/rest/src/contract/transaction/endpoints/singleton.ts (3 hunks)
  • packages/runtime/client/rest/src/index.ts (15 hunks)
  • packages/runtime/client/rest/src/payout/endpoints/collection.ts (2 hunks)
  • packages/runtime/client/rest/src/payout/endpoints/singleton.ts (1 hunks)
  • packages/runtime/client/rest/src/withdrawal/endpoints/collection.ts (3 hunks)
  • packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts (2 hunks)
  • packages/runtime/core/src/textEnvelope.ts (1 hunks)
  • packages/runtime/lifecycle/src/browser/index.ts (3 hunks)
  • packages/runtime/lifecycle/src/generic/contracts.ts (3 hunks)
  • packages/runtime/lifecycle/src/index.ts (2 hunks)
  • packages/runtime/lifecycle/src/nodejs/index.ts (1 hunks)
  • packages/runtime/lifecycle/test/examples/swap.ada.token.e2e.spec.ts (2 hunks)
  • packages/wallet/src/browser/index.ts (2 hunks)
Files skipped from review as they are similar to previous changes (24)
  • changelog.d/20240126_162752_bjorn.wilhelm.kihlberg_PLT_9368.md
  • examples/nodejs/src/experimental-features/applicable-inputs.ts
  • examples/nodejs/src/marlowe-object-flow.ts
  • examples/survey-workshop/custodian/index.js
  • examples/vesting-flow/index.html
  • jsdelivr-npm-importmap.js
  • packages/adapter/src/io-ts.ts
  • packages/runtime/client/rest/src/contract/endpoints/collection.ts
  • packages/runtime/client/rest/src/contract/endpoints/singleton.ts
  • packages/runtime/client/rest/src/contract/endpoints/sources.ts
  • packages/runtime/client/rest/src/contract/index.ts
  • packages/runtime/client/rest/src/contract/transaction/endpoints/collection.ts
  • packages/runtime/client/rest/src/contract/transaction/endpoints/singleton.ts
  • packages/runtime/client/rest/src/payout/endpoints/collection.ts
  • packages/runtime/client/rest/src/payout/endpoints/singleton.ts
  • packages/runtime/client/rest/src/withdrawal/endpoints/collection.ts
  • packages/runtime/client/rest/src/withdrawal/endpoints/singleton.ts
  • packages/runtime/core/src/textEnvelope.ts
  • packages/runtime/lifecycle/src/browser/index.ts
  • packages/runtime/lifecycle/src/generic/contracts.ts
  • packages/runtime/lifecycle/src/index.ts
  • packages/runtime/lifecycle/src/nodejs/index.ts
  • packages/runtime/lifecycle/test/examples/swap.ada.token.e2e.spec.ts
  • packages/wallet/src/browser/index.ts
Additional comments: 7
packages/runtime/client/rest/src/contract/next/endpoint.ts (2)
  • 54-66: The GetNextStepsForContractRequestGuard correctly uses io-ts for runtime type validation, ensuring that the request object adheres to the expected structure. This is a good practice for enhancing type safety in dynamic environments.
  • 67-67: The getNextStepsForContract function correctly decodes and validates the response data using the GetNextStepsForContractResponseGuard, throwing formatted validation errors if decoding fails. This approach is consistent with best practices for error handling and data validation.
packages/runtime/client/rest/src/index.ts (5)
  • 32-39: The addition of CompatibleRuntimeVersionGuard, RuntimeVersion, InvalidTypeError, and strictDynamicTypeCheck imports supports the new strict mode functionality, aligning with the PR's objective to enhance type safety and developer experience.
  • 108-108: The modification of the createContractSources method signature to accept a request object instead of individual parameters is a positive change. It improves the method's usability and consistency with the rest of the API, aligning with the PR's objectives.
  • 256-261: The mkRestClientArgumentDynamicTypeCheck function provides a runtime check for the baseURL argument, ensuring it's a string when strict mode is enabled. This is a good practice for type safety.
  • 280-291: The dynamic type checking for strict and baseURL arguments in mkRestClient enhances error reporting and type safety, aligning with the PR's objectives to improve developer experience through stricter runtime checks.
  • 317-327: The implementation of strict mode checks in methods like getContracts is consistent and correctly throws InvalidTypeError when the request object does not match the expected type. This approach significantly improves error handling and developer feedback.

@@ -1,15 +1,18 @@
import { AxiosInstance } from "axios";
import * as E from "fp-ts/lib/Either.js";
import * as TE from "fp-ts/lib/TaskEither.js";
import * as G from "@marlowe.io/language-core-v1/guards";
Copy link
Contributor

Choose a reason for hiding this comment

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

The import * as G from "@marlowe.io/language-core-v1/guards"; is not used in the file.

- import * as G from "@marlowe.io/language-core-v1/guards";

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import * as G from "@marlowe.io/language-core-v1/guards";

import { Environment, Party } from "@marlowe.io/language-core-v1";
import { Next } from "@marlowe.io/language-core-v1/next";
import { stringify } from "qs";
import { DecodingError } from "@marlowe.io/adapter/codec";
import { posixTimeToIso8601 } from "@marlowe.io/adapter/time";
import { assertGuardEqual, proxy } from "@marlowe.io/adapter/io-ts";
import * as t from "io-ts/lib/index.js";
Copy link
Contributor

Choose a reason for hiding this comment

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

The assertGuardEqual import is not used in this file. Consider removing it to clean up unused imports.

- import { assertGuardEqual, proxy } from "@marlowe.io/adapter/io-ts";

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import * as t from "io-ts/lib/index.js";
import * as t from "io-ts/lib/index.js";

Copy link
Contributor

@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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 95c4958 and e3477f0.
Files selected for processing (3)
  • packages/adapter/src/io-ts.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/collection.ts (7 hunks)
  • packages/runtime/client/rest/src/index.ts (10 hunks)
Files skipped from review as they are similar to previous changes (2)
  • packages/adapter/src/io-ts.ts
  • packages/runtime/client/rest/src/contract/endpoints/collection.ts
Additional comments: 11
packages/runtime/client/rest/src/index.ts (11)
  • 14-14: The removal of ContractBundle import is not directly visible in the provided code, but its usage seems to have been replaced with request objects in method signatures. Ensure that all instances where ContractBundle was previously used have been appropriately updated.
Verification successful

The verification process focused on ensuring that the removal or modification of ContractBundle in rest/src/index.ts was handled appropriately. The presence of ContractBundle in other parts of the project, as revealed by the script output, does not directly indicate an issue with the specific changes in rest/src/index.ts. The original review comment remains relevant, emphasizing the need to ensure that all instances where ContractBundle was previously used in the context of rest/src/index.ts have been appropriately updated. Based on the information provided, there is no direct evidence to suggest that the changes in rest/src/index.ts regarding ContractBundle were incorrectly done. Therefore, the verification process concludes with the understanding that the specific changes made to rest/src/index.ts are consistent with the project's current state and usage patterns of ContractBundle.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any remaining references to ContractBundle in the project.
rg "ContractBundle" --type ts

Length of output: 1292

* 33-40: New imports for `CompatibleRuntimeVersionGuard`, `RuntimeVersion`, `InvalidTypeError`, and `strictDynamicTypeCheck` have been added. Verify that these are correctly utilized in the context of enabling strict mode and dynamic type checking throughout the file. * 110-110: The `createContractSources` method signature has been modified to accept a `Sources.CreateContractSourcesRequest` object instead of a `ContractBundle`. This change aligns with the PR's objective to standardize function calls and improve code readability by using request objects. * 155-157: The `getContractById` method signature has been updated to accept a `Contract.GetContractByIdRequest` object. This change enhances the method's clarity and consistency with other updated method signatures. * 163-163: The `submitContract` method now takes a `Contract.SubmitContractRequest` object, aligning with the PR's goal of using request objects for better parameter handling and type safety. * 173-173: The `getTransactionsForContract` method signature has been updated to use a `Transactions.GetTransactionsForContractRequest` object, which is consistent with the PR's aim to improve function call standardization. * 193-193: The `submitContractTransaction` method signature change to accept a `Transaction.SubmitContractTransactionRequest` object is in line with the PR's objectives to enhance type safety and parameter handling. * 233-233: The `getWithdrawalById` method signature has been modified to accept a `Withdrawal.GetWithdrawalByIdRequest` object, further standardizing method signatures across the SDK. * 239-239: The `submitWithdrawal` method signature has been updated to use a `Withdrawal.SubmitWithdrawalRequest` object, aligning with the PR's goals for improved parameter handling and type safety. * 258-277: The addition of `mkRestClientArgumentDynamicTypeCheck` and `withDynamicTypeCheck` functions introduces dynamic type checking based on the `strict` mode setting. This is a crucial part of the PR's objective to enhance type safety and developer experience by providing more informative error messages during development. Ensure that these functions are correctly implemented and used throughout the file. * 255-313: > 📝 **NOTE** > This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [291-324]

The mkRestClient function has been overloaded to support an optional strict boolean parameter, enabling dynamic type checking. This change is central to the PR's goal of introducing a strict mode for runtime lifecycle management. Verify that the strict mode is correctly applied and that the dynamic type checks are performed as expected.

@nhenin nhenin merged commit c9c15bb into main Jan 30, 2024
2 checks passed
@nhenin nhenin deleted the PLT-9368 branch January 30, 2024 15:26
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.

3 participants