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-7701, PLT-7702, PLT-7703, PLT-8427: Implement remaining 1-1 endpoints #128

Merged
merged 12 commits into from Dec 14, 2023

Conversation

bjornkihlberg
Copy link
Contributor

@bjornkihlberg bjornkihlberg commented Dec 13, 2023

This PR

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Added a search functionality to the app.
    • Introduced new API endpoints for retrieving contract sources and their relationships.
    • Added functionality to obtain the next steps for a contract.
  • Enhancements

    • Improved type safety with new type guard declarations.
    • Expanded the RestClient interface to include new methods for enhanced contract interactions.
  • Refactor

    • Updated import statements to include additional necessary modules and functions.
    • Refined API interfaces to align with the new endpoint functionalities.

Copy link
Contributor

coderabbitai bot commented Dec 13, 2023

Walkthrough

The codebase has been updated to enhance contract source management and processing within a REST client context. New functions for retrieving contract sources by ID, adjacency, and closure have been added, along with type guards for response validation. The client interface now includes methods for these functions, and the Next module has been integrated, replacing the ContractNext module. These changes expand the client's capabilities in handling contract-related data.

Changes

File Path Change Summary
.../client/rest/src/contract/endpoints/sources.ts
.../client/rest/src/contract/next/endpoint.ts
.../client/rest/src/index.ts
Added Contract import, new functions for contract source retrieval, type guards, and updated client interface with new methods. Integrated Next module, replaced ContractNext with Next in API interfaces.
.../changelog.d/20231213_145052_bjorn.wilhelm.kihlberg_PLT_7701.md Added extensions to @marlowe.io/runtime-rest-client module, including getContractSourceById, getContractSourceAdjacency, getContractSourceClosure, and getNextStepsForContract. Implemented in PR-128.
.../examples/js/poc-helpers.js Modified getRuntimeUrl function to include a null check for runtimeUrlInput before accessing its value, affecting the control flow.
.../examples/merkleization-flow/index.html
.../examples/rest-client-flow/index.html
Addition of HTML files introducing scripts for asynchronous operations related to contract data retrieval using the Marlowe-ts-sdk library.

Poem

In the code where the data flows, 🌊
New paths for contract prose. 📜
A rabbit hopped through the lines, 🐇💻
Weaving functions, types entwined. 🌟

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 X ?


Tips

Chat with CodeRabbit Bot (@coderabbitai)

  • You can reply to a review comment made by CodeRabbit.
  • You can tag CodeRabbit on specific lines of code or files in the PR by tagging @coderabbitai in a comment.
  • You can tag @coderabbitai in a PR comment and ask one-off questions about the PR and the codebase. Use quoted replies to pass the context for follow-up questions.

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

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: 9

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between ebf25e7 and b1df00a.
Files selected for processing (3)
  • packages/runtime/client/rest/src/contract/endpoints/sources.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/next/endpoint.ts (3 hunks)
  • packages/runtime/client/rest/src/index.ts (10 hunks)
Additional comments: 7
packages/runtime/client/rest/src/contract/endpoints/sources.ts (3)
  • 44-52: The GetContractBySourceIdRequest and GetContractBySourceIdResponse interfaces are well-defined, and the type guard GetContractBySourceIdResponseGuard is correctly using G.Contract for validation.

  • 75-103: The GetContractSourceAdjacencyRequest and GetContractSourceAdjacencyResponse interfaces are correctly defined, and the type guard GetContractSourceAdjacencyResponseGuard is properly implemented using t.array(G.BuiltinByteString).

  • 105-114: The GetContractSourceClosureRequest and GetContractSourceClosureResponse interfaces are correctly defined, and the type guard GetContractSourceClosureResponseGuard is properly implemented using t.array(G.BuiltinByteString).

packages/runtime/client/rest/src/contract/next/endpoint.ts (3)
  • 1-6: The imports for io-ts and language-core-v1/guards are correctly added to support the new type definitions and runtime type checking.

  • 47-54: The GetNextStepsForContractRequest interface and GetNextStepsForContractResponse type are well-defined and align with the new endpoint's requirements.

  • 58-90: The getNextStepsForContract function is implemented correctly, using the new request interface and response type, and includes proper error handling with runtime type checking.

