Skip to content

Commit 5c2efbf

Browse files
committed
feat(klesia sdk): refactor to not require hono instance
1 parent c07429c commit 5c2efbf

File tree

8 files changed

+24
-40
lines changed

8 files changed

+24
-40
lines changed

apps/klesia/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
"module": "dist/index.js",
66
"types": "dist/index.d.ts",
77
"exports": {
8+
"./schema": {
9+
"types": "./dist/schema.d.ts",
10+
"default": "./dist/schema.js"
11+
},
812
".": {
913
"types": "./dist/index.d.ts",
1014
"default": "./dist/index.js"

apps/klesia/src/schema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export const RpcMethodSchema = z.discriminatedUnion("method", [
5252
}),
5353
]);
5454

55+
export type RpcRequestType = z.infer<typeof RpcMethodSchema>;
56+
5557
export const JsonRpcResponse = z.object({
5658
jsonrpc: z.literal("2.0"),
5759
});

apps/klesia/tsup.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sharedConfig from "../../packages/utils/tsup.config";
44

55
export default defineConfig({
66
...sharedConfig,
7-
entry: ["src/index.ts", "src/server.ts"],
7+
entry: ["src/index.ts", "src/server.ts", "src/schema.ts"],
88
external: ["dotenv"],
99
env: dotenv.config().parsed,
1010
});

bun.lockb

336 Bytes
Binary file not shown.

packages/connect/src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const createWalletClient = ({
5959
return result;
6060
};
6161
const getBalanceFromKlesia = async (account: Account) => {
62-
const { result } = await klesiaClient.request<"mina_getBalance">({
62+
const result = await klesiaClient.request<"mina_getBalance">({
6363
method: "mina_getBalance",
6464
params: [account.publicKey],
6565
});
@@ -75,7 +75,7 @@ export const createWalletClient = ({
7575
const getTransactionCount = async () => {
7676
if (!account)
7777
throw new Error("Account is required to get transaction count");
78-
const { result } = await klesiaClient.request<"mina_getTransactionCount">({
78+
const result = await klesiaClient.request<"mina_getTransactionCount">({
7979
method: "mina_getTransactionCount",
8080
params: [account.publicKey],
8181
});
@@ -84,7 +84,7 @@ export const createWalletClient = ({
8484
const getChainId = async () => {
8585
return match(providerSource)
8686
.with("klesia", async () => {
87-
const { result } = await klesiaClient.request<"mina_chainId">({
87+
const result = await klesiaClient.request<"mina_chainId">({
8888
method: "mina_chainId",
8989
});
9090
return result;

packages/klesia-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"typescript": "^5.0.0"
2424
},
2525
"dependencies": {
26-
"hono": "^4.5.10",
26+
"micro-ftch": "^0.4.0",
2727
"ts-pattern": "^5.3.1"
2828
}
2929
}

packages/klesia-sdk/src/client.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import { createClient } from "./client";
33

44
it("fetches transaction count", async () => {
55
const client = createClient({ network: "devnet" });
6-
const { result } = await client.request<"mina_getTransactionCount">({
6+
const count = await client.request<"mina_getTransactionCount">({
77
method: "mina_getTransactionCount",
88
params: ["B62qkYa1o6Mj6uTTjDQCob7FYZspuhkm4RRQhgJg9j4koEBWiSrTQrS"],
99
});
10-
expect(BigInt(result)).toBeGreaterThan(0);
10+
expect(BigInt(count)).toBeGreaterThan(0);
1111
});

packages/klesia-sdk/src/client.ts

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,31 @@
11
import {
22
KlesiaNetwork,
3-
type KlesiaRpc,
4-
type RpcErrorType,
3+
type RpcRequestType,
54
type RpcResponseType,
6-
} from "@mina-js/klesia";
7-
import { hc } from "hono/client";
5+
} from "@mina-js/klesia/schema";
6+
import { ftch, jsonrpc } from "micro-ftch";
87
import { match } from "ts-pattern";
98

9+
const net = ftch(fetch);
10+
1011
type CreateClientProps = {
1112
network: "mainnet" | "devnet" | "zeko_devnet";
1213
customUrl?: string;
13-
throwable?: boolean;
14-
};
15-
16-
const throwRpcError = ({
17-
code,
18-
message,
19-
}: { code: number; message: string }) => {
20-
throw new Error(`${code} - ${message}`);
2114
};
2215

23-
export const createClient = ({
24-
network,
25-
customUrl,
26-
throwable = true,
27-
}: CreateClientProps) => {
16+
export const createClient = ({ network, customUrl }: CreateClientProps) => {
2817
const baseUrl = customUrl
2918
? customUrl
3019
: match(KlesiaNetwork.parse(network))
3120
.with("devnet", () => "https://devnet.klesia.palladians.xyz")
3221
.with("mainnet", () => "https://mainnet.klesia.palladians.xyz")
3322
.with("zeko_devnet", () => "https://zeko-devnet.klesia.palladians.xyz")
3423
.exhaustive();
35-
const baseClient = hc<KlesiaRpc>(baseUrl);
36-
const rpcHandler = baseClient.api.$post;
37-
type RpcRequest = Parameters<typeof rpcHandler>[0];
38-
const request = async <T extends string>(req: RpcRequest["json"]) => {
39-
const json = (await (
40-
await baseClient.api.$post({ json: req })
41-
).json()) as Extract<RpcResponseType, { method: T }> & {
42-
error?: RpcErrorType;
43-
};
44-
if (!throwable) {
45-
return json;
46-
}
47-
if (json?.error) {
48-
const { code, message } = json.error;
49-
return throwRpcError({ code, message });
50-
}
24+
const rpc = jsonrpc(net, `${baseUrl}/api`);
25+
const request = async <T extends string>(req: RpcRequestType) => {
26+
const params = req.params ?? [];
27+
const json: Extract<RpcResponseType, { method: T }>["result"] =
28+
await rpc.call(req.method, ...params);
5129
return json;
5230
};
5331
return {

0 commit comments

Comments
 (0)