Skip to content

Commit

Permalink
refactor: ic-mgmt transform (#775)
Browse files Browse the repository at this point in the history
# Motivation

We prefer functional code and to avoid casting types.

# Changes

- Review `args` of `transform`
- Make function functional
- Use `nonNullish`
- Remove unnecessary cast

---------

Signed-off-by: David Dal Busco <david.dalbusco@dfinity.org>
  • Loading branch information
peterpeterparker authored Nov 27, 2024
1 parent 41871fa commit 230ebe2
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions packages/ic-management/src/utils/transform.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { ActorConfig, CallConfig } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { nonNullish } from "@dfinity/utils";

type CallTransform = Required<ActorConfig>["callTransform"];

Expand All @@ -18,13 +19,20 @@ type QueryTransform = Required<ActorConfig>["queryTransform"];
**/
export const transform: CallTransform | QueryTransform = (
_methodName: string,
args: unknown[],
args: (Record<string, unknown> & {
canister_id?: unknown;
})[],
_callConfig: CallConfig,
): { effectiveCanisterId: Principal } => {
const first = args[0] as { canister_id: string };
let effectiveCanisterId = Principal.fromHex("");
if (first && typeof first === "object" && first.canister_id) {
effectiveCanisterId = Principal.from(first.canister_id as unknown);
const first = args[0];

if (
nonNullish(first) &&
typeof first === "object" &&
nonNullish(first.canister_id)
) {
return { effectiveCanisterId: Principal.from(first.canister_id) };
}
return { effectiveCanisterId };

return { effectiveCanisterId: Principal.fromHex("") };
};

0 comments on commit 230ebe2

Please sign in to comment.