packages/runtime/client/rest/src/index.ts (1)
  • 90-126: The implementation of new methods in the RestClient interface aligns with the PR objectives. Ensure that the corresponding implementations in mkRestClient are correct and consistent with the interface definitions.

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 b1df00a and 7f0fed1.
Files selected for processing (1)
  • changelog.d/20231213_145052_bjorn.wilhelm.kihlberg_PLT_7701.md (1 hunks)
Additional comments: 1
changelog.d/20231213_145052_bjorn.wilhelm.kihlberg_PLT_7701.md (1)
  • 1-6: The changelog entries are clear and correctly reference the PR where the new procedures were implemented.

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.

How are you testing these endpoints, or more important how can I test them?

I don't mean the integration testing that should happen later, I'm referring to manual testing. I don't see any change to the rest-client-flow example with the addition of these endpoints.

Take a look at this PR, where I add instructions on how to manually test the createContractSources endpoint

};

export interface GetContractSourceAdjacencyRequest {
contractSourceId: BuiltinByteString;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I believe a type alias might clarify the intent.

type ContractSourceId = string 
// or 
type ContractSourceId = BuiltinByteString

We could asses also if a Branded type makes sense to show that not all strings are the same

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have we figured out yet how we want to do branded types?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes, using t.brand from io-ts

}

export interface GetContractSourceAdjacencyResponse {
results: BuiltinByteString[];
Copy link
Collaborator

Choose a reason for hiding this comment

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

This shows the importance of the type alias

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fair point.

@bjornkihlberg
Copy link
Contributor Author

@hrajchert

How are you testing these endpoints, or more important how can I test them?

Yes, I just need to write a flow for these end points.

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: 9

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 89082e9 and 3c831c0.
Files selected for processing (6)
  • changelog.d/20231213_145052_bjorn.wilhelm.kihlberg_PLT_7701.md (1 hunks)
  • examples/js/poc-helpers.js (1 hunks)
  • examples/merkleization-flow/index.html (1 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/sources.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/next/endpoint.ts (1 hunks)
  • packages/runtime/client/rest/src/index.ts (10 hunks)
Files skipped from review as they are similar to previous changes (2)
  • changelog.d/20231213_145052_bjorn.wilhelm.kihlberg_PLT_7701.md
  • packages/runtime/client/rest/src/contract/next/endpoint.ts
Additional comments: 7
examples/js/poc-helpers.js (2)
  • 39-45: The null check added to getRuntimeUrl is a good defensive programming practice to prevent potential null reference errors. This change ensures that the function will not throw an error if runtimeUrlInput is null when trying to access its value property.

  • 39-45: While the change to getRuntimeUrl is approved, it's important to ensure that this change is in line with the broader objectives of the PR, which is to implement new endpoints related to contract sources. If this change is part of a larger set of UI updates to support the new endpoints, it would be beneficial to mention this context in the PR description for clarity.

packages/runtime/client/rest/src/contract/endpoints/sources.ts (2)
  • 7-7: The addition of the Contract import aligns with the new features being implemented for contract source management.

  • 44-133: The implementation of the new functions for contract source management is consistent with the PR objectives and follows established patterns for making GET requests and using type guards.

packages/runtime/client/rest/src/index.ts (3)
  • 87-126: The addition of new methods createContractSources, getContractSourceById, getContractSourceAdjacency, getContractSourceClosure, and getNextStepsForContract to the RestClient interface aligns with the PR objective to enhance contract source management capabilities.

  • 312-326: The implementation of the new methods in the mkRestClient function correctly corresponds to the updated RestClient interface.

  • 30-30: The import of Next from "./contract/next/endpoint.js" has been added. Verify that all instances of ContractNext have been replaced with Next to maintain consistency.

Verification successful

The shell script executed a search for any remaining usages of ContractNext and did not produce any output. This suggests that there are no instances of ContractNext left in the codebase, which would imply that the replacement with Next is consistent throughout the files that were searched.

Given this information, it seems that the change from ContractNext to Next has been successfully applied across the codebase, at least in the context of the files that rg would have searched through (which is typically all text files in the repository).

Since the script did not find any occurrences of ContractNext, I can conclude that the replacement has been done correctly, and there are no inconsistencies in the files that were checked.

The import of Next from "./contract/next/endpoint.js" has been added, and based on the search results, all instances of ContractNext have been replaced with Next as no occurrences were found in the codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Search for any remaining usages of ContractNext that should have been replaced with Next.
rg 'ContractNext' --files-with-matches

Length of output: 38

@@ -40,3 +40,94 @@ export const createContractSources = (axiosInstance: AxiosInstance) => {
);
};
};

