Skip to content

Commit 505d747

Browse files
committed
feat(klesia sdk): refactor client
1 parent 1491df9 commit 505d747

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, it } from "bun:test";
2+
import { createClient } from "./client";
3+
4+
it("fetches transaction count", async () => {
5+
const client = createClient({ network: "devnet" });
6+
const { result } = await client.request({
7+
method: "mina_getTransactionCount",
8+
params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"],
9+
});
10+
expect(result).toBeGreaterThan(0);
11+
});

packages/klesia-sdk/src/client.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ import { z } from "zod";
55

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

8-
export const createClient = ({
9-
network,
10-
}: { network: "mainnet" | "devnet" }) => {
11-
return match(NetworkMatcher.parse(network))
12-
.with("devnet", () => hc<KlesiaRpc>("https://devnet.klesia.palladians.xyz"))
8+
type CreateClientProps = { network: "mainnet" | "devnet"; customUrl?: string };
9+
10+
export const createClient = ({ network, customUrl }: CreateClientProps) => {
11+
const baseClient = match(NetworkMatcher.parse(network))
12+
.with("devnet", () =>
13+
hc<KlesiaRpc>(customUrl ?? "https://devnet.klesia.palladians.xyz"),
14+
)
1315
.with("mainnet", () =>
14-
hc<KlesiaRpc>("https://mainnet.klesia.palladians.xyz"),
16+
hc<KlesiaRpc>(customUrl ?? "https://mainnet.klesia.palladians.xyz"),
1517
)
1618
.exhaustive();
19+
const rpcHandler = baseClient.api.$post;
20+
type RpcRequest = Parameters<typeof rpcHandler>[0];
21+
const request = async (req: RpcRequest["json"]) => {
22+
return (await baseClient.api.$post({ json: req })).json();
23+
};
24+
return {
25+
request,
26+
};
1727
};

0 commit comments

Comments
 (0)