https://github.com/evex-dev/singlend
Multiple operations on a single endpoint with hono and zod 🚀
https://github.com/evex-dev/singlend
hono singlend zod
Last synced: about 1 year ago
JSON representation
Multiple operations on a single endpoint with hono and zod 🚀
- Host: GitHub
- URL: https://github.com/evex-dev/singlend
- Owner: evex-dev
- License: mit
- Created: 2024-10-14T04:47:22.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-10-17T13:34:11.000Z (over 1 year ago)
- Last Synced: 2025-04-23T01:05:31.747Z (about 1 year ago)
- Topics: hono, singlend, zod
- Language: TypeScript
- Homepage: https://evex.land
- Size: 43.9 KB
- Stars: 6
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# singlend
Multiple operations on a single endpoint with hono and zod 🚀
When using singlend, headers and cookies are anti-patterns ✖ Everything is
managed by the body, so session should be stored in localStorage, etc.\
It is more secure 🔓
- 🔥 Support Hono RPC Client
- 🌪️ Fastest
- 🌍️ Support All Runtime
- 🧩 Highly Typed
## How to use
```bash
npx jsr add @evex/singlend
bunx jsr add @evex/singlend
deno add @evex/singlend
```
```ts
import { Hono } from "@hono/hono";
import { z } from "zod";
import { Singlend } from "@evex/singlend";
import { hc } from "@hono/hono/client";
const app = new Hono();
const singlend = new Singlend();
const sub = singlend.on(
"getIcon",
z.object({}),
(_query, ok) => {
return ok({
iconUrl: "default.png",
});
},
);
const singleRoute = singlend
.group(
z.object({
id: z.string(),
}),
(query, next, error) => {
if (!query.id.startsWith("@")) {
return error({
message: "Invalid id",
});
} else {
return next({
id: query.id.slice(1),
});
}
},
(singlend) =>
singlend.on(
"setIcon",
z.object({
iconUrl: z.string(),
}),
(query, value, ok) =>
ok({
message: "Set icon of " + value.id + " to " +
query.iconUrl,
}),
),
)
.on(
"setBackgroundColor",
z.object({
backgroundColor: z.string().length(7),
}),
(query, ok, error) => {
if (!query.backgroundColor.startsWith("#")) {
return error({
message: "Invalid background color",
});
}
return ok({
message: "Set background color to " + query.backgroundColor,
});
},
)
.mount(sub);
const routes = app.post("/api/singlend", singleRoute.handler());
// launch server
// in client
const client = hc("/");
const response = await client.api.singlend.$post({
json: {
type: "setIcon",
query: {
id: "@12345",
iconUrl: "default.png",
},
},
});
const data = await response.json();
```
```ts
fetch("/api/singlend", {
method: "POST",
body: JSON.stringify({
type: "setIcon",
query: {
id: "@114514",
iconUrl: "default.png",
},
}),
});
```
More:
[https://jsr.io/@evex/singlend/doc/~/Singlend](https://jsr.io/@evex/singlend/doc/~/Singlend)
## Authors
- @EdamAme-x