export interface GetContractBySourceIdRequest {
contractSourceId: BuiltinByteString;
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 BuiltinByteString directly as a type for contractSourceId is noted. Consider revisiting the previous discussion on using a type alias or branded type to clarify intent and ensure type safety.

};

export interface GetContractSourceAdjacencyRequest {
contractSourceId: BuiltinByteString;
Copy link
Contributor

Choose a reason for hiding this comment

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

Reconsider the implementation of a type alias for BuiltinByteString to improve code readability and maintainability, as previously discussed.

const runtimeGood = await restClient.healthcheck();
if (!runtimeGood) throw "Healthcheck failed!";

debugger;
Copy link
Contributor

Choose a reason for hiding this comment

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

The debugger; statements are typically used for development and should not be present in production code. Consider removing them before merging.

Comment on lines 31 to 34
const contract_pt1 = {
let: "x",
be: 5n,
then: { ref: contract_pt2_Label },
Copy link
Contributor

Choose a reason for hiding this comment

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

The object definition for contract_pt1 is incorrect. JavaScript objects should consist of key-value pairs.

- const contract_pt1 = {
-   let: "x",
-   be: 5n,
-   then: { ref: contract_pt2_Label },
- };
+ const contract_pt1 = {
+   "let": "x",
+   "be": 5n,
+   "then": { "ref": contract_pt2_Label },
+ };

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 contract_pt1 = {
let: "x",
be: 5n,
then: { ref: contract_pt2_Label },
const contract_pt1 = {
"let": "x",
"be": 5n,
"then": { "ref": contract_pt2_Label },

Comment on lines 50 to 54
await restClient.createContractSources(contract_pt1_Label, [
{ type: "contract", label: contract_pt1_Label, contract_pt1 },
{ type: "contract", label: contract_pt2_Label, contract_pt2 },
{ type: "contract", label: contract_pt3_Label, contract_pt3 },
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

The variables contract_pt1, contract_pt2, and contract_pt3 are not being passed correctly to createContractSources. They should be passed as objects.

- { type: "contract", label: contract_pt1_Label, contract_pt1 },
- { type: "contract", label: contract_pt2_Label, contract_pt2 },
- { type: "contract", label: contract_pt3_Label, contract_pt3 },
+ { type: "contract", label: contract_pt1_Label, contract: contract_pt1 },
+ { type: "contract", label: contract_pt2_Label, contract: contract_pt2 },
+ { type: "contract", label: contract_pt3_Label, contract: contract_pt3 },

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
await restClient.createContractSources(contract_pt1_Label, [
{ type: "contract", label: contract_pt1_Label, contract_pt1 },
{ type: "contract", label: contract_pt2_Label, contract_pt2 },
{ type: "contract", label: contract_pt3_Label, contract_pt3 },
]);
await restClient.createContractSources(contract_pt1_Label, [
{ type: "contract", label: contract_pt1_Label, contract: contract_pt1 },
{ type: "contract", label: contract_pt2_Label, contract: contract_pt2 },
{ type: "contract", label: contract_pt3_Label, contract: contract_pt3 },
]);

Comment on lines 44 to 45
const runtimeGood = await restClient.healthcheck();
if (!runtimeGood) throw "Healthcheck failed!";
Copy link
Contributor

Choose a reason for hiding this comment

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

The healthcheck method is called on the restClient, but it's not clear if this method exists or what it returns. Assuming it's correct, the error handling should be improved.

- const runtimeGood = await restClient.healthcheck();
- if (!runtimeGood) throw "Healthcheck failed!";
+ try {
+   const runtimeGood = await restClient.healthcheck();
+   if (!runtimeGood) {
+     console.error("Healthcheck failed!");
+     // Handle the error appropriately
+   }
+ } catch (error) {
+   console.error("Healthcheck failed with error:", error);
+   // Handle the error appropriately
+ }

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 runtimeGood = await restClient.healthcheck();
if (!runtimeGood) throw "Healthcheck failed!";
try {
const runtimeGood = await restClient.healthcheck();
if (!runtimeGood) {
console.error("Healthcheck failed!");
// Handle the error appropriately
}
} catch (error) {
console.error("Healthcheck failed with error:", error);
// Handle the error appropriately
}

@@ -195,13 +234,6 @@ export interface RestClient {
*/
healthcheck(): Promise<Boolean>;
Copy link
Contributor

Choose a reason for hiding this comment

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

The healthcheck method in mkRestClient returns a promise that resolves to a boolean, which is inconsistent with the healthcheck method in the FPTSRestAPI interface that returns a TE.TaskEither. Consider aligning the return types for consistency.

@@ -470,7 +514,7 @@ export interface ContractsAPI {
/**
* @see {@link }
*/
next: ContractNext.GET;
next: Next.GET;
Copy link
Contributor

Choose a reason for hiding this comment

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

The next property in the ContractsAPI interface is assigned a Next.GET type, but there is no corresponding implementation in the mkFPTSRestClient function. Add the missing implementation to match the interface.

+        next: Next.getViaAxios(axiosInstance),

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
next: Next.GET;
next: Next.getViaAxios(axiosInstance);

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 3c831c0 and 56ea3ee.
Files selected for processing (1)
  • examples/rest-client-flow/index.html (1 hunks)
Files skipped from review due to trivial changes (1)
  • examples/rest-client-flow/index.html

@hrajchert hrajchert merged commit b3378c5 into main Dec 14, 2023
2 checks passed
@hrajchert hrajchert deleted the PLT-7701 branch December 14, 2023 16:57
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: 3

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 56ea3ee and efe215d.
Files selected for processing (3)
  • packages/marlowe-object/src/index.ts (1 hunks)
  • packages/marlowe-object/src/reference.ts (2 hunks)
  • packages/runtime/client/rest/src/contract/endpoints/sources.ts (2 hunks)
Additional comments: 3
packages/marlowe-object/src/index.ts (1)
  • 3-8: The changes to the export statements in index.ts correctly include the new ContractSourceId and ContractSourceIdGuard exports from ./reference.js, which align with the PR objectives to extend the runtime client's REST interface for contract source management.
packages/marlowe-object/src/reference.ts (2)
  • 11-17: The introduction of ContractSourceId and ContractSourceIdGuard is consistent with the PR objectives to enhance contract source management capabilities.

  • 31-33: The update to Reference and ReferenceGuard to use ContractSourceId and ContractSourceIdGuard enhances type safety and clarity, aligning with the PR's goal of improving contract source management.

Comment on lines +11 to +14
/**
* A label for one of the {@link ObjectType}
* @category Object
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

The comment block for ContractSourceId appears to be a duplicate of the one for Label. It should be updated to accurately describe ContractSourceId.

 /**
- * A label for one of the {@link ObjectType}
+ * An identifier for a contract source within the system
  * @category Object
  */

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
/**
* A label for one of the {@link ObjectType}
* @category Object
*/
/**
* An identifier for a contract source within the system
* @category Object
*/

GetContractBySourceIdResponseGuard.decode(response.data),
E.match(
(e) => {
throw formatValidationErrors(e);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider improving error handling by wrapping validation errors in a user-friendly message or a custom error type to avoid exposing internal details and to provide more informative messages to the caller.

Also applies to: 103-103, 133-133

Comment on lines +65 to +68
const response = await axiosInstance.get(
`/contracts/sources/${encodeURIComponent(contractSourceId)}`,
{ params: { expand } }
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding error handling for the HTTP request to handle network errors or non-200 status codes.

Also applies to: 96-98, 126-128

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants