Skip to content

Commit

Permalink
feat(packages): add klesia-sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Sep 2, 2024
1 parent f54eec6 commit 41f8605
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 14 deletions.
5 changes: 5 additions & 0 deletions apps/klesia/docs/index.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Klesia RPC is a JSON-RPC 2.0 wrapper over common Mina Protocol tools and services. The intention is to provide an outstanding Developer Experience for Mina Protocol's zkApp Developers.

## TypeScript SDK

There is a TypeScript SDK available for Klesia RPC. It is available on npm as `@mina-js/klesia-sdk`.
4 changes: 0 additions & 4 deletions apps/klesia/src/docs.md → apps/klesia/docs/rpc.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Klesia RPC

Klesia RPC is a JSON-RPC 2.0 wrapper over common Mina Protocol tools and services. The intention is to provide an outstanding Developer Experience for Mina Protocol's zkApp Developers.

## Methods

### mina_getTransactionCount
Expand Down
19 changes: 15 additions & 4 deletions apps/klesia/package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
{
"name": "@mina-js/klesia",
"type": "module",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"scripts": {
"build": "tsc --noEmit && bun build src/index.ts --outdir dist",
"dev": "bun run --hot src/index.ts",
"start": "bun dist/index.js"
"build": "tsup-node",
"dev": "tsup-node --watch --onSuccess \"node dist/index.js\"",
"start": "node dist/index.js"
},
"dependencies": {
"@mina-js/shared": "workspace:*",
"@hono/node-server": "^1.12.2",
"@hono/zod-openapi": "^0.16.0",
"@mina-js/shared": "workspace:*",
"@scalar/hono-api-reference": "^0.5.143",
"@urql/core": "^5.0.6",
"dayjs": "^1.11.13",
"dotenv": "^16.4.5",
"hono": "^4.5.10",
"hono-rate-limiter": "^0.4.0",
"nanoid": "^5.0.7",
Expand Down
2 changes: 1 addition & 1 deletion apps/klesia/src/custom.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
declare module "*.md";
declare module "*.txt";
16 changes: 11 additions & 5 deletions apps/klesia/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import "dotenv/config";
import { serve } from "@hono/node-server";
import { getConnInfo } from "@hono/node-server/conninfo";
import { OpenAPIHono, createRoute } from "@hono/zod-openapi";
import { PublicKeySchema } from "@mina-js/shared";
import { apiReference } from "@scalar/hono-api-reference";
import { rateLimiter } from "hono-rate-limiter";
import { getConnInfo } from "hono/bun";
import { logger } from "hono/logger";
import { nanoid } from "nanoid";
import { match } from "ts-pattern";
import rpcDocs from "./docs.md" with { type: "text" };
import mainDocs from "../docs/index.txt";
import rpcDocs from "../docs/rpc.txt";
import { mina } from "./methods/mina";
import { RpcMethodSchema, RpcResponseSchema } from "./schema";
import { buildResponse } from "./utils/build-response";
Expand All @@ -25,7 +28,8 @@ api.doc("/api/openapi", {
openapi: "3.0.0",
info: {
version: "1.0.0",
title: "Klesia API",
title: "Klesia RPC",
description: mainDocs,
},
});

Expand All @@ -50,7 +54,7 @@ const rpcRoute = createRoute({

api.get("/", ({ redirect }) => redirect("/api", 301));

api.openapi(rpcRoute, async ({ req, json }) => {
const klesiaRpcRoute = api.openapi(rpcRoute, async ({ req, json }) => {
const body = req.valid("json");
return match(body)
.with({ method: "mina_getTransactionCount" }, async ({ params }) => {
Expand Down Expand Up @@ -93,4 +97,6 @@ api.get(
}),
);

export default api;
serve(api);

export type KlesiaRpc = typeof klesiaRpcRoute;
3 changes: 3 additions & 0 deletions apps/klesia/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sharedConfig from "../../packages/shared/tsup.config";

export default sharedConfig;
Binary file modified bun.lockb
Binary file not shown.
15 changes: 15 additions & 0 deletions packages/klesia-sdk/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# klesia-sdk

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.1.27. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
29 changes: 29 additions & 0 deletions packages/klesia-sdk/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@mina-js/klesia-sdk",
"type": "module",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.cjs",
"import": "./dist/index.js"
}
},
"scripts": {
"build": "tsup",
"test": "bun test",
"cleanup": "rimraf dist .turbo"
},
"devDependencies": {
"@mina-js/klesia": "workspace:*"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"hono": "^4.5.10",
"ts-pattern": "^5.3.1",
"zod": "^3.23.8"
}
}
17 changes: 17 additions & 0 deletions packages/klesia-sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { KlesiaRpc } from "@mina-js/klesia";
import { hc } from "hono/client";
import { match } from "ts-pattern";
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/api"),
)
.with("mainnet", () =>
hc<KlesiaRpc>("https://mainnet.klesia.palladians.xyz/api"),
)
.exhaustive();
};
1 change: 1 addition & 0 deletions packages/klesia-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { createClient } from "./client";
3 changes: 3 additions & 0 deletions packages/klesia-sdk/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import sharedConfig from "../shared/tsup.config";

export default sharedConfig;

0 comments on commit 41f8605

Please sign in to comment.