Skip to content

Commit

Permalink
feat(klesia sdk): refactor client
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 3, 2024
1 parent 1491df9 commit 505d747
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
11 changes: 11 additions & 0 deletions packages/klesia-sdk/src/client.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { expect, it } from "bun:test";
import { createClient } from "./client";

it("fetches transaction count", async () => {
const client = createClient({ network: "devnet" });
const { result } = await client.request({
method: "mina_getTransactionCount",
params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"],
});
expect(result).toBeGreaterThan(0);
});
22 changes: 16 additions & 6 deletions packages/klesia-sdk/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ import { z } from "zod";

const NetworkMatcher = z.enum(["mainnet", "devnet"]);

export const createClient = ({
network,
}: { network: "mainnet" | "devnet" }) => {
return match(NetworkMatcher.parse(network))
.with("devnet", () => hc<KlesiaRpc>("https://devnet.klesia.palladians.xyz"))
type CreateClientProps = { network: "mainnet" | "devnet"; customUrl?: string };

export const createClient = ({ network, customUrl }: CreateClientProps) => {
const baseClient = match(NetworkMatcher.parse(network))
.with("devnet", () =>
hc<KlesiaRpc>(customUrl ?? "https://devnet.klesia.palladians.xyz"),
)
.with("mainnet", () =>
hc<KlesiaRpc>("https://mainnet.klesia.palladians.xyz"),
hc<KlesiaRpc>(customUrl ?? "https://mainnet.klesia.palladians.xyz"),
)
.exhaustive();
const rpcHandler = baseClient.api.$post;
type RpcRequest = Parameters<typeof rpcHandler>[0];
const request = async (req: RpcRequest["json"]) => {
return (await baseClient.api.$post({ json: req })).json();
};
return {
request,
};
};

0 comments on commit 505d747

Please sign in to comment.